From 2747b79f37b223674ca826df644a65384c592b8a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 23 Aug 2022 23:47:43 +0200 Subject: [PATCH] implemented actions get_zone_list and get_pipeline --- Webserver/Templates/header.html | 20 +++--- Webserver/__init__.py | 104 +++++++++++++++++--------------- Webserver/static/js/action.js | 8 +++ Zone.py | 14 +++++ ZoneManager.py | 24 +++++++- 5 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 Webserver/static/js/action.js diff --git a/Webserver/Templates/header.html b/Webserver/Templates/header.html index d1eb6a4..e275f31 100644 --- a/Webserver/Templates/header.html +++ b/Webserver/Templates/header.html @@ -6,19 +6,12 @@ - - + +
@@ -43,6 +36,7 @@
+ diff --git a/Webserver/__init__.py b/Webserver/__init__.py index 417866b..319b6a3 100644 --- a/Webserver/__init__.py +++ b/Webserver/__init__.py @@ -33,57 +33,67 @@ class Webserver: return redirect(url_for('showDashboard')) @app.route('/action', methods=['GET', 'POST']) - @app.route('/action/', methods=['GET', 'POST']) - @app.route('/action//', methods=['GET', 'POST']) - @app.route('/action///', methods=['GET', 'POST']) def executeAction(command=False, index_str=False, value_str=False): sucess = False - try: - index = int(index_str) - value = int(value_str) - except: - pass - match command: - case "switch_zone_on": - if (index and value): - self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value) - sucess = True - elif(index): - self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=self.zoneManager.systemSettings.defaultManualIrrigationDuration) - sucess = True - case "switch_zone_off": - if (index and value): - self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value, instant=True) - sucess = True - elif(index): - self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=self.zoneManager.systemSettings.defaultManualOffDuration, instant=True) - sucess = True - case "switch_zone_mode": - if (index and value_str): - zone = self.zoneManager.getZone(index) - match value_str: - case "automatic": - zone.switchMode(autoMode=True) - sucess = True - case "manual": - zone.switchMode(autoMode=False) - sucess = True - case "set_desired_humidity": - if (index and value): - 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) - - + if(request.method == 'POST'): + try: + command = request.form['command'] + except: + pass + try: + index_str = request.form['index'] + index = int(index_str) + except: + pass + try: + value_str = request.form['value'] + value = int(value_str) + except: + pass + match command: + case "switch_zone_on": + if (index and value): + self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value) + sucess = True + elif(index): + self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=self.zoneManager.systemSettings.defaultManualIrrigationDuration) + sucess = True + case "switch_zone_off": + if (index and value): + self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value, instant=True) + sucess = True + elif(index): + self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=self.zoneManager.systemSettings.defaultManualOffDuration, instant=True) + sucess = True + case "switch_zone_mode": + if (index and value_str): + zone = self.zoneManager.getZone(index) + match value_str: + case "automatic": + zone.switchMode(autoMode=True) + sucess = True + case "manual": + zone.switchMode(autoMode=False) + sucess = True + case "set_desired_humidity": + if (index and value): + 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) + case "get_zone_list": + return self.zoneManager.zonesToJSON() + case "get_pipeline": + return self.zoneManager.pipelineToJSON() return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess) + @app.route('/dashboard') def showDashboard(): return render_template('dashboard.html', translater=self.translater, zoneManager=self.zoneManager) diff --git a/Webserver/static/js/action.js b/Webserver/static/js/action.js new file mode 100644 index 0000000..e6d8b62 --- /dev/null +++ b/Webserver/static/js/action.js @@ -0,0 +1,8 @@ + +function executeActionByValueID(command, index, valueID, valueFactor){ + var value = document.getElementById(valueID).value * valueFactor; + executeAction(command, index, value); +} +function switchZoneMode(autoMode, zone) { + executeAction('switch_zone_mode' ,zone, (autoMode ? 'automatic' : 'manual')) +} \ No newline at end of file diff --git a/Zone.py b/Zone.py index b86da92..f802cf8 100644 --- a/Zone.py +++ b/Zone.py @@ -1,4 +1,5 @@ import time +import json class Zone: @@ -13,6 +14,19 @@ class Zone: self.endTimeSetState = endTimeSetState self.planedDuration = planedDuration + def toJSON(self): + return { + "number": self.number, + "name": self.name, + "actualHumidity": self.actualHumidity, + "desiredHumidity": self.desiredHumidity, + "autoMode": self.autoMode, + "state": self.state, + "setState": self.setState, + "endTimeSetState" : self.endTimeSetState, + "planedDuration": self.planedDuration, + } + def setZoneManager(self, zoneManager): self.zoneManager = zoneManager diff --git a/ZoneManager.py b/ZoneManager.py index 2ef1d91..119e7cc 100644 --- a/ZoneManager.py +++ b/ZoneManager.py @@ -1,6 +1,7 @@ +import json import threading from random import Random -from time import time + from FileIO import FileIO @@ -15,6 +16,20 @@ class ZoneManager: self.pipeLine = [] self.piplineMutexLock = threading.Lock() + def zonesToJSON(self): + zoneList = [] + for zone in self.zones: + zoneList.append(zone.toJSON()) + return zoneList + + def pipelineToJSON(self): + jobList = [] + with self.piplineMutexLock: + for irrigationJob in self.pipeLine: + jobList.append(irrigationJob.toJSON()) + return jobList + + def getZone(self, number): for zone in self.zones: if(zone.number == number): @@ -100,5 +115,12 @@ class IrrigationJob: self.duration = duration self.zone.switchState(state=True, duration=duration, instant=False) + def toJSON(self): + return { + "id": self.id, + "zone": self.zone.toJSON(), + "duration": self.duration, + } + def process(self): self.zone.switchState(state=True, duration=self.duration, instant=True) \ No newline at end of file