From 734d790d9983a43c20f9ffa728f18825625e8119 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 30 Sep 2021 20:55:38 +0200 Subject: [PATCH] Convert to a map connectors and their status MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 14 +- src/charging-station/ChargingStation.ts | 175 +++++++++--------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 75 ++++---- .../ocpp/1.6/OCPP16RequestService.ts | 12 +- .../ocpp/1.6/OCPP16ResponseService.ts | 60 +++--- src/types/ChargingStationTemplate.ts | 4 +- src/types/Connectors.ts | 4 +- 7 files changed, 172 insertions(+), 172 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index a2854507..877339c2 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -40,8 +40,7 @@ export default class AutomaticTransactionGenerator { } private startConnectors(): void { - for (const connector in this.chargingStation.connectors) { - const connectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { if (connectorId > 0) { this.startConnector(connectorId); } @@ -49,8 +48,7 @@ export default class AutomaticTransactionGenerator { } private stopConnectors(): void { - for (const connector in this.chargingStation.connectors) { - const connectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { if (connectorId > 0) { this.stopConnector(connectorId); } @@ -103,11 +101,11 @@ export default class AutomaticTransactionGenerator { // Wait until end of transaction const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); + logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; await Utils.sleep(waitTrxEnd); // Stop transaction - logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); + logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString()); await this.stopTransaction(connectorId); } } else { @@ -193,8 +191,8 @@ export default class AutomaticTransactionGenerator { const beginId = PerformanceStatistics.beginMeasure(measureId); let transactionId = 0; let stopResponse: StopTransactionResponse; - if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { - transactionId = this.chargingStation.getConnector(connectorId).transactionId; + if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { + transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId), diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index cf7a19ef..6900cdc3 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -4,8 +4,8 @@ import { AvailabilityType, BootNotificationRequest, CachedRequest, IncomingReque import { BootNotificationResponse, RegistrationStatus } from '../types/ocpp/Responses'; import ChargingStationConfiguration, { ConfigurationKey } from '../types/ChargingStationConfiguration'; import ChargingStationTemplate, { CurrentType, PowerUnits, Voltage } from '../types/ChargingStationTemplate'; -import { Connector, Connectors, SampledValueTemplate } from '../types/Connectors'; import { ConnectorPhaseRotation, StandardParametersKey, SupportedFeatureProfiles } from '../types/ocpp/Configuration'; +import { ConnectorStatus, SampledValueTemplate } from '../types/Connectors'; import { MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; import WebSocket, { ClientOptions, Data, OPEN } from 'ws'; @@ -40,7 +40,7 @@ export default class ChargingStation { public stationTemplateFile: string; public authorizedTags: string[]; public stationInfo!: ChargingStationInfo; - public connectors: Connectors; + public connectors: Map; public configuration!: ChargingStationConfiguration; public wsConnection!: WebSocket; public requests: Map; @@ -63,7 +63,7 @@ export default class ChargingStation { constructor(index: number, stationTemplateFile: string) { this.index = index; this.stationTemplateFile = stationTemplateFile; - this.connectors = {} as Connectors; + this.connectors = new Map(); this.initialize(); this.stopped = false; @@ -119,15 +119,15 @@ export default class ChargingStation { } public isChargingStationAvailable(): boolean { - return this.getConnector(0).availability === AvailabilityType.OPERATIVE; + return this.getConnectorStatus(0).availability === AvailabilityType.OPERATIVE; } public isConnectorAvailable(id: number): boolean { - return this.getConnector(id).availability === AvailabilityType.OPERATIVE; + return this.getConnectorStatus(id).availability === AvailabilityType.OPERATIVE; } - public getConnector(id: number): Connector { - return this.connectors[id]; + public getConnectorStatus(id: number): ConnectorStatus { + return this.connectors.get(id); } public getCurrentOutType(): CurrentType | undefined { @@ -152,9 +152,9 @@ export default class ChargingStation { } public getTransactionIdTag(transactionId: number): string | undefined { - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === transactionId) { - return this.getConnector(Utils.convertToInt(connector)).transactionIdTag; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) { + return this.getConnectorStatus(connectorId).transactionIdTag; } } } @@ -185,24 +185,24 @@ export default class ChargingStation { public getEnergyActiveImportRegisterByTransactionId(transactionId: number): number | undefined { if (this.getMeteringPerTransaction()) { - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === transactionId) { - return this.getConnector(Utils.convertToInt(connector)).transactionEnergyActiveImportRegisterValue; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) { + return this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue; } } } - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionId === transactionId) { - return this.getConnector(Utils.convertToInt(connector)).energyActiveImportRegisterValue; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) { + return this.getConnectorStatus(connectorId).energyActiveImportRegisterValue; } } } public getEnergyActiveImportRegisterByConnectorId(connectorId: number): number | undefined { if (this.getMeteringPerTransaction()) { - return this.getConnector(connectorId).transactionEnergyActiveImportRegisterValue; + return this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue; } - return this.getConnector(connectorId).energyActiveImportRegisterValue; + return this.getConnectorStatus(connectorId).energyActiveImportRegisterValue; } public getAuthorizeRemoteTxRequests(): boolean { @@ -232,7 +232,7 @@ export default class ChargingStation { logger.debug(`${this.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${phase ? `on phase ${phase} ` : ''}in template on connectorId ${connectorId} not found in '${StandardParametersKey.MeterValuesSampledData}' OCPP parameter`); return; } - const sampledValueTemplates: SampledValueTemplate[] = this.getConnector(connectorId).MeterValues; + const sampledValueTemplates: SampledValueTemplate[] = this.getConnectorStatus(connectorId).MeterValues; for (let index = 0; !Utils.isEmptyArray(sampledValueTemplates) && index < sampledValueTemplates.length; index++) { if (!Constants.SUPPORTED_MEASURANDS.includes(sampledValueTemplates[index]?.measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER)) { logger.warn(`${this.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${phase ? `on phase ${phase} ` : ''}in template on connectorId ${connectorId}`); @@ -285,21 +285,21 @@ export default class ChargingStation { logger.error(`${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`); return; } - if (!this.getConnector(connectorId)) { + if (!this.getConnectorStatus(connectorId)) { logger.error(`${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`); return; } - if (!this.getConnector(connectorId)?.transactionStarted) { + if (!this.getConnectorStatus(connectorId)?.transactionStarted) { logger.error(`${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`); return; - } else if (this.getConnector(connectorId)?.transactionStarted && !this.getConnector(connectorId)?.transactionId) { + } else if (this.getConnectorStatus(connectorId)?.transactionStarted && !this.getConnectorStatus(connectorId)?.transactionId) { logger.error(`${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`); return; } if (interval > 0) { // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.getConnector(connectorId).transactionSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendMeterValues(connectorId, this.getConnector(connectorId).transactionId, interval); + this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(async (): Promise => { + await this.ocppRequestService.sendMeterValues(connectorId, this.getConnectorStatus(connectorId).transactionId, interval); }, interval); } else { logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${interval ? Utils.formatDurationMilliSeconds(interval) : interval}, not sending MeterValues`); @@ -332,10 +332,10 @@ export default class ChargingStation { public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise { // Stop message sequence await this.stopMessageSequence(reason); - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0) { - await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), ChargePointStatus.UNAVAILABLE); - this.getConnector(Utils.convertToInt(connector)).status = ChargePointStatus.UNAVAILABLE; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0) { + await this.ocppRequestService.sendStatusNotification(connectorId, ChargePointStatus.UNAVAILABLE); + this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE; } } if (this.isWebSocketConnectionOpened()) { @@ -384,26 +384,26 @@ export default class ChargingStation { public setChargingProfile(connectorId: number, cp: ChargingProfile): void { let cpReplaced = false; - if (!Utils.isEmptyArray(this.getConnector(connectorId).chargingProfiles)) { - this.getConnector(connectorId).chargingProfiles?.forEach((chargingProfile: ChargingProfile, index: number) => { + if (!Utils.isEmptyArray(this.getConnectorStatus(connectorId).chargingProfiles)) { + this.getConnectorStatus(connectorId).chargingProfiles?.forEach((chargingProfile: ChargingProfile, index: number) => { if (chargingProfile.chargingProfileId === cp.chargingProfileId || (chargingProfile.stackLevel === cp.stackLevel && chargingProfile.chargingProfilePurpose === cp.chargingProfilePurpose)) { - this.getConnector(connectorId).chargingProfiles[index] = cp; + this.getConnectorStatus(connectorId).chargingProfiles[index] = cp; cpReplaced = true; } }); } - !cpReplaced && this.getConnector(connectorId).chargingProfiles?.push(cp); + !cpReplaced && this.getConnectorStatus(connectorId).chargingProfiles?.push(cp); } public resetTransactionOnConnector(connectorId: number): void { - this.getConnector(connectorId).authorized = false; - this.getConnector(connectorId).transactionStarted = false; - delete this.getConnector(connectorId).authorizeIdTag; - delete this.getConnector(connectorId).transactionId; - delete this.getConnector(connectorId).transactionIdTag; - this.getConnector(connectorId).transactionEnergyActiveImportRegisterValue = 0; - delete this.getConnector(connectorId).transactionBeginMeterValue; + this.getConnectorStatus(connectorId).authorized = false; + this.getConnectorStatus(connectorId).transactionStarted = false; + 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); } @@ -508,18 +508,19 @@ export default class ChargingStation { this.stationInfo.randomConnectors = true; } const connectorsConfigHash = crypto.createHash('sha256').update(JSON.stringify(this.stationInfo.Connectors) + maxConnectors.toString()).digest('hex'); - const connectorsConfigChanged = !Utils.isEmptyObject(this.connectors) && this.connectorsConfigurationHash !== connectorsConfigHash; - if (!this.connectors || Utils.isEmptyObject(this.connectors) || connectorsConfigChanged) { - connectorsConfigChanged && (this.connectors = {} as Connectors); + const connectorsConfigChanged = this.connectors.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash; + if (!this.connectors || this.connectors.size === 0 || connectorsConfigChanged) { + connectorsConfigChanged && (this.connectors.clear()); this.connectorsConfigurationHash = connectorsConfigHash; // Add connector Id 0 let lastConnector = '0'; for (lastConnector in this.stationInfo.Connectors) { - if (Utils.convertToInt(lastConnector) === 0 && this.getUseConnectorId0() && this.stationInfo.Connectors[lastConnector]) { - this.connectors[lastConnector] = Utils.cloneObject(this.stationInfo.Connectors[lastConnector]); - this.connectors[lastConnector].availability = AvailabilityType.OPERATIVE; - if (Utils.isUndefined(this.connectors[lastConnector]?.chargingProfiles)) { - this.connectors[lastConnector].chargingProfiles = []; + const lastConnectorId = Utils.convertToInt(lastConnector); + if (lastConnectorId === 0 && this.getUseConnectorId0() && this.stationInfo.Connectors[lastConnector]) { + this.connectors.set(lastConnectorId, Utils.cloneObject(this.stationInfo.Connectors[lastConnector])); + this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE; + if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) { + this.getConnectorStatus(lastConnectorId).chargingProfiles = []; } } } @@ -527,10 +528,10 @@ export default class ChargingStation { if ((this.stationInfo.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) > 0) { for (let index = 1; index <= maxConnectors; index++) { const randConnectorId = this.stationInfo.randomConnectors ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1) : index; - this.connectors[index] = Utils.cloneObject(this.stationInfo.Connectors[randConnectorId]); - this.connectors[index].availability = AvailabilityType.OPERATIVE; - if (Utils.isUndefined(this.connectors[lastConnector]?.chargingProfiles)) { - this.connectors[index].chargingProfiles = []; + this.connectors.set(index, Utils.cloneObject(this.stationInfo.Connectors[randConnectorId])); + this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE; + if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) { + this.getConnectorStatus(index).chargingProfiles = []; } } } @@ -538,9 +539,9 @@ export default class ChargingStation { // Avoid duplication of connectors related information delete this.stationInfo.Connectors; // Initialize transaction attributes on connectors - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && !this.getConnector(Utils.convertToInt(connector))?.transactionStarted) { - this.initTransactionAttributesOnConnector(Utils.convertToInt(connector)); + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && !this.getConnectorStatus(connectorId)?.transactionStarted) { + this.initTransactionAttributesOnConnector(connectorId); } } switch (this.getOCPPVersion()) { @@ -577,17 +578,17 @@ export default class ChargingStation { } if (!this.getConfigurationKey(StandardParametersKey.ConnectorPhaseRotation)) { const connectorPhaseRotation = []; - for (const connector in this.connectors) { + for (const connectorId of this.connectors.keys()) { // AC/DC - if (Utils.convertToInt(connector) === 0 && this.getNumberOfPhases() === 0) { - connectorPhaseRotation.push(`${connector}.${ConnectorPhaseRotation.RST}`); - } else if (Utils.convertToInt(connector) > 0 && this.getNumberOfPhases() === 0) { - connectorPhaseRotation.push(`${connector}.${ConnectorPhaseRotation.NotApplicable}`); + if (connectorId === 0 && this.getNumberOfPhases() === 0) { + connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`); + } else if (connectorId > 0 && this.getNumberOfPhases() === 0) { + connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`); // AC - } else if (Utils.convertToInt(connector) > 0 && this.getNumberOfPhases() === 1) { - connectorPhaseRotation.push(`${connector}.${ConnectorPhaseRotation.NotApplicable}`); - } else if (Utils.convertToInt(connector) > 0 && this.getNumberOfPhases() === 3) { - connectorPhaseRotation.push(`${connector}.${ConnectorPhaseRotation.RST}`); + } else if (connectorId > 0 && this.getNumberOfPhases() === 1) { + connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`); + } else if (connectorId > 0 && this.getNumberOfPhases() === 3) { + connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`); } } this.addConfigurationKey(StandardParametersKey.ConnectorPhaseRotation, connectorPhaseRotation.toString()); @@ -765,8 +766,8 @@ export default class ChargingStation { private getNumberOfRunningTransactions(): number { let trxCount = 0; - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector))?.transactionStarted) { + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) { trxCount++; } } @@ -827,7 +828,7 @@ export default class ChargingStation { } private getNumberOfConnectors(): number { - return this.connectors[0] ? Object.keys(this.connectors).length - 1 : Object.keys(this.connectors).length; + return this.connectors.get(0) ? this.connectors.size - 1 : this.connectors.size; } private async startMessageSequence(): Promise { @@ -836,24 +837,24 @@ export default class ChargingStation { // Start heartbeat this.startHeartbeat(); // Initialize connectors status - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) === 0) { + for (const connectorId of this.connectors.keys()) { + if (connectorId === 0) { continue; - } else if (!this.stopped && !this.getConnector(Utils.convertToInt(connector))?.status && this.getConnector(Utils.convertToInt(connector))?.bootStatus) { + } else if (!this.stopped && !this.getConnectorStatus(connectorId)?.status && this.getConnectorStatus(connectorId)?.bootStatus) { // Send status in template at startup - await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus); - this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus; - } else if (this.stopped && this.getConnector(Utils.convertToInt(connector))?.bootStatus) { + await this.ocppRequestService.sendStatusNotification(connectorId, this.getConnectorStatus(connectorId).bootStatus); + this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; + } else if (this.stopped && this.getConnectorStatus(connectorId)?.status && this.getConnectorStatus(connectorId)?.bootStatus) { // Send status in template after reset - await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus); - this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus; - } else if (!this.stopped && this.getConnector(Utils.convertToInt(connector))?.status) { + await this.ocppRequestService.sendStatusNotification(connectorId, this.getConnectorStatus(connectorId).bootStatus); + this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; + } else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) { // Send previous status at template reload - await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).status); + await this.ocppRequestService.sendStatusNotification(connectorId, this.getConnectorStatus(connectorId).status); } else { // Send default status - await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), ChargePointStatus.AVAILABLE); - this.getConnector(Utils.convertToInt(connector)).status = ChargePointStatus.AVAILABLE; + await this.ocppRequestService.sendStatusNotification(connectorId, ChargePointStatus.AVAILABLE); + this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE; } } // Start the ATG @@ -882,9 +883,9 @@ export default class ChargingStation { this.automaticTransactionGenerator.started) { this.automaticTransactionGenerator.stop(); } else { - for (const connector in this.connectors) { - if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector))?.transactionStarted) { - const transactionId = this.getConnector(Utils.convertToInt(connector)).transactionId; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) { + const transactionId = this.getConnectorStatus(connectorId).transactionId; await this.ocppRequestService.sendStopTransaction(transactionId, this.getEnergyActiveImportRegisterByTransactionId(transactionId), this.getTransactionIdTag(transactionId), reason); } @@ -973,8 +974,8 @@ export default class ChargingStation { } private stopMeterValues(connectorId: number) { - if (this.getConnector(connectorId)?.transactionSetInterval) { - clearInterval(this.getConnector(connectorId).transactionSetInterval); + if (this.getConnectorStatus(connectorId)?.transactionSetInterval) { + clearInterval(this.getConnectorStatus(connectorId).transactionSetInterval); } } @@ -1062,10 +1063,10 @@ export default class ChargingStation { } private initTransactionAttributesOnConnector(connectorId: number): void { - this.getConnector(connectorId).authorized = false; - this.getConnector(connectorId).transactionStarted = false; - this.getConnector(connectorId).energyActiveImportRegisterValue = 0; - this.getConnector(connectorId).transactionEnergyActiveImportRegisterValue = 0; + this.getConnectorStatus(connectorId).authorized = false; + this.getConnectorStatus(connectorId).transactionStarted = false; + this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0; + this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 150a41b1..5609fe21 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -41,6 +41,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer [OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, this.handleRequestChangeAvailability.bind(this)], [OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, this.handleRequestRemoteStartTransaction.bind(this)], [OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, this.handleRequestRemoteStopTransaction.bind(this)], + [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)], [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)] ]); } @@ -86,8 +87,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer logger.error(this.chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString()); return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; } - if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { - const transactionId = this.chargingStation.getConnector(connectorId).transactionId; + if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { + const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; const stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId), @@ -98,7 +99,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_RESPONSE_UNLOCK_FAILED; } await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); - this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; + this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; return Constants.OCPP_RESPONSE_UNLOCKED; } @@ -188,36 +189,36 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } private handleRequestSetChargingProfile(commandPayload: SetChargingProfileRequest): SetChargingProfileResponse { - if (!this.chargingStation.getConnector(commandPayload.connectorId)) { + if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) { logger.error(`${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && commandPayload.connectorId !== 0) { return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } - if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE && (commandPayload.connectorId === 0 || !this.chargingStation.getConnector(commandPayload.connectorId)?.transactionStarted)) { + if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE && (commandPayload.connectorId === 0 || !this.chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted)) { return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } this.chargingStation.setChargingProfile(commandPayload.connectorId, commandPayload.csChargingProfiles); - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); + logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`, this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles); return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; } private handleRequestClearChargingProfile(commandPayload: ClearChargingProfileRequest): ClearChargingProfileResponse { - if (!this.chargingStation.getConnector(commandPayload.connectorId)) { + if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) { logger.error(`${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } - if (commandPayload.connectorId && !Utils.isEmptyArray(this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles)) { - this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles = []; - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); + if (commandPayload.connectorId && !Utils.isEmptyArray(this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles)) { + this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles = []; + logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles); return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; } if (!commandPayload.connectorId) { let clearedCP = false; - for (const connector in this.chargingStation.connectors) { - if (!Utils.isEmptyArray(this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles)) { - this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { + for (const connectorId of this.chargingStation.connectors.keys()) { + if (!Utils.isEmptyArray(this.chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { + this.chargingStation.getConnectorStatus(connectorId).chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { let clearCurrentCP = false; if (chargingProfile.chargingProfileId === commandPayload.id) { clearCurrentCP = true; @@ -232,8 +233,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer clearCurrentCP = true; } if (clearCurrentCP) { - this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles[index] = {} as OCPP16ChargingProfile; - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); + this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles[index] = {} as OCPP16ChargingProfile; + logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles); clearedCP = true; } }); @@ -248,32 +249,34 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private async handleRequestChangeAvailability(commandPayload: ChangeAvailabilityRequest): Promise { const connectorId: number = commandPayload.connectorId; - if (!this.chargingStation.getConnector(connectorId)) { + if (!this.chargingStation.getConnectorStatus(connectorId)) { logger.error(`${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`); return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; } - const chargePointStatus: OCPP16ChargePointStatus = commandPayload.type === OCPP16AvailabilityType.OPERATIVE ? OCPP16ChargePointStatus.AVAILABLE : OCPP16ChargePointStatus.UNAVAILABLE; + const chargePointStatus: OCPP16ChargePointStatus = commandPayload.type === OCPP16AvailabilityType.OPERATIVE + ? OCPP16ChargePointStatus.AVAILABLE + : OCPP16ChargePointStatus.UNAVAILABLE; if (connectorId === 0) { let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; - for (const connector in this.chargingStation.connectors) { - if (this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionStarted) { + for (const id of this.chargingStation.connectors.keys()) { + if (this.chargingStation.getConnectorStatus(id)?.transactionStarted) { response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } - this.chargingStation.getConnector(Utils.convertToInt(connector)).availability = commandPayload.type; + this.chargingStation.getConnectorStatus(id).availability = commandPayload.type; if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { - await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), chargePointStatus); - this.chargingStation.getConnector(Utils.convertToInt(connector)).status = chargePointStatus; + await this.chargingStation.ocppRequestService.sendStatusNotification(id, chargePointStatus); + this.chargingStation.getConnectorStatus(id).status = chargePointStatus; } } return response; - } else if (connectorId > 0 && (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.OPERATIVE || (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.INOPERATIVE && commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))) { - if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { - this.chargingStation.getConnector(connectorId).availability = commandPayload.type; + } else if (connectorId > 0 && (this.chargingStation.getConnectorStatus(0).availability === OCPP16AvailabilityType.OPERATIVE || (this.chargingStation.getConnectorStatus(0).availability === OCPP16AvailabilityType.INOPERATIVE && commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))) { + if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { + this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } - this.chargingStation.getConnector(connectorId).availability = commandPayload.type; + this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, chargePointStatus); - this.chargingStation.getConnector(connectorId).status = chargePointStatus; + this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus; return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; } return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; @@ -283,7 +286,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer const transactionConnectorId: number = commandPayload.connectorId; if (transactionConnectorId) { await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.PREPARING); - this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING; + this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING; if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorId)) { // Check if authorized if (this.chargingStation.getAuthorizeRemoteTxRequests()) { @@ -329,18 +332,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } private async notifyRemoteStartTransactionRejected(connectorId: number, idTag: string): Promise { - if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { + if (this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); - this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; + this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; } - logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', status ' + this.chargingStation.getConnector(connectorId).status); + logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnectorStatus(connectorId).availability + ', status ' + this.chargingStation.getConnectorStatus(connectorId).status); return Constants.OCPP_RESPONSE_REJECTED; } private setRemoteStartTransactionChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): boolean { if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { this.chargingStation.setChargingProfile(connectorId, cp); - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnector(connectorId).chargingProfiles); + logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnectorStatus(connectorId).chargingProfiles); return true; } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { logger.warn(`${this.chargingStation.logPrefix()} Not allowed to set ${cp.chargingProfilePurpose} charging profile(s) at remote start transaction`); @@ -352,10 +355,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private async handleRequestRemoteStopTransaction(commandPayload: RemoteStopTransactionRequest): Promise { const transactionId = commandPayload.transactionId; - for (const connector in this.chargingStation.connectors) { - if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === transactionId) { - await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), OCPP16ChargePointStatus.FINISHING); - this.chargingStation.getConnector(Utils.convertToInt(connector)).status = OCPP16ChargePointStatus.FINISHING; + for (const connectorId of this.chargingStation.connectors.keys()) { + if (connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId) { + await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.FINISHING); + this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.FINISHING; await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId)); return Constants.OCPP_RESPONSE_ACCEPTED; diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index d08404c9..a41a9731 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -70,7 +70,7 @@ export default class OCPP16RequestService extends OCPPRequestService { const payload: AuthorizeRequest = { ...!Utils.isUndefined(idTag) ? { idTag } : { idTag: Constants.TRANSACTION_DEFAULT_IDTAG }, }; - this.chargingStation.getConnector(connectorId).authorizeIdTag = idTag; + this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag; return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.AUTHORIZE) as OCPP16AuthorizeResponse; } catch (error) { this.handleRequestError(OCPP16RequestCommand.AUTHORIZE, error); @@ -95,9 +95,9 @@ export default class OCPP16RequestService extends OCPPRequestService { reason: OCPP16StopTransactionReason = OCPP16StopTransactionReason.NONE): Promise { try { let connectorId: number; - for (const connector in this.chargingStation.connectors) { - if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === transactionId) { - connectorId = Utils.convertToInt(connector); + for (const id of this.chargingStation.connectors.keys()) { + if (id > 0 && this.chargingStation.getConnectorStatus(id)?.transactionId === transactionId) { + connectorId = id; break; } } @@ -111,7 +111,7 @@ export default class OCPP16RequestService extends OCPPRequestService { meterStop, timestamp: new Date().toISOString(), ...reason && { reason }, - ...this.chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(this.chargingStation.getConnector(connectorId).transactionBeginMeterValue, transactionEndMeterValue) }, + ...this.chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, transactionEndMeterValue) }, }; return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.STOP_TRANSACTION) as OCPP16StartTransactionResponse; } catch (error) { @@ -125,7 +125,7 @@ export default class OCPP16RequestService extends OCPPRequestService { timestamp: new Date().toISOString(), sampledValue: [], }; - const connector = this.chargingStation.getConnector(connectorId); + const connector = this.chargingStation.getConnectorStatus(connectorId); // SoC measurand const socSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.STATE_OF_CHARGE); if (socSampledValueTemplate) { diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 7dac13e3..96d3e799 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -56,18 +56,18 @@ export default class OCPP16ResponseService extends OCPPResponseService { private handleResponseAuthorize(payload: OCPP16AuthorizeResponse, requestPayload: AuthorizeRequest): void { let authorizeConnectorId: number; - for (const connector in this.chargingStation.connectors) { - if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.authorizeIdTag === requestPayload.idTag) { - authorizeConnectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { + if (connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag === requestPayload.idTag) { + authorizeConnectorId = connectorId; break; } } if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { - this.chargingStation.getConnector(authorizeConnectorId).authorized = true; + this.chargingStation.getConnectorStatus(authorizeConnectorId).authorized = true; logger.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} authorized on connector ${authorizeConnectorId}`); } else { - this.chargingStation.getConnector(authorizeConnectorId).authorized = false; - delete this.chargingStation.getConnector(authorizeConnectorId).authorizeIdTag; + this.chargingStation.getConnectorStatus(authorizeConnectorId).authorized = false; + delete this.chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag; logger.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status ${payload.idTagInfo.status} on connector ${authorizeConnectorId}`); } } @@ -76,9 +76,9 @@ export default class OCPP16ResponseService extends OCPPResponseService { const connectorId = requestPayload.connectorId; let transactionConnectorId: number; - for (const connector in this.chargingStation.connectors) { - if (Utils.convertToInt(connector) > 0 && Utils.convertToInt(connector) === connectorId) { - transactionConnectorId = Utils.convertToInt(connector); + for (const id of this.chargingStation.connectors.keys()) { + if (id > 0 && id === connectorId) { + transactionConnectorId = id; break; } } @@ -86,17 +86,17 @@ export default class OCPP16ResponseService extends OCPPResponseService { logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction on a non existing connector Id ' + connectorId.toString()); return; } - if (this.chargingStation.getConnector(connectorId).authorized && this.chargingStation.getConnector(connectorId).authorizeIdTag !== requestPayload.idTag) { - logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload.idTag + ' different from the authorize request one ' + this.chargingStation.getConnector(connectorId).authorizeIdTag + ' on connector Id ' + connectorId.toString()); + if (this.chargingStation.getConnectorStatus(connectorId).authorized && this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag !== requestPayload.idTag) { + logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload.idTag + ' different from the authorize request one ' + this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag + ' on connector Id ' + connectorId.toString()); return; } - if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { - logger.debug(this.chargingStation.logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.chargingStation.getConnector(connectorId)); + if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { + logger.debug(this.chargingStation.logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.chargingStation.getConnectorStatus(connectorId)); return; } - if (this.chargingStation.getConnector(connectorId)?.status !== OCPP16ChargePointStatus.AVAILABLE - && this.chargingStation.getConnector(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING) { - logger.error(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${this.chargingStation.getConnector(connectorId)?.status}`); + if (this.chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.AVAILABLE + && this.chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING) { + logger.error(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${this.chargingStation.getConnectorStatus(connectorId)?.status}`); return; } if (!Number.isInteger(payload.transactionId)) { @@ -105,16 +105,16 @@ export default class OCPP16ResponseService extends OCPPResponseService { } if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { - this.chargingStation.getConnector(connectorId).transactionStarted = true; - this.chargingStation.getConnector(connectorId).transactionId = payload.transactionId; - this.chargingStation.getConnector(connectorId).transactionIdTag = requestPayload.idTag; - this.chargingStation.getConnector(connectorId).transactionEnergyActiveImportRegisterValue = 0; - this.chargingStation.getConnector(connectorId).transactionBeginMeterValue = OCPP16ServiceUtils.buildTransactionBeginMeterValue(this.chargingStation, connectorId, + this.chargingStation.getConnectorStatus(connectorId).transactionStarted = true; + this.chargingStation.getConnectorStatus(connectorId).transactionId = payload.transactionId; + this.chargingStation.getConnectorStatus(connectorId).transactionIdTag = requestPayload.idTag; + this.chargingStation.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; + this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue = OCPP16ServiceUtils.buildTransactionBeginMeterValue(this.chargingStation, connectorId, requestPayload.meterStart); this.chargingStation.getBeginEndMeterValues() && await this.chargingStation.ocppRequestService.sendTransactionBeginMeterValues(connectorId, payload.transactionId, - this.chargingStation.getConnector(connectorId).transactionBeginMeterValue); + this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue); await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.CHARGING); - this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.CHARGING; + this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING; logger.info(this.chargingStation.logPrefix() + ' Transaction ' + payload.transactionId.toString() + ' STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + connectorId.toString() + ' for idTag ' + requestPayload.idTag); if (this.chargingStation.stationInfo.powerSharedByConnectors) { this.chargingStation.stationInfo.powerDivider++; @@ -124,18 +124,18 @@ export default class OCPP16ResponseService extends OCPPResponseService { } else { logger.warn(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag); this.chargingStation.resetTransactionOnConnector(connectorId); - if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { + if (this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); - this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; + this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; } } } private async handleResponseStopTransaction(payload: OCPP16StopTransactionResponse, requestPayload: StopTransactionRequest): Promise { let transactionConnectorId: number; - for (const connector in this.chargingStation.connectors) { - if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === requestPayload.transactionId) { - transactionConnectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { + if (connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.transactionId === requestPayload.transactionId) { + transactionConnectorId = connectorId; break; } } @@ -149,10 +149,10 @@ export default class OCPP16ResponseService extends OCPPResponseService { OCPP16ServiceUtils.buildTransactionEndMeterValue(this.chargingStation, transactionConnectorId, requestPayload.meterStop)); if (!this.chargingStation.isChargingStationAvailable() || !this.chargingStation.isConnectorAvailable(transactionConnectorId)) { await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.UNAVAILABLE); - this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE; + this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE; } else { await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.AVAILABLE); - this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.AVAILABLE; + this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.AVAILABLE; } if (this.chargingStation.stationInfo.powerSharedByConnectors) { this.chargingStation.stationInfo.powerDivider--; diff --git a/src/types/ChargingStationTemplate.ts b/src/types/ChargingStationTemplate.ts index 6ecc9ec9..c855f5f8 100644 --- a/src/types/ChargingStationTemplate.ts +++ b/src/types/ChargingStationTemplate.ts @@ -1,5 +1,5 @@ import ChargingStationConfiguration from './ChargingStationConfiguration'; -import { Connectors } from './Connectors'; +import { ConnectorStatus } from './Connectors'; import { OCPPProtocol } from './ocpp/OCPPProtocol'; import { OCPPVersion } from './ocpp/OCPPVersion'; @@ -70,5 +70,5 @@ export default interface ChargingStationTemplate { phaseLineToLineVoltageMeterValues?: boolean; Configuration?: ChargingStationConfiguration; AutomaticTransactionGenerator: AutomaticTransactionGenerator; - Connectors: Connectors; + Connectors: Record; } diff --git a/src/types/Connectors.ts b/src/types/Connectors.ts index ad669c87..d2c26240 100644 --- a/src/types/Connectors.ts +++ b/src/types/Connectors.ts @@ -8,7 +8,7 @@ export interface SampledValueTemplate extends SampledValue { fluctuationPercent?: number; } -export interface Connector { +export interface ConnectorStatus { availability: AvailabilityType; bootStatus?: ChargePointStatus; status?: ChargePointStatus; @@ -24,5 +24,3 @@ export interface Connector { transactionBeginMeterValue?: MeterValue; chargingProfiles?: ChargingProfile[]; } - -export type Connectors = Record; -- 2.34.1