fix: reference a connector status clone in connectors map
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Apr 2023 18:24:38 +0000 (20:24 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Apr 2023 18:24:38 +0000 (20:24 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts

index 56ce26a19e55fd6e5478636264ca832c8c3d3ace..436d7b02fd3f3024807108e2c7aa307d5b813f96 100644 (file)
@@ -109,7 +109,7 @@ export class ChargingStation {
   public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined;
   public ocppConfiguration!: ChargingStationOcppConfiguration | undefined;
   public wsConnection!: WebSocket | null;
-  public connectors: Map<number, ConnectorStatus>;
+  public readonly connectors: Map<number, ConnectorStatus>;
   public readonly evses: Map<number, EvseStatus>;
   public readonly requests: Map<string, CachedRequest>;
   public performanceStatistics!: PerformanceStatistics | undefined;
@@ -1312,18 +1312,15 @@ export class ChargingStation {
 
   private initializeConnectorsOrEvsesFromFile(configuration: ChargingStationConfiguration): void {
     if (configuration?.connectorsStatus && !configuration?.evsesStatus) {
-      this.connectors = new Map<number, ConnectorStatus>(
-        configuration?.connectorsStatus.map((connectorStatus, connectorId) => [
-          connectorId,
-          connectorStatus,
-        ])
-      );
+      for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) {
+        this.connectors.set(connectorId, Utils.cloneObject<ConnectorStatus>(connectorStatus));
+      }
     } else if (configuration?.evsesStatus && !configuration?.connectorsStatus) {
       for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) {
-        const evseStatus = Utils.cloneObject(evseStatusConfiguration);
+        const evseStatus = Utils.cloneObject<EvseStatusConfiguration>(evseStatusConfiguration);
         delete evseStatus.connectorsStatus;
         this.evses.set(evseId, {
-          ...evseStatus,
+          ...(evseStatus as EvseStatus),
           connectors: new Map<number, ConnectorStatus>(
             evseStatusConfiguration.connectorsStatus.map((connectorStatus, connectorId) => [
               connectorId,
@@ -1557,7 +1554,7 @@ export class ChargingStation {
           fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
         }
         let configurationData: ChargingStationConfiguration =
-          Utils.cloneObject(this.getConfigurationFromFile()) ?? {};
+          Utils.cloneObject<ChargingStationConfiguration>(this.getConfigurationFromFile()) ?? {};
         if (this.getStationInfoPersistentConfiguration() && this.stationInfo) {
           configurationData.stationInfo = this.stationInfo;
         }
@@ -1632,20 +1629,9 @@ export class ChargingStation {
   }
 
   private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | undefined {
-    let configuration: ChargingStationConfiguration | undefined;
     if (this.getOcppPersistentConfiguration() === true) {
-      const configurationFromFile = this.getConfigurationFromFile();
-      configuration = configurationFromFile?.configurationKey && configurationFromFile;
-    }
-    if (!Utils.isNullOrUndefined(configuration)) {
-      delete configuration.stationInfo;
-      delete configuration.connectorsStatus;
-      delete configuration.evsesStatus;
-      delete configuration.automaticTransactionGenerator;
-      delete configuration.automaticTransactionGeneratorStatuses;
-      delete configuration.configurationHash;
+      return { configurationKey: this.getConfigurationFromFile()?.configurationKey };
     }
-    return configuration;
   }
 
   private getOcppConfiguration(): ChargingStationOcppConfiguration | undefined {
index 4084a484864006c46d964c6e895751a2da57cb97..d8f67c21460b626875d01be4bae961e93d1c3b78 100644 (file)
@@ -389,7 +389,7 @@ export class ChargingStationUtils {
   public static stationTemplateToStationInfo(
     stationTemplate: ChargingStationTemplate
   ): ChargingStationInfo {
-    stationTemplate = Utils.cloneObject(stationTemplate);
+    stationTemplate = Utils.cloneObject<ChargingStationTemplate>(stationTemplate);
     delete stationTemplate.power;
     delete stationTemplate.powerUnit;
     delete stationTemplate?.Connectors;
@@ -471,15 +471,15 @@ export class ChargingStationUtils {
     let limit: number, matchingChargingProfile: ChargingProfile;
     // Get charging profiles for connector and sort by stack level
     const chargingProfiles =
-      Utils.cloneObject(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)?.sort(
-        (a, b) => b.stackLevel - a.stackLevel
-      ) ?? [];
+      Utils.cloneObject<ChargingProfile[]>(
+        chargingStation.getConnectorStatus(connectorId)?.chargingProfiles
+      )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? [];
     // Get profiles on connector 0
     if (chargingStation.getConnectorStatus(0)?.chargingProfiles) {
       chargingProfiles.push(
-        ...Utils.cloneObject(chargingStation.getConnectorStatus(0).chargingProfiles).sort(
-          (a, b) => b.stackLevel - a.stackLevel
-        )
+        ...Utils.cloneObject<ChargingProfile[]>(
+          chargingStation.getConnectorStatus(0).chargingProfiles
+        ).sort((a, b) => b.stackLevel - a.stackLevel)
       );
     }
     if (Utils.isNotEmptyArray(chargingProfiles)) {