refactor: cleanup get composite schedule code
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 1 Aug 2023 00:20:33 +0000 (02:20 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 1 Aug 2023 00:20:33 +0000 (02:20 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index 01d1211dba709b99bbf31dda56f767d956ac4552..81f7540f74cf382c07482347a23adfead9a117a6 100644 (file)
@@ -677,7 +677,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       return OCPP16Constants.OCPP_RESPONSE_REJECTED;
     }
     const startDate = new Date();
-    const endDate = addSeconds(startDate, duration);
+    const interval: Interval = {
+      start: startDate,
+      end: addSeconds(startDate, duration),
+    };
     let compositeSchedule: OCPP16ChargingSchedule | undefined;
     for (const chargingProfile of chargingStation.getConnectorStatus(connectorId)!
       .chargingProfiles!) {
@@ -693,24 +696,18 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         continue;
       }
       if (
-        isWithinInterval(chargingProfile.chargingSchedule.startSchedule!, {
-          start: startDate,
-          end: endDate,
-        }) &&
+        isWithinInterval(chargingProfile.chargingSchedule.startSchedule!, interval) &&
         isWithinInterval(
           addSeconds(
             chargingProfile.chargingSchedule.startSchedule!,
             chargingProfile.chargingSchedule.duration!,
           ),
-          {
-            start: startDate,
-            end: endDate,
-          },
+          interval,
         )
       ) {
         compositeSchedule = {
           startSchedule: max([
-            compositeSchedule?.startSchedule ?? startDate,
+            compositeSchedule?.startSchedule ?? interval.start,
             chargingProfile.chargingSchedule.startSchedule!,
           ]),
           duration: Math.max(