From a14022a2a2426ab1f42c3ee25ffbfab8969447ea Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 28 Apr 2023 16:16:03 +0200 Subject: [PATCH] refactor: add helper to test connector 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 | 21 ++++++++++++++----- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 12 +++++------ .../ocpp/1.6/OCPP16ResponseService.ts | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 22d554d1..b28c520b 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -46,7 +46,6 @@ import { type ChargingStationInfo, type ChargingStationOcppConfiguration, type ChargingStationTemplate, - ConnectorPhaseRotation, type ConnectorStatus, ConnectorStatusEnum, CurrentType, @@ -152,6 +151,10 @@ export class ChargingStation { this.initialize(); } + public get hasEvses(): boolean { + return this.connectors.size === 0 && this.evses.size > 0; + } + private get wsConnectionUrl(): URL { return new URL( `${ @@ -166,10 +169,6 @@ export class ChargingStation { ); } - private get hasEvses(): boolean { - return this.connectors.size === 0 && this.evses.size > 0; - } - public logPrefix = (): string => { return Utils.logPrefix( ` ${ @@ -245,6 +244,18 @@ export class ChargingStation { return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative; } + public hasConnector(connectorId: number): boolean { + if (this.hasEvses) { + for (const evseStatus of this.evses.values()) { + if (evseStatus.connectors.has(connectorId)) { + return true; + } + } + return false; + } + return this.connectors.has(connectorId); + } + public isConnectorAvailable(connectorId: number): boolean { return ( connectorId > 0 && diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 642cb18c..3cb96e19 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -394,7 +394,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { commandPayload: UnlockConnectorRequest ): Promise { const connectorId = commandPayload.connectorId; - if (chargingStation.connectors.has(connectorId) === false) { + if (chargingStation.hasConnector(connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId.toString()}` ); @@ -541,7 +541,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) { return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; } - if (chargingStation.connectors.has(commandPayload.connectorId) === false) { + if (chargingStation.hasConnector(commandPayload.connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${ commandPayload.connectorId @@ -597,7 +597,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) { return OCPP16Constants.OCPP_RESPONSE_REJECTED; } - if (chargingStation.connectors.has(commandPayload.connectorId) === false) { + if (chargingStation.hasConnector(commandPayload.connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${ commandPayload.connectorId @@ -647,7 +647,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) { return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } - if (chargingStation.connectors.has(commandPayload.connectorId) === false) { + if (chargingStation.hasConnector(commandPayload.connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${ commandPayload.connectorId @@ -722,7 +722,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { commandPayload: ChangeAvailabilityRequest ): Promise { const connectorId: number = commandPayload.connectorId; - if (chargingStation.connectors.has(connectorId) === false) { + if (chargingStation.hasConnector(connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId.toString()}` ); @@ -775,7 +775,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { commandPayload: RemoteStartTransactionRequest ): Promise { const transactionConnectorId = commandPayload.connectorId; - if (chargingStation.connectors.has(transactionConnectorId) === true) { + if (chargingStation.hasConnector(transactionConnectorId) === true) { const remoteStartTransactionLogMsg = `${chargingStation.logPrefix()} Transaction remotely STARTED on ${ chargingStation.stationInfo.chargingStationId }#${transactionConnectorId.toString()} for idTag '${commandPayload.idTag}'`; diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 743455ff..5cc6cbe7 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -440,7 +440,7 @@ export class OCPP16ResponseService extends OCPPResponseService { requestPayload: OCPP16StartTransactionRequest ): Promise { const connectorId = requestPayload.connectorId; - if (connectorId === 0 || !chargingStation.connectors.has(connectorId)) { + if (connectorId === 0 || chargingStation.hasConnector(connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to start a transaction on a non existing connector id ${connectorId.toString()}` ); -- 2.34.1