fix: fix conditions requiring an heartbeat restart at boot notification
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 21 Mar 2023 00:12:06 +0000 (01:12 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 21 Mar 2023 00:12:06 +0000 (01:12 +0100)
handling

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/OCPPServiceUtils.ts
src/performance/PerformanceStatistics.ts

index 718f5eff7bfb0b21acd808e53e9cb080729e84d1..e78d6bf87bfb50715b884f300db97dca19ac7ce6 100644 (file)
@@ -386,6 +386,30 @@ export class ChargingStation {
     return localAuthListEnabled ? Utils.convertToBoolean(localAuthListEnabled.value) : false;
   }
 
+  public getHeartbeatInterval(): number {
+    const HeartbeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
+      StandardParametersKey.HeartbeatInterval
+    );
+    if (HeartbeatInterval) {
+      return Utils.convertToInt(HeartbeatInterval.value) * 1000;
+    }
+    const HeartBeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
+      StandardParametersKey.HeartBeatInterval
+    );
+    if (HeartBeatInterval) {
+      return Utils.convertToInt(HeartBeatInterval.value) * 1000;
+    }
+    this.stationInfo?.autoRegister === false &&
+      logger.warn(
+        `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
+          Constants.DEFAULT_HEARTBEAT_INTERVAL
+        }`
+      );
+    return Constants.DEFAULT_HEARTBEAT_INTERVAL;
+  }
+
   public setSupervisionUrl(url: string): void {
     if (
       this.getSupervisionUrlOcppConfiguration() &&
@@ -428,11 +452,7 @@ export class ChargingStation {
       );
     } else {
       logger.error(
-        `${this.logPrefix()} Heartbeat interval set to ${
-          this.getHeartbeatInterval()
-            ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
-            : this.getHeartbeatInterval()
-        }, not starting the heartbeat`
+        `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
       );
     }
   }
@@ -1928,11 +1948,7 @@ export class ChargingStation {
       );
     } else {
       logger.error(
-        `${this.logPrefix()} WebSocket ping interval set to ${
-          webSocketPingInterval
-            ? Utils.formatDurationSeconds(webSocketPingInterval)
-            : webSocketPingInterval
-        }, not starting the WebSocket ping`
+        `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
       );
     }
   }
@@ -1971,30 +1987,6 @@ export class ChargingStation {
     return new URL(supervisionUrls as string);
   }
 
-  private getHeartbeatInterval(): number {
-    const HeartbeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
-      this,
-      StandardParametersKey.HeartbeatInterval
-    );
-    if (HeartbeatInterval) {
-      return Utils.convertToInt(HeartbeatInterval.value) * 1000;
-    }
-    const HeartBeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
-      this,
-      StandardParametersKey.HeartBeatInterval
-    );
-    if (HeartBeatInterval) {
-      return Utils.convertToInt(HeartBeatInterval.value) * 1000;
-    }
-    this.stationInfo?.autoRegister === false &&
-      logger.warn(
-        `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
-          Constants.DEFAULT_HEARTBEAT_INTERVAL
-        }`
-      );
-    return Constants.DEFAULT_HEARTBEAT_INTERVAL;
-  }
-
   private stopHeartbeat(): void {
     if (this.heartbeatSetInterval) {
       clearInterval(this.heartbeatSetInterval);
index 18973792a1706472c91d436d4b4516f6bf40fa28..29ce6ee3360941b0e80dde5b57d0bb2d0dabe83b 100644 (file)
@@ -363,9 +363,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
         { visible: false },
         { overwrite: true, save: true }
       );
-      chargingStation.heartbeatSetInterval
-        ? chargingStation.restartHeartbeat()
-        : chargingStation.startHeartbeat();
+      OCPP16ServiceUtils.startHeartbeatInterval(chargingStation, payload.interval);
     }
     if (Object.values(RegistrationStatusEnumType).includes(payload.status)) {
       const logMsg = `${chargingStation.logPrefix()} Charging station in '${
index 971d5b8394652b82e70fb4934f348620847c1298..f11af29ee6cded9270f5255b559df30d44e36575 100644 (file)
@@ -19,7 +19,7 @@ import {
   RegistrationStatusEnumType,
   type ResponseHandler,
 } from '../../../types';
-import { Constants, logger } from '../../../utils';
+import { logger } from '../../../utils';
 import { OCPP20ServiceUtils, OCPPResponseService } from '../internal';
 
 const moduleName = 'OCPP20ResponseService';
@@ -164,9 +164,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
         {},
         { overwrite: true, save: true }
       );
-      chargingStation.heartbeatSetInterval
-        ? chargingStation.restartHeartbeat()
-        : chargingStation.startHeartbeat();
+      OCPP20ServiceUtils.startHeartbeatInterval(chargingStation, payload.interval);
     }
     if (Object.values(RegistrationStatusEnumType).includes(payload.status)) {
       const logMsg = `${chargingStation.logPrefix()} Charging station in '${
index 1225f4e1113fced99ae3fd27bf96020e123c8bd7..26b7a4ee5ccc4db42c48209eade339d08e2bf941 100644 (file)
@@ -166,6 +166,14 @@ export class OCPPServiceUtils {
     }
   }
 
+  public static startHeartbeatInterval(chargingStation: ChargingStation, interval: number): void {
+    if (!chargingStation.heartbeatSetInterval) {
+      chargingStation.startHeartbeat();
+    } else if (chargingStation.getHeartbeatInterval() !== interval) {
+      chargingStation.restartHeartbeat();
+    }
+  }
+
   protected static parseJsonSchemaFile<T extends JsonType>(
     filePath: string,
     ocppVersion: OCPPVersion,
index 8cf0e1ef5d9f9541913f9c72cbf6a7310dee19cf..b2333fded5f80ce1eba0c2638b08cb8ed8908ae2 100644 (file)
@@ -125,10 +125,7 @@ export class PerformanceStatistics {
   }
 
   public stop(): void {
-    if (this.displayInterval) {
-      clearInterval(this.displayInterval);
-      delete this.displayInterval;
-    }
+    this.stopLogStatisticsInterval();
     performance.clearMarks();
     performance.clearMeasures();
     this.performanceObserver?.disconnect();
@@ -159,28 +156,34 @@ export class PerformanceStatistics {
   }
 
   private startLogStatisticsInterval(): void {
-    if (Configuration.getLogStatisticsInterval() > 0 && !this.displayInterval) {
+    const logStatisticsInterval = Configuration.getLogStatisticsInterval();
+    if (logStatisticsInterval > 0 && !this.displayInterval) {
       this.displayInterval = setInterval(() => {
         this.logStatistics();
-      }, Configuration.getLogStatisticsInterval() * 1000);
+      }, logStatisticsInterval * 1000);
       logger.info(
-        `${this.logPrefix()} logged every ${Utils.formatDurationSeconds(
-          Configuration.getLogStatisticsInterval()
-        )}`
+        `${this.logPrefix()} logged every ${Utils.formatDurationSeconds(logStatisticsInterval)}`
       );
     } else if (this.displayInterval) {
       logger.info(
         `${this.logPrefix()} already logged every ${Utils.formatDurationSeconds(
-          Configuration.getLogStatisticsInterval()
+          logStatisticsInterval
         )}`
       );
     } else {
       logger.info(
-        `${this.logPrefix()} log interval is set to ${Configuration.getLogStatisticsInterval()?.toString()}. Not logging statistics`
+        `${this.logPrefix()} log interval is set to ${logStatisticsInterval?.toString()}. Not logging statistics`
       );
     }
   }
 
+  private stopLogStatisticsInterval(): void {
+    if (this.displayInterval) {
+      clearInterval(this.displayInterval);
+      delete this.displayInterval;
+    }
+  }
+
   private median(dataSet: number[]): number {
     if (Array.isArray(dataSet) === true && dataSet.length === 1) {
       return dataSet[0];