/
sensorLogic.py
executable file
·701 lines (598 loc) · 32.9 KB
/
sensorLogic.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#!/usr/bin/python
# -*- coding: latin-1 -*-
# include libraries -----------------------------------------------------------
import time, datetime # time functions
import requests # library for sending data over http
import thread # loop is slow, needed for fast led pulse
import gpio # led output
import os, sys, ast
from whatsapp import sendWhatsApps
import ConfigParser
from sensorHistory import sensorHistory
from time import sleep
from plotting import plotting_DeviceValues
from sensorCrypt import sensorCrypt
from sensorLight import sensorLight #ok
from sensorInterval import sensorInterval #ok
from sensorConfig import sensorConfig #ok
from sensorThreads import threadPICAM2
from sensorThreads import threadFritzActors2 #ok
from sensorThreads import threadNetDiscovery #ok
from sensorThreads import threadCreatePHPFile #ok
from sensorThreads import threadWebradioService
from sensorSwitches import sensorSwitches
from sensorDevice import moduleDevice
#from sensorService import threadSensors
import Queue
vVerbose = str(sys.argv[1])
if vVerbose.startswith('test'):
print 'sensorService starting'
sConfig = sensorConfig('/home/pi/sensorTool/sensorTool.conf', vVerbose)
sensorSwitches('/home/pi/sensorTool/switches.conf', vVerbose);
# GPIO Settings BCM Layout
#RelayIN1 = gpio.GPIOout(sConfig.getGPIORelayIN1())
#RelayIN2 = gpio.GPIOout(sConfig.getGPIORelayIN2())
#LedG = gpio.GPIOout(sConfig.getGPIOLedGreen())
#LedY = gpio.GPIOout(sConfig.getGPIOLedYellow())
#LedR = gpio.GPIOout(sConfig.getGPIOLedRed())
BMelder = gpio.GPIOin(sConfig.getGPIOmotion())
Leuchte = sensorLight(sConfig.getGPIOlight())
# Start state
#LedG.on()
#LedY.on()
#LedR.on()
boolLeuchte = False
# constants -------------------------------------------------------------------
sCrypt = sensorCrypt(sConfig.getCryptKey())
MAXTIME = sConfig.getMaxTime()
MINTIME = sConfig.getMinTime()
INTERVAL_SENSORS = sConfig.getIntervalSensors()
INTERVAL_FRITZACTORS = sConfig.getIntervalActors()
INTERVAL_WEBRADIO = sConfig.getIntervalWebradio()
INTERVAL_LANDEVICES = sConfig.getIntervalLANDevices()
SERVER = sConfig.getServer()
TriggerCountA = sConfig.getTriggerCountA()
TriggerCountB = sConfig.getTriggerCountB()
#dictInterval = {}
dictInterval = sensorInterval(sConfig.getDictIntervalle())
vTitel = sConfig.getTitel()
dictActors = {}
dictSensors = {}
dictSensors = sConfig.getDictSensors()
iControlSensor = sConfig.getiControlSensor()
iOutdoorSensor = sConfig.getiOutdoorSensor()
boolOutdoorSensor = False
if iOutdoorSensor is not None:
boolOutdoorSensor = True
sH_Log = sensorHistory('/var/sensorTool/www/loglatestentry.php', '/var/sensorTool/www/logdata.php')
iLog = 0
modules_webradio = sConfig.getModuleWebradio() # ok
webradio_active = False
modules_webradiomotion = sConfig.getModuleWebradiomotion() # ok
webradio_motionTimeOut = sConfig.getIntervalWebradiomotion()
webradio_motionActive = True
modules_surveillance = sConfig.getModuleSurveillance() # ok
modules_motiondetector = sConfig.getModuleMotiondetector()
motion_detected = False
motion_detected_report = False
ledalarm = False
modules_radiators = sConfig.getModuleRadiators() # ok
RadiatorStarted = False
modules_relais = sConfig.getModuleRelais()
modules_fritzactors = sConfig.getModuleFritzActors() # ok
modules_LANDevices = sConfig.getModuleLANDevices() # ok
LANDevices_Present = False
LANNetwork = sConfig.getLANNetwork()
dictMobileHosts = sConfig.getdictMobileHosts()
sConfig.SetupDynamicConfig('/var/sensorTool/www/dynamic.conf')
dictActiveModules = {}
dictActiveModules['time'] = list()
dictActiveModules['Motiondetector'] = moduleDevice(20)
dictActiveModules['NetworkDeviceDetector'] = moduleDevice(30)
dictActiveModules['Webradio'] = moduleDevice(40)
dictActiveModules['Radiators'] = moduleDevice(60)
dictActiveModules['ledalarm'] = moduleDevice(90)
dictTemperature = {}
dictTemperature['time'] = list()
dictRelativeHumidity = {}
dictRelativeHumidity['time'] = list()
dictAbsoluteHumidity = {}
dictAbsoluteHumidity['time'] = list()
dictPressure = {}
dictPressure['time'] = list()
for vKey in dictSensors:
sSensorName = dictSensors[vKey].GetSensor()
dictTemperature[sSensorName] = moduleDevice(0)
dictRelativeHumidity[sSensorName] = moduleDevice(0)
dictAbsoluteHumidity[sSensorName] = moduleDevice(0)
if dictSensors[vKey].GetBME():
dictPressure[sSensorName] = moduleDevice(0)
if vVerbose == 'test':
print 'MAXTIME' + str(MAXTIME)
print 'MINTIME' + str(MINTIME)
print 'INTERVAL_SENSORS' + str(INTERVAL_SENSORS)
print 'INTERVAL_FRITZACTORS' + str(INTERVAL_FRITZACTORS)
print 'INTERVAL_WEBRADIO' + str(INTERVAL_WEBRADIO)
print 'INTERVAL_LANDEVICES' + str(INTERVAL_LANDEVICES)
print 'webradio_motionTimeOut' + str(webradio_motionTimeOut)
sleep(1)
# Config Variable leeren
sConfig = None
vBoolLC = True
# fritz off moved after first fritzactors call
degC = 0
hPa = 0
hRel = 0
degC1 = 0
hPa1 = 0
vBoolSensor1 = True
counterHigh = 0
counterLow = 0
timeDuration = 0
# functions for print out and WhatsApp
def getInfoText(vStatus):
vHeader = vTitel
if vVerbose.startswith('test'):
vHeader += ' Test\n'
else:
vHeader += '\n'
vNow = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' ' + vStatus
vSensorPaket = ''
for vKey in dictSensors:
vSensorPaket += '\n ' + str(dictSensors[vKey].GetInfo(False, False))
vActorsPaket = ''
for vAKey in dictActors:
vActorsPaket += '\n' + str(dictActors[vAKey].GetInfo(False, False))
vGPIOStatus = ''
# vGPIOStatus += '\n Relais: ' + str(RadiatorStarted) + ' (' + str(RelayIN1.isOn) + '|' + str(RelayIN2.isOn) + ')'
vGPIOStatus += '\n Code: ' + str(timeDuration) + ';' + str(counterHigh) + ';' + str(counterLow)
# vGPIOStatus += '\n LED: ' + str(LedG.isOn) + str(LedY.isOn) + str(LedR.isOn)
vReturnText = vHeader + vNow + vSensorPaket + vActorsPaket + vGPIOStatus # + str(dictActors)
return vReturnText
# print CSV
def getInfoCSV():
vReturnText = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ';'
vReturnText = vReturnText + str(RadiatorStarted) + ';'
vReturnText = vReturnText + str(timeDuration) + ';' + str(counterHigh) + ';' + str(counterLow) + ";"
# vReturnText = vReturnText + str(RelayIN1.isOn) + str(RelayIN2.isOn) + ";"
# vReturnText = vReturnText + str(LedG.isOn) + str(LedY.isOn) + str(LedR.isOn) + ";"
for vKey in dictSensors:
vReturnText = vReturnText + str(dictSensors[vKey].GetInfo(True, False))
return vReturnText
# print html status output
def getHTML():
vhttpResult = ''
vhttpResult += 'echo "<DIV class=\\"sensor\\"><TABLE class=\\"sensor\\"><TR>\\n";\n'
vhttpResult += 'echo "<TD>\\n";\n'
for vKey in dictSensors:
vhttpResult += dictSensors[vKey].GetHttpTable()
vhttpResult += 'echo "</TD>\\n";\n'
vhttpResult += 'echo "<TD>\\n";\n'
for vKey in dictActiveModules:
if vKey <> 'time':
vhttpResult += dictActiveModules[vKey].GetHttpTable(vKey)
if modules_fritzactors:
for vAKey in dictActors:
vhttpResult += dictActors[vAKey].GetHttpTable()
vhttpResult += 'echo "</TD></TR></TABLE></DIV>\\n";\n'
return vhttpResult
# refresh for sensor data
refreshTime_sensors = time.time() + INTERVAL_SENSORS
# refresh for fritz actors
refreshTime_fritzactors = time.time()
refreshTime_webradio = time.time()
refreshTime_motiondetector = time.time()
# refresh for Queue
# if vVerbose.startswith('test'):
# INTERVAL_LANDEVICES = 3
refreshTime_LANDevices = time.time() + INTERVAL_LANDEVICES
qNDD = Queue.LifoQueue(maxsize=1)
DiscoveryInProgress = False
refreshTime_Queue = 5
INTERVAL_QUEUE = 5
refreshTime_webradioMotion = time.time() + webradio_motionTimeOut
# FAInProgress = False
# qFA = Queue.LifoQueue(maxsize=1)
# loop ------------------------------------------------------------------------
#LedR.off()
if vVerbose.startswith('test1'):
print 'Config loaded'
print str(refreshTime_sensors) + '##' + str(time.time())
while time.strftime('%H%M') < MAXTIME: # timeDuration <= MAXTIME:
# Check NDD Queue every INTERVAL_LANDEVICES secs
if time.time() > refreshTime_Queue:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_Queue ('+str(refreshTime_Queue)+')'
if not qNDD.empty():
#dictNetDevs = {}
#dictNetDevs = qNDD.get()
boolNDD = qNDD.get()
LANDevices_Present = boolNDD
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_Queue: NDD (True) : LANDevices_Present ('+str(LANDevices_Present)+')'
qNDD.task_done()
DiscoveryInProgress = False
refreshTime_Queue = time.time() + INTERVAL_QUEUE
if time.time() > refreshTime_motiondetector:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_motiondetector ('+str(refreshTime_motiondetector)+')'
if modules_motiondetector:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_motiondetector: modules_motiondetector'
if BMelder.status() == 1:
motion_detected = True
motion_detected_report = True
else:
motion_detected = False
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_motiondetector: modules_motiondetector: motion_detected_report('+str(motion_detected_report)+')'
if modules_surveillance and modules_motiondetector:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_motiondetector: modules_surveillance, modules_motiondetector'
if not LANDevices_Present:
if motion_detected:
threadPICAM2('surveillance')
sH_Log.Add(' surveillance take picset ')
else:
threadPICAM2('low')
sH_Log.Add(' surveillance take pic low ')
refreshTime_motiondetector = time.time() + 1
if time.time() > refreshTime_LANDevices:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_LANDevices ('+str(refreshTime_LANDevices)+')'
if modules_LANDevices:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_LANDevices: modules_LANDevices'
#parallel with threading and queue
if not DiscoveryInProgress:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_LANDevices: modules_LANDevices: Start Network Device Discovery!'
thread.start_new_thread(threadNetDiscovery, (qNDD, LANNetwork, dictMobileHosts) )
DiscoveryInProgress = True
else:
if vVerbose.startswith('test1'):
print 'Discovery running'
refreshTime_LANDevices = time.time() + INTERVAL_LANDEVICES
# get fritz actor data
if time.time() > refreshTime_fritzactors:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_fritzactors ('+str(refreshTime_fritzactors)+')'
if modules_fritzactors:
if vVerbose.startswith('test1'):
print str(time.time())+'while: refreshTime_fritzactors: modules_fritzactors'
dictActors = threadFritzActors2()
# On Startup stop running radiators
if vBoolLC:# and not FAInProgress:
dictActors[str(dictSensors[iControlSensor].GetFritzActor())].SetActor(False)
refreshTime_fritzactors = time.time() + INTERVAL_FRITZACTORS
# Check if there is a webradio setting change
if time.time() > refreshTime_webradio:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradio ('+str(refreshTime_webradio)+')'
if modules_webradio:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradio: modules_webradio'
(webradio_active1, webradio_changed, webradio_result) = threadWebradioService('/var/sensorTool/www/webradio.station', webradio_active)
if webradio_changed:
if webradio_active <> webradio_active1:
webradio_active = webradio_active1
sH_Log.Add(' Webradio change: ' + str(webradio_result))
elif not modules_webradio and webradio_active:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradio: not modules_webradio, webradio_active'
(webradio_active, webradio_changed, webradio_result) = threadWebradioService('/var/sensorTool/www/webradio.station', webradio_active, True)
sH_Log.Add(' Webradio stopped')
if webradio_motionActive and not webradio_active and modules_webradio and modules_webradiomotion:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradio: webradio_motionActive, webradio_active, modules_webradio, modules_webradiomotion'
refreshTime_webradioMotion = time.time() + webradio_motionTimeOut
(webradio_active, webradio_changed, webradio_result) = threadWebradioService('/var/sensorTool/www/webradio.station', webradio_active, False, True)
sH_Log.Add(' Webradio started')
refreshTime_webradio = time.time() + INTERVAL_WEBRADIO
#print 'time '+str(time.time())+'>'+str(refreshTime_webradioMotion)+' refreshTime_webradioMotion'
# Timeout for webradio - stop when no motion is detected (active motioncontrol)
if time.time() > refreshTime_webradioMotion:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradioMotion ('+str(refreshTime_webradioMotion)+')'
#print str(time.time())+'while: refreshTime_webradioMotion: modules_webradio ('+str(modules_webradio)+'), modules_webradiomotion ('+str(modules_webradiomotion)+'), not webradio_motionActive('+str(webradio_motionActive)+')'
if modules_webradio and modules_webradiomotion and not webradio_motionActive and webradio_active:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_webradioMotion: modules_webradio ('+str(modules_webradio)+'), modules_webradiomotion ('+str(modules_webradiomotion)+'), not webradio_motionActive('+str(webradio_motionActive)+'), webradio_active ('+str(webradio_active)+')'
(webradio_active, webradio_changed, webradio_result) = threadWebradioService('/var/sensorTool/www/webradio.station', webradio_active, True)
sH_Log.Add(' Webradio stopped, no motion')
refreshTime_webradioMotion = time.time() + INTERVAL_WEBRADIO
# every refreshtime seconds, get data from SensorService
if time.time() > refreshTime_sensors:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors ('+str(refreshTime_sensors)+')'
print str(refreshTime_sensors) + '##' + str(time.time())
# Werte holen per http request ::D
try:
dictResponse = {}
vBoolUseSensorValueFile = False
if vBoolUseSensorValueFile:
try:
handle = open ('/var/sensorTool/sensorValues', 'r')
line = " "
while line:
line = handle.readline()
print line
if len(line) > 0:
dictResponse = ast.literal_eval(line)
handle.close()
except Exception as e:
print str(e)
else:
# print 'Get HTTP Service'
r = requests.get("http://" + SERVER + ":6666")
# request enthaelt "
# print r
vR = sCrypt.Decrypt(r.text[1:len(r.text) - 1])
# print vR
dictResponse = ast.literal_eval(str(vR))
print 'dictResponse: ' + str(dictResponse)
for vSensor in dictResponse:
dictTemp = {}
dictTemp['ID'] = vSensor
if 'Time' in dictResponse[vSensor]:
dictTemp['Time'] = dictResponse[vSensor]['Time']
if 'RH' in dictResponse[vSensor]:
dictTemp['RH'] = dictResponse[vSensor]['RH']
if 'T' in dictResponse[vSensor]:
dictTemp['T'] = dictResponse[vSensor]['T']
if 'hPa' in dictResponse[vSensor]:
dictTemp['hPa'] = dictResponse[vSensor]['hPa']
boolBME280 = False
if (vSensor == 'bme280') :
boolBME280 = True
for vKey in dictSensors:
if dictSensors[vKey].GetHex() == vSensor:
dictSensors[vKey].SetSensorData(dictTemp, boolBME280)
#DeviceMotion.SetSensorData(motion_detected_report)
if modules_webradiomotion:
if motion_detected_report:
# if motion detected than increase refreshTime_webradioMotion by webradio_motionTimeOut
webradio_motionActive = True
refreshTime_webradioMotion = time.time() + webradio_motionTimeOut
print refreshTime_webradioMotion
else:
# no motion detected, but mobile device present, webradio must be acticve
if modules_LANDevices and LANDevices_Present:
webradio_motionActive = True
refreshTime_webradioMotion = time.time() + webradio_motionTimeOut
print refreshTime_webradioMotion
else:
webradio_motionActive = False
# Values for Diagramm
sPlottingTime = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
dictTemperature['time'].append(sPlottingTime)
dictRelativeHumidity['time'].append(sPlottingTime)
dictAbsoluteHumidity['time'].append(sPlottingTime)
dictPressure['time'].append(sPlottingTime)
for vKey in dictSensors:
sSensorName = dictSensors[vKey].GetSensor()
dictTemperature[sSensorName].SetModuleValue(dictSensors[vKey].GetT())
dictRelativeHumidity[sSensorName] .SetModuleValue(dictSensors[vKey].GetRH())
dictAbsoluteHumidity[sSensorName].SetModuleValue(dictSensors[vKey].GetAH())
if dictSensors[vKey].GetBME():
dictPressure[sSensorName].SetModuleValue(dictSensors[vKey].GetPA())
dictActiveModules['time'].append(sPlottingTime)
dictActiveModules['Motiondetector'].SetModuleStatus(motion_detected_report)
dictActiveModules['NetworkDeviceDetector'].SetModuleStatus(LANDevices_Present)
dictActiveModules['Webradio'].SetModuleStatus(webradio_active)
dictActiveModules['Radiators'].SetModuleStatus(RadiatorStarted)
dictActiveModules['ledalarm'].SetModuleStatus(ledalarm)
motion_detected_report = False
ledalarm = False
except Exception as e:
sH_Log.Add(str(e))
print e
# pass
# ############### Dynamic Config Start ###################
# Dynamische Config einlesen #soll ueber http request gesteuert werden.
dynamicConf = ConfigParser.ConfigParser()
dynamicConf.read('/var/sensorTool/www/dynamic.conf')
# Try to change the Relais Status
try:
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf'
if dynamicConf.getboolean('modules', 'modules_radiators') != modules_radiators:
modules_radiators = dynamicConf.getboolean('modules', 'modules_radiators')
sH_Log.Add(' modules_radiators set to ' + str(modules_radiators))
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_radiators('+str(modules_radiators)+')'
if dynamicConf.getboolean('modules', 'modules_motiondetector') != modules_motiondetector:
modules_motiondetector = dynamicConf.getboolean('modules', 'modules_motiondetector')
sH_Log.Add(' modules_motiondetector set to ' + str(modules_motiondetector))
if not modules_motiondetector and motion_detected:
motion_detected = False
motion_detected_report = False
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_motiondetector('+str(modules_motiondetector)+')'
if dynamicConf.getboolean('modules', 'modules_surveillance') != modules_surveillance:
modules_surveillance = dynamicConf.getboolean('modules', 'modules_surveillance')
sH_Log.Add(' modules_surveillance set to ' + str(modules_surveillance))
if not modules_surveillance and motion_detected:
motion_detected = False
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_surveillance('+str(modules_surveillance)+')'
if modules_surveillance and not modules_motiondetector:
sH_Log.Add(' modules_motiondetector set to ' + str(modules_motiondetector))
modules_motiondetector = True
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_surveillance: modules_motiondetector('+str(modules_motiondetector)+')'
if dynamicConf.getboolean('modules', 'modules_relais') != modules_relais:
modules_relais = dynamicConf.getboolean('modules', 'modules_relais')
sH_Log.Add(' modules_relais set to ' + str(modules_relais))
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_relais('+str(modules_relais)+')'
if dynamicConf.getboolean('modules', 'modules_fritzactors') != modules_fritzactors:
modules_fritzactors = dynamicConf.getboolean('modules', 'modules_fritzactors')
sH_Log.Add(' modules_fritzactors set to ' + str(modules_fritzactors))
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_fritzactors('+str(modules_fritzactors)+')'
if dynamicConf.getboolean('modules', 'modules_LANDevices') != modules_LANDevices:
modules_LANDevices = dynamicConf.getboolean('modules', 'modules_LANDevices')
sH_Log.Add(' modules_LANDevices set to ' + str(modules_LANDevices))
if not modules_LANDevices and LANDevices_Present:
LANDevices_Present = False
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_LANDevices('+str(modules_LANDevices)+')'
if dynamicConf.getboolean('modules', 'modules_webradio') != modules_webradio:
modules_webradio = dynamicConf.getboolean('modules', 'modules_webradio')
sH_Log.Add(' modules_webradio set to ' + str(modules_webradio))
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_webradio('+str(modules_webradio)+')'
if dynamicConf.getboolean('modules', 'modules_webradiomotion') != modules_webradiomotion:
modules_webradiomotion = dynamicConf.getboolean('modules', 'modules_webradiomotion')
sH_Log.Add(' modules_webradiomotion set to ' + str(modules_webradiomotion))
if not modules_webradiomotion and not webradio_motionActive:
webradio_motionActive = True
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_webradiomotion('+str(modules_webradiomotion)+')'
if modules_webradiomotion and not modules_webradio:
sH_Log.Add(' modules_webradio set to ' + str(modules_webradio))
modules_webradio = True
if vVerbose.startswith('test'):
print str(time.time())+'while: refreshTime_sensors: dynamicConf: modules_webradiomotion: modules_webradio('+str(modules_webradio)+')'
except Exception as e:
sH_Log.Add(str(e))
print e
pass
# iterate through sensor data
for vKey in dictSensors:
try:
vSetHighRH = False
if dictSensors[vKey].GetThresholdHighHumidity() == False:
vSetHighRH = True
elif dynamicConf.getfloat('Sensor' + str(vKey), 'threshold_high_humidity') != dictSensors[vKey].GetThresholdHighHumidity():
vSetHighRH = True
if vSetHighRH:
dictSensors[vKey].SetThresholdHighHumidity(dynamicConf.getfloat('Sensor' + str(vKey), 'threshold_high_humidity'))
sH_Log.Add('Threshold High RH S' + str(vKey) + ':' + str(dictSensors[vKey].GetThresholdHighHumidity()))
if vVerbose.startswith('test'):
print 'Threshold High RH S' + str(vKey) + ':' + str(dictSensors[vKey].GetThresholdHighHumidity())
except:
pass # Exception, e:
try:
vSetLowT = False
if dictSensors[vKey].GetThresholdLowTemperature() == False:
vSetLowT = True
elif dynamicConf.getfloat('Sensor' + str(vKey), 'threshold_low_temperature') != dictSensors[vKey].GetThresholdLowTemperature():
vSetLowT = True
if vSetHighRH:
dictSensors[vKey].SetThresholdLowTemperature(dynamicConf.getfloat('Sensor' + str(vKey), 'threshold_low_temperature'))
sH_Log.Add('Threshold Low T S' + str(vKey) + ':' + str(dictSensors[vKey].GetThresholdLowTemperature()))
if vVerbose == 'test':
print 'Threshold Low T S' + str(vKey) + ':' + str(dictSensors[vKey].GetThresholdLowTemperature())
except:
pass # Exception, e:
# ############### Dynamic Config Ende ###################
if vBoolLC == True:
sendWhatsApps(dictSensors[iControlSensor].GetMobiles(), getInfoText("Active"))
sH_Log.Add('sensorTool started')
#LedY.off()
vBoolLC = False
# ############### Check for Radiator interval start ##################
vBoolInterval = dictInterval.isNowInInterval()
# ############### Check for Radiator interval Ende ##################
if vVerbose.startswith('test1'):
print( 'Threshold: ' + str(dictSensors[iControlSensor].GetThreshold()) + ' RadiatorStarted: ' + str(RadiatorStarted) + ' vBoolInterval: ' + str(vBoolInterval) +' modules_radiators: ' + str(modules_radiators))
boolTriggerOutdoor = True
if boolOutdoorSensor:
deltaAH = 1.0 #0.5
if dictSensors[iControlSensor].GetAH() is not None and dictSensors[iOutdoorSensor].GetAH() is not None:
if (float(dictSensors[iControlSensor].GetAH())) < (float(dictSensors[iOutdoorSensor].GetAH())+ deltaAH):
boolTriggerOutdoor = False
if dictSensors[iControlSensor].GetThreshold() and RadiatorStarted == False and vBoolInterval and modules_radiators and boolTriggerOutdoor:
counterHigh = counterHigh + 1
#if LedY.isOn == False:
# LedY.on()
# sH_Log.Add(dictSensors[iControlSensor].GetSensor() + ' Yellow LED (H)!')
else:
counterHigh = 0
if dictSensors[iControlSensor].GetThreshold() == False and RadiatorStarted and vBoolInterval and modules_radiators and boolTriggerOutdoor:
counterLow = counterLow + 1
#if LedY.isOn == False:
# LedY.on()
# sH_Log.Add(dictSensors[iControlSensor].GetSensor() + ' Yellow LED (L)!')
else:
counterLow = 0
if vVerbose.startswith('test1'):
print( 'counterLow: ' + str(counterLow) + ' counterHigh: ' + str(counterHigh) )
boolInfoLight = False
# Alle anderen Sensoren (außer Keller) durchgehen
for vKey in dictSensors:
# if dictSensors[vKey].GetBME() != True:
if iControlSensor <> vKey:
vBoolThreshold = dictSensors[vKey].GetThreshold()
# Grenzwert
if vBoolThreshold and dictSensors[vKey].GetSendMessage():
sendWhatsApps(dictSensors[vKey].GetMobiles(), dictSensors[vKey].GetMessage())
dictSensors[vKey].SetSendMessage(False)
sH_Log.Add(dictSensors[vKey].GetSensor() + dictSensors[vKey].GetMessage())
boolInfoLight = True
elif dictSensors[vKey].GetSendMessage() == False:
dictSensors[vKey].SetSendMessage(True)
# Delta
vBoolDelta = dictSensors[vKey].GetDelta()
counterDelta = dictSensors[vKey].GetDeltaCounter()
if vBoolDelta and dictSensors[vKey].GetDeltaCounter() == 0:
dictSensors[vKey].SetDeltaCounter(counterDelta + 1)
elif counterDelta > 0:
if counterDelta > dictSensors[vKey].GetTriggerCount():
sendWhatsApps(dictSensors[vKey].GetMobiles(), dictSensors[vKey].GetDeltaMessage())
sH_Log.Add(dictSensors[vKey].GetDeltaMessage())
dictSensors[vKey].SetDeltaCounter(0)
boolInfoLight = True
else:
dictSensors[vKey].SetDeltaCounter(counterDelta + 1)
if boolInfoLight:
if not boolLeuchte:
boolLeuchte = Leuchte.activate(boolLeuchte)
ledalarm = True
else:
if boolLeuchte:
boolLeuchte = Leuchte.deactivate(boolLeuchte)
if counterHigh == TriggerCountA and RadiatorStarted == False and vBoolInterval and modules_radiators and boolTriggerOutdoor:
dictActors[dictSensors[iControlSensor].GetFritzActor()].SetActor(True)
#LedR.on()
#LedY.off()
RadiatorStarted = True
sendWhatsApps(dictSensors[iControlSensor].GetMobiles(), getInfoText("Started"))
sH_Log.Add('Radiator activated')
elif (counterLow == TriggerCountB or vBoolInterval == False or modules_radiators == False or boolTriggerOutdoor == False) and RadiatorStarted:
dictActors[dictSensors[iControlSensor].GetFritzActor()].SetActor(False)
#LedR.off()
#LedY.off()
RadiatorStarted = False
sendWhatsApps(dictSensors[iControlSensor].GetMobiles(), getInfoText("Stopped"))
sH_Log.Add('Radiator deactivated')
if vVerbose.startswith('test'):
#print getInfoText("Status")
print '##### - #####'
threadCreatePHPFile('/var/sensorTool/www/sensor.php', getHTML())
# thread.start_new_thread(threadCreatePHPFile, ('/var/sensorTool/www/sensor.php', getHTML(),))
plotting_DeviceValues(dictActiveModules, '1_Modules', vVerbose)
plotting_DeviceValues(dictTemperature, '3_Temperature', vVerbose, -20, 50)
plotting_DeviceValues(dictRelativeHumidity, '4_Relative_Humidity', vVerbose)
plotting_DeviceValues(dictAbsoluteHumidity, '2_Absolute_Humidity', vVerbose, 0, 30)
plotting_DeviceValues(dictPressure, '9_Pressure', vVerbose, 700, 1100)
# thread.start_new_thread(plotting, (dictPlotSens, dictPlotAussen, vVerbose,))
refreshTime_sensors = time.time() + INTERVAL_SENSORS
sleep(1) # 0.1
if RadiatorStarted:
dictActors[dictSensors[iControlSensor].GetFritzActor()].SetActor(False)
sendWhatsApps(dictSensors[iControlSensor].GetMobiles(), getInfoText("Inactive - going down"))
if vVerbose.startswith('test'):
print 'GPIO Down'
#LedY.off()
#LedR.off()
#LedG.off()
if boolLeuchte:
Leuchte.deactivate(boolLeuchte)
if not vVerbose.startswith('test'):
os.system("sudo init 0")
else:
print ' - Ende - '
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4