From ac1b251dccb40210b6489e5a5c6d5b97dbe5d253 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 7 Jun 2022 14:12:38 +0200 Subject: [PATCH] Switch to relay_rpc hadling --- .gitignore | 1 + .vscode/launch.json | 8 ++ home/app.py | 100 ++++++++++++------ home/config.json | 107 +++++++++++++++---- home/static/scripts/gewaechshaus.js | 139 +++++++++++++++++++++++++ home/static/scripts/hochbeet.js | 20 ++-- home/static/scripts/rasen_1.js | 139 +++++++++++++++++++++++++ home/static/scripts/rasen_2.js | 139 +++++++++++++++++++++++++ home/static/scripts/rasen_3.js | 139 +++++++++++++++++++++++++ home/static/scripts/tomatentuppen.js | 23 ++-- home/static/scripts/wasseranschluss.js | 139 +++++++++++++++++++++++++ home/templates/gewaechshaus.html | 61 +++++++++++ home/templates/hochbeet.html | 29 ++---- home/templates/index.html | 5 + home/templates/rasen_1.html | 61 +++++++++++ home/templates/rasen_2.html | 61 +++++++++++ home/templates/rasen_3.html | 61 +++++++++++ home/templates/tomatentuppen.html | 29 ++---- home/templates/wasseranschluss.html | 61 +++++++++++ 19 files changed, 1198 insertions(+), 124 deletions(-) create mode 100644 .gitignore create mode 100644 home/static/scripts/gewaechshaus.js create mode 100644 home/static/scripts/rasen_1.js create mode 100644 home/static/scripts/rasen_2.js create mode 100644 home/static/scripts/rasen_3.js create mode 100644 home/static/scripts/wasseranschluss.js create mode 100644 home/templates/gewaechshaus.html create mode 100644 home/templates/rasen_1.html create mode 100644 home/templates/rasen_2.html create mode 100644 home/templates/rasen_3.html create mode 100644 home/templates/wasseranschluss.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/.vscode/launch.json b/.vscode/launch.json index c383684..83a1ad7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,14 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + }, { "name": "Python: Flask", "type": "python", diff --git a/home/app.py b/home/app.py index 2c01dea..1ca9f25 100644 --- a/home/app.py +++ b/home/app.py @@ -1,6 +1,7 @@ """Flask app""" import datetime import json +import logging import os import shutil import threading @@ -15,6 +16,11 @@ from flask import jsonify CONFIG_FILE = os.path.join(os.path.expanduser('~'), ".config/home/config.json") +LOG_LEVEL = logging.INFO +LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" + +logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL) +log = logging.getLogger() class Control(threading.Thread): """Control""" def __init__(self, config_file_name: str): @@ -22,7 +28,6 @@ class Control(threading.Thread): self.run_condition = True self.config = None self.config_file = config_file_name - self.water_state = [] def reload_config(self): @@ -36,54 +41,57 @@ class Control(threading.Thread): shutil.copyfile("config.json", self.config_file) with open(self.config_file, "r", encoding="UTF-8") as handle: self.config = json.load(handle) - for _ in range(len(self.config['configs'])): - self.water_state.append(False) def run(self): while self.run_condition: configs = self.config['configs'] - water_index = 0 for config in configs: water = config["water"] autostate = water["autostate"] if autostate: times = water["times"] - idx = 0 now = datetime.datetime.now() - if int(now.hour) >= 12: - idx = 1 - on_time_pattern = times[idx]['on_time'] - on_time_pattern = on_time_pattern.split(':') - on_time = now.replace(hour=int(on_time_pattern[0]), - minute=int(on_time_pattern[1]), - second=0, - microsecond=0) - off_time_pattern = times[idx]['off_time'] - off_time_pattern = off_time_pattern.split(':') - off_time = now.replace(hour=int(off_time_pattern[0]), - minute=int(off_time_pattern[1]), - second=0, - microsecond=0) - - + switch_on = False + for entry in times: + on_time_pattern = entry['on_time'] + on_time_pattern = on_time_pattern.split(':') + on_time = now.replace(hour=int(on_time_pattern[0]), + minute=int(on_time_pattern[1]), + second=0, + microsecond=0) + off_time_pattern = entry['off_time'] + off_time_pattern = off_time_pattern.split(':') + off_time = now.replace(hour=int(off_time_pattern[0]), + minute=int(off_time_pattern[1]), + second=0, + microsecond=0) + if now > on_time and now <= off_time: + switch_on = True + break url = "http://" + config["host"] + ":" + str(config["port"]) - if now > on_time and now <= off_time and not self.water_state[water_index]: + water_state = True + try: + client = xmlrpc.client.ServerProxy(url) + water_state = client.get_relay_state(water["relay"]) + except OSError as error: + log.error(error) + log.info(url) + if switch_on and not water_state: try: client = xmlrpc.client.ServerProxy(url) - client.switch_relay(water["relay"], True) - self.water_state[water_index] = client.get_relay_state(water["relay"]) + water_state = client.switch_relay(water["relay"], True) except OSError as error: - print(error) - elif now > off_time and self.water_state[water_index]: + log.error(error) + log.info(url) + elif not switch_on and water_state: try: client = xmlrpc.client.ServerProxy(url) client.switch_relay(water["relay"], False) - self.water_state[water_index] = client.get_relay_state(water["relay"]) except OSError as error: - print(error) - water_index += 1 - time.sleep(1) + log.error(error) + log.info(url) + time.sleep(10) control = Control(CONFIG_FILE) @@ -108,6 +116,36 @@ def tomatentuppen(): return render_template('tomatentuppen.html') +@app.route('/rasen_1', methods=['GET']) +def rasen_1(): + """Handle GET to rasen_1.html""" + return render_template('rasen_1.html') + + +@app.route('/rasen_2', methods=['GET']) +def rasen_2(): + """Handle GET to rasen_2.html""" + return render_template('rasen_2.html') + + +@app.route('/rasen_3', methods=['GET']) +def rasen_3(): + """Handle GET to rasen_3.html""" + return render_template('rasen_3.html') + + +@app.route('/gewaechshaus', methods=['GET']) +def gewaechshaus(): + """Handle GET to gewaechshaus.html""" + return render_template('gewaechshaus.html') + + +@app.route('/wasseranschluss', methods=['GET']) +def wasseranschluss(): + """Handle GET to wasseranschluss.html""" + return render_template('wasseranschluss.html') + + @app.route('/sample/', methods=['GET']) def get_sample(idx='0'): """Handle GET to /sample/""" @@ -213,6 +251,6 @@ def start_control(): control.start() -if __name__ == 'app': +if __name__ == "__main__": start_control() app.run(debug=True, host='0.0.0.0', port=8000) diff --git a/home/config.json b/home/config.json index f4aa452..6b0e4a8 100644 --- a/home/config.json +++ b/home/config.json @@ -1,6 +1,5 @@ { - "configs": - [ + "configs": [ { "id": "hochbeet", "host": "hochbeet", @@ -8,16 +7,11 @@ "water": { "relay": 2, - "autostate": false, - "times": - [ + "autostate": true, + "times": [ { - "on_time": "7:00", - "off_time": "7:20" - }, - { - "on_time": "19:00", - "off_time": "19:20" + "on_time": "8:00", + "off_time": "8:10" } ] } @@ -29,16 +23,91 @@ "water": { "relay": 3, - "autostate": false, - "times": - [ + "autostate": true, + "times": [ + { + "on_time": "8:00", + "off_time": "9:00" + } + ] + } + }, + { + "id": "rasen_1", + "host": "garten", + "port": 64001, + "water": + { + "relay": 3, + "autostate": true, + "times": [ + { + "on_time": "6:00", + "off_time": "6:30" + } + ] + } + }, + { + "id": "wasseranschluss", + "host": "garten", + "port": 64001, + "water": + { + "relay": 2, + "autostate": true, + "times": [ + { + "on_time": "6:30", + "off_time": "7:00" + } + ] + } + }, + { + "id": "gewaechshaus", + "host": "garten", + "port": 64001, + "water": + { + "relay": 1, + "autostate": true, + "times": [ + { + "on_time": "8:00", + "off_time": "8:10" + } + ] + } + }, + { + "id": "rasen_2", + "host": "wels", + "port": 64001, + "water": + { + "relay": 1, + "autostate": true, + "times": [ + { + "on_time": "7:30", + "off_time": "8:00" + } + ] + } + }, + { + "id": "rasen_3", + "host": "wels", + "port": 64001, + "water": + { + "relay": 2, + "autostate": true, + "times": [ { "on_time": "7:00", - "off_time": "7:20" - }, - { - "on_time": "19:00", - "off_time": "19:20" + "off_time": "7:30" } ] } diff --git a/home/static/scripts/gewaechshaus.js b/home/static/scripts/gewaechshaus.js new file mode 100644 index 0000000..c305d89 --- /dev/null +++ b/home/static/scripts/gewaechshaus.js @@ -0,0 +1,139 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify( + { + "id": "gewaechshaus", + "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( + { + "id": "gewaechshaus", + "water": + { + "autostate": state + } + } + ); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; + var json_str = JSON.stringify( + { + "id": "gewaechshaus", + "water": + { + "times": + [ + { + "on_time": on_time, + "off_time": off_time + } + ] + } + } + ); + 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/gewaechshaus"); + 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/gewaechshaus"); + 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) { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].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 == 'gewaechshaus') { + 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/gewaechshaus'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/gewaechshaus"); +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/gewaechshaus"); +http_get_config.send(); diff --git a/home/static/scripts/hochbeet.js b/home/static/scripts/hochbeet.js index 6f3ead8..e7c6361 100644 --- a/home/static/scripts/hochbeet.js +++ b/home/static/scripts/hochbeet.js @@ -30,10 +30,8 @@ var on_switch_auto_state = function() { } 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 on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; var json_str = JSON.stringify( { "id": "hochbeet", @@ -42,12 +40,8 @@ var on_change_config = function() { "times": [ { - "on_time": on_time_one, - "off_time": off_time_one - }, - { - "on_time": on_time_two, - "off_time": off_time_two + "on_time": on_time, + "off_time": off_time } ] } @@ -91,10 +85,8 @@ var parse_config = function (event) { 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("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].off_time; } document.getElementById("auto_switch").value = output; document.getElementById("water_times").style.visibility = visibility diff --git a/home/static/scripts/rasen_1.js b/home/static/scripts/rasen_1.js new file mode 100644 index 0000000..ca69b6e --- /dev/null +++ b/home/static/scripts/rasen_1.js @@ -0,0 +1,139 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify( + { + "id": "rasen_1", + "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( + { + "id": "rasen_1", + "water": + { + "autostate": state + } + } + ); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; + var json_str = JSON.stringify( + { + "id": "rasen_1", + "water": + { + "times": + [ + { + "on_time": on_time, + "off_time": off_time + } + ] + } + } + ); + 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/rasen_1"); + 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/rasen_1"); + 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) { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].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 == 'rasen_1') { + 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/rasen_1'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/rasen_1"); +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/rasen_1"); +http_get_config.send(); diff --git a/home/static/scripts/rasen_2.js b/home/static/scripts/rasen_2.js new file mode 100644 index 0000000..1140a1e --- /dev/null +++ b/home/static/scripts/rasen_2.js @@ -0,0 +1,139 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify( + { + "id": "rasen_2", + "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( + { + "id": "rasen_2", + "water": + { + "autostate": state + } + } + ); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; + var json_str = JSON.stringify( + { + "id": "rasen_2", + "water": + { + "times": + [ + { + "on_time": on_time, + "off_time": off_time + } + ] + } + } + ); + 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/rasen_2"); + 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/rasen_2"); + 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) { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].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 == 'rasen_2') { + 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/rasen_2'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/rasen_2"); +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/rasen_2"); +http_get_config.send(); diff --git a/home/static/scripts/rasen_3.js b/home/static/scripts/rasen_3.js new file mode 100644 index 0000000..c5a2dfa --- /dev/null +++ b/home/static/scripts/rasen_3.js @@ -0,0 +1,139 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify( + { + "id": "rasen_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( + { + "id": "rasen_3", + "water": + { + "autostate": state + } + } + ); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; + var json_str = JSON.stringify( + { + "id": "rasen_3", + "water": + { + "times": + [ + { + "on_time": on_time, + "off_time": off_time + } + ] + } + } + ); + 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/rasen_3"); + 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/rasen_3"); + 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) { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].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 == 'rasen_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/rasen_3'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/rasen_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/rasen_3"); +http_get_config.send(); diff --git a/home/static/scripts/tomatentuppen.js b/home/static/scripts/tomatentuppen.js index 63988c0..3e28506 100644 --- a/home/static/scripts/tomatentuppen.js +++ b/home/static/scripts/tomatentuppen.js @@ -30,24 +30,17 @@ var on_switch_auto_state = function() { } 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 on_time = document.getElementById("water_on").value; + var off_time = document.getElementById("water_off").value; var json_str = JSON.stringify( { "id": "tomatentuppen", "water": { - "times": - [ + "times": [ { - "on_time": on_time_one, - "off_time": off_time_one - }, - { - "on_time": on_time_two, - "off_time": off_time_two + "on_time": on_time, + "off_time": off_time } ] } @@ -91,10 +84,8 @@ var parse_config = function (event) { 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("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].off_time; } document.getElementById("auto_switch").value = output; document.getElementById("water_times").style.visibility = visibility diff --git a/home/static/scripts/wasseranschluss.js b/home/static/scripts/wasseranschluss.js new file mode 100644 index 0000000..5c1b20b --- /dev/null +++ b/home/static/scripts/wasseranschluss.js @@ -0,0 +1,139 @@ +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify( + { + "id": "wasseranschluss", + "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( + { + "id": "wasseranschluss", + "water": + { + "autostate": state + } + } + ); + patch_config(json_str); +} + +var on_change_config = function() { + var on_time_one = document.getElementById("water_on").value; + var off_time_one = document.getElementById("water_off").value; + var json_str = JSON.stringify( + { + "id": "wasseranschluss", + "water": + { + "times": + [ + { + "on_time": on_time, + "off_time": off_time + } + ] + } + } + ); + 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/wasseranschluss"); + 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/wasseranschluss"); + 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) { + if(config.water.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("water_on").value = config.water.times[0].on_time; + document.getElementById("water_off").value = config.water.times[0].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 == 'wasseranschluss') { + 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/wasseranschluss'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/wasseranschluss"); +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/wasseranschluss"); +http_get_config.send(); diff --git a/home/templates/gewaechshaus.html b/home/templates/gewaechshaus.html new file mode 100644 index 0000000..5a4c7a3 --- /dev/null +++ b/home/templates/gewaechshaus.html @@ -0,0 +1,61 @@ + + + + Gewächshaus + + + + + + +

Gewächshaus

+ + + + + + + + + +
Bewässerung + +
Zeigesteuerte Bewässerung + +
+ + + + \ No newline at end of file diff --git a/home/templates/hochbeet.html b/home/templates/hochbeet.html index 12e1373..4e4c11e 100644 --- a/home/templates/hochbeet.html +++ b/home/templates/hochbeet.html @@ -26,38 +26,18 @@