fix: fix ATG connector statuses date handling
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 8 Jan 2024 11:48:33 +0000 (12:48 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 8 Jan 2024 11:48:33 +0000 (12:48 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Helpers.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index d056d8ec866ee1c948740e24ca03ed2ae4f525d4..3b27f247d3419f38756582f42ba9b83248049f9d 100644 (file)
@@ -20,6 +20,7 @@ import {
 import {
   Constants,
   cloneObject,
+  convertToDate,
   formatDurationMilliSeconds,
   getRandomInteger,
   isValidTime,
@@ -261,7 +262,11 @@ export class AutomaticTransactionGenerator {
   private setStartConnectorStatus (connectorId: number): void {
     const previousRunDuration =
       isValidTime(this.connectorsStatus.get(connectorId)?.startDate) &&
-      isValidTime(this.connectorsStatus.get(connectorId)?.lastRunDate)
+      isValidTime(this.connectorsStatus.get(connectorId)?.lastRunDate) &&
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      this.connectorsStatus.get(connectorId)!.lastRunDate! >
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        this.connectorsStatus.get(connectorId)!.startDate!
         ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         this.connectorsStatus.get(connectorId)!.lastRunDate!.getTime() -
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -379,7 +384,23 @@ export class AutomaticTransactionGenerator {
           this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
         )
         : undefined
-    this.resetConnectorStatus(connectorStatus)
+    if (connectorStatus != null) {
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      connectorStatus.startDate = convertToDate(connectorStatus.startDate)!
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      connectorStatus.lastRunDate = convertToDate(connectorStatus.lastRunDate)!
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      connectorStatus.stopDate = convertToDate(connectorStatus.stopDate)!
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      connectorStatus.stoppedDate = convertToDate(connectorStatus.stoppedDate)!
+      if (
+        !this.started &&
+        (connectorStatus.start ||
+          this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.enable !== true)
+      ) {
+        connectorStatus.start = false
+      }
+    }
     return (
       connectorStatus ?? {
         start: false,
@@ -398,19 +419,6 @@ export class AutomaticTransactionGenerator {
     )
   }
 
-  private resetConnectorStatus (connectorStatus: Status | undefined): void {
-    if (connectorStatus == null) {
-      return
-    }
-    if (
-      !this.started &&
-      (connectorStatus.start ||
-        this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.enable !== true)
-    ) {
-      connectorStatus.start = false
-    }
-  }
-
   private async startTransaction (
     connectorId: number
   ): Promise<StartTransactionResponse | undefined> {
index ab3a6f6a6dd7a25c08ee4cf80e5f6232a37d3a8a..d8ef4a328e1c5a4fdef6d09acf1da05294f9cfc8 100644 (file)
@@ -917,7 +917,7 @@ const getLimitFromChargingProfiles = (
 export const prepareChargingProfileKind = (
   connectorStatus: ConnectorStatus,
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   switch (chargingProfile.chargingProfileKind) {
@@ -945,7 +945,7 @@ export const prepareChargingProfileKind = (
 
 export const canProceedChargingProfile = (
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   if (
@@ -957,7 +957,9 @@ export const canProceedChargingProfile = (
     logger.debug(
       `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${
         chargingProfile.chargingProfileId
-      } is not valid for the current date ${currentDate.toISOString()}`
+      } is not valid for the current date ${
+        currentDate instanceof Date ? currentDate.toISOString() : currentDate
+      }`
     )
     return false
   }
@@ -1019,7 +1021,7 @@ const canProceedRecurringChargingProfile = (
  */
 const prepareRecurringChargingProfile = (
   chargingProfile: ChargingProfile,
-  currentDate: Date,
+  currentDate: string | number | Date,
   logPrefix: string
 ): boolean => {
   const chargingSchedule = chargingProfile.chargingSchedule
@@ -1088,7 +1090,9 @@ const prepareRecurringChargingProfile = (
       ).toISOString()}, ${toDate(
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         recurringInterval!.end
-      ).toISOString()}] has not been properly translated to current date ${currentDate.toISOString()} `
+      ).toISOString()}] has not been properly translated to current date ${
+        currentDate instanceof Date ? currentDate.toISOString() : currentDate
+      } `
     )
   }
   return recurringIntervalTranslated
index e0785d43e9eb265b3af00d5dcf8f6c3d1cd60fb5..64d836d5f4ce6a46995f0879dab0d7afb4c31b95 100644 (file)
@@ -748,7 +748,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         !prepareChargingProfileKind(
           connectorStatus,
           chargingProfile,
-          compositeScheduleInterval.start as Date,
+          compositeScheduleInterval.start,
           chargingStation.logPrefix()
         )
       ) {
@@ -757,7 +757,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       if (
         !canProceedChargingProfile(
           chargingProfile,
-          compositeScheduleInterval.start as Date,
+          compositeScheduleInterval.start,
           chargingStation.logPrefix()
         )
       ) {