fix: start web socket ping interval at successful connection
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 30 May 2024 01:54:15 +0000 (03:54 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 30 May 2024 01:54:15 +0000 (03:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/types/ChargingStationEvents.ts

index 2d934e066ff07a58470dc6162c342491df1adb93..64cb637b02f80782bbfb96a69bbfa723f5fa18d5 100644 (file)
@@ -236,6 +236,11 @@ export class ChargingStation extends EventEmitter {
     this.on(ChargingStationEvents.rejected, () => {
       this.wsConnectionRetried = false
     })
+    this.on(ChargingStationEvents.connected, () => {
+      if (this.wsPingSetInterval == null) {
+        this.startWebSocketPing()
+      }
+    })
     this.on(ChargingStationEvents.disconnected, () => {
       try {
         this.internalStopMessageSequence()
@@ -554,7 +559,8 @@ export class ChargingStation extends EventEmitter {
   }
 
   public startHeartbeat (): void {
-    if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval == null) {
+    const heartbeatInterval = this.getHeartbeatInterval()
+    if (heartbeatInterval > 0 && this.heartbeatSetInterval == null) {
       this.heartbeatSetInterval = setInterval(() => {
         this.ocppRequestService
           .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
@@ -564,21 +570,21 @@ export class ChargingStation extends EventEmitter {
               error
             )
           })
-      }, this.getHeartbeatInterval())
+      }, heartbeatInterval)
       logger.info(
         `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
-          this.getHeartbeatInterval()
+          heartbeatInterval
         )}`
       )
     } else if (this.heartbeatSetInterval != null) {
       logger.info(
         `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
-          this.getHeartbeatInterval()
+          heartbeatInterval
         )}`
       )
     } else {
       logger.error(
-        `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
+        `${this.logPrefix()} Heartbeat interval set to ${heartbeatInterval}, not starting the heartbeat`
       )
     }
   }
@@ -1822,6 +1828,7 @@ export class ChargingStation extends EventEmitter {
 
   private async onOpen (): Promise<void> {
     if (this.isWebSocketConnectionOpened()) {
+      this.emit(ChargingStationEvents.connected)
       this.emit(ChargingStationEvents.updated)
       logger.info(
         `${this.logPrefix()} Connection to OCPP server through ${
@@ -2222,12 +2229,12 @@ export class ChargingStation extends EventEmitter {
       })
     }
     // Start WebSocket ping
-    this.startWebSocketPing()
+    if (this.wsPingSetInterval == null) {
+      this.startWebSocketPing()
+    }
     // Start heartbeat
     if (this.heartbeatSetInterval == null) {
       this.startHeartbeat()
-    } else if (this.getHeartbeatInterval() !== this.bootNotificationResponse?.interval) {
-      this.restartHeartbeat()
     }
     // Initialize connectors status
     if (this.hasEvses) {
@@ -2318,13 +2325,14 @@ export class ChargingStation extends EventEmitter {
     }
   }
 
+  private getWebSocketPingInterval (): number {
+    return getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null
+      ? convertToInt(getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value)
+      : 0
+  }
+
   private startWebSocketPing (): void {
-    const webSocketPingInterval =
-      getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null
-        ? convertToInt(
-          getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value
-        )
-        : 0
+    const webSocketPingInterval = this.getWebSocketPingInterval()
     if (webSocketPingInterval > 0 && this.wsPingSetInterval == null) {
       this.wsPingSetInterval = setInterval(() => {
         if (this.isWebSocketConnectionOpened()) {
index 409f382ea9e8cfd0351b142eb4100d659e58f620..c7a75e43a4034960827c39e1b2dcc510fbbcacd1 100644 (file)
@@ -515,7 +515,6 @@ export class OCPP16ResponseService extends OCPPResponseService {
       if (chargingStation.isRegistered()) {
         chargingStation.emit(ChargingStationEvents.registered)
         if (chargingStation.inAcceptedState()) {
-          chargingStation.emit(ChargingStationEvents.accepted)
           addConfigurationKey(
             chargingStation,
             OCPP16StandardParametersKey.HeartbeatInterval,
@@ -530,6 +529,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
             { visible: false },
             { overwrite: true, save: true }
           )
+          chargingStation.emit(ChargingStationEvents.accepted)
         }
       } else if (chargingStation.inRejectedState()) {
         chargingStation.emit(ChargingStationEvents.rejected)
index 77546cfa58fac5dd58b374454f85b8bb95653d3d..8a6e2cacb7d7c26b4da9f56c67b2e16b25d9681c 100644 (file)
@@ -188,7 +188,6 @@ export class OCPP20ResponseService extends OCPPResponseService {
       if (chargingStation.isRegistered()) {
         chargingStation.emit(ChargingStationEvents.registered)
         if (chargingStation.inAcceptedState()) {
-          chargingStation.emit(ChargingStationEvents.accepted)
           addConfigurationKey(
             chargingStation,
             OCPP20OptionalVariableName.HeartbeatInterval,
@@ -196,6 +195,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
             {},
             { overwrite: true, save: true }
           )
+          chargingStation.emit(ChargingStationEvents.accepted)
         }
       } else if (chargingStation.inRejectedState()) {
         chargingStation.emit(ChargingStationEvents.rejected)
index b690bc916f2dca73d604349dc508f11177ff0643..3fa6adeccf6310ebebac3dbb679133dfe9f10aa3 100644 (file)
@@ -7,6 +7,7 @@ export enum ChargingStationEvents {
   registered = 'registered',
   accepted = 'accepted',
   rejected = 'rejected',
+  connected = 'connected',
   disconnected = 'disconnected',
   connectorStatusChanged = 'connectorStatusChanged'
 }