1
|
|
|
from st2reactor.sensor.base import PollingSensor |
2
|
|
|
import st2common.util.date as date |
3
|
|
|
|
4
|
|
|
# from datetime import timedelta |
5
|
|
|
|
6
|
|
|
# from astral import Astral |
7
|
|
|
from astral import Location |
8
|
|
|
|
9
|
|
|
__all__ = [ |
10
|
|
|
'AstralSunSensor' |
11
|
|
|
] |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class AstralSunSensor(PollingSensor): |
15
|
|
|
def __init__(self, sensor_service, config=None, poll_interval=None): |
16
|
|
|
super(AstralSunSensor, self).__init__(sensor_service=sensor_service, |
17
|
|
|
config=config, |
18
|
|
|
poll_interval=poll_interval) |
19
|
|
|
self._logger = self._sensor_service.get_logger(__name__) |
20
|
|
|
|
21
|
|
|
def setup(self): |
22
|
|
|
self._latitude = self._config['latitude'] |
23
|
|
|
self._longitude = self._config['longitude'] |
24
|
|
|
self._update_sun_info() |
25
|
|
|
self._update_counter = 0 |
26
|
|
|
|
27
|
|
|
def poll(self): |
28
|
|
|
if self._update_counter > 60: |
29
|
|
|
self._update_sun_info() |
30
|
|
|
self._update_counter = 0 |
31
|
|
|
|
32
|
|
|
checks = ['dawn', 'sunrise', 'sunset', 'dusk'] |
33
|
|
|
|
34
|
|
|
currenttime = date.get_datetime_utc_now() |
35
|
|
|
|
36
|
|
|
self._logger.debug("Checking current time %s for sun information" % |
37
|
|
|
str(currenttime)) |
38
|
|
|
|
39
|
|
|
for key in checks: |
40
|
|
|
if self.is_within_minute(self.sun[key], currenttime): |
41
|
|
|
trigger = 'astral.'+key |
42
|
|
|
self.sensor_service.dispatch(trigger=trigger, payload={}) |
43
|
|
|
|
44
|
|
|
self._update_counter += 1 |
45
|
|
|
|
46
|
|
|
def cleanup(self): |
47
|
|
|
pass |
48
|
|
|
|
49
|
|
|
def add_trigger(self, trigger): |
50
|
|
|
pass |
51
|
|
|
|
52
|
|
|
def update_trigger(self, trigger): |
53
|
|
|
pass |
54
|
|
|
|
55
|
|
|
def remove_trigger(self, trigger): |
56
|
|
|
pass |
57
|
|
|
|
58
|
|
|
def _update_sun_info(self): |
59
|
|
|
location = Location(('name', 'region', |
60
|
|
|
float(self._latitude), float(self._longitude), 'GMT+0', 0)) |
61
|
|
|
self.sun = location.sun() |
62
|
|
|
|
63
|
|
|
def is_within_minute(self, time1, time2): |
64
|
|
|
timediff = time1 - time2 |
65
|
|
|
diff = abs(timediff.total_seconds() // 60) |
66
|
|
|
if diff < 1: |
67
|
|
|
return True |
68
|
|
|
return False |
69
|
|
|
|