From 050a01d05e661633fa839ce27df06daafb97c7db Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 30 Mar 2022 12:02:27 +0200 Subject: [PATCH] Remove legacy code --- greenhouse/__init__.py | 1 - greenhouse/app.py | 219 ---------------------------- greenhouse/static/css/style.css | 73 ---------- greenhouse/static/scripts/config.js | 121 --------------- greenhouse/static/scripts/index.js | 66 --------- greenhouse/templates/config.html | 115 --------------- greenhouse/templates/index.html | 42 ------ 7 files changed, 637 deletions(-) delete mode 100644 greenhouse/__init__.py delete mode 100644 greenhouse/app.py delete mode 100644 greenhouse/static/css/style.css delete mode 100644 greenhouse/static/scripts/config.js delete mode 100644 greenhouse/static/scripts/index.js delete mode 100644 greenhouse/templates/config.html delete mode 100644 greenhouse/templates/index.html diff --git a/greenhouse/__init__.py b/greenhouse/__init__.py deleted file mode 100644 index c07c459..0000000 --- a/greenhouse/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .app import app diff --git a/greenhouse/app.py b/greenhouse/app.py deleted file mode 100644 index d7011a8..0000000 --- a/greenhouse/app.py +++ /dev/null @@ -1,219 +0,0 @@ -import os -import site -import json -import datetime -import logging -from threading import Thread -from time import sleep - -from flask import Flask -from flask import render_template -from flask import make_response -from flask import request - -from w1thermsensor import W1ThermSensor -import RPi.GPIO as GPIO - -sensor = W1ThermSensor() -water_pin = 22 #17/27/22 -heat_pin = 26 - -heat_state = False - -PACKAGE_PATH = site.getsitepackages()[0] -CONFIG_FILE = os.path.join(PACKAGE_PATH, "greenhouse/config/greenhouse.json") - -GPIO.setwarnings(False) -GPIO.setmode(GPIO.BCM) -GPIO.setup(water_pin, GPIO.OUT) - -log_level = logging.INFO -LOG_FILE = "/var/log/greenhouse.log" -LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" - -# logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE) -logging.basicConfig(format=LOG_FORMAT, level=log_level) -log = logging.getLogger('greenhouse') - -class Heat(Thread): - def __init__(self, pin): - super(Heat, self).__init__() - self.__pin = pin - self.__state = False - GPIO.setup(pin, GPIO.OUT) - if GPIO.input(pin): - self.__state = True - self.__run_condition = True - self.__next_update = datetime.datetime.now() - - def on(self): - self.__state = True - GPIO.output(self.__pin, 0) - - def off(self): - self.__state = False - GPIO.output(self.__pin, 1) - - def run(self): - self.__next_update = datetime.datetime.now() - while self.__run_condition: - now = datetime.datetime.now() - if now >= self.__next_update: - if self.__state: - # Do a power cycle to prevent auto-poweroff - GPIO.output(self.__pin, 1) - sleep(5) - GPIO.output(self.__pin, 0) - self.__next_update = now + datetime.timedelta(minutes=5) - sleep(1) - - def stop(self): - self.__run_condition = False - self.join() - - - def state(self): - return self.__state - -heat = Heat(heat_pin) -heat.start() - -class GreenControl(Thread): - def __init__(self, cfg_file:str): - super(GreenControl, self).__init__() - self.cfg_file = cfg_file - self.config = {} - self.__run_condition = True - self.__water_state = False - self.__trigger_read_config = True - - def reload_config(self): - self.__trigger_read_config = True - - def run(self): - global sensor - global heat - next_update = datetime.datetime.now() - while self.__run_condition: - if self.__trigger_read_config: - self.__trigger_read_config = False - with open(self.cfg_file, "r") as f: - self.config = json.load(f) - now = datetime.datetime.now() - if now >= next_update: - if self.config['heat']['state']: - temperature = sensor.get_temperature() - if float(temperature) < float(self.config['heat']['on_temperature']) and not heat.state(): - heat.on() - log.info("Switch heat on by temperature level: %.1f °C", float(temperature)) - elif float(temperature) > float(self.config['heat']['off_temperature']) and heat.state(): - heat.off() - log.info("Switch heat off by temperature level: %.1f °C", float(temperature)) - elif heat.state(): - # Do a power cycle to prevent auto-poweroff - heat.off() - sleep(5) - heat.on() - next_update = now + datetime.timedelta(minutes=5) - if self.config['water']['state']: - index = 0 - if int(now.hour) >= 12: - index = 1 - on_time_pattern = self.config['water']['times'][index]['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 = self.config['water']['times'][index]['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 and not self.__water_state: - GPIO.output(water_pin, 0) - self.__water_state = True - log.info("Switch water on by time") - elif now > off_time and self.__water_state: - GPIO.output(water_pin, 1) - self.__water_state = False - log.info("Switch water off by time") - sleep(1) - - -green_ctrl = GreenControl(CONFIG_FILE) -green_ctrl.start() - -app = Flask(__name__) - - -@app.route('/') -def index(): - return render_template('index.html') - - -@app.route('/config') -def config(): - return render_template('config.html') - -@app.route('/cfg', methods=['GET']) -def get_config(): - res = {} - with open(CONFIG_FILE, "r") as f: - res = json.load(f) - return res - -@app.route('/cfg', methods=['PATCH']) -def patch_config(): - global green_ctrl - res = make_response("", 500) - cfg = json.loads(request.data) - with open(CONFIG_FILE, "w") as f: - json.dump(cfg, f) - res = make_response("", 204) - green_ctrl.reload_config() - return res - - -@app.route('/sample', methods=['GET']) -def get_sample(): - global heat - global sensor - try: - temperature = f"{float(sensor.get_temperature()):.1f}" - except Exception: - temperature = None - - water_state = False - if not GPIO.input(water_pin): - water_state = True - - res = {} - res["id"] = str(1) - if temperature: - res["temperature"] = temperature - res["water"] = water_state - res["heat"] = heat.state() - return res - - -@app.route('/sample', methods=['PATCH']) -def patch_sample(): - global heat - record = json.loads(request.data) - if "water" in record: - if record["water"]: - GPIO.output(water_pin, 0) - log.info("Switch water on by button: {}".format(datetime.datetime.now())) - else: - GPIO.output(water_pin, 1) - log.info("Switch water off by button: {}".format(datetime.datetime.now())) - if "heat" in record: - if record["heat"]: - heat.on() - log.info("Switch heat on by button: {}".format(datetime.datetime.now())) - else: - heat.off() - log.info("Switch heat off by button: {}".format(datetime.datetime.now())) - - res = make_response("", 204) - return res - -if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0', port=8000) diff --git a/greenhouse/static/css/style.css b/greenhouse/static/css/style.css deleted file mode 100644 index 08c9ea8..0000000 --- a/greenhouse/static/css/style.css +++ /dev/null @@ -1,73 +0,0 @@ -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/greenhouse/static/scripts/config.js b/greenhouse/static/scripts/config.js deleted file mode 100644 index 983e87e..0000000 --- a/greenhouse/static/scripts/config.js +++ /dev/null @@ -1,121 +0,0 @@ -var get_cfg = function (event) { - var config = JSON.parse(event) - if(config.heat) { - if(config.heat.state) { - document.getElementById("heat_state").value = "an" - document.getElementById('heat_config').style.visibility = 'visible'; - } else { - document.getElementById("heat_state").value = "aus" - document.getElementById('heat_config').style.visibility = 'hidden'; - } - if(config.heat.on_temperature) { - document.getElementById("switch_on_temperature").value = config.heat.on_temperature; - } - if(config.heat.off_temperature) { - document.getElementById("switch_off_temperature").value = config.heat.off_temperature; - } - } - if(config.water) { - if(config.water.state) { - document.getElementById("water_state").value = "an" - document.getElementById('water_times').style.visibility = 'visible'; - } else { - document.getElementById("water_state").value = "aus" - document.getElementById('water_times').style.visibility = 'hidden'; - } - if(config.water.times) { - 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; - } - } -} - -var on_change_heat_state = function() { - if(document.getElementById("heat_state").value == "an") { - document.getElementById("heat_state").value = "aus" - document.getElementById('heat_config').style.visibility = 'hidden'; - } else { - document.getElementById("heat_state").value = "an" - document.getElementById('heat_config').style.visibility = 'visible'; - } -} - -var on_change_water_state = function() { - if(document.getElementById("water_state").value == "an") { - document.getElementById("water_state").value = "aus"; - document.getElementById('water_times').style.visibility = 'hidden'; - } else { - document.getElementById("water_state").value = "an"; - document.getElementById('water_times').style.visibility = 'visible'; - } -} - -var on_push_config = function() { - var water_state = false; - if(document.getElementById("water_state").value == 'an') { - water_state = true; - } - 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 heat_state = false; - if(document.getElementById("heat_state").value == 'an') { - heat_state = true; - } - var on_tmp = document.getElementById("switch_on_temperature").value; - var off_tmp = document.getElementById("switch_off_temperature").value; - - var json_str = JSON.stringify( - { - "water": - { - "state":water_state, - "times": - [ - { - "on_time":on_time_one, - "off_time":off_time_one - }, - { - "on_time":on_time_two, - "off_time":off_time_two - } - ] - }, - "heat": - { - "state":heat_state, - "on_temperature":on_tmp, - "off_temperature":off_tmp - } - } - ); - patch_cfg(json_str); -} - -var patch_http = new XMLHttpRequest(); -var patch_cfg = function(config) { - patch_http.abort(); - patch_http.open("PATCH", "/cfg"); - patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); - patch_http.send(config); -} - -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 - get_cfg(http.responseText); - } - } else { - // request error - } -} -http.open("GET", "/cfg"); -http.send(); diff --git a/greenhouse/static/scripts/index.js b/greenhouse/static/scripts/index.js deleted file mode 100644 index 1f03f66..0000000 --- a/greenhouse/static/scripts/index.js +++ /dev/null @@ -1,66 +0,0 @@ -var on_switch_heat = function() { - var state = true; - if(document.getElementById("heat_switch").value == "ausschalten") { - state = false; - } - var json_str = JSON.stringify({"id": "1", "heat": state}); - patch_sample(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", "water": state}); - patch_sample(json_str); -} - -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 get_sample = function (event) { - var sample = JSON.parse(event) - output = "einschalten" - out_state = "aus" - if(sample.water) { - output = "ausschalten" - out_state = "an" - } - document.getElementById("temperature_value").innerHTML = sample.temperature + " °C"; - document.getElementById("water_switch").value = output; - document.getElementById("water_state").innerHTML = out_state; - if(sample.heat) { - output = "ausschalten" - out_state = "an" - } else { - output = "einschalten" - out_state = "aus" - } - document.getElementById("heat_switch").value = output; - document.getElementById("heat_state").innerHTML = out_state; -} - -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 - get_sample(http.responseText); - setTimeout(function () { - http.open("GET", 'sample'); - http.send(); - }, 500); - } - } else { - // request error - } -} -http.open("GET", "sample"); -http.send(); diff --git a/greenhouse/templates/config.html b/greenhouse/templates/config.html deleted file mode 100644 index c7af37e..0000000 --- a/greenhouse/templates/config.html +++ /dev/null @@ -1,115 +0,0 @@ - - - - Gewächshaus - - - - - - -

Gewächshaus

-

Configuration

-
-

Zeitgesteuerte Heizung

-
- -
- -
-

Zeitgesteuerte Bewässerung

-
- -
- -
-
- -
- - - diff --git a/greenhouse/templates/index.html b/greenhouse/templates/index.html deleted file mode 100644 index 8ea49e2..0000000 --- a/greenhouse/templates/index.html +++ /dev/null @@ -1,42 +0,0 @@ - - - - Gewächshaus - - - - - - -

Gewächshaus

- - - - - - -

Temperatur

- - - - - - - - - - - -
Heizung - -
Bewässerung - -
- - -