From d7d1db72537ac2b012ca901a768d6442f3cf39fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 18 Sep 2021 12:58:54 +0200 Subject: [PATCH] Fix duration formatting for large one MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- manifest-cf-template.yml | 1 + .../AutomaticTransactionGenerator.ts | 6 ++--- src/charging-station/ChargingStation.ts | 14 +++++------ .../ocpp/1.6/OCPP16IncomingRequestService.ts | 2 +- src/performance/PerformanceStatistics.ts | 2 +- src/utils/Utils.ts | 25 ++++++++++++++++--- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/manifest-cf-template.yml b/manifest-cf-template.yml index 1883969c..cb1e8aa8 100644 --- a/manifest-cf-template.yml +++ b/manifest-cf-template.yml @@ -7,6 +7,7 @@ applications: - https://github.com/cloudfoundry/nodejs-buildpack no-route: true health-check-type: process + timeout: 30 command: node -r source-map-support/register dist/start.js env: OPTIMIZE_MEMORY: true diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 756f5b80..5526339f 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -31,7 +31,7 @@ export default class AutomaticTransactionGenerator { }); } } - logger.info(this.logPrefix() + ' started and will run for ' + Utils.milliSecondsToHHMMSS(this.stopDate.getTime() - this.startDate.getTime())); + logger.info(this.logPrefix() + ' started and will run for ' + Utils.formatDurationMilliSeconds(this.stopDate.getTime() - this.startDate.getTime())); } public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise { @@ -77,7 +77,7 @@ export default class AutomaticTransactionGenerator { } const wait = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; - logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.milliSecondsToHHMMSS(wait)); + logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait)); await Utils.sleep(wait); const start = Utils.secureRandom(); if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { @@ -91,7 +91,7 @@ export default class AutomaticTransactionGenerator { // Wait until end of transaction const waitTrxEnd = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.milliSecondsToHHMMSS(waitTrxEnd)); + logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); await Utils.sleep(waitTrxEnd); // Stop transaction if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 575d7096..833783aa 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -265,11 +265,11 @@ export default class ChargingStation { this.heartbeatSetInterval = setInterval(async (): Promise => { await this.ocppRequestService.sendHeartbeat(); }, this.getHeartbeatInterval()); - logger.info(this.logPrefix() + ' Heartbeat started every ' + Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval())); + logger.info(this.logPrefix() + ' Heartbeat started every ' + Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())); } else if (this.heartbeatSetInterval) { - logger.info(this.logPrefix() + ' Heartbeat already started every ' + Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval())); + logger.info(this.logPrefix() + ' Heartbeat already started every ' + Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())); } else { - logger.error(`${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval() ? Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval()) : this.getHeartbeatInterval()}, not starting the heartbeat`); + logger.error(`${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval() ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval()) : this.getHeartbeatInterval()}, not starting the heartbeat`); } } @@ -302,7 +302,7 @@ export default class ChargingStation { await this.ocppRequestService.sendMeterValues(connectorId, this.getConnector(connectorId).transactionId, interval); }, interval); } else { - logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${interval ? Utils.milliSecondsToHHMMSS(interval) : interval}, not sending MeterValues`); + logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${interval ? Utils.formatDurationMilliSeconds(interval) : interval}, not sending MeterValues`); } } @@ -902,11 +902,11 @@ export default class ChargingStation { this.wsConnection.ping((): void => { /* This is intentional */ }); } }, webSocketPingInterval * 1000); - logger.info(this.logPrefix() + ' WebSocket ping started every ' + Utils.secondsToHHMMSS(webSocketPingInterval)); + logger.info(this.logPrefix() + ' WebSocket ping started every ' + Utils.formatDurationSeconds(webSocketPingInterval)); } else if (this.webSocketPingSetInterval) { - logger.info(this.logPrefix() + ' WebSocket ping every ' + Utils.secondsToHHMMSS(webSocketPingInterval) + ' already started'); + logger.info(this.logPrefix() + ' WebSocket ping every ' + Utils.formatDurationSeconds(webSocketPingInterval) + ' already started'); } else { - logger.error(`${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval ? Utils.secondsToHHMMSS(webSocketPingInterval) : webSocketPingInterval}, not starting the WebSocket ping`); + logger.error(`${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval ? Utils.formatDurationSeconds(webSocketPingInterval) : webSocketPingInterval}, not starting the WebSocket ping`); } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index fa6cd8f7..a18aa5ad 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -59,7 +59,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer await Utils.sleep(this.chargingStation.stationInfo.resetTime); this.chargingStation.start(); }); - logger.info(`${this.chargingStation.logPrefix()} ${commandPayload.type} reset command received, simulating it. The station will be back online in ${Utils.milliSecondsToHHMMSS(this.chargingStation.stationInfo.resetTime)}`); + logger.info(`${this.chargingStation.logPrefix()} ${commandPayload.type} reset command received, simulating it. The station will be back online in ${Utils.formatDurationMilliSeconds(this.chargingStation.stationInfo.resetTime)}`); return Constants.OCPP_RESPONSE_ACCEPTED; } diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index ff791336..9762a3a0 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -114,7 +114,7 @@ export default class PerformanceStatistics { this.displayInterval = setInterval(() => { this.logStatistics(); }, Configuration.getLogStatisticsInterval() * 1000); - logger.info(this.logPrefix() + ' logged every ' + Utils.secondsToHHMMSS(Configuration.getLogStatisticsInterval())); + logger.info(this.logPrefix() + ' logged every ' + Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval())); } else { logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics'); } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index a83fd4d5..e09eeebd 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -17,12 +17,29 @@ export default class Utils { return new Promise((resolve) => setTimeout(resolve, milliSeconds)); } - public static secondsToHHMMSS(seconds: number): string { - return Utils.milliSecondsToHHMMSS(seconds * 1000); + public static formatDurationMilliSeconds(duration: number): string { + duration = Utils.convertToInt(duration); + const hours = Math.floor(duration / (3600 * 1000)); + const minutes = Math.floor((duration - (hours * (3600 * 1000))) / (60 * 1000)); + const seconds = (duration - (hours * 3600 * 1000) - (minutes * 60 * 1000)) / 1000; + let hoursStr: string = hours.toString(); + let minutesStr: string = minutes.toString(); + let secondsStr: string = seconds.toString(); + + if (hours < 10) { + hoursStr = '0' + hours.toString(); + } + if (minutes < 10) { + minutesStr = '0' + minutes.toString(); + } + if (seconds < 10) { + secondsStr = '0' + seconds.toString(); + } + return hoursStr + ':' + minutesStr + ':' + secondsStr; } - public static milliSecondsToHHMMSS(milliSeconds: number): string { - return new Date(milliSeconds).toISOString().substr(11, 8); + public static formatDurationSeconds(duration: number): string { + return Utils.formatDurationMilliSeconds(duration * 1000); } public static removeExtraEmptyLines(tab: string[]): void { -- 2.34.1