This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveVehicles.py
91 lines (74 loc) · 2.43 KB
/
MoveVehicles.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
import xml.etree.ElementTree
import FMMFramework as fmm
from Projection import minlon_, minlat_, maxlon_, maxlat_
import SimulationFramework as sim
import random
import threading
import queue
import time
from config import vehicle_count
from Connection import client_hack
from Utilities import expand_dict_with_address
map = xml.etree.ElementTree.parse('map_hackathon_final.osm').getroot()
nodes = {
node.get('id'): {
'lat': node.get('lat'), 'lon': node.get('lon'),
'latitude': node.get('lat'), 'longitude': node.get('lon')
}
for node in map.findall('node')
}
ways = [
[
nodes[node.get('ref')]
for node in way.findall('nd')
]
for way in map.findall('way')
]
plates = list(sim.all_license_plates())
position_queues = {
plate: queue.LifoQueue() for plate in sim.all_license_plates()
}
max_simulated_paths = 10
class QueueWorker(threading.Thread):
def __init__(self, plate):
threading.Thread.__init__(self)
self.plate = plate
def run(self):
if not position_queues[self.plate].empty():
lat_lon = position_queues[self.plate].get()
client_hack.get_database('fmm').get_collection(
'vehicle'
).find_one_and_update(
filter={'plate': self.plate},
update={
'$set': lat_lon
}
)
print('moved {} to {}'.format(self.plate, lat_lon))
position_queues[self.plate] = queue.LifoQueue()
class PositionProducer(threading.Thread):
def __init__(self, plate):
threading.Thread.__init__(self)
self.plate = plate
def run(self):
for _ in range(max_simulated_paths):
random_way = random.choice(ways)
for lat_lon in random_way:
if minlat_ < float(lat_lon['lat']) < maxlat_ and minlon_ < float(lat_lon['lon']) < maxlon_:
position_queues[self.plate].put(lat_lon)
QueueWorker(self.plate).start()
time.sleep(1.0)
else:
break
def main():
# TODO: choose a way containing the node the car was located last
vehicle_threads = {
plate: PositionProducer(plate)
for plate in sim.all_license_plates()
}
for thread in vehicle_threads.values():
thread.start()
for thread in vehicle_threads.values():
thread.join()
if __name__ == '__main__':
main()