From a5205cfbcb80c2e1090d12fc4872b22c6c487223 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 22 Aug 2018 09:02:35 +0200 Subject: [PATCH] gate: Improvement Signed-off-by: Thomas Klaehn --- gate_guard/gate.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/gate_guard/gate.py b/gate_guard/gate.py index b35bc21..9e0b509 100644 --- a/gate_guard/gate.py +++ b/gate_guard/gate.py @@ -25,7 +25,7 @@ STATE_CLOSING = "closing" LIGHT_READ_DELAY_S = 30 LIGHT_CONSECUTIVE_READS = 10 -LIGHT_LX_THRESHOLD = {"open":1, "close":0} +LIGHT_LX_THRESHOLD = 0 MQTT_HOST = "mqtt.blackfinn.de" MQTT_PORT = 8883 @@ -42,7 +42,7 @@ POWER_CONSECUTIVE_READS = 10 SLOPE_COUNT = 10 SLOPE_CNT_MIN = 2 -MAX_POWER = 400.0 +MAX_POWER = 500.0 class Gate(object): def __init__(self): @@ -106,7 +106,7 @@ class Gate(object): except (ValueError, TypeError, socket.error, ssl.CertificateError): logging.info('unable to publish to mqtt') pwr = self.__power_sensor.power_mw() - logging.info('pwr: ' + str(pwr) + ' mW') + logging.debug('pwr: ' + str(pwr) + ' mW') if pwr > MAX_POWER: next_state = STATE_INIT_2 self.__update_state(next_state) @@ -117,7 +117,7 @@ class Gate(object): self.__engine.up() self.__down_run_time = time.time() pwr = self.__power_sensor.power_mw() - logging.info('pwr: ' + str(pwr) + ' mW') + logging.debug('pwr: ' + str(pwr) + ' mW') if pwr > MAX_POWER: self.__down_run_time = (time.time() - self.__down_run_time) / 2 @@ -131,16 +131,17 @@ class Gate(object): def __check_to_open(self, light_avg): ret = False - if (light_avg != None) and (light_avg > LIGHT_LX_THRESHOLD["open"]): - ret = True current_date = datetime.datetime.now() - if (current_date.hour <= 12) and (current_date.hour >= 8): - ret = True + try: + if (current_date.hour >= 8) and (light_avg > LIGHT_LX_THRESHOLD): + ret = True + except Exception as e: + logging.error("{}".format(e)) return ret def __check_to_close(self, light_avg): ret = False - if (light_avg != None) and (light_avg <= LIGHT_LX_THRESHOLD["close"]): + if (light_avg != None) and (light_avg <= LIGHT_LX_THRESHOLD): current_date = datetime.datetime.now() if (current_date.hour >= 16) and (current_date.minute >= 0): ret = True @@ -167,8 +168,6 @@ class Gate(object): def __closed_handler(self, light_avg): next_state = self.__next_state if self.__is_transition(): - self.__engine.up() - time.sleep(0.5) self.__engine.stop() msg = str(time.time()) + " Closed" try: @@ -200,15 +199,11 @@ class Gate(object): time.sleep(1) pwr = self.__power_sensor.power_mw() - logging.info('pwr - abs: ' + str(pwr) + ' mW\tavg: ' + str(self.__power_data.average()) + ' mW') + logging.debug('pwr - abs: ' + str(pwr) + ' mW\tavg: ' + str(self.__power_data.average()) + ' mW') if pwr > MAX_POWER: deviation = abs(time.time() - self.__gate_run_time - self.__down_run_time) logging.info('runtime deviation: ' + str(deviation)) - if deviation > (self.__down_run_time / 10): - logging.info('Deviation too big. Re-initializing...') - next_state = STATE_INIT_1 - else: - next_state = STATE_OPENED + next_state = STATE_OPENED self.__update_state(next_state) def __closing_handler(self, light_avg): @@ -229,7 +224,7 @@ class Gate(object): time.sleep(1) pwr = self.__power_sensor.power_mw() - logging.info('pwr: ' + str(pwr) + ' mW') + logging.debug('pwr: ' + str(pwr) + ' mW') opening_time = time.time() - self.__gate_run_time if opening_time > self.__down_run_time: logging.info("actual running time bigger than calculated (" + str(opening_time) + " vs. " + str(self.__down_run_time) + ").")