refactor: factor out charging profiles preparation
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 1 Aug 2023 17:37:56 +0000 (19:37 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 1 Aug 2023 17:37:56 +0000 (19:37 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Helpers.ts
src/charging-station/index.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index 05773f0159f3f1378dd78af4cf2a46ac3395b373..1632f28b13514f18798ebf4992d5af23360922c2 100644 (file)
@@ -733,7 +733,7 @@ const getLimitFromChargingProfiles = (
 ): ChargingProfilesLimit | undefined => {
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
   const currentDate = new Date();
-  const connectorStatus = chargingStation.getConnectorStatus(connectorId);
+  const connectorStatus = chargingStation.getConnectorStatus(connectorId)!;
   if (!isArraySorted(chargingProfiles, (a, b) => b.stackLevel - a.stackLevel)) {
     logger.warn(
       `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profiles are not sorted by stack level. Trying to sort them`,
@@ -755,17 +755,8 @@ const getLimitFromChargingProfiles = (
       );
       chargingSchedule.startSchedule = convertToDate(chargingSchedule?.startSchedule)!;
     }
-    switch (chargingProfile.chargingProfileKind) {
-      case ChargingProfileKindType.RECURRING:
-        if (!canProceedRecurringChargingProfile(chargingProfile, logPrefix)) {
-          continue;
-        }
-        prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix);
-        break;
-      case ChargingProfileKindType.RELATIVE:
-        connectorStatus?.transactionStarted &&
-          (chargingSchedule.startSchedule = connectorStatus?.transactionStart);
-        break;
+    if (!prepareChargingProfileKind(connectorStatus, chargingProfile, currentDate, logPrefix)) {
+      continue;
     }
     if (!canProceedChargingProfile(chargingProfile, currentDate, logPrefix)) {
       continue;
@@ -859,6 +850,27 @@ const getLimitFromChargingProfiles = (
   }
 };
 
+export const prepareChargingProfileKind = (
+  connectorStatus: ConnectorStatus,
+  chargingProfile: ChargingProfile,
+  currentDate: Date,
+  logPrefix: string,
+): boolean => {
+  switch (chargingProfile.chargingProfileKind) {
+    case ChargingProfileKindType.RECURRING:
+      if (!canProceedRecurringChargingProfile(chargingProfile, logPrefix)) {
+        return false;
+      }
+      prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix);
+      break;
+    case ChargingProfileKindType.RELATIVE:
+      connectorStatus?.transactionStarted &&
+        (chargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart);
+      break;
+  }
+  return true;
+};
+
 export const canProceedChargingProfile = (
   chargingProfile: ChargingProfile,
   currentDate: Date,
@@ -891,7 +903,7 @@ export const canProceedChargingProfile = (
   return true;
 };
 
-export const canProceedRecurringChargingProfile = (
+const canProceedRecurringChargingProfile = (
   chargingProfile: ChargingProfile,
   logPrefix: string,
 ): boolean => {
@@ -914,7 +926,7 @@ export const canProceedRecurringChargingProfile = (
  * @param currentDate -
  * @param logPrefix -
  */
-export const prepareRecurringChargingProfile = (
+const prepareRecurringChargingProfile = (
   chargingProfile: ChargingProfile,
   currentDate: Date,
   logPrefix: string,
index 07dbe9b6cd3ae3cf53f1678f66deca8759fb7cb0..3eccd2581deb7fbc23c1fce1ce9a54856b7a7193 100644 (file)
@@ -7,12 +7,11 @@ export {
 } from './ConfigurationKeyUtils';
 export {
   canProceedChargingProfile,
-  canProceedRecurringChargingProfile,
   checkChargingStation,
   getIdTagsFile,
   hasFeatureProfile,
   hasReservationExpired,
-  prepareRecurringChargingProfile,
+  prepareChargingProfileKind,
   removeExpiredReservations,
   resetConnectorStatus,
 } from './Helpers';
index e3375d192d1615648f4c0910fcc19aa5a7c84f60..fbb76bec08790bb50126d4fb3557e3a8a7a194c9 100644 (file)
@@ -21,10 +21,9 @@ import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import {
   type ChargingStation,
   canProceedChargingProfile,
-  canProceedRecurringChargingProfile,
   checkChargingStation,
   getConfigurationKey,
-  prepareRecurringChargingProfile,
+  prepareChargingProfileKind,
   removeExpiredReservations,
   setConfigurationKeyValue,
 } from '../../../charging-station';
@@ -32,7 +31,6 @@ import { OCPPError } from '../../../exception';
 import {
   type ChangeConfigurationRequest,
   type ChangeConfigurationResponse,
-  ChargingProfileKindType,
   ChargingRateUnitType,
   type ClearChargingProfileRequest,
   type ClearChargingProfileResponse,
@@ -687,7 +685,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       );
       return OCPP16Constants.OCPP_RESPONSE_REJECTED;
     }
-    const connectorStatus = chargingStation.getConnectorStatus(connectorId);
+    const connectorStatus = chargingStation.getConnectorStatus(connectorId)!;
     if (
       isEmptyArray(
         connectorStatus?.chargingProfiles &&
@@ -729,21 +727,15 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           chargingProfile.chargingSchedule?.startSchedule,
         )!;
       }
-      switch (chargingProfile.chargingProfileKind) {
-        case ChargingProfileKindType.RECURRING:
-          if (!canProceedRecurringChargingProfile(chargingProfile, chargingStation.logPrefix())) {
-            continue;
-          }
-          prepareRecurringChargingProfile(
-            chargingProfile,
-            interval.start as Date,
-            chargingStation.logPrefix(),
-          );
-          break;
-        case ChargingProfileKindType.RELATIVE:
-          connectorStatus?.transactionStarted &&
-            (chargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart);
-          break;
+      if (
+        !prepareChargingProfileKind(
+          connectorStatus,
+          chargingProfile,
+          interval.start as Date,
+          chargingStation.logPrefix(),
+        )
+      ) {
+        continue;
       }
       if (
         !canProceedChargingProfile(