From a5b5637cfe14459ad8ef6d4ac1745c14048f19e1 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 5 May 2021 14:17:10 +0200 Subject: [PATCH] Initial commit --- .vscode/launch.json | 27 ++++ gardenui.service | 10 ++ gardenui/__init__.py | 2 + gardenui/__pycache__/app.cpython-37.pyc | Bin 0 -> 4021 bytes gardenui/app.py | 161 ++++++++++++++++++++++ gardenui/static/css/style.css | 73 ++++++++++ gardenui/static/scripts/garden.js | 116 ++++++++++++++++ gardenui/static/scripts/greenhouse_1.js | 176 ++++++++++++++++++++++++ gardenui/static/scripts/greenhouse_2.js | 116 ++++++++++++++++ gardenui/templates/garden.html | 77 +++++++++++ gardenui/templates/greenhouse_1.html | 123 +++++++++++++++++ gardenui/templates/greenhouse_2.html | 77 +++++++++++ gardenui/templates/index.html | 26 ++++ setup.py | 40 ++++++ 14 files changed, 1024 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 gardenui.service create mode 100644 gardenui/__init__.py create mode 100644 gardenui/__pycache__/app.cpython-37.pyc create mode 100644 gardenui/app.py create mode 100644 gardenui/static/css/style.css create mode 100644 gardenui/static/scripts/garden.js create mode 100644 gardenui/static/scripts/greenhouse_1.js create mode 100644 gardenui/static/scripts/greenhouse_2.js create mode 100644 gardenui/templates/garden.html create mode 100644 gardenui/templates/greenhouse_1.html create mode 100644 gardenui/templates/greenhouse_2.html create mode 100644 gardenui/templates/index.html create mode 100644 setup.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8622796 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Flask", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "gardenui/app.py", + "FLASK_ENV": "development", + "FLASK_DEBUG": "0" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload", + "--host=0.0.0.0", + "--port=8000" + ], + "jinja": true + } + ] +} \ No newline at end of file diff --git a/gardenui.service b/gardenui.service new file mode 100644 index 0000000..8cf6eda --- /dev/null +++ b/gardenui.service @@ -0,0 +1,10 @@ +[Unit] +Description=Garden UI service +After=multi-user.target gardencontrol.service + +[Service] +Type=idle +ExecStart=gunicorn --bind 0.0.0.0:80 gardenui:app + +[Install] +WantedBy=multi-user.target diff --git a/gardenui/__init__.py b/gardenui/__init__.py new file mode 100644 index 0000000..0ccc333 --- /dev/null +++ b/gardenui/__init__.py @@ -0,0 +1,2 @@ +from .app import app + diff --git a/gardenui/__pycache__/app.cpython-37.pyc b/gardenui/__pycache__/app.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b1e8ef2fdd0212da105948937f79fc2a3ac65547 GIT binary patch literal 4021 zcmb7HOK%*<5uTpczL(_kttTysw$=}>EXRqg5Qc3*vJEhlAu9qr2W>{%&E=B&pnGN& zie6qUUj+y`=ClNINdAKlIR-&~!CZY3;G<7E<*Vx9?ut<$c?MHcT~*!H(_d9}&u67l z-oo`w^Dlq;W!AEOqQ>#%f%ybabWd28w4`WTfq9ESaN7hU=KM%glPmHK)|_U|1$i;;b4jl9SZ7%N=1BgQyv*{mYF1Wv z?WaQDfyFubwtPpvE8lzKU?s%YR`tp=TJ6oMNW5F^Fxmw(rBzodU5`~~uN}q;qEaW^ zRdubRyHR4uBRil;F z%Wd^gwYy>3>Zm9Ro6sB-x8r#4dad@|;q~thlU%#k2;1AeC~k@8mQ}SAf2*fEVVrn8 zz;jV+(5#Oy8_W$nQ5Gb|cRi*rvo{a&EquuS+IHM&SA}+<{fDIscrkf?p|;)Ys9L<+ zt~Em~Rkz<7GvVIe+TNqY;~|!5baG?ax&(TVNey3b9tAV}?4c98N8->P zAWU{`9YgNK9@z{WX|G!Yd*~0WBWgL^%24aj9k^P;)=O6lp_)8{dLSY_6&I2Y#!2vwPVY$IHAo6qPY61yc6FN%?OiT5yU_f_Iy5rfj>mNxxX!+U#Yy>us}~jQ*7VRWoNJ$EkiiV(e&V1$=yN1p0ts9iMN{VJ zQ$$92r#aU1(||TV(0Bi7Vr+FToNy(dc!zT1^uZqX3{i zV-x^?L*o?z@Zj|V0O3GD+d~7S!*CPq25vWn+fCulG2Ct%Liw}>&<8I1WC4C5E;9Hz z20y>4=P*}250d1-mkA3^AdGuYk);uu$&91J(-+9`MIxi<`2QhcQ2h+n z(F(}PkSKEqm{8zgSc479v5BO_`|8uXH@;|4N4ltF(@eM={1r@W?TDcrJ4k6Oc86Z< z?_|IWDF$NK*8$KG%Q#V>Gz1d(rZQ*Q(i;N{=(GMv2&-F0J2!5pBRrKr7`>iC4KnN0(~ct6MyHYP9gyr?D`tY`nhCji}-0Gqh94P%4sg)-8V z1&J%eI1I|sG1g|l=|)?HdNTE=$YLV1gBfEoA>w4O+KgY~Yt`s!c`~7kdtuzzPE-0D zOdz5%VA(~a?Hs4@v|Sd{;OMP?2OV%TaE}0^iPOV?T>^Zs08E>uJhY`fu(9m-2H>#l zUocp<1aPBe&q0^A0TNkj28X540j?t)_9UwKu8#xLeF^~SEnB8#cp5?ae4x4ypQ&E| zJJpi!e`3^v^w}>`e^@lU%#5smPOUSr{uvgf)Pln6*k2{J$u3)%O9fWt)QW=c*y>tp zm1C=Gu$oG(DCmx@?x$9Hwz?0iX=6pjZ}h!CrdH!G{1JLr`U6wQtlo7tfESj|IjCdaPM^iG~X>3b8=>T#T0n z4))G6r_R)-sVA$598MyAnK+6+eFY?0Y;_x2g*ZK(sBnu?yeD-zM@7k#~vk4)Pvxlck&s_-{$dd-_GZwD(U?#}#LA63DU)WuH$5)8Z8FtfSX4a+0mrJ7KF^ zuirh$Tv_A0^$GU9s*`w9J?J-aj>4{*xO+W~-xiIZP3!n|o&iZR_+x3dy3M3y&QfdU zD8&dS-WL5HY1&cvPr@tuZN~T0!_zq zO=n|13~AZ~xuTl2aCE;rQJxtaK6GW*J3ZNNtB>h3A__orwBqGw2z)wn*+mD7>}2hV SYwofrF5y{Nsw{n&7ykuQ!nW}M literal 0 HcmV?d00001 diff --git a/gardenui/app.py b/gardenui/app.py new file mode 100644 index 0000000..9e21dd1 --- /dev/null +++ b/gardenui/app.py @@ -0,0 +1,161 @@ + +import json +import logging +import xmlrpc.client + +from flask import Flask +from flask import render_template +from flask import make_response +from flask import request +from flask import jsonify + +LOG_LEVEL = logging.INFO +LOG_FILE = "/var/log/sauna.log" +LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" + +URL = 'http://{}:{}'.format('localhost', 64001) + +# logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE) +logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL) +log = logging.getLogger() + + +app = Flask(__name__) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/garden') +def garden(): + return render_template('garden.html') + +@app.route('/greenhouse_1') +def greenhouse_1(): + return render_template('greenhouse_1.html') + +@app.route('/greenhouse_2') +def greenhouse_2(): + return render_template('greenhouse_2.html') + +@app.route('/sample/', methods=['GET']) +def get_sample(idx='0'): + idx = int(idx) + res = {} + client = xmlrpc.client.ServerProxy(URL) + + if idx == 1: + heat = {} + heat['id'] = str(idx) + heat['state'] = client.get_current_heat_state(idx) + temp = client.get_current_temperature(idx) + if temp: + temperature = {} + temperature['id'] = str(idx) + temperature['value'] = temp + res['heat'] = heat + res['temperature'] = temperature + + water = {} + water['id'] = str(idx) + water['state'] = client.get_current_water_state(idx) + res['water'] = water + + return make_response(jsonify(res), 200) + +@app.route('/config/', methods=['GET']) +def get_config(idx='0'): + idx = int(idx) + res = {} + client = xmlrpc.client.ServerProxy(URL) + + if idx == 1: + heat = {} + heat['id'] = str(idx) + heat['autostate'] = client.get_heat_autostate(idx) + heat['on_temperature'] = client.get_on_temperature(idx) + heat['off_temperature'] = client.get_off_temperature(idx) + res['heat'] = heat + + water = {} + water['id'] = str(idx) + water['autostate'] = client.get_water_autostate(idx) + water['times'] = client.get_water_times(idx) + res['water'] = water + + return make_response(jsonify(res), 200) + + +@app.route('/sample', methods=['PATCH']) +def patch_sample(): + client = xmlrpc.client.ServerProxy(URL) + record = json.loads(request.data) + if 'id' in record and record['id'] == '1': + if 'heatstate' in record: + if record['heatstate']: + client.set_heat_state(record['id']) + else: + client.clear_heat_state(record['id']) + if 'id' in record and 'waterstate' in record: + if record['waterstate']: + client.set_water_state(record['id']) + else: + client.clear_water_state(record['id']) + return make_response("", 204) + + +@app.route('/config', methods=['PATCH']) +def patch_config(): + client = xmlrpc.client.ServerProxy(URL) + record = json.loads(request.data) + if 'water' in record: + water = record['water'] + if 'autostate' in water: + if water['autostate']: + client.set_water_autostate(water['id']) + else: + client.clear_water_autostate(water['id']) + if 'times' in water: + client.set_water_times(water['id'], water['times']) + if 'heat' in record: + if 'id' in record['heat'] and record['heat']['id'] == '1': + if 'autostate' in record['heat']: + if record['heat']['autostate']: + client.set_heat_autostate('1') + else: + client.clear_heat_autostate('1') + if 'increase_on_temperature' in record['heat']: + if record['heat']['increase_on_temperature']: + client.increase_on_temperature('1') + if 'decrease_on_temperature' in record['heat']: + if record['heat']['decrease_on_temperature']: + client.decrease_on_temperature('1') + if 'increase_off_temperature' in record['heat']: + if record['heat']['increase_off_temperature']: + client.increase_off_temperature('1') + if 'decrease_off_temperature' in record['heat']: + if record['heat']['decrease_off_temperature']: + client.decrease_off_temperature('1') + + # prepare answer + res = {} + if 'heat' in record and record['heat']['id'] == '1': + heat = {} + heat['id'] = '1' + heat['autostate'] = client.get_heat_autostate(1) + heat['on_temperature'] = client.get_on_temperature(1) + heat['off_temperature'] = client.get_off_temperature(1) + res['heat'] = heat + if 'water' in record: + water = {} + water['id'] = record['water']['id'] + water['autostate'] = client.get_water_autostate(int(record['water']['id'])) + water['times'] = client.get_water_times(int(record['water']['id'])) + res['water'] = water + + + return make_response(jsonify(res), 200) + + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0', port=8000) diff --git a/gardenui/static/css/style.css b/gardenui/static/css/style.css new file mode 100644 index 0000000..08c9ea8 --- /dev/null +++ b/gardenui/static/css/style.css @@ -0,0 +1,73 @@ +html, body { + font-size: 22px !important; + font-family: arial, verdana, helvetica, sans-serif; + color: #b6b6b6; + background: #282929; +} + +h1, h2, h3 {text-align: center;} +p {text-align: center;} +div {text-align: center;} + +.center { + margin-left: auto; + margin-right: auto; + text-align: center; +} + +input { + border: 1px #999999 solid; + background-color: #f6f6f6; + padding: 2px 4px 2px 4px; + margin: 0 0 0.5rem 0; + font-size: 1.0rem; + border-radius: 8px; +} + +input[type="submit" i] { + color: #b6b6b6; + background-color: #282929; + padding: 10px; + margin: 10px 0; + border-color: #b6b6b6; + border-width: 3px; + border-radius: 18px; + font-size: 20px; +} + +input[type="text"] { + border-radius: 8px; +} + +td { + padding: 8px; +} + +.table_left { + text-align: right; +} + +footer { + position: fixed; + /* left: 50%; */ + bottom: 20px; + /* transform: translate(-50%, -50%); */ + margin: 0 auto; +} + +a { + outline: none; + color: #b6b6b6; + text-decoration-line: none; +} + +select { + color: #b6b6b6; + background-color: #282929; + padding: 10px; + margin: 10px 0; + border-color: #b6b6b6; + border-width: 3px; + border-radius: 16px; + font-size: 20px; +} diff --git a/gardenui/static/scripts/garden.js b/gardenui/static/scripts/garden.js new file mode 100644 index 0000000..08734f0 --- /dev/null +++ b/gardenui/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/gardenui/static/scripts/greenhouse_1.js b/gardenui/static/scripts/greenhouse_1.js new file mode 100644 index 0000000..f958ae7 --- /dev/null +++ b/gardenui/static/scripts/greenhouse_1.js @@ -0,0 +1,176 @@ +var on_switch_heat = function() { + var state = true; + if(document.getElementById("heat_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"id": "1", "heatstate": state}); + patch_sample(json_str); +} + +var on_switch_heat_autostate = function() { + var state = true; + if(document.getElementById("heat_auto_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"heat": {"id": "1", "autostate": state}}); + patch_config(json_str); +} + +var on_increase_on_temperature = function() { + var json_str = JSON.stringify({"heat": {"id": "1", "increase_on_temperature": true}}); + patch_config(json_str); +} + +var on_increase_off_temperature = function() { + var json_str = JSON.stringify({"heat": {"id": "1", "increase_off_temperature": true}}); + patch_config(json_str); +} + +var on_decrease_on_temperature = function() { + var json_str = JSON.stringify({"heat": {"id": "1", "decrease_on_temperature": true}}); + patch_config(json_str); +} + +var on_decrease_off_temperature = function() { + var json_str = JSON.stringify({"heat": {"id": "1", "decrease_off_temperature": true}}); + patch_config(json_str); +} + +var on_switch_water = function() { + var state = true; + if(document.getElementById("water_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"id": "1", "waterstate": state}); + patch_sample(json_str); +} + +var on_switch_water_auto_state = function() { + var state = true; + if(document.getElementById("water_auto_switch").value == "ausschalten") { + state = false; + } + var json_str = JSON.stringify({"water": {"id": "1", "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": "1", "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.heat && config.heat.id == '1') { + if(config.heat.autostate) { + output = "ausschalten" + visibility = "visible" + document.getElementById("on_temperature").innerHTML = config.heat.on_temperature; + document.getElementById("off_temperature").innerHTML = config.heat.off_temperature; + } + document.getElementById("heat_auto_switch").value = output; + document.getElementById("config_temperatures").style.visibility = visibility + } + output = "einschalten" + visibility = 'hidden' + if(config.water && config.water.id == '1') { + 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_auto_switch").value = output; + document.getElementById("water_times").style.visibility = visibility + } +} + +var parse_sample = function (event) { + var sample = JSON.parse(event) + if(sample.heat && sample.heat.id == '1') { + var switch_caption = "einschalten"; + if(sample.heat.state) { + switch_caption = "ausschalten"; + } + document.getElementById("heat_switch").value = switch_caption; + } + if(sample.temperature && sample.temperature.id == '1') { + document.getElementById("temperature_value").innerHTML = sample.temperature.value; + } + if(sample.water && sample.water.id == '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/1'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/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/1"); +http_get_config.send(); diff --git a/gardenui/static/scripts/greenhouse_2.js b/gardenui/static/scripts/greenhouse_2.js new file mode 100644 index 0000000..a8581f5 --- /dev/null +++ b/gardenui/static/scripts/greenhouse_2.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": "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({"water": {"id": "2", "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": "2", "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 == '2') { + 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 == '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/2'); + http.send(); + }, 500); + } + } else { + // request error + } +} +http.open("GET", "sample/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/2"); +http_get_config.send(); diff --git a/gardenui/templates/garden.html b/gardenui/templates/garden.html new file mode 100644 index 0000000..9d66846 --- /dev/null +++ b/gardenui/templates/garden.html @@ -0,0 +1,77 @@ + + + + Garten + + + + + + +

Garten

+ + + + + + + + + +
Bewässerung + +
Zeigesteuerte Bewässerung + +
+ + + + \ No newline at end of file diff --git a/gardenui/templates/greenhouse_1.html b/gardenui/templates/greenhouse_1.html new file mode 100644 index 0000000..f7cac27 --- /dev/null +++ b/gardenui/templates/greenhouse_1.html @@ -0,0 +1,123 @@ + + + + Gewächshaus 1 + + + + + + +

Gewächshaus 1

+
+ + + + + +

Temperatur

+ + + + + + + + + +
Heizung + +
Frostwächter + +
+ +
+ + + + + + + + + +
Bewässerung + +
Zeigesteuerte Bewässerung + +
+ + + + \ No newline at end of file diff --git a/gardenui/templates/greenhouse_2.html b/gardenui/templates/greenhouse_2.html new file mode 100644 index 0000000..0d2d075 --- /dev/null +++ b/gardenui/templates/greenhouse_2.html @@ -0,0 +1,77 @@ + + + + Gewächshaus 2 + + + + + + +

Gewächshaus 2

+ + + + + + + + + +
Bewässerung + +
Zeigesteuerte Bewässerung + +
+ + + + \ No newline at end of file diff --git a/gardenui/templates/index.html b/gardenui/templates/index.html new file mode 100644 index 0000000..15a3e89 --- /dev/null +++ b/gardenui/templates/index.html @@ -0,0 +1,26 @@ + + + + Garten + + + + + +

Garten

+ + + + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4915a13 --- /dev/null +++ b/setup.py @@ -0,0 +1,40 @@ + +import os +import shutil +import stat +from setuptools import setup +from setuptools.command.install import install + +NAME = 'Gardenui' +VERSION = '1' +AUTHOR = 'Thomas Klaehn' +EMAIL = 'tkl@blackfinn.de' +PACKAGES = ['gardenui'] +REQUIRES = ['Flask', 'gunicorn'] +PACKAGE_DATA = {'gardenui': ['templates/*', 'static/css/*', 'static/scripts/*']} + +SERVICEDIR = "/lib/systemd/system" +DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'gardenui.service') + +LOGFILE = "/var/log/gardenui.log" + +class Install(install): + def run(self): + install.run(self) + os.makedirs(SERVICEDIR, exist_ok=True) + shutil.copyfile('gardenui.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT)) + os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) + + try: + open(LOGFILE, 'r') + except FileNotFoundError: + os.makedirs(os.path.dirname(LOGFILE), exist_ok=True) + open(LOGFILE, 'x') + os.chmod(LOGFILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | + stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) + + +setup(name=NAME, version=VERSION, long_description=__doc__, author=AUTHOR, + author_email=EMAIL, packages=PACKAGES, include_package_data=True, + package_data=PACKAGE_DATA, zip_safe=False, install_requires=REQUIRES, + cmdclass={'install': Install})