forked from Hydrosys4/Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautowateringmod.py
588 lines (503 loc) · 27.5 KB
/
autowateringmod.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
import logging
from datetime import datetime, time ,timedelta
import hardwaremod
import os
import subprocess
import emailmod
import autowateringdbmod
import sensordbmod
import actuatordbmod
import autofertilizermod
logger = logging.getLogger("hydrosys4."+__name__)
# status array, required to check the ongoing actions within a watering cycle
elementlist= autowateringdbmod.getelementlist()
AUTO_data={} # dictionary of dictionary
for element in elementlist:
AUTO_data[element]={"cyclestartdate":datetime.now(),"lastwateringtime":datetime.now(),"cyclestatus":"done", "checkcounter":0, "alertcounter":0, "watercounter":0}
allowwateringplan={} # define th flat that control the waterscheduling activation
# cyclestartdate, datetime of the latest cycle start
# cyclestatus, describe the status of the cycle: lowthreshold, rampup, done
# "lowthreshold" means that the cycle is just started with lowthreshold activation, if the lowthreshold persists for several checks them alarm should be issued
# "rampup", this is in full auto mode, the status in between the lowthreshold and high, not reach yet high. if this status persist then alarm should be issued
# "done", ready for next start with lowthreshold
# sample of database filed
# {"element": "", "threshold": ["2.0", "4.0"],"workmode": "None","sensor": "","wtstepsec": "100","maxstepnumber": "3","allowedperiod": ["21:00","05:00"],"maxdaysbetweencycles": "10", "pausebetweenwtstepsmin":"45", "mailalerttype":"warningonly" , "sensorminacceptedvalue":"0.5"}
def cyclereset(element):
global AUTO_data
waitingtime=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
AUTO_data[element]={"cyclestartdate":datetime.now(),"lastwateringtime":datetime.now() - timedelta(minutes=waitingtime),"cyclestatus":"done", "checkcounter":0, "alertcounter":0, "watercounter":0}
def cycleresetall():
global AUTO_data
elementlist= autowateringdbmod.getelementlist()
for element in elementlist:
AUTO_data[element]={"cyclestartdate":datetime.now(),"lastwateringtime":datetime.now(),"cyclestatus":"done", "checkcounter":0, "alertcounter":0, "watercounter":0}
def autowateringcheck(refsensor):
logger.info('Starting Autowatering Evaluation ')
# iterate among the water actuators
elementlist= autowateringdbmod.getelementlist()
for element in elementlist:
autowateringexecute(refsensor,element)
return
def autowateringexecute(refsensor,element):
global AUTO_data
sensor=autowateringdbmod.searchdata("element",element,"sensor")
# check the sensor
if refsensor==sensor:
print "auto watering check -----------------------------------------> ", element
logger.info('auto watering check --------------------------> %s', element)
print AUTO_data[element]
# check the watering mode
modelist=["None", "Full Auto" , "Emergency Activation" , "Alert Only"]
workmode=checkworkmode(element)
if not(sensor in sensordbmod.gettablelist()):
print "Sensor does not exist " ,sensor , ", element: " , element
logger.error("Sensor does not exist %s , element: %s " ,sensor, element)
return "sensor not Exist"
maxthreshold=hardwaremod.tonumber(autowateringdbmod.searchdata("element",element,"threshold")[1],0)
minthreshold=hardwaremod.tonumber(autowateringdbmod.searchdata("element",element,"threshold")[0],maxthreshold)
# exit condition in case of data inconsistency
if minthreshold>=maxthreshold:
print "Data inconsistency , element: " , element
logger.error("Data inconsistency , element: %s " , element)
return "data inconsistency"
now = datetime.now()
nowtime = now.time()
starttimeh=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"allowedperiod")[0].split(":")[0],0)
starttimem=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"allowedperiod")[0].split(":")[1],0)
endtimeh=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"allowedperiod")[1].split(":")[0],1)
endtimem=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"allowedperiod")[1].split(":")[1],0)
starttime=time(starttimeh,starttimem)
endtime=time(endtimeh,endtimem)
duration=1000*hardwaremod.toint(autowateringdbmod.searchdata("element",element,"wtstepsec"),0)
maxstepnumber=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"maxstepnumber"),0)
maxdays=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"maxdaysbetweencycles"),0)
waitingtime=hardwaremod.toint(autowateringdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
mailtype=autowateringdbmod.searchdata("element",element,"mailalerttype")
minaccepted=hardwaremod.tonumber(autowateringdbmod.searchdata("element",element,"sensorminacceptedvalue"),0.1)
# ------------------------ Workmode split
if workmode=="Full Auto":
# block the wateringplan activation as by definition of "Full Auto"
allowwateringplan[element]=False
# check if inside the allowed time period
print "full Auto Mode"
logger.info('full auto mode --> %s', element)
timeok=isNowInTimePeriod(starttime, endtime, nowtime)
print "inside allowed time ", timeok , " starttime ", starttime , " endtime ", endtime
logger.info('full auto mode')
if timeok:
logger.info('inside allowed time')
belowthr,valid=checkminthreshold(sensor,minthreshold,minaccepted)
if valid:
if belowthr:
status="lowthreshold"
logger.info('below threshold')
# wait to seek a more stable reading of hygrometer
# check if time between watering events is larger that the waiting time (minutes)
print ' Previous watering: ' , AUTO_data[element]["lastwateringtime"] , ' Now: ', datetime.now()
timedifference=sensordbmod.timediffinminutes(AUTO_data[element]["lastwateringtime"],datetime.now())
print 'Time interval between watering steps', timedifference ,'. threshold', waitingtime
logger.info('Time interval between watering steps %d threshold %d', timedifference,waitingtime)
if timedifference>waitingtime:
print " Sufficient waiting time"
logger.info('Sufficient waiting time')
# activate watering in case the maxstepnumber is not exceeded
if maxstepnumber>AUTO_data[element]["watercounter"]:
#activate pump
activatewater(element, duration)
# invia mail, considered as info, not as alert
if mailtype!="warningonly":
textmessage="INFO: " + sensor + " value below the minimum threshold " + str(minthreshold) + ", activating the watering :" + element
emailmod.sendallmail("alert", textmessage)
AUTO_data[element]["watercounter"]=AUTO_data[element]["watercounter"]+1
AUTO_data[element]["lastwateringtime"]=datetime.now()
else: # critical, sensor below minimum after all watering activations are done
logger.info('Number of watering time per cycle has been exceeeded')
# read hystory data and calculate the slope
timelist=hardwaremod.gettimedata(sensor)
startdate=AUTO_data[element]["cyclestartdate"] - timedelta(minutes=timelist[1])
enddate=AUTO_data[element]["lastwateringtime"] + timedelta(minutes=waitingtime)
isslopeok=checkinclination(sensor,startdate,enddate) # still to decide if use the enddate
if isslopeok:
# invia mail if couner alert is lower than 1
if AUTO_data[element]["alertcounter"]<1:
textmessage="WARNING: Please consider to increase the amount of water per cycle, the "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times. System will automatically reset the watering cycle to allow more water"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# reset watering cycle
status="done"
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["checkcounter"]=-1
AUTO_data[element]["alertcounter"]=0
AUTO_data[element]["cyclestartdate"]=datetime.now()
else: # slope not OK, probable hardware problem
if AUTO_data[element]["alertcounter"]<3:
textmessage="CRITICAL: Possible hardware problem, "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# update the status
AUTO_data[element]["cyclestatus"]=status
AUTO_data[element]["checkcounter"]=AUTO_data[element]["checkcounter"]+1
# RAMPUP case above threshold but below maxthreshold
elif sensorreading(sensor)<maxthreshold: # intermediate state where the sensor is above the minthreshold but lower than the max threshold
# check the status of the automatic cycle
if AUTO_data[element]["cyclestatus"]!="done":
status="rampup"
# wait to seek a more stable reading of hygrometer
# check if time between watering events is larger that the waiting time (minutes)
if sensordbmod.timediffinminutes(AUTO_data[element]["lastwateringtime"],datetime.now())>waitingtime:
if maxstepnumber>AUTO_data[element]["watercounter"]:
#activate pump
activatewater(element, duration)
# invia mail, considered as info, not as alert
if mailtype!="warningonly":
textmessage="INFO: " + sensor + " value below the Maximum threshold " + str(maxthreshold) + ", activating the watering :" + element
emailmod.sendallmail("alert", textmessage)
AUTO_data[element]["watercounter"]=AUTO_data[element]["watercounter"]+1
AUTO_data[element]["lastwateringtime"]=datetime.now()
else:
# give up to reache the maximum threshold, proceed as done, send alert
logger.info('Number of watering time per cycle has been exceeeded')
# invia mail if couner alert is lower than 1 --------------
# only if the info is activated
if mailtype!="warningonly":
if AUTO_data[element]["alertcounter"]<2:
textmessage="INFO "+ sensor + " value below the Maximum threshold " + str(maxthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# reset watering cycle
status="done"
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["checkcounter"]=-1
AUTO_data[element]["alertcounter"]=0
AUTO_data[element]["cyclestartdate"]=datetime.now()
# update the status
AUTO_data[element]["cyclestatus"]=status
AUTO_data[element]["checkcounter"]=AUTO_data[element]["checkcounter"]+1
else:
# update the status, reset cycle
AUTO_data[element]["cyclestatus"]="done"
AUTO_data[element]["checkcounter"]=0
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["alertcounter"]=0
elif workmode=="Emergency Activation":
# check if inside the allow time period
logger.info('Emergency Mode')
timeok=isNowInTimePeriod(starttime, endtime, nowtime)
print "inside allowed time ", timeok , " starttime ", starttime , " endtime ", endtime
if timeok:
belowthr,valid=checkminthreshold(sensor,minthreshold,minaccepted)
if valid:
if belowthr:
# wait to seek a more stable reading of hygrometer
# check if time between watering events is larger that the waiting time (minutes)
if sensordbmod.timediffinminutes(AUTO_data[element]["lastwateringtime"],datetime.now())>waitingtime:
# activate watering in case the maxstepnumber is not exceeded
if maxstepnumber>AUTO_data[element]["watercounter"]:
#activate pump
activatewater(element, duration)
# invia mail, considered as info, not as alert
if mailtype!="warningonly":
textmessage="INFO: " + sensor + " value below the minimum threshold " + str(minthreshold) + ", activating the watering :" + element
emailmod.sendallmail("alert", textmessage)
AUTO_data[element]["watercounter"]=AUTO_data[element]["watercounter"]+1
AUTO_data[element]["lastwateringtime"]=datetime.now()
else:
logger.info('Number of watering time per cycle has been exceeeded')
# read hystory data and calculate the slop
timelist=hardwaremod.gettimedata(sensor)
startdate=AUTO_data[element]["cyclestartdate"] - timedelta(minutes=timelist[1])
enddate=AUTO_data[element]["lastwateringtime"] + timedelta(minutes=waitingtime)
isslopeok=checkinclination(sensor,startdate,enddate)
if isslopeok:
# invia mail if couner alert is lower than 1
if AUTO_data[element]["alertcounter"]<1:
textmessage="WARNING: Please consider to increase the amount of water per cycle, the "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times. System will automatically reset the watering cycle to allow more water"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# reset watering cycle
status="done"
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["checkcounter"]=-1
AUTO_data[element]["alertcounter"]=0
AUTO_data[element]["cyclestartdate"]=datetime.now()
else: # slope not OK, probable hardware problem
if AUTO_data[element]["alertcounter"]<3:
textmessage="CRITICAL: Possible hardware problem, "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# update the status
AUTO_data[element]["cyclestatus"]="lowthreshold"
AUTO_data[element]["checkcounter"]=AUTO_data[element]["checkcounter"]+1
else:
# update the status
AUTO_data[element]["cyclestatus"]="done"
AUTO_data[element]["checkcounter"]=0
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["alertcounter"]=0
elif workmode=="under MIN over MAX":
logger.info('under MIN over MAX')
# normally watering plan is allowed unless over MAX threshold
allowwateringplan[element]=True
# check if inside the allow time period
timeok=isNowInTimePeriod(starttime, endtime, nowtime)
print "inside allowed time ", timeok , " starttime ", starttime , " endtime ", endtime
if timeok:
logger.info('Insede operative time')
belowthr,valid=checkminthreshold(sensor,minthreshold,minaccepted)
if valid:
logger.info('valid sensor reading')
if belowthr:
logger.info('sensor reading below threshold')
# wait to seek a more stable reading of hygrometer
# check if time between watering events is larger that the waiting time (minutes)
if sensordbmod.timediffinminutes(AUTO_data[element]["lastwateringtime"],datetime.now())>waitingtime:
logger.info('enough time between activations')
# activate watering in case the maxstepnumber is not exceeded
if maxstepnumber>AUTO_data[element]["watercounter"]:
logger.info('water Count not exceeded')
#activate pump
activatewater(element, duration)
# invia mail, considered as info, not as alert
if mailtype!="warningonly":
textmessage="INFO: " + sensor + " value below the minimum threshold " + str(minthreshold) + ", activating the watering :" + element
emailmod.sendallmail("alert", textmessage)
AUTO_data[element]["watercounter"]=AUTO_data[element]["watercounter"]+1
AUTO_data[element]["lastwateringtime"]=datetime.now()
else:
logger.info('Number of watering time per cycle has been exceeeded')
# read hystory data and calculate the slope
timelist=hardwaremod.gettimedata(sensor)
startdate=AUTO_data[element]["cyclestartdate"] - timedelta(minutes=timelist[1])
enddate=AUTO_data[element]["lastwateringtime"] + timedelta(minutes=waitingtime)
isslopeok=checkinclination(sensor,startdate,enddate)
if isslopeok:
# invia mail if couner alert is lower than 1
if AUTO_data[element]["alertcounter"]<1:
textmessage="WARNING: Please consider to increase the amount of water per cycle, the "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times. System will automatically reset the watering cycle to allow more water"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# reset watering cycle
status="done"
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["checkcounter"]=-1
AUTO_data[element]["alertcounter"]=0
AUTO_data[element]["cyclestartdate"]=datetime.now()
else: # slope not OK, probable hardware problem
if AUTO_data[element]["alertcounter"]<3:
textmessage="CRITICAL: Possible hardware problem, "+ sensor + " value below the MINIMUM threshold " + str(minthreshold) + " still after activating the watering :" + element + " for " + str(maxstepnumber) + " times"
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# update the status
AUTO_data[element]["cyclestatus"]="lowthreshold"
AUTO_data[element]["checkcounter"]=AUTO_data[element]["checkcounter"]+1
else: # above minimum threshold
logger.info('sensor reading above min threshold')
# update the status
AUTO_data[element]["cyclestatus"]="done"
AUTO_data[element]["checkcounter"]=0
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["alertcounter"]=0
if sensorreading(sensor)>maxthreshold:
logger.info('sensor reading above MAX threshold, deactivate scheduled irrigation')
# do not activate the irrigation scheduled in the time plan
allowwateringplan[element]=False
elif workmode=="Alert Only":
belowthr,valid=checkminthreshold(sensor,minthreshold,minaccepted)
if valid:
if belowthr:
# invia mail if couter alert is lower than
if AUTO_data[element]["alertcounter"]<2:
textmessage="WARNING "+ sensor + " value below the minimum threshold " + str(minthreshold) + " watering system: " + element
print textmessage
#send alert mail notification
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
# update the status
AUTO_data[element]["cyclestatus"]="lowthreshold"
AUTO_data[element]["checkcounter"]=AUTO_data[element]["checkcounter"]+1
else:
# update the status
AUTO_data[element]["cyclestatus"]="done"
AUTO_data[element]["checkcounter"]=0
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["alertcounter"]=0
else: # None case
print "No Action required, workmode set to None, element: " , element
logger.info("No Action required, workmode set to None, element: %s " , element)
if AUTO_data[element]["cyclestatus"]=="lowthreshold":
if AUTO_data[element]["checkcounter"]==1:
AUTO_data[element]["cyclestartdate"]=datetime.now()
# implment alert message for the cycle exceeding days, and reset the cycle
if workmode!="None":
timedeltadays=sensordbmod.timediffdays(datetime.now(),AUTO_data[element]["cyclestartdate"])
if (timedeltadays > maxdays): #the upper limit is set in case of abrupt time change
textmessage="WARNING "+ sensor + " watering cycle is taking too many days, watering system: " + element + ". Reset watering cycle"
print textmessage
# in case of full Auto, activate pump for minimum pulse period
if workmode=="Full Auto":
if (timedeltadays < maxdays+2): #the upper limit is set in case of abrupt time change
textmessage="WARNING "+ sensor + " watering cycle is taking too many days, watering system: " + element + ". Activate Min pulse + Reset watering cycle"
activatewater(element, duration)
#send alert mail notification
if mailtype!="warningonly":
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
logger.error("Cycle started %s, Now is %s ", AUTO_data[element]["cyclestartdate"].strftime("%Y-%m-%d %H:%M:%S"), datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# reset cycle
AUTO_data[element]["cyclestatus"]="done"
AUTO_data[element]["checkcounter"]=0
AUTO_data[element]["watercounter"]=0
AUTO_data[element]["alertcounter"]=0
AUTO_data[element]["cyclestartdate"]=datetime.now()
# implment Critical alert message in case the threshold is below the 0.5 of the minimum
if workmode!="None":
belowthr,valid=checkminthreshold(sensor,minthreshold*0.5,minaccepted)
if valid:
if belowthr:
logger.info('sensor %s below half of the actual set threshold', sensor)
textmessage="CRITICAL: Plant is dying, "+ sensor + " reading below half of the minimum threshold, need to check the " + element
print textmessage
#send alert mail notification
if AUTO_data[element]["alertcounter"]<5:
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
else:
logger.info('sensor %s below valid data', sensor)
textmessage="WARNING: "+ sensor + " below valid data range, need to check sensor"
print textmessage
#send alert mail notification
if AUTO_data[element]["alertcounter"]<3:
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["alertcounter"]+1
return
def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
def checkminthreshold(sensor,minthreshold,minaccepted):
belowthr=False
validity=True
# check the hygrometer sensor levels
sensorreadingaverage=sensorreading(sensor)
# if the average level after 4 measure (15 min each) is below threshold apply emergency
print " Min accepted threshold " , minaccepted
if (sensorreadingaverage>minaccepted):
if (sensorreadingaverage>minthreshold):
logger.info('Soil moisture check, Sensor reading=%s > Minimum threshold=%s ', str(sensorreadingaverage), str(minthreshold))
print 'Soil moisture check, Sensor reading=%s > Minimum threshold=%s '
else:
logger.warning('Soil moisture check, Sensor reading=%s < Minimum threshold=%s ', str(sensorreadingaverage), str(minthreshold))
logger.info('Start watering procedure ')
print 'Soil moisture check, activating watering procedure '
belowthr=True
else:
logger.warning('Sensor reading lower than acceptable values %s no action', str(sensorreadingaverage))
print 'Sensor reading lower than acceptable values ', sensorreadingaverage ,' no action'
validity=False
return belowthr, validity
def checkinclination(sensorname,startdate,enddate):
# startdate is UTC now, need to be evaluated
print "Check inclination of the hygrometer curve after watering done " , sensorname
logger.info('Check inclination of the hygrometer curve after watering done: %s', sensorname)
logger.info('Start eveluation from %s to %s', startdate.strftime("%Y-%m-%d %H:%M:%S") , enddate.strftime("%Y-%m-%d %H:%M:%S"))
print "Start eveluation from " , startdate.strftime("%Y-%m-%d %H:%M:%S") , " to " , enddate.strftime("%Y-%m-%d %H:%M:%S")
datax=[]
datay=[]
if sensorname:
lenght=sensordbmod.getSensorDataPeriodXminutes(sensorname,datax,datay,startdate,enddate)
print "datax ", datax
print "datay ", datay
if lenght>0:
avex=sum(datax)/float(lenght)
avey=sum(datay)/float(lenght)
print " Average: " , avex, " " , avey
num=0
den=0
for inde in range(len(datax)):
x2=(datax[inde]-avex)*(datax[inde]-avex)
xy=(datax[inde]-avex)*(datay[inde]-avey)
num=num+xy
den=den+x2
print " ----> Lenght " ,lenght , " num: ", num , " Den: ", den
if den>0.0001: # this is to avoid problem with division
slope=num/den
logger.info('Inclination value: %.4f', slope)
print " Slope Value" , slope
# here an arbitray min slope that should be reasonable for a working system
yvolt=0.2
xminute=2*60
minslope=yvolt/xminute # 0.2 volt per 2 hours
if slope>minslope:
logger.info('Inclination value %.4f above the min reference %.4f (0.2 volt per 2 hours), restaring watering cycle',slope,minslope)
return True
return False
def sensorreading(sensorname):
MinutesOfAverage=70 #about one hour, 4 samples at 15min samples rate
if sensorname:
# old
#sensordata=[]
#sensordbmod.getsensordbdata(sensorname,sensordata)
# get number of samples
timelist=hardwaremod.gettimedata(sensorname)
theinterval=timelist[1] # minutes
if theinterval>0:
samplesnumber=int(MinutesOfAverage/theinterval+1)
else:
samplesnumber=1
# new procedure should be faster on database reading for large amount of data
sensordata=[]
sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
# still necessary to filter the sample based on timestamp, due to the possibility of missing samples
starttimecalc=datetime.now()-timedelta(minutes=int(MinutesOfAverage))
quantity=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datetime.now())["max"]
return quantity
def lastsensorreading(sensorname):
if sensorname:
sensordata=[]
sensordbmod.getsensordbdata(sensorname,sensordata)
data=sensordata[-1]
try:
number=float(data[1])
except:
number=0
return number
def checkworkmode(element):
return autowateringdbmod.searchdata("element",element,"workmode")
def activatewater(element, duration):
# check the activation of the doser before the pump
doseron=autofertilizermod.checkactivate(element,duration)
#activate pump
hardwaremod.makepulse(element,duration)
# salva su database
logger.info('%s Pump ON, optional time for msec = %s', element, duration)
print 'Pump ON, optional time for msec =', duration
actuatordbmod.insertdataintable(element,duration)
if __name__ == '__main__':
"""
prova functions
"""