refactor: factor out feature profile test
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 Jul 2023 15:23:19 +0000 (17:23 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 Jul 2023 15:23:19 +0000 (17:23 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/index.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 3be6dfd931712306226100fcea9d500b6e93c5f4..57fbac97588a100f0eabe3cccd1362794db838d4 100644 (file)
@@ -43,6 +43,7 @@ import {
   getIdTagsFile,
   getMaxNumberOfEvses,
   getPhaseRotationValue,
+  hasFeatureProfile,
   initializeConnectorsMapStatus,
   propagateSerialNumber,
   stationTemplateToStationInfo,
@@ -668,7 +669,7 @@ export class ChargingStation {
         if (this.getEnableStatistics() === true) {
           this.performanceStatistics?.start();
         }
-        if (this.hasFeatureProfile(SupportedFeatureProfiles.Reservation)) {
+        if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
           this.startReservationExpirationSetInterval();
         }
         this.openWSConnection();
@@ -731,7 +732,7 @@ export class ChargingStation {
         if (this.getEnableStatistics() === true) {
           this.performanceStatistics?.stop();
         }
-        if (this.hasFeatureProfile(SupportedFeatureProfiles.Reservation)) {
+        if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
           this.stopReservationExpirationSetInterval();
         }
         this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
@@ -763,13 +764,6 @@ export class ChargingStation {
     }
   }
 
-  public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined {
-    return getConfigurationKey(
-      this,
-      StandardParametersKey.SupportedFeatureProfiles,
-    )?.value?.includes(featureProfile);
-  }
-
   public bufferMessage(message: string): void {
     this.messageBuffer.add(message);
   }
@@ -973,8 +967,6 @@ export class ChargingStation {
     const connector = this.getConnectorStatus(reservation.connectorId)!;
     switch (reason) {
       case ReservationTerminationReason.CONNECTOR_STATE_CHANGED:
-        delete connector.reservation;
-        break;
       case ReservationTerminationReason.TRANSACTION_STARTED:
         delete connector.reservation;
         break;
index 6c2732460c8cbd84c388ce58958f2d8f7f231657..2be30929d9107c2c416498c1ce3cc6f632524b27 100644 (file)
@@ -19,6 +19,7 @@ import {
 } from 'date-fns';
 
 import type { ChargingStation } from './ChargingStation';
+import { getConfigurationKey } from './ChargingStationConfigurationUtils';
 import { BaseError } from '../exception';
 import {
   AmpereUnits,
@@ -41,6 +42,8 @@ import {
   type OCPP20BootNotificationRequest,
   OCPPVersion,
   RecurrencyKindType,
+  StandardParametersKey,
+  SupportedFeatureProfiles,
   Voltage,
 } from '../types';
 import {
@@ -449,6 +452,16 @@ export const propagateSerialNumber = (
     : stationInfoDst?.meterSerialNumber && delete stationInfoDst.meterSerialNumber;
 };
 
+export const hasFeatureProfile = (
+  chargingStation: ChargingStation,
+  featureProfile: SupportedFeatureProfiles,
+): boolean | undefined => {
+  return getConfigurationKey(
+    chargingStation,
+    StandardParametersKey.SupportedFeatureProfiles,
+  )?.value?.includes(featureProfile);
+};
+
 export const getAmperageLimitationUnitDivider = (stationInfo: ChargingStationInfo): number => {
   let unitDivider = 1;
   switch (stationInfo.amperageLimitationUnit) {
index 7e992fe2d1d91cd273325e4a63830b5691dffc82..2823c1e263b8f951d236a3860872f94385506f9e 100644 (file)
@@ -5,4 +5,9 @@ export {
   getConfigurationKey,
   setConfigurationKeyValue,
 } from './ChargingStationConfigurationUtils';
-export { getIdTagsFile, checkChargingStation, resetConnectorStatus } from './ChargingStationUtils';
+export {
+  getIdTagsFile,
+  checkChargingStation,
+  resetConnectorStatus,
+  hasFeatureProfile,
+} from './ChargingStationUtils';
index d0afb3678dcdacf99b1682ed3397079027d38031..7f065d30bed7dd29c684edaa358c82c0866cbe9e 100644 (file)
@@ -2,7 +2,7 @@
 
 import type { JSONSchemaType } from 'ajv';
 
-import { type ChargingStation, getIdTagsFile } from '../../../charging-station';
+import { type ChargingStation, getIdTagsFile, hasFeatureProfile } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
   type ClearChargingProfileRequest,
@@ -55,7 +55,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     featureProfile: OCPP16SupportedFeatureProfiles,
     command: OCPP16RequestCommand | OCPP16IncomingRequestCommand,
   ): boolean {
-    if (!chargingStation.hasFeatureProfile(featureProfile)) {
+    if (!hasFeatureProfile(chargingStation, featureProfile)) {
       logger.warn(
         `${chargingStation.logPrefix()} Trying to '${command}' without '${featureProfile}' feature enabled in ${
           OCPP16StandardParametersKey.SupportedFeatureProfiles