diff --git a/gardenui/static/scripts/garden.js b/gardenui/static/scripts/garden.js deleted file mode 100644 index 08734f0..0000000 --- a/gardenui/static/scripts/garden.js +++ /dev/null @@ -1,116 +0,0 @@ -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_2.js b/gardenui/static/scripts/greenhouse_2.js deleted file mode 100644 index a8581f5..0000000 --- a/gardenui/static/scripts/greenhouse_2.js +++ /dev/null @@ -1,116 +0,0 @@ -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 deleted file mode 100644 index 9d66846..0000000 --- a/gardenui/templates/garden.html +++ /dev/null @@ -1,77 +0,0 @@ - - - - Garten - - - - - - -

Garten

- - - - - - - - - -
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 deleted file mode 100644 index 0d2d075..0000000 --- a/gardenui/templates/greenhouse_2.html +++ /dev/null @@ -1,77 +0,0 @@ - - - - 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 deleted file mode 100644 index 15a3e89..0000000 --- a/gardenui/templates/index.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - Garten - - - - - -

Garten

-
- Gewächshaus 1
- Gewächshaus 2
- Gemüsegarten -
- - - - diff --git a/gardenui.service b/greenhouseui.service similarity index 56% rename from gardenui.service rename to greenhouseui.service index 8cf6eda..1a89b3d 100644 --- a/gardenui.service +++ b/greenhouseui.service @@ -1,10 +1,10 @@ [Unit] -Description=Garden UI service +Description=Greenhouse UI service After=multi-user.target gardencontrol.service [Service] Type=idle -ExecStart=gunicorn --bind 0.0.0.0:80 gardenui:app +ExecStart=gunicorn --bind 0.0.0.0:80 greenhouseui:app [Install] WantedBy=multi-user.target diff --git a/gardenui/__init__.py b/greenhouseui/__init__.py similarity index 100% rename from gardenui/__init__.py rename to greenhouseui/__init__.py diff --git a/gardenui/__pycache__/app.cpython-37.pyc b/greenhouseui/__pycache__/app.cpython-37.pyc similarity index 100% rename from gardenui/__pycache__/app.cpython-37.pyc rename to greenhouseui/__pycache__/app.cpython-37.pyc diff --git a/gardenui/app.py b/greenhouseui/app.py similarity index 92% rename from gardenui/app.py rename to greenhouseui/app.py index 9e21dd1..feb64bd 100644 --- a/gardenui/app.py +++ b/greenhouseui/app.py @@ -1,4 +1,4 @@ - +"""Flask application for greenhouse""" import json import logging import xmlrpc.client @@ -10,10 +10,12 @@ from flask import request from flask import jsonify LOG_LEVEL = logging.INFO -LOG_FILE = "/var/log/sauna.log" +LOG_FILE = "/var/log/greenhouseui.log" LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" -URL = 'http://{}:{}'.format('localhost', 64001) +HOST = "localhost" +PORT = 64001 +URL = f"http://{HOST}:{PORT}" # logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE) logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL) @@ -22,24 +24,14 @@ log = logging.getLogger() app = Flask(__name__) -@app.route('/') +@app.route('/', methods=['GET']) def index(): + """Handle GET to index.html""" 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'): + """Handle GET to /sample/""" idx = int(idx) res = {} client = xmlrpc.client.ServerProxy(URL) @@ -65,6 +57,7 @@ def get_sample(idx='0'): @app.route('/config/', methods=['GET']) def get_config(idx='0'): + """Hadnle GET to config/""" idx = int(idx) res = {} client = xmlrpc.client.ServerProxy(URL) @@ -88,6 +81,7 @@ def get_config(idx='0'): @app.route('/sample', methods=['PATCH']) def patch_sample(): + """Handle PATCH to /sample""" client = xmlrpc.client.ServerProxy(URL) record = json.loads(request.data) if 'id' in record and record['id'] == '1': @@ -106,6 +100,7 @@ def patch_sample(): @app.route('/config', methods=['PATCH']) def patch_config(): + """Handle PATCH to /config""" client = xmlrpc.client.ServerProxy(URL) record = json.loads(request.data) if 'water' in record: @@ -153,7 +148,6 @@ def patch_config(): water['times'] = client.get_water_times(int(record['water']['id'])) res['water'] = water - return make_response(jsonify(res), 200) diff --git a/gardenui/static/css/style.css b/greenhouseui/static/css/style.css similarity index 100% rename from gardenui/static/css/style.css rename to greenhouseui/static/css/style.css diff --git a/gardenui/static/scripts/greenhouse_1.js b/greenhouseui/static/scripts/greenhouse.js similarity index 100% rename from gardenui/static/scripts/greenhouse_1.js rename to greenhouseui/static/scripts/greenhouse.js diff --git a/gardenui/templates/greenhouse_1.html b/greenhouseui/templates/index.html similarity index 91% rename from gardenui/templates/greenhouse_1.html rename to greenhouseui/templates/index.html index f7cac27..8dc2e98 100644 --- a/gardenui/templates/greenhouse_1.html +++ b/greenhouseui/templates/index.html @@ -1,14 +1,14 @@ - Gewächshaus 1 + Gewächshaus - + -

Gewächshaus 1

+

Gewächshaus


@@ -112,12 +112,4 @@
- \ No newline at end of file diff --git a/setup.py b/setup.py index 4915a13..c3aa44a 100644 --- a/setup.py +++ b/setup.py @@ -1,35 +1,36 @@ - +"""Installer""" import os import shutil import stat from setuptools import setup from setuptools.command.install import install -NAME = 'Gardenui' +NAME = 'Greenhouseui' VERSION = '1' AUTHOR = 'Thomas Klaehn' EMAIL = 'tkl@blackfinn.de' -PACKAGES = ['gardenui'] +PACKAGES = ['greenhouseui'] REQUIRES = ['Flask', 'gunicorn'] -PACKAGE_DATA = {'gardenui': ['templates/*', 'static/css/*', 'static/scripts/*']} +PACKAGE_DATA = {'greenhouseui': ['templates/*', 'static/css/*', 'static/scripts/*']} SERVICEDIR = "/lib/systemd/system" -DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'gardenui.service') +DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'greenhouseui.service') -LOGFILE = "/var/log/gardenui.log" +LOGFILE = "/var/log/greenhouseui.log" class Install(install): + """Installer""" def run(self): install.run(self) os.makedirs(SERVICEDIR, exist_ok=True) - shutil.copyfile('gardenui.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT)) + shutil.copyfile('greenhouseui.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') + open(LOGFILE, 'r', encoding="UTF-8") except FileNotFoundError: os.makedirs(os.path.dirname(LOGFILE), exist_ok=True) - open(LOGFILE, 'x') + open(LOGFILE, 'x', encoding="UTF-8") os.chmod(LOGFILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)