From ae9633462e96416c3971e60c57872264883fb8d4 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 29 Mar 2022 14:05:07 +0200 Subject: [PATCH] hochbeet: Fix missing switch water on/off --- hochbeet/app.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hochbeet/app.py b/hochbeet/app.py index 9a51c9f..8dc8531 100644 --- a/hochbeet/app.py +++ b/hochbeet/app.py @@ -26,6 +26,8 @@ log = logging.getLogger('hochbeet') class GreenControl(Thread): + """Green Control""" + def __init__(self): super(GreenControl, self).__init__() self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json") @@ -87,33 +89,39 @@ class GreenControl(Thread): sleep(1) def state(self): + """Return water state""" return self.__water_state def get_auto_state(self): + """Return auto state""" state = False if self.__config['water'][0]['autostate']: state = self.__config['water'][0]['autostate'] return state def set_auto_state(self, state): + """Set the auto state""" self.__config['water'][0]['autostate'] = state with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) self.__trigger_read_config = True def get_times(self): + """Return the times for auto state""" times = None if self.__config['water'][0]['times']: times = self.__config['water'][0]['times'] return times def set_times(self, times): + """Set the times for auto state""" self.__config['water'][0]['times'] = times with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) self.__trigger_read_config = True def switch_water(self, state): + """Switch the water on/off""" if state: GPIO.output(self.__pin, 1) else: @@ -128,10 +136,12 @@ app = Flask(__name__) @app.route('/') def index(): + """Handle index.html""" return render_template('index.html') @app.route('/sample', methods=['GET']) def get_sample(): + """Handle GET request for /sample""" global green_ctrl sample = {} @@ -141,17 +151,21 @@ def get_sample(): @app.route('/sample', methods=['PATCH']) def patch_sample(): + """Handle PATCH request for sample""" global green_ctrl record = json.loads(request.data) if "waterstate" in record: if record["waterstate"]: log.info("Switch water on by button: %s", datetime.datetime.now()) + green_ctrl.switch_water(True) else: log.info("Switch water off by button: %s", datetime.datetime.now()) + green_ctrl.switch_water(False) return make_response("", 204) @app.route('/config', methods=['GET']) def get_config(): + """Handle GER request for /config""" global green_ctrl config = {} config["autostate"] = green_ctrl.get_auto_state() @@ -160,6 +174,7 @@ def get_config(): @app.route('/config', methods=['PATCH']) def patch_config(): + """Handle PATCH request for sample""" global green_ctrl record = json.loads(request.data) if "id" in record and record['id'] == '1':