hochbeet: Fix missing switch water on/off

This commit is contained in:
Thomas Klaehn 2022-03-29 14:05:07 +02:00
parent c157fffcc3
commit ae9633462e

View File

@ -26,6 +26,8 @@ log = logging.getLogger('hochbeet')
class GreenControl(Thread): class GreenControl(Thread):
"""Green Control"""
def __init__(self): def __init__(self):
super(GreenControl, self).__init__() super(GreenControl, self).__init__()
self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json") self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json")
@ -87,33 +89,39 @@ class GreenControl(Thread):
sleep(1) sleep(1)
def state(self): def state(self):
"""Return water state"""
return self.__water_state return self.__water_state
def get_auto_state(self): def get_auto_state(self):
"""Return auto state"""
state = False state = False
if self.__config['water'][0]['autostate']: if self.__config['water'][0]['autostate']:
state = self.__config['water'][0]['autostate'] state = self.__config['water'][0]['autostate']
return state return state
def set_auto_state(self, state): def set_auto_state(self, state):
"""Set the auto state"""
self.__config['water'][0]['autostate'] = state self.__config['water'][0]['autostate'] = state
with open(self.__config_file, "w") as handle: with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle) json.dump(self.__config, handle)
self.__trigger_read_config = True self.__trigger_read_config = True
def get_times(self): def get_times(self):
"""Return the times for auto state"""
times = None times = None
if self.__config['water'][0]['times']: if self.__config['water'][0]['times']:
times = self.__config['water'][0]['times'] times = self.__config['water'][0]['times']
return times return times
def set_times(self, times): def set_times(self, times):
"""Set the times for auto state"""
self.__config['water'][0]['times'] = times self.__config['water'][0]['times'] = times
with open(self.__config_file, "w") as handle: with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle) json.dump(self.__config, handle)
self.__trigger_read_config = True self.__trigger_read_config = True
def switch_water(self, state): def switch_water(self, state):
"""Switch the water on/off"""
if state: if state:
GPIO.output(self.__pin, 1) GPIO.output(self.__pin, 1)
else: else:
@ -128,10 +136,12 @@ app = Flask(__name__)
@app.route('/') @app.route('/')
def index(): def index():
"""Handle index.html"""
return render_template('index.html') return render_template('index.html')
@app.route('/sample', methods=['GET']) @app.route('/sample', methods=['GET'])
def get_sample(): def get_sample():
"""Handle GET request for /sample"""
global green_ctrl global green_ctrl
sample = {} sample = {}
@ -141,17 +151,21 @@ def get_sample():
@app.route('/sample', methods=['PATCH']) @app.route('/sample', methods=['PATCH'])
def patch_sample(): def patch_sample():
"""Handle PATCH request for sample"""
global green_ctrl global green_ctrl
record = json.loads(request.data) record = json.loads(request.data)
if "waterstate" in record: if "waterstate" in record:
if record["waterstate"]: if record["waterstate"]:
log.info("Switch water on by button: %s", datetime.datetime.now()) log.info("Switch water on by button: %s", datetime.datetime.now())
green_ctrl.switch_water(True)
else: else:
log.info("Switch water off by button: %s", datetime.datetime.now()) log.info("Switch water off by button: %s", datetime.datetime.now())
green_ctrl.switch_water(False)
return make_response("", 204) return make_response("", 204)
@app.route('/config', methods=['GET']) @app.route('/config', methods=['GET'])
def get_config(): def get_config():
"""Handle GER request for /config"""
global green_ctrl global green_ctrl
config = {} config = {}
config["autostate"] = green_ctrl.get_auto_state() config["autostate"] = green_ctrl.get_auto_state()
@ -160,6 +174,7 @@ def get_config():
@app.route('/config', methods=['PATCH']) @app.route('/config', methods=['PATCH'])
def patch_config(): def patch_config():
"""Handle PATCH request for sample"""
global green_ctrl global green_ctrl
record = json.loads(request.data) record = json.loads(request.data)
if "id" in record and record['id'] == '1': if "id" in record and record['id'] == '1':