forked from Hydrosys4/Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomationmod.py
341 lines (276 loc) · 12.6 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
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
import logging
from datetime import datetime, time ,timedelta
import hardwaremod
import os
import subprocess
import emailmod
import automationdbmod
import sensordbmod
import actuatordbmod
import autofertilizermod
logger = logging.getLogger("hydrosys4."+__name__)
# status array, required to check the ongoing actions
elementlist= automationdbmod.getelementlist()
AUTO_data={} # dictionary of dictionary
for element in elementlist:
AUTO_data[element]={"lastactiontime":datetime.now(),"actionvalue":0, "alertcounter":0, "infocounter":0, "status":"ok"}
def cyclereset(element):
global AUTO_data
waitingtime=hardwaremod.toint(automationdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
AUTO_data[element]={"lastactiontime":datetime.now() - timedelta(minutes=waitingtime),"status":"ok","actionvalue":0, "alertcounter":0, "infocounter":0}
def cycleresetall():
global AUTO_data
elementlist= automationdbmod.getelementlist()
for element in elementlist:
waitingtime=hardwaremod.toint(automationdbmod.searchdata("element",element,"pausebetweenwtstepsmin"),0)
AUTO_data[element]={"lastactiontime":datetime.now() - timedelta(minutes=waitingtime),"status":"ok","actionvalue":0, "alertcounter":0, "infocounter":0}
def automationcheck(refsensor):
global AUTO_data
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)
print AUTO_data[element]
# 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
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
now = datetime.now()
nowtime = now.time()
starttimeh=hardwaremod.toint(automationdbmod.searchdata("element",element,"allowedperiod")[0].split(":")[0],0)
starttimem=hardwaremod.toint(automationdbmod.searchdata("element",element,"allowedperiod")[0].split(":")[1],0)
endtimeh=hardwaremod.toint(automationdbmod.searchdata("element",element,"allowedperiod")[1].split(":")[0],1)
endtimem=hardwaremod.toint(automationdbmod.searchdata("element",element,"allowedperiod")[1].split(":")[1],0)
starttime=time(starttimeh,starttimem)
endtime=time(endtimeh,endtimem)
# 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")
# 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):
P.append(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, nowtime)
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)
Inde=0
maxs=sensorminthreshold+Inde*interval
if sensorvalue<=maxs:
status="belowthreshold"
logger.info('below Minthreshold')
value=P[Inde]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
Inde=Inde+1
for I in range(Inde, maxstepnumber):
mins=sensorminthreshold+(I-1)*interval
maxs=sensorminthreshold+I*interval
if mins<sensorvalue<=maxs:
value=P[I]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
Inde=maxstepnumber
mins=sensorminthreshold+(Inde-1)*interval
if mins<sensorvalue:
print "INDE:",Inde
value=P[Inde]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
# END MAIN ALGORITHM
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=0
maxs=sensorminthreshold+Inde*interval
if sensorvalue>=maxs:
status="belowthreshold"
logger.info('Above MAXthreshold')
value=P[Inde]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
Inde=Inde+1
for I in range(Inde, maxstepnumber):
mins=sensorminthreshold+(I-1)*interval
maxs=sensorminthreshold+I*interval
if mins>sensorvalue>=maxs:
value=P[I]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
Inde=maxstepnumber
mins=sensorminthreshold+(Inde-1)*interval
if mins>sensorvalue:
print "INDE:",Inde
value=P[Inde]
# do relevant stuff
CheckActivateNotify(element,waitingtime,value,mailtype,sensor,sensorvalue)
# END MAIN ALGORITHM - Reverse
else:
logger.info('No valid calculation operation on the stored sensor data')
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
if AUTO_data[element]["alertcounter"]<2:
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["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
if AUTO_data[element]["alertcounter"]<2:
emailmod.sendallmail("alert", textmessage)
logger.error(textmessage)
AUTO_data[element]["alertcounter"]=AUTO_data[element]["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)
print ' Previous action: ' , AUTO_data[element]["lastactiontime"] , ' Now: ', datetime.now()
timedifference=sensordbmod.timediffinminutes(AUTO_data[element]["lastactiontime"],datetime.now())
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)
isok=activateactuator(element, value)
# invia mail, considered as info, not as alert
if mailtype!="warningonly":
textmessage="INFO: " + sensor + " value " + str(sensorvalue) + ", activating:" + element + " with Value " + str(value)
emailmod.sendallmail("alert", textmessage)
if isok:
AUTO_data[element]["lastactiontime"]=datetime.now()
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.
# check the actuator
isok=False
actuatortype=hardwaremod.searchdata(hardwaremod.HW_INFO_NAME,target,hardwaremod.HW_CTRL_CMD)
supportedactuators=["pulse","servo","stepper"]
# stepper motor
if actuatortype=="stepper":
out, isok = hardwaremod.GO_stepper_position(target,value)
if isok:
actuatordbmod.insertdataintable(target,value)
# pulse
if actuatortype=="pulse":
duration=1000*hardwaremod.toint(value,0)
# check the fertilizer doser flag before activating the pulse
doseron=autofertilizermod.checkactivate(element,duration)
# start pulse
pulseok=hardwaremod.makepulse(target,duration)
# salva su database
if "Started" in pulseok:
actuatordbmod.insertdataintable(target,duration)
isok=True
# servo motor
if actuatortype=="servo":
out, isok = hardwaremod.servoangle(target,value,0.5)
if isok:
actuatordbmod.insertdataintable(target,value)
return isok
def isNowInTimePeriod(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(MinutesOfAverage/theinterval+1)
else:
samplesnumber=1
theinterval=15
quantity=0
MinutesOfAverage=samplesnumber*theinterval
if samplesnumber>0:
sensordata=[]
sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
starttimecalc=datetime.now()-timedelta(minutes=(MinutesOfAverage+theinterval)) #if Minutes of average is zero, it allows to have at least one sample
quantity=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datetime.now())[operation]
isok=True
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
"""