From 179ed3677e63ff8cc9c49acb55b5d6330ee3802b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 23 May 2023 00:08:46 +0200 Subject: [PATCH] fix: ensure the runtime ATG configuration is saved MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 49 ++++----------- .../ChargingStationConfigurationUtils.ts | 59 +++++++++++++++++++ src/utils/MessageChannelUtils.ts | 30 ++++------ src/utils/index.ts | 6 ++ 4 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 src/utils/ChargingStationConfigurationUtils.ts diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 2fed2cc4..0851c41a 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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( - configurationData, - chargingStationAutomaticTransactionGeneratorConfiguration - ); - } + configurationData = merge( + 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 index 00000000..3549fa7b --- /dev/null +++ b/src/utils/ChargingStationConfigurationUtils.ts @@ -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; + } + }); +}; diff --git a/src/utils/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts index 4e353f4c..06054d5e 100644 --- a/src/utils/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -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), }), }; } diff --git a/src/utils/index.ts b/src/utils/index.ts index 03c88c57..b034c9a2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; -- 2.34.1