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):
"""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':