-
Notifications
You must be signed in to change notification settings - Fork 37
/
automationmod.py
executable file
·307 lines (239 loc) · 11.7 KB
/
automationmod.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
from __future__ import print_function
from __future__ import division
from builtins import str
from builtins import range
from past.utils import old_div
import logging
from datetime import datetime, time ,timedelta
import hardwaremod
import os
import subprocess
import emailmod
import automationdbmod
import sensordbmod
import actuatordbmod
import autofertilizermod
import statusdataDBmod
import math
import threading
import ActuatorControllermod
logger = logging.getLogger("hydrosys4."+__name__)
# status array, required to check the ongoing actions
elementlist= automationdbmod.getelementlist()
AUTO_data={} # dictionary of dictionary
AUTO_data["default"]={"lastactiontime":datetime.utcnow(),"actionvalue":0, "alertcounter":0, "infocounter":0, "status":"ok"}
def cyclereset(element):
global AUTO_data
waitingtime=hardwaremod.toint(automationdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
statusdataDBmod.write_status_data(AUTO_data,element,"lastactiontime",datetime.utcnow() - timedelta(minutes=waitingtime))
statusdataDBmod.write_status_data(AUTO_data,element,"status","ok")
statusdataDBmod.write_status_data(AUTO_data,element,"actionvalue",0)
statusdataDBmod.write_status_data(AUTO_data,element,"alertcounter",0)
statusdataDBmod.write_status_data(AUTO_data,element,"infocounter",0)
def cycleresetall():
elementlist= automationdbmod.getelementlist()
for element in elementlist:
waitingtime=hardwaremod.toint(automationdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
#print("Cycle reset all ------------------------------------_>", waitingtime , " ", element)
cyclereset(element)
def automationcheck(refsensor):
logger.info('Starting Automation Evaluation, Sensor: %s' , refsensor)
# iterate among the actuators
elementlist= automationdbmod.getelementlist()
for element in elementlist:
automationexecute(refsensor,element)
return
def automationexecute(refsensor,element):
sensor=automationdbmod.searchdata("element",element,"sensor")
# check the sensor
if refsensor==sensor:
logger.info('automation Pairing OK ---> Actuator: %s , Sensor: %s', element, sensor)
# check the watering mode
modelist=["None", "Full Auto" , "Emergency Activation" , "Alert Only"]
workmode=checkworkmode(element)
if (workmode=="None"):
# None case
print("No Action required, workmode set to None, element: " , element)
# logger.info("No Action required, workmode set to None, element: %s " , element)
return
if (workmode==""):
logger.info("Not able to get the workmode: %s " , element)
return
logger.info('Automantion, Get all the parameters')
sensormaxthreshold=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"sensor_threshold")[1],0)
sensorminthreshold=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"sensor_threshold")[0],sensormaxthreshold)
actuatormaxthreshold=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"actuator_threshold")[1],0)
actuatorminthreshold=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"actuator_threshold")[0],actuatormaxthreshold)
# evaluate variables for operational period check
starttime = datetime.strptime(automationdbmod.searchdata("element",element,"allowedperiod")[0], '%H:%M').time()
endtime = datetime.strptime(automationdbmod.searchdata("element",element,"allowedperiod")[1], '%H:%M').time()
# get other parameters
maxstepnumber=hardwaremod.toint(automationdbmod.searchdata("element",element,"stepnumber"),1)
waitingtime=hardwaremod.toint(automationdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),1)
mailtype=automationdbmod.searchdata("element",element,"mailalerttype")
averageminutes=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"averagesample"),1)
mathoperation=automationdbmod.searchdata("element",element,"mathoperation")
# new parameter for the activationdelay
activationdelay=hardwaremod.tonumber(automationdbmod.searchdata("element",element,"activationdelay"),0)
# check sensor timetrigger
sensorcontrolcommand=hardwaremod.searchdata(hardwaremod.HW_INFO_NAME,refsensor,hardwaremod.HW_CTRL_CMD)
logger.info('Sensor control command: %s , Sensor: %s', sensorcontrolcommand, sensor)
if sensorcontrolcommand=="returnzero":
logger.info('Modify parameter for the timetrigger')
#adjust the parameters in the way the activation condition is always obtained
sensormaxthreshold=1
sensorminthreshold=-1
maxstepnumber=1
averageminutes=0
# Calculated Variables
if maxstepnumber<1:
# not possible to proceed
print("No Action required, maxstepnumber <1, element: " , element)
logger.info("No Action required, maxstepnumber <1, element: %s " , element)
return
interval=(sensormaxthreshold-sensorminthreshold)/maxstepnumber
actuatorinterval=(actuatormaxthreshold-actuatorminthreshold)/maxstepnumber
P=[]
for I in range(0, maxstepnumber+1): # I goes from 0 to maxstepnumber (tihs is how range works)
P.append(int(actuatorminthreshold+I*actuatorinterval))
# ------------------------ Automation alghoritm
if workmode=="Full Auto":
# check if inside the allowed time period
print("full Auto Mode")
logger.info('full auto mode --> %s', element)
timeok=isNowInTimePeriod(starttime, endtime, datetime.now().time()) # don't use UTC here!
print("inside allowed time ", timeok , " starttime ", starttime , " endtime ", endtime)
if timeok:
logger.info('inside allowed time')
isok,sensorvalue=sensorreading(sensor,averageminutes,mathoperation) # operation of sensor readings for a number of sample
if isok:
print("Sensor Value ", sensorvalue)
if sensorminthreshold<=sensormaxthreshold:
print("Algorithm , element: " , element)
logger.info("Forward algorithm , element: %s " , element)
else: # to be added case of inverse sensor condition, where the sensorminthreshold is higher than the sensormaxthreshold
print("Reverse Algorithm , element: " , element)
logger.info("Reverse Algorithm , element: %s " , element)
Inde=math.ceil((sensorvalue-sensorminthreshold)/interval)
if Inde<0:
status="belowthreshold"
logger.info('under left threshold')
Inde=0
if Inde>maxstepnumber:
logger.info('beyond right threshold')
Inde=maxstepnumber
value=P[Inde]
# END MAIN ALGORITHM
logger.info('Activation phase Value=%d',value)
# do relevant stuff with delay
argvect=[element,waitingtime,value,mailtype,sensor,sensorvalue]
t = threading.Timer(activationdelay, CheckActivateNotify, argvect).start()
#CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
else:
logger.error('No valid calculation operation on the stored sensor data')
else:
logger.info('Outside allowed Time, Stop')
elif workmode=="Emergency Activation":
print("Emergency Activation")
elif workmode=="Alert Only":
print("Alert Only")
# implment Critical alert message in case the sensor value is one interval more than Max_threshold
isok,sensorvalue=sensorreading(sensor,averageminutes,mathoperation) # operation of sensor readings for a number of sample
if isok:
if sensorminthreshold<=sensormaxthreshold:
if sensorvalue>sensormaxthreshold+interval:
logger.info('sensor %s exceeding limits', sensor)
textmessage="CRITICAL: "+ sensor + " reading " + str(sensorvalue) + " exceeding threshold limits, need to check the " + element
print(textmessage)
#send alert mail notification
alertcounter=statusdataDBmod.read_status_data(AUTO_data,element,"alertcounter")
if alertcounter<2:
if (mailtype!="none"):
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
statusdataDBmod.write_status_data(AUTO_data,element,"alertcounter",alertcounter+1)
else:
if sensorvalue<sensormaxthreshold+interval:
logger.info('sensor %s exceeding limits', sensor)
textmessage="CRITICAL: "+ sensor + " reading " + str(sensorvalue) + " exceeding threshold limits, need to check the " + element
print(textmessage)
#send alert mail notification
alertcounter=statusdataDBmod.read_status_data(AUTO_data,element,"alertcounter")
if alertcounter<2:
if (mailtype!="none"):
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
statusdataDBmod.write_status_data(AUTO_data,element,"alertcounter",alertcounter+1)
return
def CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue):
global AUTO_data
# check if time between watering events is larger that the waiting time (minutes)
lastactiontime=statusdataDBmod.read_status_data(AUTO_data,element,"lastactiontime")
print(' Previous action: ' , lastactiontime , ' Now: ', datetime.utcnow())
timedifference=sensordbmod.timediffinminutes(lastactiontime,datetime.utcnow())
print('Time interval between actions', timedifference ,'. threshold', waitingtime)
logger.info('Time interval between Actions %d threshold %d', timedifference,waitingtime)
if timedifference>=waitingtime: # sufficient time between actions
print(" Sufficient waiting time")
logger.info('Sufficient waiting time')
# action
print("Implement Actuator Value ", value)
logger.info('Procedure to start actuator %s, for value = %s', element, value)
msg , isok=activateactuator(element, value)
# invia mail, considered as info, not as alert
if (mailtype!="warningonly")and(mailtype!="none"):
textmessage="INFO: " + sensor + " value " + str(sensorvalue) + ", activating:" + element + " with Value " + str(value)
emailmod.sendallmail("alert", textmessage)
if isok:
statusdataDBmod.write_status_data(AUTO_data,element,"lastactiontime",datetime.utcnow())
statusdataDBmod.write_status_data(AUTO_data,element,"actionvalue",value)
else:
logger.info('Need to wait more time')
def activateactuator(target, value): # return true in case the state change: activation is >0 or a different position from prevoius position.
return ActuatorControllermod.activateactuator(target,value)
def isNowInTimePeriod(startTime, endTime, nowTime):
print(startTime," ", endTime," " , nowTime)
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
def sensorreading(sensorname,MinutesOfAverage,operation):
isok=False
# operation "average", "min" , "max" , "sum"
if sensorname:
timelist=hardwaremod.gettimedata(sensorname)
theinterval=timelist[1] # minutes
if theinterval>0:
samplesnumber=int(old_div(MinutesOfAverage,theinterval)+1)
else:
samplesnumber=1
theinterval=15
quantity=0
MinutesOfAverage=samplesnumber*theinterval
if samplesnumber>0:
sensordata=[]
sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
datenow=datetime.now() # dont put UTC here !!!
starttimecalc=datenow-timedelta(minutes=(MinutesOfAverage+theinterval)) #if Minutes of average is zero, it allows to have at least one sample
isok, quantitylist=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datenow)
quantity=quantitylist[operation]
# sensor reading value
logger.info('Sensor reading <%s>=<%s>',sensorname,str(quantity))
print("sensor Reading ", sensorname, "=" , quantity)
return isok , 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 automationdbmod.searchdata("element",element,"workmode")
if __name__ == '__main__':
"""
prova functions
"""