X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Focpp-server%2Fserver.py;h=65064afbbd0056ad75bec4f95ad339b5f44fc9f0;hb=e3861e416821629094520003d116dd3f714fa15a;hp=3001ebd843cb7b787bef94b34da24752f0c28ff4;hpb=3a314928355d4ad376b1d5333b6ea864d191b708;p=e-mobility-charging-stations-simulator.git diff --git a/tests/ocpp-server/server.py b/tests/ocpp-server/server.py index 3001ebd8..65064afb 100644 --- a/tests/ocpp-server/server.py +++ b/tests/ocpp-server/server.py @@ -11,6 +11,7 @@ from ocpp.v201.enums import ( Action, AuthorizationStatusType, ClearCacheStatusType, + GenericDeviceModelStatusType, RegistrationStatusType, ReportBaseType, TransactionEventType, @@ -101,7 +102,7 @@ class ChargePoint(ocpp.v201.ChargePoint): ): logging.info("Received %s", Action.GetBaseReport) return ocpp.v201.call_result.GetBaseReport( - id_token_info={"status": ReportBaseType.accepted} + status=GenericDeviceModelStatusType.accepted ) # Request handlers to emit OCPP messages. @@ -116,39 +117,43 @@ class ChargePoint(ocpp.v201.ChargePoint): async def send_get_base_report(self): request = ocpp.v201.call.GetBaseReport( - reportBase=ReportBaseType.ConfigurationInventory + request_id=1, report_base=ReportBaseType.full_inventory ) response = await self.call(request) - if response.status == ReportBaseType.accepted: + if response.status == GenericDeviceModelStatusType.accepted: logging.info("%s successful", Action.GetBaseReport) else: logging.info("%s failed", Action.GetBaseReport) - -# Function to send OCPP command -async def send_ocpp_command(cp, command_name, delay=None, period=None): - try: + async def send_command(self, command_name: Action): + logging.debug("Sending OCPP command: %s", command_name) match command_name: case Action.ClearCache: - logging.info("%s Send:", Action.ClearCache) - await cp.send_clear_cache() + await self.send_clear_cache() case Action.GetBaseReport: - logging.info("%s Send:", Action.GetBaseReport) - await cp.send_get_base_report() - except Exception: - logging.exception( - f"Not supported or Failure while processing command {command_name}" - ) + await self.send_get_base_report() + case _: + logging.info(f"Not supported command {command_name}") - if delay: - await asyncio.sleep(delay) - if period: - my_timer = RepeatTimer( - period, asyncio.create_task, [cp.send_ocpp_command(command_name)] - ) - my_timer.start() +# Function to send OCPP command +async def send_ocpp_command(cp, command_name, delay=None, period=None): + try: + if delay: + await asyncio.sleep(delay) + cp.send_command(command_name) + if period: + command_timer = RepeatTimer( + period, + cp.send_command, + [command_name], + ) + command_timer.start() + except ConnectionClosed: + logging.info("ChargePoint %s closed connection", cp.id) + ChargePoints.remove(cp) + logging.debug("Connected ChargePoint(s): %d", len(ChargePoints)) # Function to handle new WebSocket connections. @@ -178,7 +183,6 @@ async def on_connect(websocket, path): ChargePoints.add(cp) try: await cp.start() - except ConnectionClosed: logging.info("ChargePoint %s closed connection", cp.id) ChargePoints.remove(cp) @@ -206,7 +210,7 @@ async def main(): if args.command: for cp in ChargePoints: - asyncio.create_task( + await asyncio.create_task( send_ocpp_command(cp, args.command, args.delay, args.period) )