-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.py
250 lines (223 loc) · 10.3 KB
/
driver.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from locust import HttpUser, task, between
import json
import datetime
import time
from lib import getRideOtpBpp
from addDriverVehicle import add_vehicle
from mobileNumberGenerator import generate_random_mobile_number
class DriverApp(HttpUser):
wait_time = between(1, 5)
host = "<Host-Url-Here>"
status = "IDLE"
@task
def on_start(self):
self.status = "IDLE"
self.environment_vars = {
"driver_merchant_id": "7f7896dd-787e-4a0b-8675-e9e6fe93bb8f",
"baseURL_namma_P" : "<Host-Url-Here>/dev/dobpp/ui",
"baseUrl_lts" : "<Host-Url-Here>/dev/dobpp/ui",
"origin-lat": 12.942247365419119,
"origin-lon": 77.62198115675885,
"dest-lat": 12.9325404,
"dest-lon": 77.6287519,
}
self.auth()
self.auth_verification()
self.get_profile()
self.startDriverApp()
def set_environment_variable(self, key, value):
self.environment_vars[key] = value
def get_environment_variable(self, key):
return self.environment_vars.get(key, '')
def auth(self):
mobileNumber = generate_random_mobile_number(['7','8','9'])
self.set_environment_variable("mobile_number", mobileNumber)
payload = {
"mobileNumber": mobileNumber,
"mobileCountryCode": "+91",
"merchantId": self.get_environment_variable("driver_merchant_id")
}
headers = {'Content-Type': 'application/json;charset=utf-8'}
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/auth", json=payload, headers=headers)
if response.status_code == 200:
jsonData = response.json()
self.set_environment_variable("driver_authId", jsonData["authId"])
time.sleep(0.5)
def auth_verification(self):
auth_id = self.get_environment_variable("driver_authId")
payload = {
"otp": "7891",
"deviceToken": "doDpYhFKfEoJgGTjTbJSDj:APA91bE8vTiYAVx89Q0vKCo0zRp7K2SyJukMjADCithsfhsd0jlNPyR18BUdav-ocA0gcMvkwp8XhE0dOkVvXou-DfSs7LHjOotRlTEX1suQQTya0wOWn7UUCTZAFip_GYMCavSDzpEE"
}
headers = {'Content-Type': 'application/json;charset=utf-8'}
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/auth/{auth_id}/verify", json=payload, headers=headers)
if response.status_code == 200:
jsonData = response.json()
add_vehicle(jsonData["person"]["id"])
self.set_environment_variable("driver_token", jsonData["token"])
time.sleep(0.5)
def set_online_offline(self):
headers = {'Accept': 'application/json;charset=utf-8', 'token': self.get_environment_variable("driver_token")}
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/driver/setActivity?active=true&mode=\"ONLINE\"", headers=headers)
time.sleep(0.5)
def get_profile(self):
headers = {'token': self.get_environment_variable("driver_token")}
response = self.client.get(f"{self.get_environment_variable('baseURL_namma_P')}/driver/profile", headers=headers)
if response.status_code == 200:
body = response.json()
self.set_environment_variable("mId", body["organization"]["id"])
self.set_environment_variable("vt", body["linkedVehicle"]["variant"])
self.set_environment_variable("driver_id", body["id"])
self.set_environment_variable("dm", body["mode"])
time.sleep(0.5)
def set_driver_location(self):
headers = {
'Content-Type': 'application/json;charset=utf-8',
'Accept': 'application/json;charset=utf-8',
'vt': self.get_environment_variable("vt"),
'mId': self.get_environment_variable("mId"),
'dm': self.get_environment_variable("dm"),
'token': self.get_environment_variable("driver_token")
}
current_time = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
self.set_environment_variable("current_time", current_time)
payload = [
{
"pt": {
"lat": self.get_environment_variable("origin-lat"),
"lon": self.get_environment_variable("origin-lon")
},
"ts": self.get_environment_variable("current_time"),
"acc": 1,
"v": 5
} for _ in range(10)
]
response = self.client.post(f"{self.get_environment_variable('baseUrl_lts')}/driver/location", json=payload, headers=headers)
time.sleep(2)
def nearby_ride_request(self):
headers = {
"Accept": "application/json;charset=utf-8",
"token": self.get_environment_variable("driver_token")
}
response = self.client.get(f"{self.get_environment_variable('baseURL_namma_P')}/driver/nearbyRideRequest", headers=headers)
if response.status_code == 200:
body = response.json()
if body.get("searchRequestsForDriver") and (len(body["searchRequestsForDriver"]) != 0):
searchRequestId = body["searchRequestsForDriver"][0]["searchRequestId"]
if(searchRequestId ==None):
return
self.set_environment_variable("driver_search_request_id", searchRequestId)
self.status = "QUOTE_OFFERED"
elif response.status_code == 400:
print("No nearby ride request 400 error")
self.status = "IDLE"
elif response.status_code == 401:
print("No nearby ride request 401 error")
self.auth()
self.auth_verification()
self.status = "IDLE"
else:
print(f"No nearby ride request {response.status_code} error")
self.status = "IDLE"
time.sleep(0.5)
def quote_customer_for_ride(self):
headers = {
"Content-Type": "application/json;charset=utf-8",
"Accept": "application/json;charset=utf-8",
"token": self.get_environment_variable("driver_token")
}
srid = self.get_environment_variable("driver_search_request_id")
payload = {
"searchRequestId": srid,
"response": "Accept"
}
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/driver/searchRequest/quote/respond", json=payload, headers=headers)
if response.status_code == 200:
self.status = "REACHING_PICKUP"
elif response.status_code == 400:
print("quote offer 400 error", response.json(), srid)
self.status = "IDLE"
elif response.status_code == 401:
print("quote offer 401 error", response.json(), srid)
self.status = "IDLE"
else:
print(f"quote offer {response.status_code} error", response.json(), srid)
self.status = "IDLE"
time.sleep(0.5)
def driver_ride_id_of_newly_booked_ride(self):
headers = {
"token": self.get_environment_variable("driver_token")
}
response = self.client.get(f"{self.get_environment_variable('baseURL_namma_P')}/driver/ride/list?limit=1", headers=headers)
if response.status_code == 200:
body = response.json()
new_ride = next((ride for ride in body["list"] if ride["status"] == "NEW"), None)
if new_ride!=None and new_ride != "" :
self.set_environment_variable("driver_ride_id", new_ride["id"])
time.sleep(0.5)
def driver_start_ride_by_entering_otp(self):
headers = {
"Content-Type": "application/json;charset=utf-8",
"token": self.get_environment_variable("driver_token")
}
payload = {
"rideOtp": getRideOtpBpp(self.get_environment_variable("driver_ride_id")),
"point": {
"lat": self.get_environment_variable("origin-lat"),
"lon": self.get_environment_variable("origin-lon")
},
"odometer": None
}
ride_id = self.get_environment_variable("driver_ride_id")
retry = 5
count = int(self.get_environment_variable("rstart_count") or 0)
if ride_id == None or ride_id == "":
count += 1
self.set_environment_variable("rstart_count", count)
return
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/driver/ride/{ride_id}/start", json=payload, headers=headers)
if(count>retry):
self.status = "REACHING_PICKUP"
elif response.status_code == 200:
self.status = "ON_RIDE"
else:
count += 1
self.set_environment_variable("rstart_count", count)
print(f"Start Ride - Failed with status code {response.status_code}", response.json(), ride_id)
time.sleep(0.5)
def driver_ride_completion(self):
headers = {
"token": self.get_environment_variable("driver_token")
}
payload = {
"point": {
"lat": self.get_environment_variable("dest-lat"),
"lon": self.get_environment_variable("dest-lon")
}
}
ride_id = self.get_environment_variable("driver_ride_id")
response = self.client.post(f"{self.get_environment_variable('baseURL_namma_P')}/driver/ride/{ride_id}/end", json=payload, headers=headers)
retry = 5
count = int(self.get_environment_variable("rend_count") or 0)
if(count>retry):
self.status = "IDLE"
elif response.status_code == 200:
self.status = "IDLE"
else:
count += 1
self.set_environment_variable("rend_count", count)
print(f"Ride Completion - Failed with status code {response.status_code}", response.json())
time.sleep(0.5)
def startDriverApp(self):
if(self.status == "IDLE"):
self.set_online_offline()
self.set_driver_location()
self.nearby_ride_request()
elif(self.status == "QUOTE_OFFERED"):
self.quote_customer_for_ride()
elif(self.status == "REACHING_PICKUP"):
self.driver_ride_id_of_newly_booked_ride()
self.driver_start_ride_by_entering_otp()
elif(self.status == "ON_RIDE"):
self.driver_ride_completion()
self.startDriverApp()