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 pathFMMFramework.py
174 lines (133 loc) · 5.12 KB
/
FMMFramework.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
import pymongo.database
import random
import datetime
import requests
def get_vehicle_collection(client: pymongo.MongoClient
) -> pymongo.database.Collection:
return client.get_database('fmm').get_collection('vehicle')
def get_location_id(client: pymongo.MongoClient, location_name: str):
return client.get_database('common').get_collection('location').find_one({
'name': location_name
})['_id']
def get_some_e_smart(from_client: pymongo.MongoClient,
from_location: str) -> dict:
e_smart_query = {
'locationId': get_location_id(
from_client, from_location
),
'model': "SMART", 'engine_type': "ED",
'status': "green",
'hardwareVersion': "HW3",
}
e_smarts = (
from_client.get_database('fmm').get_collection('vehicle').find(
e_smart_query
)
)
return e_smarts[random.randint(0, e_smarts.count()-1)]
def import_vehicle(to_client: pymongo.MongoClient,
to_location: str,
vehicle: dict):
to_location_id = get_location_id(to_client, to_location)
vehicle['locationId'] = to_location_id
to_client.get_database('fmm').get_collection('vehicle').insert(vehicle)
def import_e_smart(from_client: pymongo.MongoClient,
from_location: str,
to_client: pymongo.MongoClient,
to_location: str):
some_e_smart = get_some_e_smart(from_client, from_location)
import_vehicle(to_client, to_location, some_e_smart)
def get_vehicle_fields(client: pymongo.MongoClient,
vehicle_id: str,
keys: list):
vehicle = get_vehicle_by_id(client, vehicle_id)
return {key: vehicle[key] for key in keys}
def set_vehicle_fields(client: pymongo.MongoClient,
vehicle_id: str,
update: dict):
get_vehicle_collection(client).update_one(
{'_id': vehicle_id},
{'$set': update},
bypass_document_validation=True
)
update_last_change_date(client, vehicle_id)
def update_last_change_date(client: pymongo.MongoClient,
vehicle_id: str):
update_date = {
'lastChangeDate': datetime.datetime.utcnow(),
'lastCABAUpdateDate': datetime.datetime.utcnow()
}
get_vehicle_collection(client).find_one_and_update(
{'_id': vehicle_id},
{'$set': update_date}
)
def get_some_vehicle(client: pymongo.MongoClient,
location_name: str) -> dict:
location_id = get_location_id(client, location_name)
vehicles = get_vehicle_collection(client).find({
'locationId': location_id
})
return vehicles[random.randint(0, vehicles.count()-1)]
def get_vehicle_by_id(client: pymongo.MongoClient, vehicle_id: str) -> dict:
return get_vehicle_collection(client).find_one({'_id': vehicle_id})
def get_vehicle_by_license_plate(client: pymongo.MongoClient,
license_plate: str) -> dict:
return get_vehicle_collection(client).find_one(
filter={
'plate': license_plate,
'numberPlate': license_plate
}
)
def get_tasks(client: pymongo.MongoClient, filter: dict=None):
return client.get_database('fmm').get_collection('task').find(
filter if filter is not None else None
)
def get_task(client: pymongo.MongoClient, task_id):
return client.get_database('fmm').get_collection('task').find_one(
{'_id': task_id}
)
def create_task(client: pymongo.MongoClient,
task_type: dict,
vehicle_id: str,
location_id: str,
user_id: str,
url: str,
access_token: str,
additional_fields: dict=None):
task = {
'vehicleId': vehicle_id,
'vehicle_comment': None,
'assignedTo': None,
'assigned': None,
'status': 'OPEN',
'locationId': location_id,
'followOn': False,
'createdBy': pymongo.database.ObjectId(user_id),
'photos': [],
'activities': [],
'created': datetime.datetime.utcnow(),
'disregardStatus': False,
'taskType': [task_type],
'comments': [],
}
if additional_fields is not None:
task.update(additional_fields)
client.get_database('fmm').get_collection('task').insert(
task
)
requests.get(
url=url+'/protected/vehicles/syncByVehicleId/'+str(vehicle_id),
headers={'accessToken': access_token, 'userId': user_id}
)
def assign_task_to_vehicle(client: pymongo.MongoClient,
task_id):
pass
def delete_tasks(client: pymongo.MongoClient, filter: dict):
client.get_database('fmm').get_collection('task').delete_many(filter)
# unit-tests
if __name__ == '__main__':
from Connection import client_hack
import SimulationFramework as sim
vehicle = get_vehicle_by_license_plate(client_hack, 'NH-6')
vehicle.update(sim.random_coordinates_from_hackathon_location())
set_vehicle_fields(client_hack, vehicle_id=vehicle['_id'], update=vehicle)