From b1c371d9b3095e035c0c890f0a6f1cfc1d6218e5 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sat, 27 Aug 2022 00:29:22 +0200 Subject: [PATCH] implemented autorefresh --- Webserver/Templates/dashboard/dashboard.html | 91 +++++++---- Webserver/Templates/dashboard/pipeline.html | 28 +--- Webserver/Templates/dashboard/zone.html | 145 ++++++++--------- Webserver/Templates/header.html | 161 +++++++++---------- Webserver/__init__.py | 130 ++++++++------- Webserver/static/Styles/dashboard.css | 6 + Webserver/static/js/webhook.js | 27 ++-- Zone.py | 4 +- ZoneManager.py | 16 +- main.py | 2 +- 10 files changed, 319 insertions(+), 291 deletions(-) diff --git a/Webserver/Templates/dashboard/dashboard.html b/Webserver/Templates/dashboard/dashboard.html index 18c83ec..8047d81 100644 --- a/Webserver/Templates/dashboard/dashboard.html +++ b/Webserver/Templates/dashboard/dashboard.html @@ -1,7 +1,9 @@ +{% include "header.html" %} + {{ translater.getTranslation("Irrigation") }}{{ translater.getTranslation("system") }} @@ -24,26 +26,8 @@ - - - {% include "header.html" %}
@@ -51,15 +35,29 @@

{{ translater.getTranslation("Dashboard") }}

-

{{ translater.getTranslation("planed irrigationjobs") }}

-

- -

-
- {% include "dashboard/pipeline.html" %} +
+
+
+ + + + + + + + + + + + + {% include "dashboard/pipeline.html" %} + +
{{ translater.getTranslation("planed irrigationjobs") }}
+
+ Text unter Table +
+

{{ translater.getTranslation("irrigation zones") }}

@@ -79,4 +77,43 @@



- \ No newline at end of file + + +{% include "footer.html" %} \ No newline at end of file diff --git a/Webserver/Templates/dashboard/pipeline.html b/Webserver/Templates/dashboard/pipeline.html index 8d852c4..9492586 100644 --- a/Webserver/Templates/dashboard/pipeline.html +++ b/Webserver/Templates/dashboard/pipeline.html @@ -1,21 +1,9 @@ - - - - - - - - - - - {% for job in zoneManager.pipeLine %} - - - - - - - {% endfor %} - -
ID{{ translater.getTranslation("Zone") }}{{ translater.getTranslation("planed duration") }}
{{ job.id }}{{ job.zone.number|string + ": " + job.zone.name }}{{ ((job.duration/60)|int)|string + " " + translater.getTranslation("minutes")}}
\ No newline at end of file +{% for job in zoneManager.pipeLine %} + {% include "dashboard/irrigationJob.html" %} +{% endfor %} + + + + + diff --git a/Webserver/Templates/dashboard/zone.html b/Webserver/Templates/dashboard/zone.html index 14f40cb..6c4c044 100644 --- a/Webserver/Templates/dashboard/zone.html +++ b/Webserver/Templates/dashboard/zone.html @@ -1,92 +1,79 @@ - -

{{ zone.name }}

-
- - - - - - - - - - - {% if (zone.setState == 1 or zone.setState == 2) %} + + +

{{ zone.name }}

+
+
- - - - {{ translater.getTranslation("state") }}: - {{ translater.getTranslation("switched on") if zone.state else translater.getTranslation("switched off") }} - - - - - -
+ - - - + + - {% endif %} - {% if zone.planedDuration > 0 %} - + + + + + + + - {% endif %} - - - - - - - - - - - - - - - - - - -
- {{translater.getTranslation("until") + zone.endTimeSetState|string}} + + + + {{ translater.getTranslation("state") }}: - + + +
- {{translater.getTranslation("irragation is planed for") + " " + ((zone.planedDuration/60)|int)|string + " " + translater.getTranslation("minutes") + "." }} + {{translater.getTranslation("until") }}

+
+ +
+ {{translater.getTranslation("irragation is planed for") }}

{{ translater.getTranslation("minutes") + "." }}
- -

{{'A' if zone.autoMode else 'M'}}

-
-
- - {{ translater.getTranslation("operating mode") }}:{{translater.getTranslation("automatic mode") if zone.autoMode else translater.getTranslation("manual mode")}} - -
- - - - {{ translater.getTranslation("actual humidity") }}:{{ zone.actualHumidity}}
- - {{ translater.getTranslation("desired humidity") }}:{{ zone.desiredHumidity }}
-

{{ zone.number }}

\ No newline at end of file + + + +

+
+ + + + + {{ translater.getTranslation("operating mode") }}: + + + + + + + + + {{ translater.getTranslation("actual humidity") }}: + + + + + + + {{ translater.getTranslation("desired humidity") }}: + + + + +

{{ zone.number }}

\ No newline at end of file diff --git a/Webserver/Templates/header.html b/Webserver/Templates/header.html index ecb074d..5af1b2b 100644 --- a/Webserver/Templates/header.html +++ b/Webserver/Templates/header.html @@ -1,94 +1,91 @@ - - - - - - - - } - function switchZoneState(command, zone, value) { - executeAction(command, zone, value); - sleep(1000).then(() => { - refreshZone(zone); - refreshPipeline(); - }); - } - function switchZoneStateByValueID(command, zone, valueID, valueFactor) { - executeActionByValueID(command, zone, valueID, valueFactor); - sleep(1000).then(() => { - refreshZone(zone); - refreshPipeline(); - }); - } - function deleteJobsForZone(command, zone) { - executeAction(command, zone); - sleep(1000).then(() => { - refreshZone(zone); - refreshPipeline(); - }); - } - function deleteJobById(command, jobId, zone) { - executeAction(command, jobId); - sleep(1000).then(() => { - refreshZone(zone); - refreshPipeline(); - }); - } + + +
+ + + -setInterval(refreshContent, {{ (zoneManager.systemSettings.cronJobFrequency + 1) * 1000 }}) +

{{ translater.getTranslation("Irrigation") }}-
{{ translater.getTranslation("system") }}

+
- - - - -
- - - - - -

{{ translater.getTranslation("Irrigation") }}-
{{ translater.getTranslation("system") }}

-
- - - -
- - - + + +
+ + + diff --git a/Webserver/__init__.py b/Webserver/__init__.py index c638980..bc90e53 100644 --- a/Webserver/__init__.py +++ b/Webserver/__init__.py @@ -50,59 +50,73 @@ class Webserver: 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_dashboard_zone_html": - if (index): - zone = self.zoneManager.getZone(index) - return render_template('dashboard/zone.html', translater=self.translater, zoneManager=self.zoneManager, zone=zone) - case "get_dashboard_pipeline_html": - return render_template("dashboard/pipeline.html", translater=self.translater, zoneManager=self.zoneManager) - case "get_zone_list": - return self.zoneManager.zonesToJSON() - case "get_zone_info": - if (index): - zone = self.zoneManager.getZone(index) - return zone.toJSON() - case "get_pipeline": - return self.zoneManager.pipelineToJSON() - return self.zoneManager.zonesToJSON() - #return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess) + elif(request.method == 'GET'): + try: + command = request.args['command'] + except: + pass + try: + index_str = request.args['index'] + index = int(index_str) + except: + pass + try: + value_str = request.args['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_dashboard_zone_html": + if (index): + zone = self.zoneManager.getZone(index) + return render_template('dashboard/zone.html', translater=self.translater, zoneManager=self.zoneManager, zone=zone) + case "get_dashboard_pipeline_html": + return render_template("dashboard/pipeline.html", translater=self.translater, zoneManager=self.zoneManager) + case "get_zone_list": + return self.zoneManager.zonesToJSON(self.translater) + case "get_zone_info": + if (index): + zone = self.zoneManager.getZone(index) + return zone.toJSON(self.translater) + case "get_pipeline": + return self.zoneManager.pipelineToJSON(self.translater) + return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess) @app.route('/dashboard') @@ -113,16 +127,16 @@ class Webserver: @app.route('/zones/') def showZones(zoneNumber=False): if (zoneNumber): - return render_template('zones/zone.html', translater=self.translater, zone=self.zoneManager.getZone(zoneNumber)) + return render_template('zones/zone.html', translater=self.translater, zoneManager=self.zoneManager, zone=self.zoneManager.getZone(zoneNumber)) else: - return render_template('zones/zones.html', translater=self.translater, zones=self.zoneManager.zones) + return render_template('zones/zones.html', translater=self.translater, zoneManager=self.zoneManager) @app.route('/times') def showTimes(): - return render_template('times.html', translater=self.translater) + return render_template('times.html', translater=self.translater, zoneManager=self.zoneManager) @app.route('/system') def showSystem(): - return render_template('system.html', translater=self.translater) + return render_template('system.html', translater=self.translater, zoneManager=self.zoneManager) - app.run(debug=True, port=self.port) + app.run(debug=True, host="0.0.0.0", port=self.port) diff --git a/Webserver/static/Styles/dashboard.css b/Webserver/static/Styles/dashboard.css index 7fd7357..b8df3be 100644 --- a/Webserver/static/Styles/dashboard.css +++ b/Webserver/static/Styles/dashboard.css @@ -1,3 +1,5 @@ + + #zones{ display: grid; grid-template-columns: repeat(3, 1fr); @@ -57,6 +59,10 @@ td.value{ text-align: left; position: relative; } +p.end_time_value, p.planed_duration_value { + display: inline; +} + diff --git a/Webserver/static/js/webhook.js b/Webserver/static/js/webhook.js index eebdd88..4cc0853 100644 --- a/Webserver/static/js/webhook.js +++ b/Webserver/static/js/webhook.js @@ -1,12 +1,4 @@ -function sleep (time) { - - return new Promise((resolve) => setTimeout(resolve, time)); - -} - - - -function send_web_request(url, messageString, varString, elementId) { +function send_web_request(url, messageString, varString, handleResponse) { // Browserkompatibles Request-Objekt erzeugen: r = null; @@ -40,16 +32,19 @@ function send_web_request(url, messageString, varString, elementId) { { - // HTTP-POST - r.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - document.getElementById(elementId).innerHTML = this.responseText; - } - }; - + r.open('POST', url, true); r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + + if(handleResponse != null) { + // HTTP-POST + r.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + handleResponse(this.responseText); + } + }; + } r.send(varString); diff --git a/Zone.py b/Zone.py index f802cf8..5694dd8 100644 --- a/Zone.py +++ b/Zone.py @@ -14,14 +14,16 @@ class Zone: self.endTimeSetState = endTimeSetState self.planedDuration = planedDuration - def toJSON(self): + def toJSON(self, translater): return { "number": self.number, "name": self.name, "actualHumidity": self.actualHumidity, "desiredHumidity": self.desiredHumidity, "autoMode": self.autoMode, + "operationMode_text": translater.getTranslation("automatic mode" if self.autoMode else "manual mode"), "state": self.state, + "state_text": translater.getTranslation("switched on" if self.state else "switched off"), "setState": self.setState, "endTimeSetState" : self.endTimeSetState, "planedDuration": self.planedDuration, diff --git a/ZoneManager.py b/ZoneManager.py index 119e7cc..114567a 100644 --- a/ZoneManager.py +++ b/ZoneManager.py @@ -16,17 +16,17 @@ class ZoneManager: self.pipeLine = [] self.piplineMutexLock = threading.Lock() - def zonesToJSON(self): + def zonesToJSON(self, translater): zoneList = [] for zone in self.zones: - zoneList.append(zone.toJSON()) + zoneList.append(zone.toJSON(translater)) return zoneList - def pipelineToJSON(self): + def pipelineToJSON(self, translater): jobList = [] with self.piplineMutexLock: for irrigationJob in self.pipeLine: - jobList.append(irrigationJob.toJSON()) + jobList.append(irrigationJob.toJSON(translater)) return jobList @@ -67,7 +67,9 @@ class ZoneManager: return False def switchZoneState(self, zone, state, duration, instant=False): - if(instant or self.systemSettings.multiZoneIrrigation or state==False ): #or (not self.isAnyZoneBusy()) + if(not duration > 0): + zone.switchState(state=False, duration=0, instant=True) + elif(instant or self.systemSettings.multiZoneIrrigation or state==False ): #or (not self.isAnyZoneBusy()) zone.switchState(state=state, duration=duration, instant=True) else: self.addIrrigationJob(IrrigationJob(id=self.random.randint(a=100000000, b=999999999), zone=zone, duration=duration)) @@ -115,10 +117,10 @@ class IrrigationJob: self.duration = duration self.zone.switchState(state=True, duration=duration, instant=False) - def toJSON(self): + def toJSON(self, translater): return { "id": self.id, - "zone": self.zone.toJSON(), + "zone": self.zone.toJSON(translater), "duration": self.duration, } diff --git a/main.py b/main.py index fce8609..60a44cc 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(cronJobFrequency=2, dataDir="/Data", multiZoneIrrigation=False, defaultAutoIrrigationDuration=10, defaultManualIrrigationDuration=10, defaultManualOffDuration=10, webDurationOptions=[1, 5, 10, 15, 20, 25, 30, 45, 60]) +systemSettings = SystemSettings(cronJobFrequency=0.5, 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)