From 8f953431be60a5520dba937dd2cbd394a72af25d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 21 Mar 2023 01:12:06 +0100 Subject: [PATCH] fix: fix conditions requiring an heartbeat restart at boot notification handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 60 ++++++++----------- .../ocpp/1.6/OCPP16ResponseService.ts | 4 +- .../ocpp/2.0/OCPP20ResponseService.ts | 6 +- src/charging-station/ocpp/OCPPServiceUtils.ts | 8 +++ src/performance/PerformanceStatistics.ts | 25 ++++---- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 718f5eff..e78d6bf8 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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); diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 18973792..29ce6ee3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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 '${ diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 971d5b83..f11af29e 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -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 '${ diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 1225f4e1..26b7a4ee 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -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( filePath: string, ocppVersion: OCPPVersion, diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 8cf0e1ef..b2333fde 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -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]; -- 2.34.1