fix: avoid gaps in get composite schedule
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 6912f765742efd86b9970066e92240aa45cc5b9c..d24a2d4cd36e2716ab35b375af7b433017e4a9f3 100644 (file)
@@ -8,6 +8,7 @@ import type { JSONSchemaType } from 'ajv';
 import { Client, type FTPResponse } from 'basic-ftp';
 import {
   addSeconds,
+  differenceInSeconds,
   isAfter,
   isBefore,
   isDate,
@@ -27,6 +28,7 @@ import {
   canProceedChargingProfile,
   checkChargingStation,
   getConfigurationKey,
+  getConnectorChargingProfiles,
   prepareChargingProfileKind,
   removeExpiredReservations,
   setConfigurationKeyValue,
@@ -104,7 +106,6 @@ import {
 } from '../../../types';
 import {
   Constants,
-  cloneObject,
   convertToDate,
   convertToInt,
   formatDurationMilliSeconds,
@@ -703,16 +704,16 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       start: currentDate,
       end: addSeconds(currentDate, duration),
     };
-    const storedChargingProfiles: OCPP16ChargingProfile[] = cloneObject<OCPP16ChargingProfile[]>(
-      (connectorStatus?.chargingProfiles ?? []).concat(
-        chargingStation.getConnectorStatus(0)?.chargingProfiles ?? [],
-      ),
-    ).sort((a, b) => b.stackLevel - a.stackLevel);
+    // Get charging profiles sorted by connector id then stack level
+    const storedChargingProfiles: OCPP16ChargingProfile[] = getConnectorChargingProfiles(
+      chargingStation,
+      connectorId,
+    );
     const chargingProfiles: OCPP16ChargingProfile[] = [];
     for (const storedChargingProfile of storedChargingProfiles) {
       if (
-        connectorStatus?.transactionStarted &&
-        isNullOrUndefined(storedChargingProfile.chargingSchedule?.startSchedule)
+        isNullOrUndefined(storedChargingProfile.chargingSchedule?.startSchedule) &&
+        connectorStatus?.transactionStarted
       ) {
         logger.debug(
           `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
@@ -722,6 +723,21 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
         storedChargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart;
       }
+      if (
+        !isNullOrUndefined(storedChargingProfile.chargingSchedule?.startSchedule) &&
+        isNullOrUndefined(storedChargingProfile.chargingSchedule?.duration)
+      ) {
+        logger.debug(
+          `${chargingStation.logPrefix()} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${
+            storedChargingProfile.chargingProfileId
+          } has no duration defined and will be set to the maximum time allowed`,
+        );
+        // OCPP specifies that if duration is not defined, it should be infinite
+        storedChargingProfile.chargingSchedule.duration = differenceInSeconds(
+          maxTime,
+          storedChargingProfile.chargingSchedule.startSchedule!,
+        );
+      }
       if (!isDate(storedChargingProfile.chargingSchedule?.startSchedule)) {
         logger.warn(
           `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetCompositeSchedule: Charging profile id ${
@@ -754,31 +770,139 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       // Add active charging profiles into chargingProfiles array
       if (
         isValidTime(storedChargingProfile.chargingSchedule?.startSchedule) &&
-        isWithinInterval(storedChargingProfile.chargingSchedule.startSchedule!, interval) &&
-        (isEmptyArray(chargingProfiles) ||
-          (isNotEmptyArray(chargingProfiles) &&
-            (isBefore(
+        isWithinInterval(storedChargingProfile.chargingSchedule.startSchedule!, interval)
+      ) {
+        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!,
-              min(
-                chargingProfiles.map(
-                  (chargingProfile) => chargingProfile.chargingSchedule.startSchedule ?? maxTime,
-                ),
+            );
+          }
+          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,
               ),
-            ) ||
-              isAfter(
+            ),
+          };
+          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!,
-                max(
-                  chargingProfiles.map(
-                    (chargingProfile) =>
+                storedChargingProfile.chargingSchedule.duration!,
+              ),
+              chargingProfilesInterval.end,
+            )
+          ) {
+            // Remove charging schedule periods that are before the end of the active profiles interval
+            storedChargingProfile.chargingSchedule.chargingSchedulePeriod =
+              storedChargingProfile.chargingSchedule.chargingSchedulePeriod.filter(
+                (schedulePeriod, index) => {
+                  if (
+                    isWithinInterval(
                       addSeconds(
-                        chargingProfile.chargingSchedule.startSchedule!,
-                        chargingProfile.chargingSchedule.duration!,
-                      ) ?? minTime,
-                  ),
-                ),
-              ))))
-      ) {
-        chargingProfiles.push(storedChargingProfile);
+                        storedChargingProfile.chargingSchedule.startSchedule!,
+                        schedulePeriod.startPeriod,
+                      ),
+                      {
+                        start: chargingProfilesInterval.end,
+                        end: interval.end,
+                      },
+                    )
+                  ) {
+                    return true;
+                  }
+                  if (
+                    !isWithinInterval(
+                      addSeconds(
+                        storedChargingProfile.chargingSchedule.startSchedule!,
+                        schedulePeriod.startPeriod,
+                      ),
+                      {
+                        start: chargingProfilesInterval.end,
+                        end: interval.end,
+                      },
+                    ) &&
+                    index <
+                      storedChargingProfile.chargingSchedule.chargingSchedulePeriod.length - 1 &&
+                    isWithinInterval(
+                      addSeconds(
+                        storedChargingProfile.chargingSchedule.startSchedule!,
+                        storedChargingProfile.chargingSchedule.chargingSchedulePeriod[index + 1]
+                          .startPeriod,
+                      ),
+                      {
+                        start: chargingProfilesInterval.end,
+                        end: interval.end,
+                      },
+                    )
+                  ) {
+                    return true;
+                  }
+                  return false;
+                },
+              );
+            addChargingProfile = true;
+          }
+          addChargingProfile && chargingProfiles.push(storedChargingProfile);
+        }
       }
     }
     const compositeScheduleStart: Date = min(
@@ -787,11 +911,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(
@@ -815,8 +940,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!,
         ),
       ),
     };