From 503b87579b48e122da2726e8eed92f4da82b1685 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 17 Aug 2022 02:14:53 +0200 Subject: [PATCH] implemented pipeline --- Webserver/Translater.py | 6 +++++ Webserver/__init__.py | 10 +++++++- Zone.py | 13 +++++++--- ZoneManager.py | 57 +++++++++++++++++++++++++++++++++-------- main.py | 2 +- 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Webserver/Translater.py b/Webserver/Translater.py index 5dfa484..90212af 100644 --- a/Webserver/Translater.py +++ b/Webserver/Translater.py @@ -26,6 +26,12 @@ class Translater: "switch to manual mode": "Auf Handbetrieb umstellen", "switch to automatic mode": "Auf Automatikbetrieb umstellen", "minutes": "Minuten", + "until": "bis", + "irragation is planed for": "Bewässerung ist geplant für", + "planed irrigationjobs": "geplante Bewässerungsaufträge", + "planed duration": "geplante Dauer", + "cancel": "abbrechen", + "delete": "löschen", } diff --git a/Webserver/__init__.py b/Webserver/__init__.py index 3c0bf0c..417866b 100644 --- a/Webserver/__init__.py +++ b/Webserver/__init__.py @@ -9,7 +9,7 @@ class Webserver: self.zoneManager = zoneManager self.translater_EN = Translater(Language.ENGLISH) self.translater_DE = Translater(Language.GERMAN) - self.translater = self.translater_EN + self.translater = self.translater_DE def startWebserver(self): app = Flask("Bewässerungssystem", template_folder="Webserver/templates", static_folder="Webserver/static") @@ -73,6 +73,14 @@ class Webserver: zone = self.zoneManager.getZone(index) zone.desiredHumidity = value sucess = True + case "delete_jobs_for_zone": + if (index): + zone = self.zoneManager.getZone(index) + self.zoneManager.deleteIrrigationJobsForZone(zone) + case "delete_job_by_id": + if (index): + self.zoneManager.deleteIrrigationJobByID(index) + return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess) diff --git a/Zone.py b/Zone.py index 56ca729..abba3a8 100644 --- a/Zone.py +++ b/Zone.py @@ -22,10 +22,11 @@ class Zone: def refreshStateAutomode(self): match self.setState: case 0: - if(self.desiredHumidity > self.actualHumidity): + self.state = False + if(self.planedDuration > 0): + self.setState = 3 + elif(self.desiredHumidity > self.actualHumidity): self.zoneManager.switchZoneState(zone=self, state=True, duration=self.zoneManager.systemSettings.defaultAutoIrrigationDuration, instant=False) - else: - self.state = False case 1: if(self.timeOver()): self.setState = 0 @@ -45,6 +46,10 @@ class Zone: def refreshStateManualmode(self): if(self.setState > 0): match self.setState: + case 0: + self.state = False + if (self.planedDuration > 0): + self.setState = 3 case 1: if(self.timeOver()): self.setState = 0 @@ -60,6 +65,7 @@ class Zone: def refreshState(self): + self.planedDuration = self.zoneManager.getPlanedDurationForZone(zone=self) if(self.autoMode): self.refreshStateAutomode() else: @@ -81,7 +87,6 @@ class Zone: else: if(state): self.setState = 3 - self.planedDuration = duration self.refreshState() diff --git a/ZoneManager.py b/ZoneManager.py index 516e840..ba64103 100644 --- a/ZoneManager.py +++ b/ZoneManager.py @@ -1,13 +1,19 @@ +import threading +from random import Random +from time import time + from FileIO import FileIO class ZoneManager: def __init__(self, systemSettings, fileIO): + self.random = Random() self.systemSettings = systemSettings self.fileIO = FileIO self.zones = fileIO.loadZones() for zone in self.zones: zone.setZoneManager(self) - self.pipeLine = [] + self.pipeLine = [] #todo Thread Synchron + #self.lock = threading.Lock() def getZone(self, number): for zone in self.zones: @@ -15,7 +21,28 @@ class ZoneManager: return zone def addIrrigationJob(self, job): - self.pipeLine.append(job) + #with self.lock: + self.pipeLine.append(job) + + def deleteIrrigationJobByID(self, id): + #with self.lock: + i = 0 + while i < (len(self.pipeLine)): + irrigationJob = self.pipeLine[i] + if(irrigationJob.id == id): + self.pipeLine.pop(i) + else: + i = i + 1 + + def deleteIrrigationJobsForZone(self, zone): + #with self.lock: + i = 0 + while i < (len(self.pipeLine)): + irrigationJob = self.pipeLine[i] + if (irrigationJob.zone == zone): + self.pipeLine.pop(i) + else: + i = i + 1 def isAnyZoneBusy(self): for zone in self.zones: @@ -24,35 +51,45 @@ class ZoneManager: return False def switchZoneState(self, zone, state, duration, instant=False): - if(instant or self.systemSettings.multiZoneIrrigation): + if(instant or self.systemSettings.multiZoneIrrigation or state==False or (not self.isAnyZoneBusy())): zone.switchState(state=state, duration=duration, instant=True) else: - self.addIrrigationJob(IrrigationJob(zone, duration)) + self.addIrrigationJob(IrrigationJob(id=self.random.randint(a=100000000, b=999999999), zone=zone, duration=duration)) def switchZoneIndexState(self, zoneIndex, state, duration, instant=False): zone = self.getZone(zoneIndex) self.switchZoneState(zone, state, duration, instant) + def getPlanedDurationForZone(self, zone): + totalDuration = 0 + #with self.lock: + for irrigationJob in self.pipeLine: + if(irrigationJob.zone == zone): + totalDuration = totalDuration + irrigationJob.duration + return totalDuration + def refreshStates(self): for zone in self.zones: zone.refreshState() def processPipeline(self): - if(len(self.pipeLine) > 0 and (not self.isAnyZoneBusy())): - irrigationJob = self.pipeLine[0] - irrigationJob.process() - self.pipeLine.pop(0) + #with self.lock: + if(len(self.pipeLine) > 0 and (not self.isAnyZoneBusy())): + irrigationJob = self.pipeLine[0] + irrigationJob.process() + self.pipeLine.pop(0) def cronJobs(self): self.refreshStates() self.processPipeline() - print("Executed Cron Jobs of ZoneManager") + print("Executed Cron Jobs of ZoneManager.\nactual Time: " + str(time())) class IrrigationJob: - def __init__(self, zone, duration=0): + def __init__(self, id, zone, duration=0): + self.id = id self.zone = zone self.duration = duration self.zone.switchState(state=True, duration=duration, instant=False) diff --git a/main.py b/main.py index 93cfb6e..3358fea 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from ZoneManager import ZoneManager from SystemSettings import SystemSettings from FileIO import FileIO -systemSettings = SystemSettings(dataDir="/Data", multiZoneIrrigation=False, defaultAutoIrrigationDuration=10, defaultManualIrrigationDuration=10, defaultManualOffDuration=10, webDurationOptions=[5, 10, 15, 20, 25, 30, 45, 60]) +systemSettings = SystemSettings(dataDir="/Data", multiZoneIrrigation=False, defaultAutoIrrigationDuration=10, defaultManualIrrigationDuration=10, defaultManualOffDuration=10, webDurationOptions=[1, 5, 10, 15, 20, 25, 30, 45, 60]) fileIO = FileIO(systemSettings) zoneManager = ZoneManager(systemSettings=systemSettings, fileIO=fileIO) webserver = Webserver(zoneManager=zoneManager, port=80)