/
haAstral.py
115 lines (95 loc) · 4.27 KB
/
haAstral.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import ConfigParser
import os
import time
from astral import Astral
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet import task
from houseagent import config_to_location
from houseagent.plugins import pluginapi
from houseagent.plugins.pluginapi import Logging
# Fix to support both twisted.scheduling and txscheduling (new version)
try:
from txscheduling.cron import CronSchedule
from txscheduling.task import ScheduledCall
except ImportError:
from twisted.scheduling.cron import CronSchedule
from twisted.scheduling.task import ScheduledCall
class AstralWrapper(object):
def __init__(self):
'''
Load initial Astral configuration from Astral.conf
'''
config_file = config_to_location('Astral.conf')
self.config = ConfigParser.RawConfigParser()
self.config.read(os.path.join(config_file))
# Get broker information (ZeroMQ)
self.coordinator_host = self.config.get("coordinator", "host")
self.coordinator_port = self.config.getint("coordinator", "port")
self.loglevel = self.config.get('general', 'loglevel')
self.log = Logging("Astral", console=True)
self.log.set_level(self.loglevel)
self.id = self.config.get('general', 'id')
callbacks = {}
self.pluginapi = pluginapi.PluginAPI(self.id, 'Astral',
broker_host=self.coordinator_host,
broker_port=self.coordinator_port,
**callbacks)
self.pluginapi.ready()
c = CronSchedule("* * * * *")
s = ScheduledCall(f=self.fire_minute)
s.start(c)
reactor.run()
return True
def fire_minute(self):
city = Astral()[self.config.get("astral", "city")]
sun = city.sun()
self.log.debug(sun)
delta = int(time.time()) - int(time.mktime(sun["sunrise"].timetuple())) + 30
if delta > 0:
self.log.info("Sunrise was {0} minutes ago".format(delta // 60))
else:
self.log.info("Sunrise in {0} minutes".format(-(delta // 60)))
self.pluginapi.value_update("1", {"Sunrise delta": delta // 60})
delta = int(time.time()) - int(time.mktime(sun["sunset"].timetuple())) + 30
if delta > 0:
self.log.info("Sunset was {0} minutes ago".format(delta // 60))
else:
self.log.info("Sunset in {0} minutes".format(-(delta // 60)))
self.pluginapi.value_update("1", {"Sunset delta": delta // 60})
delta = int(time.time()) - int(time.mktime(sun["dawn"].timetuple())) + 30
if delta > 0:
self.log.info("Dawn was {0} minutes ago".format(delta // 60))
else:
self.log.info("Dawn in {0} minutes".format(-(delta // 60)))
self.pluginapi.value_update("1", {"Dawn delta": delta // 60})
delta = int(time.time()) - int(time.mktime(sun["dusk"].timetuple())) + 30
if delta > 0:
self.log.info("Dusk was {0} minutes ago".format(delta // 60))
else:
self.log.info("Dusk in {0} minutes".format(-(delta // 60)))
self.pluginapi.value_update("1", {"Dusk delta": delta // 60})
delta = int(time.time()) - int(time.mktime(sun["noon"].timetuple())) + 30
if delta > 0:
self.log.info("Solar noon was {0} minutes ago".format(delta // 60))
else:
self.log.info("Solar noon in {0} minutes".format(-(delta // 60)))
self.pluginapi.value_update("1", {"Solar noon delta": delta // 60})
if os.name == 'nt':
class AstralService(pluginapi.WindowsService):
'''
This class provides a Windows Service interface for the Astral plugin.
'''
_svc_name_ = "haAstral"
_svc_display_name_ = "HouseAgent - Astral Service"
def start(self):
'''
Start the Astral interface.
'''
AstralWrapper()
if __name__ == '__main__':
if os.name == 'nt':
# We want to start as a Windows service on Windows.
pluginapi.handle_windowsservice(AstralService)
else:
AstralWrapper()