From ec94a3cf52930554aa2ffbdf67c06f23095cabb6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 5 Jun 2023 21:03:28 +0200 Subject: [PATCH] refactor: use helper to set and send connector status in reservation code 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 | 50 ++++++------------- src/charging-station/ocpp/OCPPServiceUtils.ts | 29 +++++++---- 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index fc6a409a..24f94682 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -937,21 +937,12 @@ export class ChargingStation { } const connectorStatus = this.getConnectorStatus(reservation.connectorId); connectorStatus.reservation = reservation; - connectorStatus.status = ConnectorStatusEnum.Reserved; - if (reservation.connectorId === 0) { - return; - } - await this.ocppRequestService.requestHandler< - StatusNotificationRequest, - StatusNotificationResponse - >( + await OCPPServiceUtils.sendAndSetConnectorStatus( this, - RequestCommand.STATUS_NOTIFICATION, - OCPPServiceUtils.buildStatusNotificationRequest( - this, - reservation.connectorId, - ConnectorStatusEnum.Reserved - ) + reservation.connectorId, + ConnectorStatusEnum.Reserved, + null, + { send: reservation.connectorId !== 0 } ); } @@ -961,35 +952,22 @@ export class ChargingStation { ): Promise { const connector = this.getConnectorStatus(reservation.connectorId); switch (reason) { - case ReservationTerminationReason.TRANSACTION_STARTED: { - delete connector.reservation; - if (reservation.connectorId === 0) { - connector.status = ConnectorStatusEnum.Available; - } - break; - } case ReservationTerminationReason.CONNECTOR_STATE_CHANGED: { delete connector.reservation; break; } - default: { - // ReservationTerminationReason.EXPIRED, ReservationTerminationReason.CANCELED - connector.status = ConnectorStatusEnum.Available; - delete connector.reservation; - await this.ocppRequestService.requestHandler< - StatusNotificationRequest, - StatusNotificationResponse - >( + case ReservationTerminationReason.TRANSACTION_STARTED: + case ReservationTerminationReason.EXPIRED: + case ReservationTerminationReason.RESERVATION_CANCELED: + await OCPPServiceUtils.sendAndSetConnectorStatus( this, - RequestCommand.STATUS_NOTIFICATION, - OCPPServiceUtils.buildStatusNotificationRequest( - this, - reservation.connectorId, - ConnectorStatusEnum.Available - ) + reservation.connectorId, + ConnectorStatusEnum.Available, + null, + { send: reservation.connectorId !== 0 } ); + delete connector.reservation; break; - } } } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 078fa0a0..b01ff1e0 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -185,17 +185,26 @@ export class OCPPServiceUtils { chargingStation: ChargingStation, connectorId: number, status: ConnectorStatusEnum, - evseId?: number + evseId?: number, + options: { send: boolean } = { send: true } ) { - OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status); - await chargingStation.ocppRequestService.requestHandler< - StatusNotificationRequest, - StatusNotificationResponse - >( - chargingStation, - RequestCommand.STATUS_NOTIFICATION, - OCPPServiceUtils.buildStatusNotificationRequest(chargingStation, connectorId, status, evseId) - ); + options = { send: true, ...options }; + if (options.send) { + OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status); + await chargingStation.ocppRequestService.requestHandler< + StatusNotificationRequest, + StatusNotificationResponse + >( + chargingStation, + RequestCommand.STATUS_NOTIFICATION, + OCPPServiceUtils.buildStatusNotificationRequest( + chargingStation, + connectorId, + status, + evseId + ) + ); + } chargingStation.getConnectorStatus(connectorId).status = status; } -- 2.34.1