test(ocpp-server): keep track of connected CS
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 15 Jun 2024 20:33:49 +0000 (22:33 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 15 Jun 2024 20:33:49 +0000 (22:33 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
tests/ocpp-server/server.py

index 15229a1d18520a5a5239be52c321920da3f87155..27f09c40af311cb419a962881751ef3e3c7180a5 100644 (file)
@@ -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()