More bugfixes. Getting there..

This commit is contained in:
Paulus Schoutsen 2013-09-19 22:32:08 -07:00
parent 0ff00ae5e0
commit cfc6708283
4 changed files with 14 additions and 11 deletions

View File

@ -10,12 +10,14 @@ class EventBus:
def __init__(self):
self.listeners = defaultdict(list)
self.lock = RLock()
self.logger =logging.getLogger("EventBus")
self.logger =logging.getLogger(__name__)
def fire(self, event):
assert isinstance(event, Event), "event needs to be an instance of Event"
# We dont want the eventbus to be blocking, run in a thread
# We dont want the eventbus to be blocking,
# We dont want the eventbus to crash when one of its listeners throws an Exception
# So run in a thread
def run():
self.lock.acquire()

View File

@ -6,6 +6,7 @@ from phue import Bridge
from app.observer.WeatherWatcher import STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON
from app.StateMachine import track_state_change
from app.DeviceTracker import STATE_CATEGORY_ALL_DEVICES, STATE_DEVICE_HOME, STATE_DEVICE_NOT_HOME
from app.observer.Timer import track_time_change
LIGHTS_TURNING_ON_BEFORE_SUN_SET_PERIOD = timedelta(minutes=20)
@ -30,7 +31,7 @@ class HueTrigger:
track_state_change(eventbus, STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON, self.handle_sun_rising)
# If the sun is already above horizon schedule the time-based pre-sun set event
if statemachine.get_state(STATE_CATEGORY_SUN, SUN_STATE_ABOVE_HORIZON):
if True or statemachine.get_state(STATE_CATEGORY_SUN) == SUN_STATE_ABOVE_HORIZON:
self.handle_sun_rising()
def get_lights_status(self):
@ -61,7 +62,7 @@ class HueTrigger:
def handle_sun_rising(self, event=None):
# Schedule an event X minutes prior to sun setting
track_time_change(self.eventBus, self.handle_sun_setting, datetime=self.weather.next_sun_setting()-LIGHTS_TURNING_ON_BEFORE_SUN_SET_PERIOD)
track_time_change(self.eventbus, self.handle_sun_setting, datetime=self.weather.next_sun_setting()-LIGHTS_TURNING_ON_BEFORE_SUN_SET_PERIOD)
# Gets called when darkness starts falling in, slowly turn on the lights

View File

@ -15,7 +15,7 @@ class TomatoDeviceScanner:
def __init__(self, config):
self.config = config
self.logger = logging.getLogger("TomatoDeviceScanner")
self.logger = logging.getLogger(__name__)
self.lock = Lock()
self.date_updated = None
self.last_results = None
@ -64,7 +64,7 @@ class TomatoDeviceScanner:
self.last_results = [mac for iface, mac, rssi, tx, rx, quality, unknown_num in wldev]
except:
self.logger.error("Scanning failed")
self.logger.exception("Scanning failed")
self.lock.release()

View File

@ -14,7 +14,7 @@ SUN_STATE_BELOW_HORIZON = "below_horizon"
class WeatherWatcher:
def __init__(self, config, eventbus, statemachine):
self.logger = logging.getLogger("WeatherWatcher")
self.logger = logging.getLogger(__name__)
self.config = config
self.eventbus = eventbus
self.statemachine = statemachine
@ -25,7 +25,7 @@ class WeatherWatcher:
self.sun = ephem.Sun()
statemachine.add_category(STATE_CATEGORY_SUN, SOLAR_STATE_BELOW_HORIZON)
statemachine.add_category(STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON)
self.update_sun_state()
@ -35,7 +35,7 @@ class WeatherWatcher:
def next_sun_setting(self):
return ephem.localtime(self.observer.next_setting(self.sun))
def update_sun_state(self, update_callback):
def update_sun_state(self, now=None):
next_rising = ephem.localtime(self.observer.next_rising(self.sun))
next_setting = ephem.localtime(self.observer.next_setting(self.sun))
@ -47,11 +47,11 @@ class WeatherWatcher:
new_state = SUN_STATE_BELOW_HORIZON
next_change = next_rising
self.logger.info("Updating solar state for {} to {}. Next change: {}".format(STATE_CATEGORY_SUN, new_state, next_change))
self.logger.info("Updating sun state to {}. Next change: {}".format(new_state, next_change))
self.statemachine.set_state(STATE_CATEGORY_SUN, new_state)
# +10 seconds to be sure that the change has occured
track_time_change(self.eventbus, update_callback, datetime=next_change + timedelta(seconds=10))
track_time_change(self.eventbus, self.update_sun_state, datetime=next_change + timedelta(seconds=10))