From: Jérôme Benoit Date: Fri, 12 Jan 2024 21:30:19 +0000 (+0100) Subject: fix: ensure dates in ISO string format are properly converted to Date X-Git-Tag: v1.2.32~31 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=95dab6cfb3e2de1f775f0a8648bccbc62c4a9214;p=e-mobility-charging-stations-simulator.git fix: ensure dates in ISO string format are properly converted to Date Signed-off-by: Jérôme Benoit --- 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)) {