From 7c4a6a3a560304197ef37d377f5a5f2b53bc463c Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 12 Apr 2022 09:26:57 +0200 Subject: [PATCH] Re-add garden interface --- greenhouseui/app.py | 8 ++ greenhouseui/static/scripts/garden.js | 116 ++++++++++++++++++++ greenhouseui/static/scripts/greenhouse.js | 2 +- greenhouseui/templates/garden.html | 76 +++++++++++++ greenhouseui/templates/greenhouse.html | 124 ++++++++++++++++++++++ greenhouseui/templates/index.html | 121 +++------------------ setup.py | 2 +- 7 files changed, 340 insertions(+), 109 deletions(-) create mode 100644 greenhouseui/static/scripts/garden.js create mode 100644 greenhouseui/templates/garden.html create mode 100644 greenhouseui/templates/greenhouse.html diff --git a/greenhouseui/app.py b/greenhouseui/app.py index 0714d50..5dffbd8 100644 --- a/greenhouseui/app.py +++ b/greenhouseui/app.py @@ -101,6 +101,14 @@ def index(): draw_temperature_image() return render_template('index.html') +@app.route('/garden') +def garden(): + return render_template('garden.html') + +@app.route('/greenhouse') +def greenhouse_1(): + return render_template('greenhouse.html') + @app.route('/sample/', methods=['GET']) def get_sample(idx='0'): """Handle GET to /sample/""" diff --git a/greenhouseui/static/scripts/garden.js b/greenhouseui/static/scripts/garden.js new file mode 100644 index 0000000..08734f0 --- /dev/null +++ b/greenhouseui/static/scripts/garden.js @@ -0,0 +1,116 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"id": "3", "waterstate": state}); + patch_sample(json_str); +} + +var on_switch_auto_state = function() { + var state = true; + if(document.getElementById("auto_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"water": {"id": "3", "autostate": state}}); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on_one").value; + var off_time_one = document.getElementById("water_off_one").value; + var on_time_two = document.getElementById("water_on_two").value; + var off_time_two = document.getElementById("water_off_two").value; + var json_str = JSON.stringify({"water": {"id": "3", "times": [{"on_time": on_time_one, "off_time": off_time_one}, {"on_time": on_time_two, "off_time": off_time_two}]}}); + patch_config(json_str); +} + +var http_patch_config = new XMLHttpRequest(); +http_patch_config.onreadystatechange = function () { + if (http_patch_config.readyState === 4) { + var status = http_patch_config.status; + if (status === 0 || (status >= 200 && status < 400)) { + // The request has been completed successfully + parse_config(http_patch_config.responseText); + } + } else { + // request error + } +} +var patch_config = function(config) { + http_patch_config.abort(); + http_patch_config.open("PATCH", "/config"); + http_patch_config.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + http_patch_config.send(config); +} + +var patch_http = new XMLHttpRequest(); +var patch_sample = function(sample) { + patch_http.abort(); + patch_http.open("PATCH", "/sample"); + patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + patch_http.send(sample); +} + +var parse_config = function (event) { + var config = JSON.parse(event) + var output = "einschalten" + var visibility = 'hidden' + if(config.water && config.water.id == '3') { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on_one").value = config.water.times[0].on_time; + document.getElementById("water_off_one").value = config.water.times[0].off_time; + document.getElementById("water_on_two").value = config.water.times[1].on_time; + document.getElementById("water_off_two").value = config.water.times[1].off_time; + } + document.getElementById("auto_switch").value = output; + document.getElementById("water_times").style.visibility = visibility + } +} + +var parse_sample = function (event) { + var sample = JSON.parse(event) + if(sample.water && sample.water.id == '3') { + var switch_caption = "einschalten"; + if(sample.water.state) { + switch_caption = "ausschalten"; + } + document.getElementById("water_switch").value = switch_caption; + } +} + +var http = new XMLHttpRequest(); +http.onreadystatechange = function () { + if (http.readyState === 4) { + var status = http.status; + if (status === 0 || (status >= 200 && status < 400)) { + // The request has been completed successfully + parse_sample(http.responseText); + setTimeout(function () { + http.open("GET", 'sample/3'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/3"); +http.send(); + +var http_get_config = new XMLHttpRequest(); +http_get_config.onreadystatechange = function () { + if (http_get_config.readyState === 4) { + var status = http_get_config.status; + if (status === 0 || (status >= 200 && status < 400)) { + // The request has been completed successfully + parse_config(http_get_config.responseText); + } + } else { + // request error + } +} +http_get_config.open("GET", "config/3"); +http_get_config.send(); diff --git a/greenhouseui/static/scripts/greenhouse.js b/greenhouseui/static/scripts/greenhouse.js index fc2a32a..67e63cd 100644 --- a/greenhouseui/static/scripts/greenhouse.js +++ b/greenhouseui/static/scripts/greenhouse.js @@ -161,7 +161,7 @@ http.onreadystatechange = function () { setTimeout(function () { http.open("GET", 'sample/1'); http.send(); - }, 500); + }, 1000); } } else { // request error diff --git a/greenhouseui/templates/garden.html b/greenhouseui/templates/garden.html new file mode 100644 index 0000000..7366c20 --- /dev/null +++ b/greenhouseui/templates/garden.html @@ -0,0 +1,76 @@ + + + + Garten + + + + + + +

Garten

+ + + + + + + + + +
Bewässerung + +
Zeigesteuerte Bewässerung + +
+ + + + \ No newline at end of file diff --git a/greenhouseui/templates/greenhouse.html b/greenhouseui/templates/greenhouse.html new file mode 100644 index 0000000..2a77988 --- /dev/null +++ b/greenhouseui/templates/greenhouse.html @@ -0,0 +1,124 @@ + + + + Gewächshaus + + + + + + +

Gewächshaus

+ + + + + + +

Temperatur

°C

+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
Frostwächter + +
+ +
Zeigesteuerte Bewässerung + +
+
+ + + \ No newline at end of file diff --git a/greenhouseui/templates/index.html b/greenhouseui/templates/index.html index f8af2fb..46ddcef 100644 --- a/greenhouseui/templates/index.html +++ b/greenhouseui/templates/index.html @@ -1,117 +1,24 @@ - Gewächshaus + Garten - -

Gewächshaus

- - - - - - -

Temperatur

°C

-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
Frostwächter - -
- -
Zeigesteuerte Bewässerung - -
+

Garten

+ - \ No newline at end of file + + + diff --git a/setup.py b/setup.py index eb4324c..f1e9723 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ from setuptools import setup from setuptools.command.install import install NAME = 'Greenhouseui' -VERSION = '3' +VERSION = '4' AUTHOR = 'Thomas Klaehn' EMAIL = 'tkl@blackfinn.de' PACKAGES = ['greenhouseui']