feat: add support charging profile validity interval
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 10:33:50 +0000 (12:33 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 10:33:50 +0000 (12:33 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts

index 81096ef1eb3d3eafd45fe526b320461f39c08fc1..8d595b22acae2be8e7d0a4fe0f19edd243f884ad 100644 (file)
@@ -14,6 +14,7 @@ import {
   endOfWeek,
   isAfter,
   isBefore,
+  isWithinInterval,
   startOfDay,
   startOfWeek,
 } from 'date-fns';
@@ -468,12 +469,12 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
   connectorId: number,
 ): number | undefined => {
   let limit: number | undefined, matchingChargingProfile: ChargingProfile | undefined;
-  // Get charging profiles for connector and sort by stack level
+  // Get charging profiles for connector id and sort by stack level
   const chargingProfiles =
     cloneObject<ChargingProfile[]>(
       chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!,
     )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? [];
-  // Get profiles on connector 0
+  // Get charging profiles on connector 0 and sort by stack level
   if (chargingStation.getConnectorStatus(0)?.chargingProfiles) {
     chargingProfiles.push(
       ...cloneObject<ChargingProfile[]>(
@@ -686,7 +687,21 @@ const getLimitFromChargingProfiles = (
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
   const currentDate = new Date();
   for (const chargingProfile of chargingProfiles) {
-    // Set helpers
+    if (
+      chargingProfile.validFrom &&
+      chargingProfile.validTo &&
+      !isWithinInterval(currentDate, {
+        start: chargingProfile.validFrom,
+        end: chargingProfile.validTo,
+      })
+    ) {
+      logger.debug(
+        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${
+          chargingProfile.chargingProfileId
+        } is not valid for the current date ${currentDate.toISOString()}`,
+      );
+      continue;
+    }
     const chargingSchedule = chargingProfile.chargingSchedule;
     if (!chargingSchedule?.startSchedule) {
       logger.debug(
@@ -702,6 +717,15 @@ const getLimitFromChargingProfiles = (
       );
       chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
     }
+    if (
+      chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
+      isNullOrUndefined(chargingProfile.recurrencyKind)
+    ) {
+      logger.error(
+        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`,
+      );
+      continue;
+    }
     // Adjust recurring start schedule
     if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) {
       switch (chargingProfile.recurrencyKind) {
@@ -754,51 +778,53 @@ const getLimitFromChargingProfiles = (
     if (
       isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate)
     ) {
-      let lastButOneSchedule: ChargingSchedulePeriod | undefined;
-      // Search the right schedule period
-      for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
+      if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
         // Handling of only one period
         if (
           chargingSchedule.chargingSchedulePeriod.length === 1 &&
-          schedulePeriod.startPeriod === 0
+          chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0
         ) {
-          const result = {
-            limit: schedulePeriod.limit,
+          const result: ChargingProfilesLimit = {
+            limit: chargingSchedule.chargingSchedulePeriod[0].limit,
             matchingChargingProfile: chargingProfile,
           };
           logger.debug(debugLogMsg, result);
           return result;
         }
-        // Find the right schedule period
-        if (
-          isAfter(
-            addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod),
-            currentDate,
-          )
-        ) {
-          // Found the schedule: last but one is the correct one
-          const result = {
-            limit: lastButOneSchedule!.limit,
-            matchingChargingProfile: chargingProfile,
-          };
-          logger.debug(debugLogMsg, result);
-          return result;
-        }
-        // Keep it
-        lastButOneSchedule = schedulePeriod;
-        // Handle the last schedule period
-        if (
-          schedulePeriod.startPeriod ===
-          chargingSchedule.chargingSchedulePeriod[
-            chargingSchedule.chargingSchedulePeriod.length - 1
-          ].startPeriod
-        ) {
-          const result = {
-            limit: lastButOneSchedule.limit,
-            matchingChargingProfile: chargingProfile,
-          };
-          logger.debug(debugLogMsg, result);
-          return result;
+        let lastButOneSchedule: ChargingSchedulePeriod | undefined;
+        // Search for the right schedule period
+        for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
+          // Find the right schedule period
+          if (
+            isAfter(
+              addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod),
+              currentDate,
+            )
+          ) {
+            // Found the schedule: last but one is the correct one
+            const result: ChargingProfilesLimit = {
+              limit: lastButOneSchedule!.limit,
+              matchingChargingProfile: chargingProfile,
+            };
+            logger.debug(debugLogMsg, result);
+            return result;
+          }
+          // Keep it
+          lastButOneSchedule = schedulePeriod;
+          // Handle the last schedule period
+          if (
+            schedulePeriod.startPeriod ===
+            chargingSchedule.chargingSchedulePeriod[
+              chargingSchedule.chargingSchedulePeriod.length - 1
+            ].startPeriod
+          ) {
+            const result: ChargingProfilesLimit = {
+              limit: lastButOneSchedule.limit,
+              matchingChargingProfile: chargingProfile,
+            };
+            logger.debug(debugLogMsg, result);
+            return result;
+          }
         }
       }
     }