forked from ramteid/gnublin-weatherballoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
587 lines (483 loc) · 16.5 KB
/
listener.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/usr/bin/python
# this script needs root privileges
import datetime
import thread
import gammu
import sys
import time
import os
import random
from temperature import Temperature
from subprocess import Popen, PIPE
from gpsParser import GpsParser
from ctypes import CDLL, c_float
class Listener(object):
def __init__(self):
self.number = '+4917621929963'
self.pictureInterval = 20
self.temperatureInterval = 60
self.smsListeningInterval = 10
self.gpsReportingInterval = 120
self.threadLockNumberReports = 0
self.threadLockNumberPictures = 0
self.threadLockNumberTemperature = 0
self.threadLockNumberGpsTime = 0
self.sendGPSSMS = False
self.pictureDir = "/root/pictures"
self.sendTemperature = False
self.getTimeFromGpsLoop = True
self.gpsParser = GpsParser()
self.temperatureClass = Temperature()
try:
# Create state machine object
self.sm = gammu.StateMachine()
# Read ~/.gammurc
self.sm.ReadConfig(Filename = "/root/.gammurc")
# Connect to phone
self.sm.Init()
except Exception as e:
# Unplugging the UMTS stick requires a restart
print e
self.logMessage(e, "__init__")
if e.Code == 4:
print "restarting in 300 sec..."
time.sleep(300)
os.system("reboot")
def sendSMS(self, text):
try:
message = {
'Text': text,
'SMSC': {'Location': 1},
'Number': self.number,
}
self.sm.SendSMS(message)
except Exception as e:
print "sendSMS"
print e
self.logMessage(e, "sendSMS")
def getAllSMS(self):
smslist = []
for folder in [3]: # 1=Inbox SIM, 3=Inbox Device (maybe inversed)
lastloc = 0
start = True
while 1:
try:
if start:
sms = self.sm.GetNextSMS(Start = True, Folder=folder)
if len(sms) > 0:
lastloc = sms[0]['Location']
smslist.append(sms[0])
start = False
else:
break
else:
sms = self.sm.GetNextSMS(Location = lastloc, Folder=folder)
if len(sms) > 0:
lastloc = sms[0]['Location']
smslist.append(sms[0])
else:
break
except Exception:
break
return smslist
def deleteReadSMS(self, smslist):
for sms in smslist:
try:
folder = sms['Folder']
location = sms['Location'] - 100000 # in our case theres somehow a large offset
self.sm.DeleteSMS(Folder = folder, Location = location)
except Exception as e:
print "deleteReadSMS"
print e
self.logMessage(e, "deleteReadSMS")
# parameter must be a string
def logAllRecords(self, temperatureExternal, temperatureInternal, gpsInfo, coords, height, networkInfo):
try:
with open("/root/logs/allRecords.log", "a") as myfile:
s = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
s += " " + temperatureExternal + "\n"
s += " " + temperatureInternal + "\n"
s += " " + str(gpsInfo) + "\n"
s += " " + coords + "\n"
s += " " + height + "\n"
s += " " + str(networkInfo) + "\n\n"
print ""
print temperatureExternal
print temperatureInternal
print str(gpsInfo)
print coords
print height
print str(networkInfo)
print ""
myfile.write(s)
except Exception as e:
print "logAllRecords"
print e
self.logMessage(e, "logAllRecords")
# parameter must be a string
def logCoords(self, coords):
try:
with open("/root/logs/coordinates.log", "a") as myfile:
s = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
s += " " + coords + "\n"
myfile.write(s)
except Exception as e:
print "logCoords"
print e
self.logMessage(e, "logCoords")
# first parameter must be something convertable to s, second parameter must be a string
def logMessage(self, message, origin):
try:
with open("/root/logs/messages.log", "a") as myfile:
s = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
s += " " + str(origin) + " " + str(message) + "\n"
myfile.write(s)
except Exception as e:
print "Error while logging, can't be logged ..."
print e
# first parameter must be something convertable to s, second parameter must be a string
def logTemperature(self, message, origin):
try:
with open("/root/logs/temperatures.log", "a") as myfile:
s = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
s += " " + str(origin) + " " + str(message) + "\n"
myfile.write(s)
except Exception as e:
print "logTemperature"
print e
self.logMessage(e, "logCoords")
# thread that loops and logs/sends gps coordinates
def logGPScoordinates(self, myThreadLockNumber, seconds):
interval = self.gpsReportingInterval
try:
interval = int(seconds)
except Exception as e:
#print "logGPScoordinates-listener"
#print e
self.logMessage(e, "logGPScoordinates-interval")
interval = self.gpsReportingInterval
# to avoid multiple threads, there'a lock number
while (myThreadLockNumber == self.threadLockNumberReports):
try:
lines = self.gpsParser.readGpsData(timeout=6)
coords, height = self.gpsParser.getGpsCoordinatesParser(lines, sendSMS=self.sendGPSSMS)
self.logCoords(coords + ": " + height + "m")
if self.sendGPSSMS:
self.sendSMS(coords + ": " + height + "m\n" + "http://maps.google.de/maps?q=" + coords)
time.sleep(interval)
except Exception as e:
print "logGPScoordinates-listener-loop"
print e
self.logMessage(e, "logGPScoordinates-report")
def initGetTimeFromGPS(self, interval):
newThreadLockNumber = random.randint(1,99999)
self.threadLockNumberGpsTime = newThreadLockNumber
thread.start_new_thread( self.getTimeFromGPS, (newThreadLockNumber, interval) )
# thread to get the gps timestamp and set the system clock
def getTimeFromGPS(self, myThreadLockNumber, interval=300):
while self.getTimeFromGpsLoop and myThreadLockNumber == self.threadLockNumberGpsTime:
try:
# set the system date/time
gpsData = self.gpsParser.readGpsData(timeout=2)
success = self.gpsParser.parseAndSetDateTime(gpsData)
time.sleep(interval)
except Exception as e:
#print e
self.logMessage(e, "getTimeFromGPS")
time.sleep(interval)
# parse various gps information from NMEA output
def getGpsInfo(self):
try:
gpsLines = self.gpsParser.readGpsData(timeout = 6)
gps_info = self.gpsParser.parseGpsData(gpsLines)
coords, height = self.getGPScoordinates(lines=gpsLines, sendSMS=False)
return gps_info, coords, height
except Exception as e:
print "getGpsInfo"
print e
self.logMessage(e, "parseGpsData")
return "-","-","-"
# get gps coordinates on demand
def getGPScoordinates(self, lines = [], sendSMS=False):
try:
if not lines:
lines = self.gpsParser.readGpsData(timeout=6)
coords, height = self.gpsParser.getGpsCoordinatesParser(lines)
if coords == "-":
raise Exception("coordinates invalid: -")
if sendSMS:
self.sendSMS(coords + ": " + height + "m\n" + "http://maps.google.de/maps?q=" + coords)
return coords, height
except Exception as e:
#print "getGPScoordinates-listener"
#print e
self.logMessage(e, "getGPScoordinates")
return "-","-"
def setSystemTime(self, datetime):
#expects a string in format 20130717121400
try:
year = datetime[0:4]
month = datetime[4:6]
day = datetime[6:8]
hour = datetime[8:10]
min = datetime[10:12]
sec = datetime [12:14]
s = 'date -s "{0}-{1}-{2} {3}:{4}:{5}"'.format(year, month, day, hour, min, sec)
os.system(s)
print "date set:"
os.system("date")
except Exception as e:
print "setSystemTime"
print e
self.logMessage(e, "setSystemTime")
def getSystemTime(self):
try:
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.sendSMS(time)
except Exception as e:
print "getSystemTime"
print e
self.logMessage(e, "getSystemTime")
def getNetworkInfo(self, sendSMS=False):
# Reads network information from phone
try:
netinfo = self.sm.GetNetworkInfo() # 'State': 'NoNetwork' = no signal
signalQuality = self.sm.GetSignalQuality()
if sendSMS:
s = "Signal: " + signalQuality['SignalPercent'] + "% \n"
for key in netinfo:
s += key + ": " + netinfo[key] + "\n"
print s
self.sendSMS(s)
return dict(netinfo, **signalQuality)
except Exception as e:
print "getNetworkInfo"
print e
self.logMessage(e, "getNetworkInfo")
def initCapturing(self, interval):
newThreadLockNumber = random.randint(1,99999)
self.threadLockNumberPictures = newThreadLockNumber
thread.start_new_thread( self.takePictures, (newThreadLockNumber, interval) )
print "thread gestartet: " + str(newThreadLockNumber)
def takePictures(self, myThreadLockNumber, seconds):
if not os.path.isdir(self.pictureDir):
print "pictures directory does not exist"
return
interval = self.pictureInterval
try:
interval = int(seconds)
except Exception as e:
print "takePicutes-upper"
print e
self.logMessage(e, "takePictures-interval")
interval = self.pictureInterval
# continue with file numbers
try:
if len(os.listdir(self.pictureDir)) > 0:
# get largest file number
# assuming file names like 1.jpg, 2.jpg, ...
numbers = []
index = 0
#iterate over all files and retrieve file names
for i,file_name in enumerate(sorted(os.listdir(self.pictureDir))):
try:
s = file_name.split(".")
n = s[0]
o = int(n)
numbers.append(o)
# sort the list descending
numbers.sort(reverse=True)
# the highest file number plus one is the new image index
index = numbers[0] + 1
except Exception as e:
print "takePictures-inner"
print e
self.logMessage(e, "takePictures-filenames")
index = 0
else:
index = 0
# capture pictures
while (myThreadLockNumber == self.threadLockNumberPictures):
filename = "{0}/{1}.jpg".format(self.pictureDir, index)
command = "uvccapture -o{0} -m -x640 -y480 2> /dev/null".format(filename)
print "capture picture " + str(index)
os.system(command)
if os.path.exists(filename):
index += 1
time.sleep(interval)
except Exception as e:
print "takePictures"
print e
self.logMessage(e, "takePictures")
time.sleep(60)
# start thread for logging PT1000 temperature
def initLogTemperature(self, seconds):
newThreadLockNumber = random.randint(1, 99999)
self.threadLockNumberTemperature = newThreadLockNumber
thread.start_new_thread(self.logTemperatureStart, (newThreadLockNumber, seconds))
print "thread gestartet: " + str(newThreadLockNumber)
# thread for logging PT1000 temperature
def logTemperatureStart(self, myThreadLockNumber, seconds):
try:
tempLib = CDLL("/usr/local/lib/temperature.so")
tempLib.calculateTemperature.restype = c_float
interval = self.temperatureInterval
interval = int(seconds)
except Exception as e:
print "logTemperatureStart"
print e
self.logMessage(e, "logTemperatureStart-init")
interval = self.temperatureInterval
try:
while (myThreadLockNumber == self.threadLockNumberTemperature):
celsius = tempLib.calculateTemperature()
fahrenheit = celsius * 33.8
tempString = "{0} Celsius / {1} Fahrenheit".format(celsius, fahrenheit)
self.logTemperature(tempString, "logTemperatureStart")
if self.sendTemperature:
self.sendSMS(tempString)
time.sleep(interval)
except Exception as e:
print "logTemperatureStart-loop"
print e
self.logMessage(e, "logTemperatureStart")
# return PT1000 temperature
def getTemperatureExternal(self):
try:
#tempLib = CDLL("/usr/local/lib/temperature.so")
#tempLib.calculateTemperature.restype = c_float
#celsius = tempLib.calculateTemperature()
#return str(celsius)
t = self.temperatureClass.calculateTemperature()
return str(t)
except Exception as e:
print "getTemperatureExternal"
print e
self.logMessage(e, "getTemperatureExtern")
return "-"
# return Gnublin temperature module temperature
def getTemperatureInternal(self):
try:
(stdout, stderr) = Popen(["gnublin-lm75", "-b"], stdout=PIPE).communicate()
return stdout
except Exception as e:
print "getTemperatureInternal"
print e
self.logMessage(e, "getTemperatureIntern")
return "-"
def processCommands(self, smslist):
for sms in smslist:
try:
cmd = sms['Text'].lower()
print "cmd: " + cmd
# stop gps logging
if cmd == 'loggpsstop':
self.logMessage("cmd: " + cmd, "processCommands")
self.threadLockNumberReports = 0
print "thread gestoppt"
# start sending gps coordinates via sms
elif cmd == 'repgpsstart':
self.sendGPSSMS = True
# stop sending gps coordinates via sms
elif cmd == 'repgpsstop':
self.sendGPSSMS = False
# stop setting time via gps
elif cmd == 'gpstimestop':
self.logMessage("cmd: " + cmd, "processCommands")
self.threadLockNumberGpsTime = 0
print "thread gestoppt"
# get gps coordinates on demand
elif cmd == 'getgps':
self.getGPScoordinates(lines=[], sendSMS=True)
# stop capturing images with camera
elif cmd == 'capstop':
self.logMessage("cmd: " + cmd, "processCommands")
self.threadLockNumberPictures = 0
print "thread gestoppt"
# stop logging temperature
elif cmd == 'logtempstop':
self.logMessage("cmd: " + cmd, "processCommands")
self.threadLockNumberTemperature = 0
print "thread gestoppt"
# start sending temperatures via sms
elif cmd == 'sendtempstart':
self.logMessage("cmd: " + cmd, "processCommands")
self.sendTemperature = True
# stop sending temperatures via sms
elif cmd == 'sendtempstop':
self.logMessage("cmd: " + cmd, "processCommands")
self.sendTemperature = False
# send the time via sms
elif cmd == 'gettime':
self.logMessage("cmd: " + cmd, "processCommands")
self.getSystemTime()
# send network information via sms
elif cmd == 'getnetwork':
self.logMessage("cmd: " + cmd, "processCommands")
self.getNetworkInfo(sendSMS=True)
# reboot system
elif cmd == 'restart_system' or cmd == 'reboot_system':
self.logMessage("cmd: " + cmd, "processCommands")
os.system("reboot")
# set time to a value specified in a sms
elif cmd[0:7] == 'settime': #e.g. settime20130802120500
self.logMessage("cmd: " + cmd, "processCommands")
datetime = cmd[7:]
self.setSystemTime(datetime)
# run any system command
elif cmd[0:7] == 'system_': #e.g. system_reboot
print "executing command: " + cmd
self.logMessage("cmd: " + cmd, "processCommands")
os.system(cmd[7:])
# start capturing pictures with camera
elif cmd[0:8] == 'capstart': #e.g. capstart30
self.logMessage("cmd: " + cmd, "processCommands")
self.initCapturing(cmd[8:])
# start logging temperatures
elif cmd[0:9] == 'logtempstart': #e.g. tempstart30
self.logMessage("cmd: " + cmd, "processCommands")
self.initLogTemperature(cmd[9:])
# start logging gps coordinates
elif cmd[0:11] == 'loggpsstart': #e.g. loggpsstart30
self.logMessage("cmd: " + cmd, "processCommands")
newThreadLockNumber = random.randint(1,99999)
self.threadLockNumberReports = newThreadLockNumber
thread.start_new_thread( self.logGPScoordinates, (newThreadLockNumber, cmd[11:]) )
print "thread gestartet: " + str(newThreadLockNumber)
# start retrieving timestamp from GPS and set the system clock
elif cmd[0:12] == 'gpstimestart': #e.g. gpstimestart300
self.initGetTimeFromGPS(cmd[0:12])
self.logMessage("cmd: " + cmd, "processCommands")
print "thread gestartet: " + str(newThreadLockNumber)
# change sms sending phone number
elif cmd[0:12] == 'changenumber': #e.g. changenumber+491703939393
self.number = cmd[12:]
else:
print "command not recognized"
self.logMessage("unknown command: " + cmd, "processCommands")
except Exception as e:
print "processCommands"
print e
self.logMessage(e, "processCommands")
def listenForCommands(self):
while 1:
try:
time.sleep(self.smsListeningInterval)
smslist = self.getAllSMS()
if len(smslist) > 0:
self.processCommands(smslist)
self.deleteReadSMS(smslist)
else:
self.sm.GetNetworkInfo() #just to detect if stick is unplugged
# Unplugging the UMTS stick requires a restart
except gammu.ERR_DEVICEWRITEERROR as e:
print "listenForCommands-DEVICEWRITEERR"
print e
self.logMessage(e, "listenForCommands")
print "restarting in 300 sec..."
time.sleep(300)
os.system("reboot")
except Exception as e:
print "listenForCommands-Exception"
print e
self.logMessage(e, "listenForCommands")