diff --git a/src/communiate/protocol.py b/src/communiate/protocol.py index 41087f7..f71de5f 100644 --- a/src/communiate/protocol.py +++ b/src/communiate/protocol.py @@ -16,17 +16,17 @@ class mqtt: def connect(self): if not self.__is_connected: self.__client.on_connect = self.__on_connect - result = self.__client.connect(self.__hostname, self.__port, self.__keepalive) - result |= self.__client.loop_start() - if 0 == result: - self.__is_connected = True - return True + self.__client.connect(self.__hostname, self.__port, self.__keepalive) + self.__client.loop_start() + self.__is_connected = True + return True return False def disconnect(self): if self.__is_connected: self.__client.loop_stop() self.__client.disconnect() + self.__is_connected = False return True return False diff --git a/src/engine/engine.py b/src/engine/engine.py index d3ec24a..6cf76fe 100644 --- a/src/engine/engine.py +++ b/src/engine/engine.py @@ -1,4 +1,4 @@ -from gpio import gpio.gpio +from gpio.gpio import gpio class engine: def __init__(self, gpio_1 = 13, gpio_2 = 19): diff --git a/src/gate/gate_handler.py b/src/gate/gate_handler.py new file mode 100644 index 0000000..f72589d --- /dev/null +++ b/src/gate/gate_handler.py @@ -0,0 +1,32 @@ +''' +Created on Dec 19, 2016 + +@author: klaehn +''' + +from engine.engine import engine +from power_sensor.power_sensor import power_sensor +from time import time +import numpy as np + +class gate_handler: + def __init__(self): + self.__engine = engine(13, 19) + self.__power_sensor = power_sensor(1, 0x40) + self.__max_current_up = 315 + self.__max_current_down = 280 + self.__max_time_open = 250 + self.__max_time_close = 250 + self.__timeout_open = 0 + self.__timeout_close = 0 + return + + def open(self): + self.__timeout_open = time() + self.__max_time_open + self.__engine.run_up() + power = np.array([]) + timestamps = np.array([]) + while(time() < self.__max_time_open): + timestamps = np.append(timestamps, time()) + power = np.append(power, self.__power_sensor.power_mw()) + \ No newline at end of file diff --git a/src/gate_guard.py b/src/gate_guard.py index d607151..44d5206 100644 --- a/src/gate_guard.py +++ b/src/gate_guard.py @@ -8,7 +8,7 @@ import time class gate_guard: def __init__(self): - self.__light_read_delay_s = 6 + self.__light_read_delay_s = 60 self.__consequtive_light_reads = 10 self.__light_lx_close = 0 self.__light_lx_open = 0 @@ -24,6 +24,7 @@ class gate_guard: self.__comserver.connect() self.__comserver.transmit(topic, payload) self.__comserver.disconnect() + self.__gate_state.set_state("close") return None def open_gate(self): @@ -32,6 +33,7 @@ class gate_guard: self.__comserver.connect() self.__comserver.transmit(topic, payload) self.__comserver.disconnect() + self.__gate_state.set_state("open") return None def handle_gate_state(self, light_avg=0): @@ -39,22 +41,21 @@ class gate_guard: if light_avg <= self.__light_lx_close: self.close_gate() elif self.__gate_state.get_state() == "close": - if light_avg >= self.__light_lx_open: + if light_avg > self.__light_lx_open: self.open_gate() elif self.__gate_state.get_state() == "unknown": '''TODO: bring gate in a defined position''' def run(self): - if self.__gate_state.get_state() == "unknown": - self.handle_gate_state() + self.__gate_state.set_state("open") try: while True: - light_data.push(light_sensor.read()) - light_avg = light_data.average() - if light_avg: - print "light average: " + str(light_avg) + rd = self.__light_sensor.read() + self.__light_data.push(rd) + light_avg = self.__light_data.average() + if light_avg != None: self.handle_gate_state(light_avg) time.sleep(self.__light_read_delay_s)