From 95dab6cfb3e2de1f775f0a8648bccbc62c4a9214 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 12 Jan 2024 22:30:19 +0100 Subject: [PATCH] fix: ensure dates in ISO string format are properly converted to Date 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 | 5 +++++ .../ocpp/1.6/OCPP16IncomingRequestService.ts | 8 +++++--- src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts | 6 +++++- src/utils/Utils.ts | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9efe7b30..92fbb80c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -134,6 +134,7 @@ import { buildUpdatedMessage, cloneObject, convertToBoolean, + convertToDate, convertToInt, exponentialDelay, formatDurationMilliSeconds, @@ -1776,6 +1777,10 @@ export class ChargingStation extends EventEmitter { >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { skipBufferingOnError: true }) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.bootNotificationResponse.currentTime = convertToDate( + this.bootNotificationResponse.currentTime + )! if (!this.isRegistered()) { this.stationInfo?.registrationMaxRetries !== -1 && ++registrationRetryCount await sleep( diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 9d661dc6..6937f2bd 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -1099,15 +1099,15 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) return OCPP16Constants.OCPP_RESPONSE_EMPTY } - let { retrieveDate } = commandPayload + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + commandPayload.retrieveDate = convertToDate(commandPayload.retrieveDate)! + const { retrieveDate } = commandPayload if (chargingStation.stationInfo?.firmwareStatus !== OCPP16FirmwareStatus.Installed) { logger.warn( `${chargingStation.logPrefix()} ${moduleName}.handleRequestUpdateFirmware: Cannot simulate firmware update: firmware update is already in progress` ) return OCPP16Constants.OCPP_RESPONSE_EMPTY } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - retrieveDate = convertToDate(retrieveDate)! const now = Date.now() if (retrieveDate.getTime() <= now) { this.updateFirmwareSimulation(chargingStation).catch(Constants.EMPTY_FUNCTION) @@ -1548,6 +1548,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) { return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + commandPayload.expiryDate = convertToDate(commandPayload.expiryDate)! const { reservationId, idTag, connectorId } = commandPayload let response: OCPP16ReserveNowResponse try { diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 1f119a2e..51cbbd16 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -38,7 +38,7 @@ import { type OCPP16SupportedFeatureProfiles, OCPPVersion } from '../../../types/index.js' -import { isNotEmptyArray, logger, roundTo } from '../../../utils/index.js' +import { convertToDate, isNotEmptyArray, logger, roundTo } from '../../../utils/index.js' import { OCPPServiceUtils } from '../OCPPServiceUtils.js' export class OCPP16ServiceUtils extends OCPPServiceUtils { @@ -164,6 +164,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion chargingStation.getConnectorStatus(connectorId)!.chargingProfiles = [] } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + cp.validFrom = convertToDate(cp.validFrom)! + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + cp.validTo = convertToDate(cp.validTo)! let cpReplaced = false if (isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index df802f3e..e8c52603 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -66,7 +66,7 @@ export const formatDurationSeconds = (duration: number): string => { } // More efficient time validation function than the one provided by date-fns -export const isValidTime = (date: unknown): boolean => { +export const isValidTime = (date: Date | number | undefined): boolean => { if (typeof date === 'number') { return !isNaN(date) } else if (isDate(date)) { -- 2.34.1