gate: Improvement

Signed-off-by: Thomas Klaehn <thomas.klaehn@u-blox.com>
This commit is contained in:
Thomas Klaehn 2018-08-22 09:02:35 +02:00
parent 16931c50ba
commit a5205cfbcb

View File

@ -25,7 +25,7 @@ STATE_CLOSING = "closing"
LIGHT_READ_DELAY_S = 30 LIGHT_READ_DELAY_S = 30
LIGHT_CONSECUTIVE_READS = 10 LIGHT_CONSECUTIVE_READS = 10
LIGHT_LX_THRESHOLD = {"open":1, "close":0} LIGHT_LX_THRESHOLD = 0
MQTT_HOST = "mqtt.blackfinn.de" MQTT_HOST = "mqtt.blackfinn.de"
MQTT_PORT = 8883 MQTT_PORT = 8883
@ -42,7 +42,7 @@ POWER_CONSECUTIVE_READS = 10
SLOPE_COUNT = 10 SLOPE_COUNT = 10
SLOPE_CNT_MIN = 2 SLOPE_CNT_MIN = 2
MAX_POWER = 400.0 MAX_POWER = 500.0
class Gate(object): class Gate(object):
def __init__(self): def __init__(self):
@ -106,7 +106,7 @@ class Gate(object):
except (ValueError, TypeError, socket.error, ssl.CertificateError): except (ValueError, TypeError, socket.error, ssl.CertificateError):
logging.info('unable to publish to mqtt') logging.info('unable to publish to mqtt')
pwr = self.__power_sensor.power_mw() pwr = self.__power_sensor.power_mw()
logging.info('pwr: ' + str(pwr) + ' mW') logging.debug('pwr: ' + str(pwr) + ' mW')
if pwr > MAX_POWER: if pwr > MAX_POWER:
next_state = STATE_INIT_2 next_state = STATE_INIT_2
self.__update_state(next_state) self.__update_state(next_state)
@ -117,7 +117,7 @@ class Gate(object):
self.__engine.up() self.__engine.up()
self.__down_run_time = time.time() self.__down_run_time = time.time()
pwr = self.__power_sensor.power_mw() pwr = self.__power_sensor.power_mw()
logging.info('pwr: ' + str(pwr) + ' mW') logging.debug('pwr: ' + str(pwr) + ' mW')
if pwr > MAX_POWER: if pwr > MAX_POWER:
self.__down_run_time = (time.time() - self.__down_run_time) / 2 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): def __check_to_open(self, light_avg):
ret = False ret = False
if (light_avg != None) and (light_avg > LIGHT_LX_THRESHOLD["open"]):
ret = True
current_date = datetime.datetime.now() current_date = datetime.datetime.now()
if (current_date.hour <= 12) and (current_date.hour >= 8): try:
if (current_date.hour >= 8) and (light_avg > LIGHT_LX_THRESHOLD):
ret = True ret = True
except Exception as e:
logging.error("{}".format(e))
return ret return ret
def __check_to_close(self, light_avg): def __check_to_close(self, light_avg):
ret = False 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() current_date = datetime.datetime.now()
if (current_date.hour >= 16) and (current_date.minute >= 0): if (current_date.hour >= 16) and (current_date.minute >= 0):
ret = True ret = True
@ -167,8 +168,6 @@ class Gate(object):
def __closed_handler(self, light_avg): def __closed_handler(self, light_avg):
next_state = self.__next_state next_state = self.__next_state
if self.__is_transition(): if self.__is_transition():
self.__engine.up()
time.sleep(0.5)
self.__engine.stop() self.__engine.stop()
msg = str(time.time()) + " Closed" msg = str(time.time()) + " Closed"
try: try:
@ -200,14 +199,10 @@ class Gate(object):
time.sleep(1) time.sleep(1)
pwr = self.__power_sensor.power_mw() 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: if pwr > MAX_POWER:
deviation = abs(time.time() - self.__gate_run_time - self.__down_run_time) deviation = abs(time.time() - self.__gate_run_time - self.__down_run_time)
logging.info('runtime deviation: ' + str(deviation)) 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) self.__update_state(next_state)
@ -229,7 +224,7 @@ class Gate(object):
time.sleep(1) time.sleep(1)
pwr = self.__power_sensor.power_mw() 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 opening_time = time.time() - self.__gate_run_time
if opening_time > self.__down_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) + ").") logging.info("actual running time bigger than calculated (" + str(opening_time) + " vs. " + str(self.__down_run_time) + ").")