fix: ensure the runtime ATG configuration is saved
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 22 May 2023 22:08:46 +0000 (00:08 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 22 May 2023 22:08:46 +0000 (00:08 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/utils/ChargingStationConfigurationUtils.ts [new file with mode: 0644]
src/utils/MessageChannelUtils.ts
src/utils/index.ts

index 2fed2cc45e99a635e9cccc5771f9f4939bddb891..0851c41abf6308a99774cfb4bc28b29371679c0d 100644 (file)
@@ -35,7 +35,6 @@ import {
   type BootNotificationRequest,
   type BootNotificationResponse,
   type CachedRequest,
-  type ChargingStationAutomaticTransactionGeneratorConfiguration,
   type ChargingStationConfiguration,
   type ChargingStationInfo,
   type ChargingStationOcppConfiguration,
@@ -93,6 +92,9 @@ import {
   FileUtils,
   MessageChannelUtils,
   Utils,
+  buildChargingStationAutomaticTransactionGeneratorConfiguration,
+  buildConnectorsStatus,
+  buildEvsesStatus,
   logger,
 } from '../utils';
 
@@ -1527,18 +1529,8 @@ export class ChargingStation {
     return configuration;
   }
 
-  private saveChargingStationAutomaticTransactionGeneratorConfiguration(
-    stationTemplate?: ChargingStationTemplate
-  ): void {
-    this.saveConfiguration({
-      automaticTransactionGenerator: (stationTemplate ?? this.getTemplateFromFile())
-        .AutomaticTransactionGenerator,
-      ...(!Utils.isNullOrUndefined(this.automaticTransactionGenerator?.connectorsStatus) && {
-        automaticTransactionGeneratorStatuses: [
-          ...this.automaticTransactionGenerator.connectorsStatus.values(),
-        ],
-      }),
-    });
+  private saveChargingStationAutomaticTransactionGeneratorConfiguration(): void {
+    this.saveConfiguration();
   }
 
   private saveConnectorsStatus() {
@@ -1549,9 +1541,7 @@ export class ChargingStation {
     this.saveConfiguration();
   }
 
-  private saveConfiguration(
-    chargingStationAutomaticTransactionGeneratorConfiguration?: ChargingStationAutomaticTransactionGeneratorConfiguration
-  ): void {
+  private saveConfiguration(): void {
     if (Utils.isNotEmptyString(this.configurationFile)) {
       try {
         if (!fs.existsSync(path.dirname(this.configurationFile))) {
@@ -1565,30 +1555,15 @@ export class ChargingStation {
         if (this.getOcppPersistentConfiguration() && this.ocppConfiguration?.configurationKey) {
           configurationData.configurationKey = this.ocppConfiguration.configurationKey;
         }
-        if (chargingStationAutomaticTransactionGeneratorConfiguration) {
-          configurationData = merge<ChargingStationConfiguration>(
-            configurationData,
-            chargingStationAutomaticTransactionGeneratorConfiguration
-          );
-        }
+        configurationData = merge<ChargingStationConfiguration>(
+          configurationData,
+          buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
+        );
         if (this.connectors.size > 0) {
-          configurationData.connectorsStatus = [...this.connectors.values()].map(
-            // eslint-disable-next-line @typescript-eslint/no-unused-vars
-            ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
-          );
+          configurationData.connectorsStatus = buildConnectorsStatus(this);
         }
         if (this.evses.size > 0) {
-          configurationData.evsesStatus = [...this.evses.values()].map((evseStatus) => {
-            const status = {
-              ...evseStatus,
-              connectorsStatus: [...evseStatus.connectors.values()].map(
-                // eslint-disable-next-line @typescript-eslint/no-unused-vars
-                ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
-              ),
-            };
-            delete status.connectors;
-            return status as EvseStatusConfiguration;
-          });
+          configurationData.evsesStatus = buildEvsesStatus(this);
         }
         delete configurationData.configurationHash;
         const configurationHash = crypto
diff --git a/src/utils/ChargingStationConfigurationUtils.ts b/src/utils/ChargingStationConfigurationUtils.ts
new file mode 100644 (file)
index 0000000..3549fa7
--- /dev/null
@@ -0,0 +1,59 @@
+import { Utils } from './Utils';
+import type { ChargingStation } from '../charging-station';
+import type {
+  ChargingStationAutomaticTransactionGeneratorConfiguration,
+  ConnectorStatus,
+  EvseStatusConfiguration,
+} from '../types';
+
+export const buildChargingStationAutomaticTransactionGeneratorConfiguration = (
+  chargingStation: ChargingStation
+): ChargingStationAutomaticTransactionGeneratorConfiguration => {
+  return {
+    automaticTransactionGenerator: chargingStation.getAutomaticTransactionGeneratorConfiguration(),
+    ...(!Utils.isNullOrUndefined(
+      chargingStation.automaticTransactionGenerator?.connectorsStatus
+    ) && {
+      automaticTransactionGeneratorStatuses: [
+        ...chargingStation.automaticTransactionGenerator.connectorsStatus.values(),
+      ],
+    }),
+  };
+};
+
+export const buildConnectorsStatus = (chargingStation: ChargingStation): ConnectorStatus[] => {
+  return [...chargingStation.connectors.values()].map(
+    // eslint-disable-next-line @typescript-eslint/no-unused-vars
+    ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
+  );
+};
+
+export const enum OutputFormat {
+  configuration = 'configuration',
+  ipc = 'ipc',
+}
+
+export const buildEvsesStatus = (
+  chargingStation: ChargingStation,
+  outputFormat: OutputFormat = OutputFormat.configuration
+): EvseStatusConfiguration[] => {
+  return [...chargingStation.evses.values()].map((evseStatus) => {
+    const connectorsStatus = [...evseStatus.connectors.values()].map(
+      // eslint-disable-next-line @typescript-eslint/no-unused-vars
+      ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
+    );
+    if (outputFormat === OutputFormat.ipc) {
+      return {
+        ...evseStatus,
+        connectors: connectorsStatus,
+      };
+    } else if (outputFormat === OutputFormat.configuration) {
+      const status = {
+        ...evseStatus,
+        connectorsStatus,
+      };
+      delete status.connectors;
+      return status;
+    }
+  });
+};
index 4e353f4c6588e43beb6602cf2cb9d935c641a309..06054d5e80ef3d2787831c0210b6a9e161ff2d1a 100644 (file)
@@ -1,3 +1,9 @@
+import {
+  OutputFormat,
+  buildChargingStationAutomaticTransactionGeneratorConfiguration,
+  buildConnectorsStatus,
+  buildEvsesStatus,
+} from './ChargingStationConfigurationUtils';
 import type { ChargingStation } from '../charging-station';
 import {
   type ChargingStationData,
@@ -53,30 +59,14 @@ export class MessageChannelUtils {
     return {
       started: chargingStation.started,
       stationInfo: chargingStation.stationInfo,
-      connectors: [...chargingStation.connectors.values()].map(
-        // eslint-disable-next-line @typescript-eslint/no-unused-vars
-        ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
-      ),
-      evses: [...chargingStation.evses.values()].map((evseStatus) => {
-        return {
-          ...evseStatus,
-          connectors: [...evseStatus.connectors.values()].map(
-            // eslint-disable-next-line @typescript-eslint/no-unused-vars
-            ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest
-          ),
-        };
-      }),
+      connectors: buildConnectorsStatus(chargingStation),
+      evses: buildEvsesStatus(chargingStation, OutputFormat.ipc),
       ocppConfiguration: chargingStation.ocppConfiguration,
       wsState: chargingStation?.wsConnection?.readyState,
       bootNotificationResponse: chargingStation.bootNotificationResponse,
       ...(chargingStation.automaticTransactionGenerator && {
-        automaticTransactionGenerator: {
-          automaticTransactionGenerator:
-            chargingStation.getAutomaticTransactionGeneratorConfiguration(),
-          automaticTransactionGeneratorStatuses: [
-            ...chargingStation.automaticTransactionGenerator.connectorsStatus.values(),
-          ],
-        },
+        automaticTransactionGenerator:
+          buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation),
       }),
     };
   }
index 03c88c5737e4db0e68ce81427c0a4faeefb5435a..b034c9a28f26b93b1ae5c6fa94a07213023ee1af 100644 (file)
@@ -1,5 +1,11 @@
 export { ACElectricUtils, DCElectricUtils } from './ElectricUtils';
 export { AsyncLock, AsyncLockType } from './AsyncLock';
+export {
+  OutputFormat,
+  buildChargingStationAutomaticTransactionGeneratorConfiguration,
+  buildConnectorsStatus,
+  buildEvsesStatus,
+} from './ChargingStationConfigurationUtils';
 export { CircularArray } from './CircularArray';
 export { Configuration } from './Configuration';
 export { Constants } from './Constants';