From 35ff6ec4653ae55241f25d813a8dcac31391fd99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 5 Apr 2021 18:54:17 +0200 Subject: [PATCH] Factor out code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCCP16IncomingRequestService.ts | 5 +++-- src/charging-station/ocpp/1.6/OCPP16ResponseService.ts | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts index 5a41771d..6b844352 100644 --- a/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts @@ -17,11 +17,12 @@ import logger from '../../../utils/Logger'; export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record): Promise { let response; + const methodName = `handleRequest${commandName}`; // Call - if (typeof this['handleRequest' + commandName] === 'function') { + if (typeof this[methodName] === 'function') { try { // Call the method to build the response - response = await this['handleRequest' + commandName](commandPayload); + response = await this[methodName](commandPayload); } catch (error) { // Log logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error); diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 0a848692..cf1c9212 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -11,11 +11,11 @@ import logger from '../../../utils/Logger'; export default class OCPP16ResponseService extends OCPPResponseService { public async handleResponse(commandName: OCPP16RequestCommand, payload: Record | string, requestPayload: Record): Promise { - const responseCallbackFn = 'handleResponse' + commandName; - if (typeof this[responseCallbackFn] === 'function') { - await this[responseCallbackFn](payload, requestPayload); + const responseCallbackMethodName = `handleResponse${commandName}`; + if (typeof this[responseCallbackMethodName] === 'function') { + await this[responseCallbackMethodName](payload, requestPayload); } else { - logger.error(this.chargingStation.logPrefix() + ' Trying to call an undefined response callback function: ' + responseCallbackFn); + logger.error(this.chargingStation.logPrefix() + ' Trying to call an undefined response callback method: ' + responseCallbackMethodName); } } -- 2.34.1