From 04b1261ce300681bfe95b8e5ad1c100c6a47bd54 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 27 Apr 2023 00:48:49 +0200 Subject: [PATCH] refactor: move helpers to utils 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 | 112 +++++------------- src/charging-station/ChargingStationUtils.ts | 82 +++++++++++++ .../ocpp/1.6/OCPP16ResponseService.ts | 19 ++- 3 files changed, 126 insertions(+), 87 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 96bf6c1b..716ad017 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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) @@ -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)); 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 - ): Map { - const connectorsMap = new Map(); - for (const connector in connectors) { - const connectorStatus = connectors[connector]; - const connectorId = Utils.convertToInt(connector); - this.checkStationInfoConnectorStatus(connectorId, connectorStatus); - connectorsMap.set(connectorId, Utils.cloneObject(connectorStatus)); - connectorsMap.get(connectorId).availability = AvailabilityType.Operative; - if (Utils.isUndefined(connectorsMap.get(connectorId)?.chargingProfiles)) { - connectorsMap.get(connectorId).chargingProfiles = []; - } - } - return connectorsMap; - } - - private initializeConnectorsMapStatus(connectors: Map): 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; - } } diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index e2e9821e..f61dd463 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -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, + logPrefix: string, + templateFile: string + ): Map { + const connectorsMap = new Map(); + 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)); + connectorsMap.get(connectorId).availability = AvailabilityType.Operative; + if (Utils.isUndefined(connectorsMap.get(connectorId)?.chargingProfiles)) { + connectorsMap.get(connectorId).chargingProfiles = []; + } + } + return connectorsMap; + } + + public static initializeConnectorsMapStatus( + connectors: Map, + 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 diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index f8c563c8..c83c83ef 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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 { - 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 '${ -- 2.34.1