forked from Hydrosys4/Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofertilizermod.py
92 lines (77 loc) · 3.07 KB
/
autofertilizermod.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
import logging
from datetime import datetime, date ,timedelta
import time
import hardwaremod
import os
import subprocess
import emailmod
import autofertilizerdbmod
import sensordbmod
import actuatordbmod
import fertilizerdbmod
logger = logging.getLogger("hydrosys4."+__name__)
# status array, required to check the ongoing actions within a watering cycle
elementlist= autofertilizerdbmod.getelementlist()
AUTO_data={} # dictionary of dictionary
for element in elementlist:
AUTO_data[element]={"triggerdate":datetime.utcnow(),"tobeactivated":False, "duration":0, "alertcounter":0}
# triggerdate, datetime when the doser have been triggered to start
# tobeactivated, doser need to be activated in the next opportunity
def isschedulermode(element):
recordkey="element"
recordvalue=element
keytosearch="workmode"
workmode=autofertilizerdbmod.searchdata(recordkey,recordvalue,keytosearch)
if workmode=="SceduledTime":
return True
else:
return False
def checkactivate(elementwater,durationwater):
elementlist=fertilizerdbmod.getelementlist()
waterok=False
for doserelement in elementlist: # provided the waterelement, search for corresponding doserelement
linkedwaterelement=autofertilizerdbmod.searchdata("element",doserelement,"waterZone")
if linkedwaterelement==elementwater:
waterok=True
element=doserelement
break
if waterok: # there is a corresponding doser element
minwaterduration=hardwaremod.toint(autofertilizerdbmod.searchdata("element",element,"minactivationsec"),0)
if not isschedulermode(element): #setup is not for scheduled time
print " Fertilizer " ,element ," set to autowater"
print " Check Water duration ", durationwater ,">", minwaterduration*1000
if durationwater>minwaterduration*1000: # watering time above the set threshold
print " OK Water duration "
if AUTO_data[element]["tobeactivated"]: #if flag is ON
print " Activate ", element
durationfer=AUTO_data[element]["duration"]
activatedoser(element,durationfer)
time.sleep(durationfer/1000) #this blocks the system (and watering activation) for n seconds ... not best practice
else:
print " No pending request to activate ", element
def activatedoser(target, duration):
print target, " ",duration, " " , datetime.now()
logger.info('Doser Pulse, pulse time for ms = %s', duration)
pulseok=hardwaremod.makepulse(target,duration)
# salva su database
actuatordbmod.insertdataintable(target,duration)
# put flag down
global AUTO_data
AUTO_data[target]["tobeactivated"]=False
AUTO_data[target]["duration"]=0
AUTO_data[target]["triggerdate"]=datetime.now()
def checkworkmode(element):
return autofertilizerdbmod.searchdata("element",element,"workmode")
def timelist(element):
if isschedulermode(element):
fertime=autofertilizerdbmod.searchdata("element",element,"time")
print "fertime " , fertime
timelist=hardwaremod.separatetimestringint(fertime)
else:
print "non scheduler mode "
timelist=hardwaremod.separatetimestringint("00:20:00") # get up 0 minutes and check for doseractivation
return timelist
if __name__ == '__main__':
"""
prova functions
"""