-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrafficscreen.py
137 lines (105 loc) · 3.65 KB
/
trafficscreen.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
import json
import os
import SimpleHTTPServer
import SocketServer
import threading
import time
import logging
import sys
from datetime import datetime
from datetime import time as time2
from location import Location
import googlemaps
import yaml
def savelocations():
with open('data.json', 'w') as output:
output.write(json.dumps([obj.__dict__ for obj in LOCATIONS]))
def loadlocations():
with open('locations.yml', 'r') as output:
locs = yaml.safe_load(output)['locations']
logging.info('%d locations loaded', len(locs))
return [
Location(loc['name'].encode('utf-8').strip(),
loc['address'].encode('utf-8').strip()) for loc in locs
]
def savesettings():
with open('www/settings.json', 'w') as inp:
inp.write(json.dumps(SETTINGS))
logging.info('settings saved')
def loadsettings():
with open("settings.yml", 'r') as stream:
logging.info('settings loaded')
return yaml.safe_load(stream)
def startwebserver():
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", 8901), handler)
logging.info('serving at port 8901')
httpd.serve_forever()
def isonduty():
now = datetime.now().time()
if ON_TIME <= OFF_TIME:
return ON_TIME <= now < OFF_TIME
else:
return ON_TIME <= now or now < OFF_TIME
def run():
while True:
if not isonduty():
logging.debug("out of working hours")
else:
try:
pagesize = 25
origins = [SETTINGS['origin']['address']]
alldestinations = [dest.address for dest in LOCATIONS]
pageddestinations = [
alldestinations[i:i + pagesize]
for i in range(0, len(alldestinations), pagesize)
]
for page, destinations in enumerate(pageddestinations):
ret = GMAPS.distance_matrix(
origins,
destinations,
mode=SETTINGS['api_params']['mode'],
language=SETTINGS['api_params']['language'],
units=SETTINGS['api_params']['units'],
departure_time=datetime.now(),
traffic_model=SETTINGS['api_params']['traffic_model'])
for i, element in enumerate(ret['rows'][0]['elements']):
locationitem = LOCATIONS[(pagesize * page) + i]
if element['status'] == 'OK':
locationitem.update(element)
else:
logging.info('%s couldn' 't updated', locationitem.name)
locationitem.reset()
savelocations()
logging.info('data.json file updated %d/%d', page + 1,
len(pageddestinations))
except Exception as e:
logging.error('Error when getting data: %s', str(e.message))
time.sleep(60)
logging.info('sleeping for %d min', UPDATE_INTERVAL)
time.sleep(UPDATE_INTERVAL * 60)
def startupdate():
thread = threading.Thread(target=run)
thread.daemon = True
thread.start()
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
LOCATIONS = loadlocations()
SETTINGS = loadsettings()
GMAPS = googlemaps.Client(key=SETTINGS['api_params']['api_key'])
SETTINGS['api_params']['api_key'] = ""
savesettings()
ON_TIME = time2(
int(SETTINGS['schedule']['onhour']), int(SETTINGS['schedule']['onmin']))
OFF_TIME = time2(
int(SETTINGS['schedule']['offhour']), int(SETTINGS['schedule']['offmin']))
UPDATE_INTERVAL = int(SETTINGS['schedule']['update_interval'])
logging.info('working hours is between %s -> %s', ON_TIME, OFF_TIME)
logging.info('%d locations are loaded', len(LOCATIONS))
logging.info('starting server and updater')
os.chdir('www')
startupdate()
startwebserver()