From a5c2d21fa2d2bee31c329622e76cd8ac391ca37d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 15 Jun 2024 22:33:49 +0200 Subject: [PATCH] test(ocpp-server): keep track of connected CS MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/ocpp-server/server.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/ocpp-server/server.py b/tests/ocpp-server/server.py index 15229a1d..27f09c40 100644 --- a/tests/ocpp-server/server.py +++ b/tests/ocpp-server/server.py @@ -13,10 +13,13 @@ from ocpp.v201.enums import ( RegistrationStatusType, TransactionEventType, ) +from websockets import ConnectionClosed # Setting up the logging configuration to display debug level messages. logging.basicConfig(level=logging.DEBUG) +ChargePoints = set() + class RepeatTimer(Timer): """Class that inherits from the Timer class. It will run a @@ -29,6 +32,21 @@ class RepeatTimer(Timer): # Define a ChargePoint class inheriting from the OCPP 2.0.1 ChargePoint class. class ChargePoint(ocpp.v201.ChargePoint): + def __init__(self, charge_point_id, connection): + super().__init__(self, charge_point_id, connection) + self._ws_ping_timer = RepeatTimer(60, self.web_socket_ping) + self._ws_ping_timer.start() + + def stop(self): + self._ws_ping_timer.cancel() + + def web_socket_ping(self): + try: + self._connection.ping() + except ConnectionClosed: + ChargePoints.remove(self) + self.stop() + # Message handlers to receive OCPP messages. @on(Action.BootNotification) async def on_boot_notification(self, charging_station, reason, **kwargs): @@ -124,6 +142,7 @@ async def on_connect(websocket, path): charge_point_id = path.strip("/") cp = ChargePoint(charge_point_id, websocket) + ChargePoints.add(cp) # Start the ChargePoint instance to listen for incoming messages. await cp.start() -- 2.34.1