From 94a464f92b8113a17129baa63d0d1469385b081b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 7 Mar 2022 13:59:03 +0100 Subject: [PATCH 1/1] Introduce a generic OCPP message sending handler MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And start making use of it Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 3 +- .../ocpp/1.6/OCPP16RequestService.ts | 30 ++++++++++++++----- .../ocpp/OCPPRequestService.ts | 8 ++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 7a8e8751..a71d56d9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -390,7 +390,7 @@ export default class ChargingStation { ) { // eslint-disable-next-line @typescript-eslint/no-misused-promises this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendHeartbeat(); + await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT); }, this.getHeartbeatInterval()); logger.info( this.logPrefix() + diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 0e5918db..f8ca8de6 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -9,6 +9,7 @@ import { MessageTrigger, OCPP16AvailabilityType, OCPP16IncomingRequestCommand, + OCPP16RequestCommand, OCPP16TriggerMessageRequest, RemoteStartTransactionRequest, RemoteStopTransactionRequest, @@ -830,7 +831,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer case MessageTrigger.Heartbeat: setTimeout(() => { this.chargingStation.ocppRequestService - .sendHeartbeat({ triggerMessage: true }) + .sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { triggerMessage: true }) .catch(() => { /* This is intentional */ }); diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 18d6dac0..27ba76ec 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -17,6 +17,7 @@ import { StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; import { MeterValuesRequest, OCPP16MeterValue } from '../../../types/ocpp/1.6/MeterValues'; +import { ResponseType, SendParams } from '../../../types/ocpp/Requests'; import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; @@ -30,7 +31,6 @@ import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; -import { SendParams } from '../../../types/ocpp/Requests'; import Utils from '../../../utils/Utils'; const moduleName = 'OCPP16RequestService'; @@ -43,9 +43,25 @@ export default class OCPP16RequestService extends OCPPRequestService { super(chargingStation, ocppResponseService); } - public async sendHeartbeat(params?: SendParams): Promise { - const payload: HeartbeatRequest = {}; - await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.HEARTBEAT, params); + public async sendMessageHandler( + commandName: OCPP16RequestCommand, + commandParams?: JsonType, + params?: SendParams + ): Promise { + if (Object.values(OCPP16RequestCommand).includes(commandName)) { + return this.sendMessage( + Utils.generateUUID(), + this.buildCommandPayload(commandName, commandParams), + commandName, + params + ); + } + throw new OCPPError( + ErrorType.NOT_SUPPORTED, + `${moduleName}.sendMessageHandler: Unsupported OCPP command ${commandName}`, + commandName, + { commandName } + ); } public async sendBootNotification( @@ -176,7 +192,7 @@ export default class OCPP16RequestService extends OCPPRequestService { transactionId: number, interval: number ): Promise { - const meterValue = OCPP16ServiceUtils.buildMeterValue( + const meterValue: OCPP16MeterValue = OCPP16ServiceUtils.buildMeterValue( this.chargingStation, connectorId, transactionId, @@ -231,7 +247,7 @@ export default class OCPP16RequestService extends OCPPRequestService { private buildCommandPayload( commandName: OCPP16RequestCommand, - commandParams: JsonType + commandParams?: JsonType ): JsonType { switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: @@ -316,7 +332,7 @@ export default class OCPP16RequestService extends OCPPRequestService { throw new OCPPError( ErrorType.NOT_SUPPORTED, // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `Unsupported OCPP command: ${commandName}`, + `${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`, commandName, { commandName } ); diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index fc465b8c..90af961e 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -44,6 +44,7 @@ export default abstract class OCPPRequestService { ) { this.chargingStation = chargingStation; this.ocppResponseService = ocppResponseService; + this.sendMessageHandler.bind(this); } public static getInstance( @@ -326,7 +327,12 @@ export default abstract class OCPPRequestService { } } - public abstract sendHeartbeat(params?: SendParams): Promise; + public abstract sendMessageHandler( + commandName: RequestCommand, + commandParams?: JsonType, + params?: SendParams + ): Promise; + public abstract sendBootNotification( chargePointModel: string, chargePointVendor: string, -- 2.34.1