summaryrefslogtreecommitdiff
path: root/eventStreamer/eventStreamer.py
blob: 9f062e0ff9ca66793ca3b4aad55a443dc7fa9b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python

import MySQLdb,datetime,string,time,socket
import config

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-t", "--time", type="string", dest="time", default="",help="start time HH:MM:SS.sss")
parser.add_option("-d", "--date", type="string", dest="date", default="",help="start date YYYY-MM-DD")
parser.add_option("-a", "--accelerate", type="float", dest="acc", default=1.0,help="playback acceleration factor")
(options, args) = parser.parse_args()

def sqldatetime(t):
	return string.replace(t.isoformat(),"T"," ")
	
def main():

	startTime=None

	date=options.date
	logtime=options.time
	
	now=datetime.datetime.now()
	
	if logtime=="":
		if date=="":
			logtime=str(now.hour)+":"+str(now.minute)+":"+str(now.second+now.microsecond)
		else:
			logtime="0:00:00.0"
	
	if date=="":
		date=str(now.year)+"-"+str(now.month)+"-"+str(now.day)
		
	startTime=datetime.datetime.strptime(date+" "+logtime,"%Y-%m-%d %H:%M:%S.%f")

	db = MySQLdb.connect(config.mysql_ip,config.mysql_user,config.mysql_pword,config.mysql_db)
	cursor = db.cursor()
	
	sql = "SELECT * FROM IMSIs"
	devices={}
	
	try:
	   cursor.execute(sql)
	   results = cursor.fetchall()
	   for row in results:
		d={}
		d['IMSI']=row[1]
		d['number']=row[2]
		d['ts']=row[3]
		devices[row[0]]=d
	except:
	   print "Error: unable to fetch data"
	
	#get 1st event
	sql = "SELECT * FROM events Where ts Between '"+sqldatetime(startTime)+"' And '"+sqldatetime(now)+"' LIMIT 1"
	
	outsock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM )

	try:
		cursor.execute(sql)
		results = cursor.fetchall()
		id=long(results[0][0])
		device_id=results[0][3]
		now=results[0][2]
		print str(results[0][3])+" "+str(results[0][1])+" "+devices[results[0][3]]['IMSI'], (config.viz_ip, config.viz_port)
		outsock.sendto( str(results[0][3])+" "+str(results[0][1])+" "+devices[results[0][3]]['IMSI'], (config.viz_ip, config.viz_port) )
		while True:
			id+=1
			sql = "SELECT * FROM events Where id = "+str(id)
			print sql
			cursor.execute(sql)
			results = cursor.fetchall()
			print "waiting ",(((results[0][2]-now).total_seconds()+(results[0][2]-now).microseconds))*options.acc
			time.sleep((((results[0][2]-now).total_seconds()+(results[0][2]-now).microseconds))*options.acc)
			now=results[0][2]
			print str(results[0][3])+" "+str(results[0][1])+" "+devices[results[0][3]]['IMSI']
			outsock.sendto( str(results[0][3])+" "+str(results[0][1])+" "+devices[results[0][3]]['IMSI'], (config.viz_ip, config.viz_port) )
	except:
		outsock.sendto( "end of data", (config.viz_ip, config.viz_port) )
		print "Error: unable to fetch data"
	   
	#2 modes of operation depending whether realtime or historical
		   
	db.close()

if __name__ == '__main__': main()