Skip to content

Commit 950ac8a

Browse files
committed
fix: move unique ID generation, error suppression & response validation to generic_call
1 parent fafaa4a commit 950ac8a

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

ocpp/charge_point.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,13 @@ async def call(self, payload, suppress=True, unique_id=None):
307307
"""
308308
camel_case_payload = snake_to_camel_case(asdict(payload))
309309

310-
unique_id = (
311-
unique_id if unique_id is not None else str(self._unique_id_generator())
312-
)
313-
314310
response = await self.generic_call(
315311
action=payload.__class__.__name__[:-7],
316312
payload_json=camel_case_payload,
317313
suppress=suppress,
318314
unique_id=unique_id
319315
)
320316

321-
if response.message_type_id == MessageType.CallError:
322-
LOGGER.warning("Received a CALLError: %s'", response)
323-
if suppress:
324-
return
325-
raise response.to_exception()
326-
else:
327-
response.action = call.action
328-
validate_payload(response, self._ocpp_version)
329-
330317
snake_case_payload = camel_to_snake_case(response.payload)
331318
# Create the correct Payload instance based on the received payload. If
332319
# this method is called with a call.BootNotificationPayload, then it
@@ -358,6 +345,10 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
358345
CallError.
359346
"""
360347

348+
unique_id = (
349+
unique_id if unique_id is not None else str(self._unique_id_generator())
350+
)
351+
361352
call = Call(
362353
unique_id=unique_id,
363354
action=action,
@@ -371,7 +362,7 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
371362
async with self._call_lock:
372363
await self._send(call.to_json())
373364
try:
374-
return await self._get_specific_response(
365+
response = await self._get_specific_response(
375366
call.unique_id, self._response_timeout
376367
)
377368
except asyncio.TimeoutError:
@@ -380,6 +371,16 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
380371
f"{call.to_json()}."
381372
)
382373

374+
if response.message_type_id == MessageType.CallError:
375+
LOGGER.warning("Received a CALLError: %s'", response)
376+
if suppress:
377+
return
378+
raise response.to_exception()
379+
else:
380+
response.action = call.action
381+
validate_payload(response, self._ocpp_version)
382+
return response
383+
383384
async def _get_specific_response(self, unique_id, timeout):
384385
"""
385386
Return response with given unique ID or raise an asyncio.TimeoutError.

0 commit comments

Comments
 (0)