Further reorg of code and bug fixes

This commit is contained in:
Paulus Schoutsen committed 2013-09-18 00:07:39 -07:00
1 parent b20bd6c9c0
commit a96919f902
6 files changed
+95 -87

No files matched your search

+16 -3
View File
@@ -3,8 +3,14 @@ from datetime import datetime, timedelta
import ephem
from app.EventBus import Event
from app.observer.Timer import track_time_change
PRE_SUN_SET_WARNING_TIME = 20 # minutes
EVENT_PRE_SUN_SET_WARNING = "sun_set_soon"
STATE_CATEGORY_TEMPLATE_SOLAR = "solar.{}"
STATE_CATEGORY_SUN = STATE_CATEGORY_TEMPLATE_SOLAR.format("sun")
@@ -26,14 +32,14 @@ class WeatherWatcher:
def update_sun_state(self, now=datetime.now()):
self.update_solar_state(ephem.Sun(), STATE_CATEGORY_SUN, self.update_sun_state)
def update_solar_state(self, solar_object, state_category, update_callback):
def update_solar_state(self, solar_body, state_category, update_callback):
# We don't cache these objects because we use them so rarely
observer = ephem.Observer()
observer.lat = self.config.get('common','latitude')
observer.long = self.config.get('common','longitude')
next_rising = ephem.localtime(observer.next_rising(solar_object))
next_setting = ephem.localtime(observer.next_setting(solar_object))
next_rising = ephem.localtime(observer.next_rising(solar_body))
next_setting = ephem.localtime(observer.next_setting(solar_body))
if next_rising > next_setting:
new_state = SOLAR_STATE_ABOVE_HORIZON
@@ -49,3 +55,10 @@ class WeatherWatcher:
# +10 seconds to be sure that the change has occured
track_time_change(self.eventbus, update_callback, datetime=next_change + timedelta(seconds=10))
# If the sun is visible, schedule to fire an event X minutes before sun set
if solar_body.name == 'Sun' and new_state == SOLAR_STATE_ABOVE_HORIZON:
track_time_change(self.eventbus, lambda time: self.eventbus.fire(Event(EVENT_PRE_SUN_SET_WARNING, {'sun_setting':next_change})),
datetime=next_change - timedelta(minutes=PRE_SUN_SET_WARNING_TIME))