Bewaesserungsanlage/Zone.py

113 lines
3.9 KiB
Python
Raw Permalink Normal View History

2022-08-15 23:58:07 +02:00
import time
import json
2022-08-15 23:58:07 +02:00
2022-08-15 01:10:34 +02:00
class Zone:
2022-08-15 23:58:07 +02:00
def __init__(self, number=0, name="", actualHumidity="100", desiredHumidity="0", autoMode=False, state=False, setState=0, endTimeSetState=0, planedDuration=0):
2022-08-15 01:10:34 +02:00
self.number = number
self.name = name
2022-08-15 23:58:07 +02:00
self.actualHumidity = actualHumidity
self.desiredHumidity = desiredHumidity
2022-08-15 01:10:34 +02:00
self.autoMode = autoMode #False = manual, True = automatic
2022-08-15 23:58:07 +02:00
self.state = state #True = on, False = off
self.setState = setState #0=default, 1=turned off for time, 2=turned on for time, 3=command in pipeline
self.endTimeSetState = endTimeSetState
self.planedDuration = planedDuration
2022-08-15 01:10:34 +02:00
2022-08-27 00:29:22 +02:00
def toJSON(self, translater):
return {
"number": self.number,
"name": self.name,
"actualHumidity": self.actualHumidity,
"desiredHumidity": self.desiredHumidity,
"autoMode": self.autoMode,
2022-08-27 00:29:22 +02:00
"operationMode_text": translater.getTranslation("automatic mode" if self.autoMode else "manual mode"),
"state": self.state,
2022-08-27 00:29:22 +02:00
"state_text": translater.getTranslation("switched on" if self.state else "switched off"),
"setState": self.setState,
"endTimeSetState" : self.endTimeSetState,
"planedDuration": self.planedDuration,
}
2022-08-16 03:13:10 +02:00
def setZoneManager(self, zoneManager):
self.zoneManager = zoneManager
2022-08-15 23:58:07 +02:00
def timeOver(self):
return time.time() > self.endTimeSetState
2022-08-15 01:10:34 +02:00
2022-08-15 23:58:07 +02:00
def refreshStateAutomode(self):
match self.setState:
case 0:
2022-08-17 02:14:53 +02:00
self.state = False
if(self.planedDuration > 0):
self.setState = 3
elif(self.desiredHumidity > self.actualHumidity):
2022-08-16 03:13:10 +02:00
self.zoneManager.switchZoneState(zone=self, state=True, duration=self.zoneManager.systemSettings.defaultAutoIrrigationDuration, instant=False)
2022-08-15 23:58:07 +02:00
case 1:
if(self.timeOver()):
self.setState = 0
self.refreshStateAutomode()
else:
self.state = False
case 2:
if (self.timeOver()):
self.setState = 0
self.refreshStateAutomode()
else:
self.state = True
case 3:
2022-08-15 01:10:34 +02:00
self.state = False
2022-08-17 02:41:49 +02:00
if (not self.planedDuration > 0):
self.setState = 0
2022-08-15 01:10:34 +02:00
2022-08-15 23:58:07 +02:00
def refreshStateManualmode(self):
if(self.setState > 0):
match self.setState:
2022-08-17 02:14:53 +02:00
case 0:
self.state = False
if (self.planedDuration > 0):
self.setState = 3
2022-08-15 23:58:07 +02:00
case 1:
if(self.timeOver()):
self.setState = 0
self.state = False
case 2:
if (self.timeOver()):
self.setState = 0
self.state = False
else:
self.state = True
case 3:
self.state = False
2022-08-17 02:41:49 +02:00
if (not self.planedDuration > 0):
self.setState = 0
2022-08-15 23:58:07 +02:00
def refreshState(self):
2022-08-17 02:14:53 +02:00
self.planedDuration = self.zoneManager.getPlanedDurationForZone(zone=self)
2022-08-15 23:58:07 +02:00
if(self.autoMode):
self.refreshStateAutomode()
else:
self.refreshStateManualmode()
def switchMode(self, autoMode):
if(self.autoMode != autoMode):
self.autoMode = autoMode
self.state = False
self.refreshState()
def switchState(self, state, duration, instant):
if(instant):
if(not state):
self.setState = 1
else:
self.setState = 2
self.endTimeSetState = time.time() + duration
else:
if(state):
self.setState = 3
self.refreshState()