diff options
Diffstat (limited to 'eventStreamer/eventStreamer.py')
| -rwxr-xr-x | eventStreamer/eventStreamer.py | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/eventStreamer/eventStreamer.py b/eventStreamer/eventStreamer.py index 326ff89..9f062e0 100755 --- a/eventStreamer/eventStreamer.py +++ b/eventStreamer/eventStreamer.py @@ -1,6 +1,6 @@ #!/usr/bin/python -import MySQLdb,datetime,string +import MySQLdb,datetime,string,time,socket import config from optparse import OptionParser @@ -8,6 +8,7 @@ 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): @@ -18,17 +19,20 @@ def main(): startTime=None date=options.date - time=options.time + 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) - - if time=="": - time="0:00:00.0" - startTime=datetime.datetime.strptime(date+" "+time,"%Y-%m-%d %H:%M:%S.%f") + 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() @@ -50,20 +54,31 @@ def main(): #get 1st event sql = "SELECT * FROM events Where ts Between '"+sqldatetime(startTime)+"' And '"+sqldatetime(now)+"' LIMIT 1" - print sql + + 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] - print "id:",id,results[0][1],devices[device_id]['IMSI'] - sql = "SELECT * FROM events Where id = "+str(id+1) - cursor.execute(sql) - results = cursor.fetchall() - print "next:",results[0][1] + 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: - print "Error: unable to fetch data" + 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 |
