forked from info-beamer/package-conference-room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·96 lines (74 loc) · 3.3 KB
/
service
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
#!/usr/bin/python
from hosted import CONFIG
from hosted import NODE
from waffel.waffel import Waffel
from calendar import timegm
from datetime import datetime, timedelta
import pytz
import json
import os
import sys
import time
CONFIG.restart_on_update()
def log(msg):
print >>sys.stderr, "[service] %s" % msg
def __get_now_in_config_tz(now):
now = now.replace(tzinfo=pytz.utc)
return now.astimezone(pytz.timezone(CONFIG['timezone']))
def current_time():
# now = datetime.utcnow().replace(tzinfo=pytz.utc) # TODO: re-enable realtime
now = datetime(2024, 2, 28, 15, 0).replace(tzinfo=pytz.utc)
timestamp = timegm(now.timetuple()) + now.microsecond / 1000000.
return now, timestamp
def send_clock(now, ts):
now = __get_now_in_config_tz(now)
NODE.send('/clock/set:%f,%d,%d,%d' % (ts, now.hour, now.minute, now.second))
config_timestamps = {}
for sponsor in CONFIG['sponsors']:
schedule = sponsor['schedule']
if not isinstance(schedule, basestring):
startdate_string = schedule['start']
startdate = pytz.timezone(CONFIG['timezone']).localize(datetime.strptime(startdate_string, '%Y-%m-%d'))
config_timestamps[startdate_string] = timegm(startdate.astimezone(pytz.utc).timetuple())
for datestring, timestamp in config_timestamps.items():
NODE.send('/clock/timestamps/%s:%d' % (datestring, timestamp))
def send_animation_timer_reset():
NODE.send('/animation-time/reset')
def main():
now, ts = current_time()
last_animation_time_reset = now
waffel = Waffel(CONFIG['eis_api_url'], CONFIG['year'], CONFIG['tracks'], CONFIG['locations'],
max_delta=timedelta(hours=500), timezone=pytz.timezone(CONFIG['timezone'])) # TODO: set timedelta back to 15h
while 1:
now, ts = current_time()
if now.year < 2000:
# Guard against NTP time not beeing synchronized yet.
# On the pi the year will be 1970 in that case.
log("too soon! ... waiting a little for NTP to sync the clock (current time: %s / %s)" % (now, ts))
time.sleep(1)
continue
schedule = waffel.get_events(now)
if schedule:
with file("schedule.json.new", "wb") as f:
f.write(json.dumps(schedule, indent=2).encode('utf8'))
os.rename("schedule.json.new", "schedule.json")
log("successfully updated schedule, new schedule contains %d locations and %d events" % (
len(schedule), sum([len(schedule[x]) for x in schedule])))
else:
log("EIS didn't return any events ... not updating schedule")
# 6*5 times 10s (aka 5 minutes of only updating the time but not
# re-downloading the schedule)
for i in xrange(6 * 5):
now, ts = current_time()
log("current time: %s / %s" % (now, ts))
send_clock(now, ts)
time.sleep(10)
# reset animation time if it's around 6 o'clock and the last reset
# was at least 22 hours (aka 80000 seconds) ago
since = now - last_animation_time_reset
if __get_now_in_config_tz(now).hour == 6\
and since.total_seconds() > 80000:
log("trigger reset of animation time")
send_animation_timer_reset()
if __name__ == "__main__":
main()