-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist_bus.py
54 lines (46 loc) · 1.51 KB
/
dist_bus.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
# dist_bus.py
from socket import socket, AF_INET, SOCK_DGRAM, timeout
import sys
import tasklib
class BusMonitorTask(tasklib.Task):
def __init__(self,target):
super(BusMonitorTask,self).__init__(name="busmonitor")
self.target = target
# Method that runs in its own thread, updating the buses dictionary
def run(self):
sock = socket(AF_INET, SOCK_DGRAM)
sock.settimeout(5)
try:
sock.sendto(b"",("localhost",31337))
while self.runnable:
try:
msg, addr = sock.recvfrom(8192)
except timeout:
continue
msg = msg.decode('ascii')
fields = msg.split(",")
# Make a dictionary from the update data
bus = {
'timestamp' : float(fields[1]),
'id': fields[2],
'run' : fields[3],
'route' : fields[4],
'lat' : float(fields[5]),
'lon' : float(fields[6])
}
tasklib.send(self.target, ('bus',bus))
finally:
sock.close()
if __name__ == '__main__':
import taskdist
import time
import logging
logging.basicConfig(level=logging.INFO)
# Fire up the resolver
taskdist.start_resolver(authkey=b"peekaboo")
# Launch the bus monitor
busmon_task = BusMonitorTask(target="busdb")
busmon_task.start()
# Spin
while True:
time.sleep(1)