changeset 48:12d6fd69d166

added simulator
author gyorgyf
date Thu, 14 May 2015 12:31:39 +0100
parents 2db17c224664
children 8f9e483b5798
files simulator/simulator.py
diffstat 1 files changed, 123 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simulator/simulator.py	Thu May 14 12:31:39 2015 +0100
@@ -0,0 +1,123 @@
+#!/usr/bin/env python
+# encoding: utf-8
+"""
+simulator.py
+
+Created by George Fazekas on 2013-08-08.
+Copyright (c) 2013 . All rights reserved.
+"""
+
+import sys,os,time
+import httplib as ht
+import urllib
+import random as r
+import datetime as dt
+
+# IP = "127.0.0.1:8030"
+IP = "138.37.95.215" # this is the IP of kakapo<=>golden
+
+HTTP_TIMEOUT = 3
+
+class DataPoint(object):
+	
+	def __init__(self,x,y,ts):
+		self.x = x
+		self.y = y
+		self.ts = ts
+		
+	def __repr__(self):
+		return "DataPoint(%f,%f,%s)" %(self.x,self.y,str(self.ts))
+		
+
+class Simulator(object):
+	
+	def __init__(self):
+		self.connect()
+		self.data = []
+		self.first_date = None
+		pass
+		
+	def connect(self):
+		self.conn = ht.HTTPConnection(IP,timeout=HTTP_TIMEOUT)
+		
+	def read_logdata(self,file):
+		'''Read the log file'''
+		with open(file,'r') as fh:
+			lines = fh.readlines()[1000:]
+			for line in lines :
+				# x,y,ts = self.parse_logline(line)
+				x,y,ts = self.parse_logline_newformat(line)
+				self.data.append(DataPoint(x,y,ts))
+		pass
+		
+	def parse_logline_newformat(self,line):
+		'''parse a single line of the logfile and convert it to the appropriate datatypes'''
+		x,y,ts = 0,0,0
+		date,rest = line.split("]")
+		h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:])
+		if self.first_date == None:
+			self.first_date = dt.datetime(1,1,1,h,m,s)
+		ts = dt.datetime(1,1,1,h,m,s) - self.first_date
+		ip,x,y,t = map(lambda x: x.strip(), rest.strip().split(","))
+		# print ts,ip,x,y
+		return float(x),float(y),ts
+	
+				
+	def parse_logline(self,line):
+		'''parse a single line of the logfile and convert it to the appropriate datatypes'''
+		x,y,ts = 0,0,0
+		date,rest = line.split("]")
+		h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:])
+		if self.first_date == None:
+			self.first_date = dt.datetime(1,1,1,h,m,s)
+		ts = dt.datetime(1,1,1,h,m,s) - self.first_date
+		ip,x,y, = map(lambda x: x.strip(), rest.split("-"))
+		# print ts,ip,x,y
+		return float(x),float(y),ts
+		
+	def yield_data(self):
+		for d in self.data :
+			yield d
+		
+	def simulate(self):
+		deltas = map(lambda x:(x[1].ts-x[0].ts), zip(self.data,self.data[1:]))
+		# deltas = map(lambda x: x.seconds+(x.microseconds/1000000.0), deltas)
+		deltas = map(lambda x: x.seconds+r.random(), deltas)
+		# deltas = map(lambda x: x.seconds, deltas)		
+		datagen = self.yield_data()
+		for d in deltas :
+			datapoint = datagen.next()
+			print datapoint
+			self.send_coordinate(datapoint.x, datapoint.y)
+			time.sleep(d)
+		pass
+			
+	def random(self):
+		'''Send random numbers'''
+		while True :
+			self.send_coordinate(r.random(),r.random())
+			time.sleep(0.3)
+		
+	def send_coordinate(self,x,y):		
+		self.conn.putrequest("GET","/moodconductor/mood?x=%(x)s&y=%(y)s" %locals(), skip_host=True)
+		self.conn.putheader("Host", "www.isophonics.net")
+		self.conn.endheaders()
+		res = self.conn.getresponse()
+		res.read()
+		print res.status, res.reason	
+		print res.read()
+		
+
+def main():
+	
+	s = Simulator()
+	# s.random()
+	# s.read_logdata("../performance2.log")
+	s.read_logdata("../performance-09Aug2013Barbican.log")
+	s.simulate()
+
+	
+
+if __name__ == '__main__':
+	main()
+