diff --git a/gate_guard/gate.py b/gate_guard/gate.py index 22e6166..74fab8b 100644 --- a/gate_guard/gate.py +++ b/gate_guard/gate.py @@ -5,6 +5,7 @@ Created on Dec 19, 2016 ''' import datetime import logging +import pytz import socket import ssl import time @@ -14,6 +15,8 @@ import gate_guard.light_sensor import gate_guard.engine import gate_guard.power_sensor +from astral import Astral + STATE_INIT_1 = "init_1" STATE_INIT_2 = "init_2" STATE_INIT_3 = "init_3" @@ -67,6 +70,25 @@ def check_to_close(light_avg): ret = True return ret +class FakeLight(object): + '''Simulate light sensor time based in order to eliminate defect light sensor.''' + def __init__(self, bus, addr): + self.astral = Astral() + self.astral.solar_depression = 'civil' + self.city = self.astral['Berlin'] + self.opening_hour = 8 + + def read(self): + fake_light = 0 + now = datetime.datetime.now(pytz.timezone(self.city.timezone)) + + sun = self.city.sun(now, local=True) + sunset = sun['sunset'] + + if now.hour >= self.opening_hour and now < sunset: + fake_light = 1 + return fake_light + class Gate(object): '''Main class of the chickenhouse gates.''' @@ -83,8 +105,9 @@ class Gate(object): STATE_CLOSING_2:self.__closing_2_handler} self.__next_state = STATE_INIT_1 self.__last_state = STATE_OPENED - self.__light_sensor = gate_guard.light_sensor.LightSensor( - LIGHT_SENSOR_I2C_BUS, LIGHT_SENSOR_I2C_ADDRESS) +# self.__light_sensor = gate_guard.light_sensor.LightSensor( +# LIGHT_SENSOR_I2C_BUS, LIGHT_SENSOR_I2C_ADDRESS) + self.__light_sensor = FakeLight(LIGHT_SENSOR_I2C_BUS, LIGHT_SENSOR_I2C_ADDRESS) self.__light_data = gate_guard.data_buffer.DataBuffer( LIGHT_CONSECUTIVE_READS) self.__engine_1 = gate_guard.engine.Engine(gpio_1=ENGINE_1_PIN_1, gpio_2=ENGINE_1_PIN_2)