fix: avoid overlapping schedule periods in get composite schedule
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index f4c0e6ad657423ab86e563907850f5a9a6276813..1941b665f00a5f7da58756c8186850abbb24b5ec 100644 (file)
@@ -8,10 +8,15 @@ import type { JSONSchemaType } from 'ajv';
 import { Client, type FTPResponse } from 'basic-ftp';
 import {
   addSeconds,
+  differenceInSeconds,
+  isAfter,
+  isBefore,
   isDate,
   isWithinInterval,
+  max,
   maxTime,
   min,
+  minTime,
   secondsToMilliseconds,
 } from 'date-fns';
 import { create } from 'tar';
@@ -681,10 +686,9 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       return OCPP16Constants.OCPP_RESPONSE_REJECTED;
     }
     if (chargingRateUnit) {
-      logger.error(
-        `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported`,
+      logger.warn(
+        `${chargingStation.logPrefix()} Get composite schedule with a specified rate unit is not yet supported, no conversion will be done`,
       );
-      return OCPP16Constants.OCPP_RESPONSE_REJECTED;
     }
     const connectorStatus = chargingStation.getConnectorStatus(connectorId)!;
     if (
@@ -700,38 +704,39 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       start: currentDate,
       end: addSeconds(currentDate, duration),
     };
-    const chargingProfiles: OCPP16ChargingProfile[] = [];
-    for (const chargingProfile of cloneObject<OCPP16ChargingProfile[]>(
+    const storedChargingProfiles: OCPP16ChargingProfile[] = cloneObject<OCPP16ChargingProfile[]>(
       (connectorStatus?.chargingProfiles ?? []).concat(
         chargingStation.getConnectorStatus(0)?.chargingProfiles ?? [],
       ),
-    ).sort((a, b) => b.stackLevel - a.stackLevel)) {
+    ).sort((a, b) => b.stackLevel - a.stackLevel);
+    const chargingProfiles: OCPP16ChargingProfile[] = [];
+    for (const storedChargingProfile of storedChargingProfiles) {
       if (
         connectorStatus?.transactionStarted &&
-        isNullOrUndefined(chargingProfile.chargingSchedule?.startSchedule)
+        isNullOrUndefined(storedChargingProfile.chargingSchedule?.startSchedule)
       ) {
         logger.debug(
           `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
-            chargingProfile.chargingProfileId
+            storedChargingProfile.chargingProfileId
           } has no startSchedule defined. Trying to set it to the connector current transaction start date`,
         );
         // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
-        chargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart;
+        storedChargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart;
       }
-      if (!isDate(chargingProfile.chargingSchedule?.startSchedule)) {
+      if (!isDate(storedChargingProfile.chargingSchedule?.startSchedule)) {
         logger.warn(
           `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
-            chargingProfile.chargingProfileId
+            storedChargingProfile.chargingProfileId
           } startSchedule property is not a Date object. Trying to convert it to a Date object`,
         );
-        chargingProfile.chargingSchedule.startSchedule = convertToDate(
-          chargingProfile.chargingSchedule?.startSchedule,
+        storedChargingProfile.chargingSchedule.startSchedule = convertToDate(
+          storedChargingProfile.chargingSchedule?.startSchedule,
         )!;
       }
       if (
         !prepareChargingProfileKind(
           connectorStatus,
-          chargingProfile,
+          storedChargingProfile,
           interval.start as Date,
           chargingStation.logPrefix(),
         )
@@ -740,7 +745,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       }
       if (
         !canProceedChargingProfile(
-          chargingProfile,
+          storedChargingProfile,
           interval.start as Date,
           chargingStation.logPrefix(),
         )
@@ -749,10 +754,108 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       }
       // Add active charging profiles into chargingProfiles array
       if (
-        isValidTime(chargingProfile.chargingSchedule?.startSchedule) &&
-        isWithinInterval(chargingProfile.chargingSchedule.startSchedule!, interval)
+        isValidTime(storedChargingProfile.chargingSchedule?.startSchedule) &&
+        isWithinInterval(storedChargingProfile.chargingSchedule.startSchedule!, interval)
       ) {
-        chargingProfiles.push(chargingProfile);
+        if (isEmptyArray(chargingProfiles)) {
+          if (
+            isAfter(
+              addSeconds(
+                storedChargingProfile.chargingSchedule.startSchedule!,
+                storedChargingProfile.chargingSchedule.duration!,
+              ),
+              interval.end,
+            )
+          ) {
+            storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
+              storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
+                (schedulePeriod) =>
+                  isWithinInterval(
+                    addSeconds(
+                      storedChargingProfile.chargingSchedule.startSchedule!,
+                      schedulePeriod.startPeriod,
+                    )!,
+                    interval,
+                  ),
+              );
+            storedChargingProfile.chargingSchedule.duration = differenceInSeconds(
+              interval.end,
+              storedChargingProfile.chargingSchedule.startSchedule!,
+            );
+          }
+          chargingProfiles.push(storedChargingProfile);
+        } else if (isNotEmptyArray(chargingProfiles)) {
+          const chargingProfilesInterval: Interval = {
+            start: min(
+              chargingProfiles.map(
+                (chargingProfile) => chargingProfile.chargingSchedule.startSchedule ?? maxTime,
+              ),
+            ),
+            end: max(
+              chargingProfiles.map(
+                (chargingProfile) =>
+                  addSeconds(
+                    chargingProfile.chargingSchedule.startSchedule!,
+                    chargingProfile.chargingSchedule.duration!,
+                  ) ?? minTime,
+              ),
+            ),
+          };
+          let addChargingProfile = false;
+          if (
+            isBefore(interval.start, chargingProfilesInterval.start) &&
+            isBefore(
+              storedChargingProfile.chargingSchedule.startSchedule!,
+              chargingProfilesInterval.start,
+            )
+          ) {
+            // Remove charging schedule periods that are after the start of the active profiles interval
+            storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
+              storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
+                (schedulePeriod) =>
+                  isWithinInterval(
+                    addSeconds(
+                      storedChargingProfile.chargingSchedule.startSchedule!,
+                      schedulePeriod.startPeriod,
+                    ),
+                    {
+                      start: interval.start,
+                      end: chargingProfilesInterval.start,
+                    },
+                  ),
+              );
+            addChargingProfile = true;
+          }
+          if (
+            isBefore(chargingProfilesInterval.end, interval.end) &&
+            isAfter(
+              addSeconds(
+                storedChargingProfile.chargingSchedule.startSchedule!,
+                storedChargingProfile.chargingSchedule.duration!,
+              ),
+              chargingProfilesInterval.end,
+            )
+          ) {
+            // Remove charging schedule periods that are before the end of the active profiles interval
+            // FIXME: can lead to a gap in the charging schedule: chargingProfilesInterval.end -> first matching schedulePeriod.startPeriod
+            storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
+              storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
+                (schedulePeriod) =>
+                  isWithinInterval(
+                    addSeconds(
+                      storedChargingProfile.chargingSchedule.startSchedule!,
+                      schedulePeriod.startPeriod,
+                    ),
+                    {
+                      start: chargingProfilesInterval.end,
+                      end: interval.end,
+                    },
+                  ),
+              );
+            addChargingProfile = true;
+          }
+          addChargingProfile && chargingProfiles.push(storedChargingProfile);
+        }
       }
     }
     const compositeScheduleStart: Date = min(
@@ -761,11 +864,12 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       ),
     );
     const compositeScheduleDuration: number = Math.max(
-      ...chargingProfiles.map(
-        (chargingProfile) => chargingProfile.chargingSchedule.duration ?? -Infinity,
+      ...chargingProfiles.map((chargingProfile) =>
+        isNaN(chargingProfile.chargingSchedule.duration!)
+          ? -Infinity
+          : chargingProfile.chargingSchedule.duration!,
       ),
     );
-    // FIXME: remove overlapping charging schedule periods
     const compositeSchedulePeriods: OCPP16ChargingSchedulePeriod[] = chargingProfiles
       .map((chargingProfile) => chargingProfile.chargingSchedule.chargingSchedulePeriod)
       .reduce(
@@ -789,8 +893,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         : OCPP16ChargingRateUnitType.AMPERE,
       chargingSchedulePeriod: compositeSchedulePeriods,
       minChargeRate: Math.min(
-        ...chargingProfiles.map(
-          (chargingProfile) => chargingProfile.chargingSchedule.minChargeRate ?? Infinity,
+        ...chargingProfiles.map((chargingProfile) =>
+          isNaN(chargingProfile.chargingSchedule.minChargeRate!)
+            ? Infinity
+            : chargingProfile.chargingSchedule.minChargeRate!,
         ),
       ),
     };