Factor out code.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 5 Apr 2021 16:54:17 +0000 (18:54 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 5 Apr 2021 16:54:17 +0000 (18:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index 5a41771d57ca0c4c9638acacfaed5d28a1420a2f..6b8443520490718cd4075e5fcb420b9f2f2324ee 100644 (file)
@@ -17,11 +17,12 @@ import logger from '../../../utils/Logger';
 export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
   public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> {
     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);
index 0a848692a983e23b80c679c911624a040e57c701..cf1c9212d87ee4ff26be6888edd26ac378fb37e1 100644 (file)
@@ -11,11 +11,11 @@ import logger from '../../../utils/Logger';
 
 export default class OCPP16ResponseService extends OCPPResponseService {
   public async handleResponse(commandName: OCPP16RequestCommand, payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
-    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);
     }
   }