refactor: move helpers to utils
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 26 Apr 2023 22:48:49 +0000 (00:48 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 26 Apr 2023 22:48:49 +0000 (00:48 +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/ocpp/1.6/OCPP16ResponseService.ts

index 96bf6c1b3370fc29a772fc5baa01d5ef172c5509..716ad017e689f76e7a077931ea36356235c7d1e3 100644 (file)
@@ -534,6 +534,12 @@ export class ChargingStation {
     }
   }
 
+  public stopMeterValues(connectorId: number) {
+    if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
+      clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
+    }
+  }
+
   public start(): void {
     if (this.started === false) {
       if (this.starting === false) {
@@ -629,21 +635,6 @@ export class ChargingStation {
     }
   }
 
-  public resetConnectorStatus(connectorId: number): void {
-    this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
-    this.getConnectorStatus(connectorId).idTagAuthorized = false;
-    this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
-    this.getConnectorStatus(connectorId).transactionStarted = false;
-    delete this.getConnectorStatus(connectorId)?.localAuthorizeIdTag;
-    delete this.getConnectorStatus(connectorId)?.authorizeIdTag;
-    delete this.getConnectorStatus(connectorId)?.transactionId;
-    delete this.getConnectorStatus(connectorId)?.transactionIdTag;
-    this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
-    delete this.getConnectorStatus(connectorId)?.transactionBeginMeterValue;
-    this.stopMeterValues(connectorId);
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
-  }
-
   public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined {
     return ChargingStationConfigurationUtils.getConfigurationKey(
       this,
@@ -1294,7 +1285,12 @@ export class ChargingStation {
             this.getUseConnectorId0(stationInfo) === true &&
             connectorStatus
           ) {
-            this.checkStationInfoConnectorStatus(lastConnectorId, connectorStatus);
+            ChargingStationUtils.checkStationInfoConnectorStatus(
+              lastConnectorId,
+              connectorStatus,
+              this.logPrefix(),
+              this.templateFile
+            );
             this.connectors.set(
               lastConnectorId,
               Utils.cloneObject<ConnectorStatus>(connectorStatus)
@@ -1315,7 +1311,12 @@ export class ChargingStation {
               ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1)
               : index;
             const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()];
-            this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus);
+            ChargingStationUtils.checkStationInfoConnectorStatus(
+              randConnectorId,
+              connectorStatus,
+              this.logPrefix(),
+              this.templateFile
+            );
             this.connectors.set(index, Utils.cloneObject<ConnectorStatus>(connectorStatus));
             this.getConnectorStatus(index).availability = AvailabilityType.Operative;
             if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) {
@@ -1344,42 +1345,7 @@ export class ChargingStation {
         connectorId > 0 &&
         Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted)
       ) {
-        this.initializeConnectorStatus(this.getConnectorStatus(connectorId));
-      }
-    }
-  }
-
-  private buildConnectorsMap(
-    connectors: Record<string, ConnectorStatus>
-  ): Map<number, ConnectorStatus> {
-    const connectorsMap = new Map<number, ConnectorStatus>();
-    for (const connector in connectors) {
-      const connectorStatus = connectors[connector];
-      const connectorId = Utils.convertToInt(connector);
-      this.checkStationInfoConnectorStatus(connectorId, connectorStatus);
-      connectorsMap.set(connectorId, Utils.cloneObject<ConnectorStatus>(connectorStatus));
-      connectorsMap.get(connectorId).availability = AvailabilityType.Operative;
-      if (Utils.isUndefined(connectorsMap.get(connectorId)?.chargingProfiles)) {
-        connectorsMap.get(connectorId).chargingProfiles = [];
-      }
-    }
-    return connectorsMap;
-  }
-
-  private initializeConnectorsMapStatus(connectors: Map<number, ConnectorStatus>): void {
-    for (const connectorId of connectors.keys()) {
-      if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted === true) {
-        logger.warn(
-          `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
-            connectors.get(connectorId)?.transactionId
-          }`
-        );
-      }
-      if (
-        connectorId > 0 &&
-        Utils.isNullOrUndefined(connectors.get(connectorId)?.transactionStarted)
-      ) {
-        this.initializeConnectorStatus(connectors.get(connectorId));
+        ChargingStationUtils.initializeConnectorStatus(this.getConnectorStatus(connectorId));
       }
     }
   }
@@ -1409,10 +1375,17 @@ export class ChargingStation {
         this.evsesConfigurationHash = evsesConfigHash;
         for (const evse in stationInfo?.Evses) {
           this.evses.set(Utils.convertToInt(evse), {
-            connectors: this.buildConnectorsMap(stationInfo?.Evses[evse]?.Connectors),
+            connectors: ChargingStationUtils.buildConnectorsMap(
+              stationInfo?.Evses[evse]?.Connectors,
+              this.logPrefix(),
+              this.templateFile
+            ),
             availability: AvailabilityType.Operative,
           });
-          this.initializeConnectorsMapStatus(this.evses.get(Utils.convertToInt(evse))?.connectors);
+          ChargingStationUtils.initializeConnectorsMapStatus(
+            this.evses.get(Utils.convertToInt(evse))?.connectors,
+            this.logPrefix()
+          );
         }
       }
     } else {
@@ -1424,20 +1397,6 @@ export class ChargingStation {
     }
   }
 
-  private checkStationInfoConnectorStatus(
-    connectorId: number,
-    connectorStatus: ConnectorStatus
-  ): void {
-    if (!Utils.isNullOrUndefined(connectorStatus?.status)) {
-      logger.warn(
-        `${this.logPrefix()} Charging station information from template ${
-          this.templateFile
-        } with connector ${connectorId} status configuration defined, undefine it`
-      );
-      delete connectorStatus.status;
-    }
-  }
-
   private getConfigurationFromFile(): ChargingStationConfiguration | undefined {
     let configuration: ChargingStationConfiguration | undefined;
     if (this.configurationFile && fs.existsSync(this.configurationFile)) {
@@ -2076,12 +2035,6 @@ export class ChargingStation {
     }
   }
 
-  private stopMeterValues(connectorId: number) {
-    if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
-      clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
-    }
-  }
-
   private getReconnectExponentialDelay(): boolean {
     return this.stationInfo?.reconnectExponentialDelay ?? false;
   }
@@ -2140,13 +2093,4 @@ export class ChargingStation {
     | undefined {
     return this.getTemplateFromFile()?.AutomaticTransactionGenerator;
   }
-
-  private initializeConnectorStatus(connectorStatus: ConnectorStatus): void {
-    connectorStatus.idTagLocalAuthorized = false;
-    connectorStatus.idTagAuthorized = false;
-    connectorStatus.transactionRemoteStarted = false;
-    connectorStatus.transactionStarted = false;
-    connectorStatus.energyActiveImportRegisterValue = 0;
-    connectorStatus.transactionEnergyActiveImportRegisterValue = 0;
-  }
 }
index e2e9821e28c915804c7a9aec4bac79468023e956..f61dd46361613d4db2b6a731dc23fd5cb14553be 100644 (file)
@@ -9,6 +9,7 @@ import type { ChargingStation } from './internal';
 import { BaseError } from '../exception';
 import {
   AmpereUnits,
+  AvailabilityType,
   type BootNotificationRequest,
   BootReasonEnumType,
   type ChargingProfile,
@@ -154,6 +155,87 @@ export class ChargingStationUtils {
     }
   }
 
+  public static checkStationInfoConnectorStatus(
+    connectorId: number,
+    connectorStatus: ConnectorStatus,
+    logPrefix: string,
+    templateFile: string
+  ): void {
+    if (!Utils.isNullOrUndefined(connectorStatus?.status)) {
+      logger.warn(
+        `${logPrefix} Charging station information from template ${templateFile} with connector ${connectorId} status configuration defined, undefine it`
+      );
+      delete connectorStatus.status;
+    }
+  }
+
+  public static buildConnectorsMap(
+    connectors: Record<string, ConnectorStatus>,
+    logPrefix: string,
+    templateFile: string
+  ): Map<number, ConnectorStatus> {
+    const connectorsMap = new Map<number, ConnectorStatus>();
+    for (const connector in connectors) {
+      const connectorStatus = connectors[connector];
+      const connectorId = Utils.convertToInt(connector);
+      ChargingStationUtils.checkStationInfoConnectorStatus(
+        connectorId,
+        connectorStatus,
+        logPrefix,
+        templateFile
+      );
+      connectorsMap.set(connectorId, Utils.cloneObject<ConnectorStatus>(connectorStatus));
+      connectorsMap.get(connectorId).availability = AvailabilityType.Operative;
+      if (Utils.isUndefined(connectorsMap.get(connectorId)?.chargingProfiles)) {
+        connectorsMap.get(connectorId).chargingProfiles = [];
+      }
+    }
+    return connectorsMap;
+  }
+
+  public static initializeConnectorsMapStatus(
+    connectors: Map<number, ConnectorStatus>,
+    logPrefix: string
+  ): void {
+    for (const connectorId of connectors.keys()) {
+      if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted === true) {
+        logger.warn(
+          `${logPrefix} Connector ${connectorId} at initialization has a transaction started: ${
+            connectors.get(connectorId)?.transactionId
+          }`
+        );
+      }
+      if (
+        connectorId > 0 &&
+        Utils.isNullOrUndefined(connectors.get(connectorId)?.transactionStarted)
+      ) {
+        ChargingStationUtils.initializeConnectorStatus(connectors.get(connectorId));
+      }
+    }
+  }
+
+  public static initializeConnectorStatus(connectorStatus: ConnectorStatus): void {
+    connectorStatus.idTagLocalAuthorized = false;
+    connectorStatus.idTagAuthorized = false;
+    connectorStatus.transactionRemoteStarted = false;
+    connectorStatus.transactionStarted = false;
+    connectorStatus.energyActiveImportRegisterValue = 0;
+    connectorStatus.transactionEnergyActiveImportRegisterValue = 0;
+  }
+
+  public static resetConnectorStatus(connectorStatus: ConnectorStatus): void {
+    connectorStatus.idTagLocalAuthorized = false;
+    connectorStatus.idTagAuthorized = false;
+    connectorStatus.transactionRemoteStarted = false;
+    connectorStatus.transactionStarted = false;
+    delete connectorStatus?.localAuthorizeIdTag;
+    delete connectorStatus?.authorizeIdTag;
+    delete connectorStatus?.transactionId;
+    delete connectorStatus?.transactionIdTag;
+    connectorStatus.transactionEnergyActiveImportRegisterValue = 0;
+    delete connectorStatus?.transactionBeginMeterValue;
+  }
+
   public static createBootNotificationRequest(
     stationInfo: ChargingStationInfo,
     bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp
index f8c563c856d9acda14faeceb395173f9de384fc8..c83c83eff5b6b421c2d46582d870ffbfcfaa2638 100644 (file)
@@ -1,8 +1,15 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import { parentPort } from 'node:worker_threads';
+
 import type { JSONSchemaType } from 'ajv';
 
-import { type ChargingStation, ChargingStationConfigurationUtils } from '../../../charging-station';
+import {
+  type ChargingStation,
+  ChargingStationConfigurationUtils,
+  ChargingStationUtils,
+  MessageChannelUtils,
+} from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
   type ChangeAvailabilityResponse,
@@ -593,7 +600,9 @@ export class OCPP16ResponseService extends OCPPResponseService {
     chargingStation: ChargingStation,
     connectorId: number
   ): Promise<void> {
-    chargingStation.resetConnectorStatus(connectorId);
+    ChargingStationUtils.resetConnectorStatus(chargingStation.getConnectorStatus(connectorId));
+    chargingStation.stopMeterValues(connectorId);
+    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(chargingStation));
     if (
       chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.Available
     ) {
@@ -655,7 +664,11 @@ export class OCPP16ResponseService extends OCPPResponseService {
     if (chargingStation.stationInfo.powerSharedByConnectors) {
       chargingStation.powerDivider--;
     }
-    chargingStation.resetConnectorStatus(transactionConnectorId);
+    ChargingStationUtils.resetConnectorStatus(
+      chargingStation.getConnectorStatus(transactionConnectorId)
+    );
+    chargingStation.stopMeterValues(transactionConnectorId);
+    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(chargingStation));
     const logMsg = `${chargingStation.logPrefix()} Transaction ${requestPayload.transactionId.toString()} STOPPED on ${
       chargingStation.stationInfo.chargingStationId
     }#${transactionConnectorId?.toString()} with status '${