fix: ensure dates in ISO string format are properly converted to Date
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 12 Jan 2024 21:30:19 +0000 (22:30 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 12 Jan 2024 21:30:19 +0000 (22:30 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/utils/Utils.ts

index 9efe7b3087fac1a46136255fe050e88966ba00a0..92fbb80c6e21bde391c74ffccdd52100a38bd26a 100644 (file)
@@ -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(
index 9d661dc605b4d5965e70e772cda2d494ee751d56..6937f2bde3fe335b40f01b5b651a0a5149d9fac1 100644 (file)
@@ -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 {
index 1f119a2e4753fb476bb6ee1ddf8e13e5e96cfd8c..51cbbd168699708acc6903565f9698bd42863a30 100644 (file)
@@ -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
index df802f3ebe98530c5a3568f36dfc2adde28a74c0..e8c52603fbc238ce2d7f5278844ed662a93ea30c 100644 (file)
@@ -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)) {