forked from vr000m/SensorTrafficGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.py
More file actions
executable file
·186 lines (156 loc) · 5.33 KB
/
sensor.py
File metadata and controls
executable file
·186 lines (156 loc) · 5.33 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import random
import time
import socket
import os
import csv
import glob
import signal
MTU=1300
#Camera_data_size = 0
sensor_dir="./sensor_log/"
def signal_handler(signal, frame):
print 'shutting down sensor...'
# if Camera_data_size > 0:
# print Camera_data_size
sys.exit(0)
def usage():
print "sensor.py <sensor_type> <server_ip> <server_port> <id>\n\
valid sensor_type: [temp, device, gps, camera]"
def sensor_send(message, ipaddr, port):
'''
send data
'''
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (ipaddr, port)
sentbytes=0
while sentbytes < len(message):
#we could fragment the data??
#temp = sock.sendto(message[sentbytes:(sentbytes+MTU)], server_address)
temp = sock.sendto(message[sentbytes:len(message)], server_address)
sentbytes += temp
# if (sentbytes>MTU):
# print round(time.time(),3), len(message), ip, port
# if (sentbytes<MTU):
# print message, len(message), ip, port
def main(argv):
'''
very simple sensor generator
'''
if len(argv) < 4 or argv[0] == "-h" or argv[0]=="--help":
usage()
sys.exit(0)
start_time = time.time()
sensor_type= argv[0].lower()
ip=argv[1]
port=int(argv[2])
dev_id = sensor_type+"_"+argv[3]
#print " localhost and port:",port
#choose deviceid?
#randomly choose a mean temperature
mean_temp=random.uniform(-30, 50)
#randomly choose if we initially detect motion or not
motion=random.choice([0,1])
vid_stime= start_time
# start with the first co-ordinate on the map
paths = []
iRow = 0
with open('path.txt', 'rb') as gpsfile:
p = csv.reader(gpsfile, delimiter='\t')
for row in p:
r = [float(row[0]), float(row[1]), float(row[2])]
paths.append(r)
iRow +=1
fwdDir = True
try:
os.makedirs(sensor_dir)
except OSError:
print "Directory " + sensor_dir + " already exists"
logFname = sensor_dir+dev_id+'.log'
logFile = open(logFname, 'wb')
logWriter = csv.writer(logFile, delimiter='\t')
j=0
seq_no=0
while (True):
curr_time = round(time.time(),3)
if (sensor_type =="temp"):
val= str(round(random.normalvariate(mean_temp, 10),1))+" C"
timeout= 1.0
elif (sensor_type =="device"):
val=random.choice(["OFF","ON"])
timeout= float(random.uniform(0.1,5))
elif (sensor_type =="gps"):
dist= paths[j][2]
units= float(1000.0/3600.0)
speed=random.uniform(30.0*units, 100.0*units)
t = float(dist/speed)
val = [paths[j][0], paths[j][1]]
#we limit t to 60s
if (t>60):
t=60
if(fwdDir):
j +=1
if(j+1==iRow):
fwdDir=False
else:
j -=1
if(j==0):
fwdDir=True
timeout = t
elif (sensor_type =="camera"):
#did we detect motion?
if (motion):
#yes there was motion
#sends lots of data, often
fps=15
#bit rate is between 50 kbps to 200 kbps
bitrate= int(random.uniform(50000, 200000))
#generating random bytes to simulate MPEG2 video payload
#in MPEG2 all frames are equal sized
val= os.urandom(bitrate/8/fps)
timeout=float(1.0/fps)
#period of motion is random
motion_time=float(random.uniform(1,5))
if(time.time()-vid_stime>motion_time):
motion=0
else:
#no motion, no data, sleep random time
val="NO_MOTION"
motion=random.choice([0,1])
timeout=float(random.uniform(1,10))
vid_stime=time.time()+timeout
else:
print "argument:",argv[0],"not defined"
files=glob.glob(logFname)
for f in files:
os.unlink(f)
usage()
break
#pack the data into a dictionary
message={}
seq_no+=1
message["dev_id"]=str(dev_id)
message["ts"]=str(curr_time)
message["seq_no"]=str(seq_no)
message["data_size"]=str(len(str(val)))
message["sensor_data"]=str(val)
sensor_send(str(message), ip, port)
#print timeout
if(sensor_type!="camera"):
#could have stored the dictionary (pickle it)
logWriter.writerow([curr_time, seq_no, val])
else:
# print len(str(message))
logWriter.writerow([curr_time, seq_no, message["data_size"]])
if(val!="NO_MOTION"):
#global Camera_data_size
#Camera_data_size += val_len
camFile = open(sensor_dir+dev_id+'.data', 'ab')
camFile.write(str(val))
camFile.close
time.sleep(timeout)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
main(sys.argv[1:])