From e3861e416821629094520003d116dd3f714fa15a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 27 Jun 2024 17:24:08 +0200 Subject: [PATCH] fix: fix OCPP2 mock server command sending MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/ocpp-server/README.md | 6 ++--- tests/ocpp-server/server.py | 52 ++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/tests/ocpp-server/README.md b/tests/ocpp-server/README.md index e6a4dfa1..32e13619 100644 --- a/tests/ocpp-server/README.md +++ b/tests/ocpp-server/README.md @@ -20,9 +20,9 @@ poetry run task server The server will start listening for connections on port 9000. -## Running the server with command sending +## Running the server with OCPP command sending -You can also specify a command and a period duration with the --request and --period options respectively when running the server. The server will then send your chosen command to the connected client every period seconds. +You can also specify a command and a period duration with the --command and --period options respectively when running the server. The server will then send your chosen command to the connected client(s) every period seconds. ### GetBaseReport Command @@ -40,7 +40,7 @@ To run the server and send a ClearCache command every 5 seconds, use: poetry run task server --command ClearCache --period 5 ``` -Please be mindful that these commands were examples according to the provided scenario, the available commands and their syntax might vary depending on the ocpp version and the implemented functionalities on your server. +Please be mindful that these commands were examples according to the provided scenario, the available commands and their syntax might vary depending on the ocpp version and the implemented functionalities on your client. ## Overview of the Server Scripts 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) ) -- 2.34.1