This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb_api.py
544 lines (474 loc) · 17.4 KB
/
mb_api.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import asyncio
import json
import logging
import math
from abc import ABC, abstractmethod
from enum import Enum
from http import HTTPStatus as codes
from typing import Optional
from oauthlib.oauth2 import MismatchingStateError
from prometheus_client import Gauge
from requests import Response
from requests_oauthlib import OAuth2Session
logger = logging.getLogger(__name__)
class MbCustomer(OAuth2Session):
AUTHORIZATION_URL = "https://ssoalpha.dvb.corpinter.net/v1/auth"
TOKEN_URL = "https://ssoalpha.dvb.corpinter.net/v1/token"
def __init__(self, client_id: str, client_secret: str):
super().__init__(
client_id=client_id,
scope=[
"openid",
"offline_access",
"mb:vehicle:mbdata:evstatus",
"mb:vehicle:mbdata:fuelstatus",
"mb:vehicle:mbdata:payasyoudrive",
"mb:vehicle:mbdata:vehiclelock",
"mb:vehicle:mbdata:vehiclestatus",
],
auto_refresh_url=self.TOKEN_URL,
token_updater=self._update_token,
redirect_uri="http://localhost:8080/oauth.redirect",
)
self.__client_secret = str(client_secret)
self.__async_lock = asyncio.Lock()
try:
self.restore()
except Exception as e:
logger.warning("Error during restoring the token on init: %s", e)
def _update_token(self, token):
self.token = token
try:
self.persist()
except Exception as e:
logger.error("Error during persisting the updated token: %s", e)
def persist(self):
with open("state.json", mode="w") as f:
json.dump(self.token, f, indent=True)
def restore(self):
with open("state.json", mode="r") as f:
self.token = json.load(f)
def authorization_url(self, **kwargs):
return super().authorization_url(self.AUTHORIZATION_URL)
def fetch_token(self, **kwargs):
if "state" in kwargs and kwargs["state"] != self._state:
raise MismatchingStateError()
return super().fetch_token(
self.TOKEN_URL, client_secret=self.__client_secret, **kwargs
)
def refresh_token(self, token_url, **kwargs):
kwargs["client_id"] = self.client_id
kwargs["client_secret"] = self.__client_secret
return super().refresh_token(token_url, **kwargs)
def request(self, *args, **kwargs):
if "timeout" not in kwargs:
kwargs["timeout"] = (5, 30)
if "headers" not in kwargs:
kwargs["headers"] = {}
if "accept" not in kwargs["headers"]:
kwargs["headers"]["accept"] = "application/json;charset=utf-8"
return super().request(*args, **kwargs)
async def request_async(self, *args, **kwargs):
async with self.__async_lock:
return await asyncio.to_thread(self.request, *args, **kwargs)
async def get_async(self, url, **kwargs):
kwargs.setdefault("allow_redirects", True)
return await self.request_async("GET", url, **kwargs)
async def options_async(self, url, **kwargs):
kwargs.setdefault("allow_redirects", True)
return await self.request_async("OPTIONS", url, **kwargs)
async def head_async(self, url, **kwargs):
kwargs.setdefault("allow_redirects", False)
return await self.request_async("HEAD", url, **kwargs)
async def post_async(self, url, data=None, json=None, **kwargs):
return await self.request_async("POST", url, data=data, json=json, **kwargs)
async def put_async(self, url, data=None, **kwargs):
return await self.request_async("PUT", url, data=data, **kwargs)
async def patch_async(self, url, data=None, **kwargs):
return await self.request_async("PATCH", url, data=data, **kwargs)
async def delete_async(self, url, **kwargs):
return await self.request_async("DELETE", url, **kwargs)
def _kilometers_to_meters(val):
return float(val) * 1000
def _text_to_bool(val: str):
return json.loads(val.lower())
def _text_to_bool_not(val: str):
return not json.loads(val.lower())
class MbPromResRepr(Enum):
STATE_OF_CHARGE = (
"soc",
"mb_electric_state_of_charge",
"State of Charge obtained from electric vehicle api",
)
ELECTRIC_RANGE = (
"rangeelectric",
"mb_electric_range",
"Electric range in kilometers",
"meters",
_kilometers_to_meters,
)
LIQUID_FUEL_LEVEL = (
"tanklevelpercent",
"mb_liquid_fuel_level",
"Liquid fuel level",
)
LIQUID_RANGE = (
"rangeliquid",
"mb_liquid_range",
"Liquid range",
"meters",
_kilometers_to_meters,
)
ODOMETER = ("odo", "mb_odometer", "Odometer", "meters", _kilometers_to_meters)
DECK_LID_LOCK_STATUS = (
"doorlockstatusdecklid",
"mb_deck_lid_lock_status",
"Deck lid (Kofferraum) lock status",
None,
_text_to_bool_not,
)
VEHICLE_LOCK_STATUS = (
"doorlockstatusvehicle",
"mb_vehicle_lock_status",
"Vehicle lock status, 0: vehicle unlocked, 1: vehicle internal locked, 2: vehicle external locked, 3: vehicle selective unlocked",
)
GAS_TANK_LOCK_STATUS = (
"doorlockstatusgas",
"mb_gas_tank_lock_status",
"Status of gas tank door lock",
None,
_text_to_bool_not,
)
VEHICLE_HEADING_POSITION = (
"positionHeading",
"mb_vehicle_heading_position",
"Vehicle heading position",
"degrees",
)
DECK_LID_STATUS = (
"decklidstatus",
"mb_deck_lid_open",
"Deck lid latch status opened/closed state",
None,
_text_to_bool,
)
DOOR_STATUS_FRONT_LEFT = (
"doorstatusfrontleft",
"mb_door_status_front_left",
"Status of the front left door",
None,
_text_to_bool,
)
DOOR_STATUS_FRONT_RIGHT = (
"doorstatusfrontright",
"mb_door_status_front_right",
"Status of the front right door",
None,
_text_to_bool,
)
DOOR_STATUS_REAR_LEFT = (
"doorstatusrearleft",
"mb_door_status_rear_left",
"Status of the rear left door",
None,
_text_to_bool,
)
DOOR_STATUS_REAR_RIGHT = (
"doorstatusrearright",
"mb_door_status_rear_right",
"Status of the rear right door",
None,
_text_to_bool,
)
INTERIOR_LIGHTS_FRONT = (
"interiorLightsFront",
"mb_interior_front_light_status",
"Front light inside",
None,
_text_to_bool,
)
INTERIOR_LIGHTS_REAR = (
"interiorLightsRear",
"mb_interior_rear_light_status",
"Rear light inside",
None,
_text_to_bool,
)
LIGHT_SWITCH_POSITION = (
"lightswitchposition",
"mb_light_switch_position",
"Light switch position: 0: auto; 1: headlights; 2: sidelight left; 3: sidelight right; 4: parking light",
)
READING_LAMP_FRONT_LEFT = (
"readingLampFrontLeft",
"mb_reading_lamp_front_left",
"Front left reading light",
None,
_text_to_bool,
)
READING_LAMP_FRONT_RIGHT = (
"readingLampFrontRight",
"mb_reading_lamp_front_right",
"Front right reading light",
None,
_text_to_bool,
)
ROOF_TOP_STATUS = (
"rooftopstatus",
"mb_roof_top_status",
"Status of the convertible top opened/closed: 0: unlocked; 1: open and locked; 2: closed and locked",
)
SUN_ROOF_STATUS = (
"sunroofstatus",
"mb_sun_roof_status",
"Status of the sunroof; "
"0: Tilt/slide sunroof is closed; "
"1: Tilt/slide sunroof is complete open; "
"2: Lifting roof is open;"
"3: Tilt/slide sunroof is running; "
"4: Tilt/slide sunroof in anti-booming position; "
"5: Sliding roof in intermediate position;"
"6: Lifting roof in intermediate position",
)
WINDOW_STATUS_FRONT_LEFT = (
"windowstatusfrontleft",
"mb_window_status_front_left",
"Status of the front left window; "
"0: window in intermediate position; "
"1: window completely opened; "
"2: window completely closed; "
"3: window airing position; "
"4: window intermediate airing position; "
"5: window currently running",
)
WINDOW_STATUS_FRONT_RIGHT = (
"windowstatusfrontright",
"mb_window_status_front_right",
"Status of the front right window; "
"0: window in intermediate position; "
"1: window completely opened; "
"2: window completely closed; "
"3: window airing position; "
"4: window intermediate airing position; "
"5: window currently running",
)
WINDOW_STATUS_REAR_LEFT = (
"windowstatusrearleft",
"mb_window_status_rear_left",
"Status of the rear left window; "
"0: window in intermediate position; "
"1: window completely opened; "
"2: window completely closed; "
"3: window airing position; "
"4: window intermediate airing position; "
"5: window currently running",
)
WINDOW_STATUS_REAR_RIGHT = (
"windowstatusrearright",
"mb_window_status_rear_right",
"Status of the rear right window; "
"0: window in intermediate position; "
"1: window completely opened; "
"2: window completely closed; "
"3: window airing position; "
"4: window intermediate airing position; "
"5: window currently running",
)
def __init__(
self,
resource_name: str,
metric_base_name: str,
help_text: str,
unit_name: Optional[str] = None,
value_mapper=None,
):
self.resource_name = resource_name
metric_name = metric_base_name
if unit_name:
metric_name += "_" + unit_name
self.__metric = Gauge(metric_name, help_text, ["vin"])
self.__measurement_time = Gauge(
f"{metric_base_name}_measurement_time_seconds",
f"Measurement time of {metric_name}",
["vin"],
)
self.__update_time = Gauge(
f"{metric_base_name}_update_time_seconds",
f"Update time of {metric_name}",
["vin"],
)
self.__value_mapper = value_mapper or float
def new_value(self, vin: str, data: dict):
self.__metric.labels(vin).set(self.__value_mapper(data["value"]))
self.__measurement_time.labels(vin).set(data["timestamp"] / 1000)
self.__update_time.labels(vin).set_to_current_time()
def no_new_value(self, vin: str):
self.__update_time.labels(vin).set_to_current_time()
class MbBaseVehicleApi(ABC):
def __init__(
self,
mb_customer: MbCustomer,
vin: str,
calls_per_hour: float,
expected_resources: set[MbPromResRepr],
):
self.mb_customer = mb_customer
self.vin = str(vin)
self._calls_per_hour = float(calls_per_hour)
self.expected_resources = expected_resources
@abstractmethod
async def request(self) -> Response:
...
def process_response(self, resp: Response):
if resp.status_code == codes.NO_CONTENT:
for v in self.expected_resources:
v.no_new_value(self.vin)
elif resp.status_code == codes.OK:
data = resp.json()
expected = {v.resource_name: v for v in self.expected_resources}
for item in data:
for key in item.keys():
if key in expected:
expected[key].new_value(self.vin, item[key])
del expected[key]
break
else:
logger.warning(f"unexpected resource {key}")
for value in expected.values():
value.no_new_value(self.vin)
async def refresh(self):
resp = await self.request()
if resp.status_code in (codes.OK, codes.NO_CONTENT, codes.TOO_MANY_REQUESTS):
self.process_response(resp)
else:
logger.error(
"Unexpected status code %d during requesting %s. Response text %s",
resp.status_code,
resp.request.url,
resp.text,
)
async def continuous_refresh(self):
while True:
await self.refresh()
await asyncio.sleep(math.ceil(3600 / self._calls_per_hour) + 1)
class MbElectricVehicleStatus(MbBaseVehicleApi):
def __init__(self, mb_customer: MbCustomer, vin: str):
super().__init__(
mb_customer,
vin,
30,
{MbPromResRepr.ELECTRIC_RANGE, MbPromResRepr.STATE_OF_CHARGE},
)
async def request(self) -> Response:
return await self.mb_customer.get_async(
f"https://api.mercedes-benz.com/vehicledata/v2/vehicles/{self.vin}/containers/electricvehicle"
)
class MbFuelStatus(MbBaseVehicleApi):
def __init__(self, mb_customer: MbCustomer, vin: str):
super().__init__(
mb_customer,
vin,
1,
{MbPromResRepr.LIQUID_FUEL_LEVEL, MbPromResRepr.LIQUID_RANGE},
)
async def request(self) -> Response:
return await self.mb_customer.get_async(
f"https://api.mercedes-benz.com/vehicledata/v2/vehicles/{self.vin}/containers/fuelstatus"
)
class MbOdometerStatus(MbBaseVehicleApi):
def __init__(self, mb_customer: MbCustomer, vin: str):
super().__init__(mb_customer, vin, 1, set())
self._odometer = MbPromResRepr.ODOMETER
async def request(self) -> Response:
return await self.mb_customer.get_async(
f"https://api.mercedes-benz.com/vehicledata/v2/vehicles/{self.vin}/resources/odo"
)
def process_response(self, resp: Response):
if resp.status_code == codes.NO_CONTENT:
self._odometer.no_new_value(self.vin)
elif resp.status_code == codes.OK:
data = resp.json()
self._odometer.new_value(self.vin, data["odo"])
class MbVehicleLockStatus(MbBaseVehicleApi):
def __init__(self, mb_customer: MbCustomer, vin: str):
super().__init__(
mb_customer,
vin,
50,
{
MbPromResRepr.DECK_LID_LOCK_STATUS,
MbPromResRepr.VEHICLE_LOCK_STATUS,
MbPromResRepr.GAS_TANK_LOCK_STATUS,
MbPromResRepr.VEHICLE_HEADING_POSITION,
},
)
async def request(self) -> Response:
return await self.mb_customer.get_async(
f"https://api.mercedes-benz.com/vehicledata/v2/vehicles/{self.vin}/containers/vehiclelockstatus"
)
class MbVehicleStatus(MbBaseVehicleApi):
def __init__(self, mb_customer: MbCustomer, vin: str):
super().__init__(
mb_customer,
vin,
50,
{
MbPromResRepr.DECK_LID_STATUS,
MbPromResRepr.DOOR_STATUS_FRONT_LEFT,
MbPromResRepr.DOOR_STATUS_FRONT_RIGHT,
MbPromResRepr.DOOR_STATUS_REAR_LEFT,
MbPromResRepr.DOOR_STATUS_REAR_RIGHT,
MbPromResRepr.INTERIOR_LIGHTS_FRONT,
MbPromResRepr.INTERIOR_LIGHTS_REAR,
MbPromResRepr.LIGHT_SWITCH_POSITION,
MbPromResRepr.READING_LAMP_FRONT_LEFT,
MbPromResRepr.READING_LAMP_FRONT_RIGHT,
MbPromResRepr.ROOF_TOP_STATUS,
MbPromResRepr.SUN_ROOF_STATUS,
MbPromResRepr.WINDOW_STATUS_FRONT_LEFT,
MbPromResRepr.WINDOW_STATUS_FRONT_RIGHT,
MbPromResRepr.WINDOW_STATUS_REAR_LEFT,
MbPromResRepr.WINDOW_STATUS_REAR_RIGHT,
},
)
async def request(self) -> Response:
return await self.mb_customer.get_async(
f"https://api.mercedes-benz.com/vehicledata/v2/vehicles/{self.vin}/containers/vehiclestatus"
)
class MbHybridVehicle:
def __init__(self, mb_customer: MbCustomer, vin: str):
self.mb_customer = mb_customer
self.vin = vin
self.apis = [
MbElectricVehicleStatus(mb_customer, vin),
MbFuelStatus(mb_customer, vin),
MbOdometerStatus(mb_customer, vin),
MbVehicleLockStatus(mb_customer, vin),
MbVehicleStatus(mb_customer, vin),
]
self.continuous_refresh_task: Optional[asyncio.Task] = None
async def refresh(self):
await asyncio.gather((api.refresh()) for api in self.apis)
async def continuous_refresh(self):
await asyncio.gather(*[(api.continuous_refresh()) for api in self.apis])
def start(self):
if (
self.continuous_refresh_task
and not self.continuous_refresh_task.cancelled()
):
return
if self.mb_customer.authorized:
self.continuous_refresh_task = asyncio.create_task(
self.continuous_refresh()
)
def stop(self):
if (
self.continuous_refresh_task
and not self.continuous_refresh_task.cancelled()
):
self.continuous_refresh_task.cancel()
def running(self) -> bool:
return self.continuous_refresh_task and not (
self.continuous_refresh_task.done()
or self.continuous_refresh_task.cancelled()
)