-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice
executable file
·52 lines (35 loc) · 1.19 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
#!/usr/bin/python
from hosted import CONFIG
from hosted import NODE
from calendar import timegm
from datetime import datetime
import pytz
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)
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))
def main():
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
log("current time: %s / %s" % (now, ts))
send_clock(now, ts)
time.sleep(10)
if __name__ == "__main__":
main()