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 pathSimulationFramework.py
186 lines (140 loc) · 5.61 KB
/
SimulationFramework.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import FMMFramework as FMM
import random
from Connection import client_hack, accessToken
from Projection import hackathon_projector, minlat_, maxlat_, minlon_, maxlon_
from Utilities import get_address
from config import vehicle_count, fmm_api_url
from typing import Union, Generator
from numbers import Number
app_opening_task_type= (
client_hack.get_database('fmm').get_collection('taskType')
).find_one({'name': 'App Opening'})
charging_station_task_type = (
client_hack.get_database('fmm').get_collection('taskType')
).find_one({'name': 'Charging Station'})
admin_user_id = '55b95aa8010149cd54d6e089'
def all_license_plates() -> Generator:
return ('NH-{}'.format(i) for i in range(vehicle_count))
def random_coordinates_from_hackathon_location():
lat = str(random.uniform(minlat_, maxlat_))
lon = str(random.uniform(minlon_, maxlon_))
address = get_address(lat, lon)
return {
'lat': lat,
'latitude': lat,
'lon': lon,
'longitude': lon,
'full_address': address,
'address': address
}
def get_fuel_level(license_plate: str) -> float:
return float(FMM.get_vehicle_by_license_plate(
client_hack, license_plate
)['fuel_level'])
def set_fuel_level(license_plate: str, fuel_level: Union[str, Number]):
fuel_level = float(fuel_level)
if fuel_level < .0:
fuel_level = .0
elif fuel_level > 100.0:
fuel_level = 100.0
vehicle_id = FMM.get_vehicle_by_license_plate(
client_hack, license_plate
)['_id']
FMM.set_vehicle_fields(
client_hack, vehicle_id, {
'fuel_level': '{:.4}'.format(fuel_level),
'fuel': '{:.4f}'.format(fuel_level)
}
)
def set_battery_voltage(license_plate: str,
battery_voltage: Union[str, Number]):
battery_voltage = float(battery_voltage)
assert .0 <= battery_voltage <= 14.0
vehicle_id = FMM.get_vehicle_by_license_plate(
client_hack, license_plate
)['_id']
FMM.set_vehicle_fields(
client_hack, vehicle_id, {
'battery_voltage': '{:.3f}'.format(battery_voltage),
'batteryVoltage': '{:.3f}'.format(battery_voltage)
}
)
def create_app_opening(license_plate: str):
vehicle_id = FMM.get_vehicle_by_license_plate(client_hack,
license_plate)['_id']
while True:
origin = random_coordinates_from_hackathon_location()
destination = random_coordinates_from_hackathon_location()
distance = hackathon_projector.distance(origin, destination)
if distance >= 300.0:
break
location_id = FMM.get_location_id(client_hack, 'New Hackshire')
additional_fields = {
'origin': origin,
'destination': destination,
'revenue': distance * 1/100,
'picked_up': False,
'delivered': False
}
FMM.create_task(
client_hack, app_opening_task_type,
vehicle_id, location_id, admin_user_id,
fmm_api_url, accessToken, additional_fields
)
def create_charging_station_task(charging_station: str):
vehicle_id = FMM.get_vehicle_by_license_plate(client_hack,
charging_station)['_id']
location_id = FMM.get_location_id(client_hack, 'New Hackshire')
FMM.create_task(client_hack, charging_station_task_type, vehicle_id,
location_id, admin_user_id, fmm_api_url, accessToken)
def revive_12v_battery(license_plate: str):
set_battery_voltage(license_plate, random.uniform(12.0, 14.0))
def kill_12v_battery(license_plate: str):
new_battery_voltage = random.uniform(0.0, 11.6)
set_battery_voltage(license_plate, new_battery_voltage)
def drain_fuel(license_plate: str, drain: Union[str, Number]):
old_fuel_level = get_fuel_level(license_plate)
new_fuel_level = max(old_fuel_level-float(drain), .0)
set_fuel_level(license_plate, new_fuel_level)
def get_lon_lat_of_vehicle(license_plate: str) -> dict:
vehicle = FMM.get_vehicle_by_license_plate(client_hack, license_plate)
return {'lon': vehicle['lon'], 'lat': vehicle['lat']}
def get_all_charging_stations():
stations = client_hack.get_database('fmm').get_collection('vehicle').find(
filter={'status': 'blue'}
)
return {station['plate']: {'lon': station['lon'], 'lat': station['lat']}
for station in stations}
def get_tasks_for_vehicle(license_plate: str):
return FMM.get_tasks(
client_hack,
{
'vehicleId': FMM.get_vehicle_by_license_plate(client_hack,
license_plate)
}
)
def delete_tasks_for_vehicle(license_plate: str):
vehicle_id = FMM.get_vehicle_by_license_plate(client_hack,
license_plate)['_id']
FMM.delete_tasks(client_hack, {'vehicleId': vehicle_id})
if __name__ == '__main__':
"""
vehicle = FMM.get_vehicle_by_license_plate(client_hack, 'NH-0')
print(get_fuel_level(plate))
print(set_fuel_level(plate, 120))
print(get_fuel_level(plate))
print(get_tasks_for_vehicle(plate))
# delete_tasks_for_vehicle(plate)
print([task for task in get_tasks_for_vehicle(plate)])
# kill_12v_battery(plate)
revive_12v_battery(plate)
# delete_tasks_for_vehicle(plate)
"""
create_app_opening('NH-0')
"""
for plate in all_license_plates():
vehicle = FMM.get_vehicle_by_license_plate(client_hack, plate)
client_hack.get_database('fmm').get_collection('vehicle').update_many(
{}, {'$set': {'locationAlias': 'newhack'}}
)
"""