Introduce a generic OCPP message sending handler
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 7 Mar 2022 12:59:03 +0000 (13:59 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 7 Mar 2022 12:59:03 +0000 (13:59 +0100)
And start making use of it

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 7a8e8751a1268c11931d434bdcaba2b759d4ddee..a71d56d9d56f373e149ba29ebb0106e9e29b83d4 100644 (file)
@@ -390,7 +390,7 @@ export default class ChargingStation {
     ) {
       // eslint-disable-next-line @typescript-eslint/no-misused-promises
       this.heartbeatSetInterval = setInterval(async (): Promise<void> => {
-        await this.ocppRequestService.sendHeartbeat();
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT);
       }, this.getHeartbeatInterval());
       logger.info(
         this.logPrefix() +
index 0e5918db515068f4dee02a9a1e98f952b6de11f1..f8ca8de69745192c0baefea62b68ba38ba804507 100644 (file)
@@ -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 */
               });
index 18d6dac0e3d777b208a2b412e09d1330a4eacbff..27ba76ec10f626e9f1154c40a56c2b7560e9a4b0 100644 (file)
@@ -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<void> {
-    const payload: HeartbeatRequest = {};
-    await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.HEARTBEAT, params);
+  public async sendMessageHandler(
+    commandName: OCPP16RequestCommand,
+    commandParams?: JsonType,
+    params?: SendParams
+  ): Promise<ResponseType> {
+    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<void> {
-    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 }
         );
index fc465b8c3e8ac784d025f44e7fa1cd146bc2995d..90af961e0851c997cac548539f26aa61339ce095 100644 (file)
@@ -44,6 +44,7 @@ export default abstract class OCPPRequestService {
   ) {
     this.chargingStation = chargingStation;
     this.ocppResponseService = ocppResponseService;
+    this.sendMessageHandler.bind(this);
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -326,7 +327,12 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendHeartbeat(params?: SendParams): Promise<void>;
+  public abstract sendMessageHandler(
+    commandName: RequestCommand,
+    commandParams?: JsonType,
+    params?: SendParams
+  ): Promise<ResponseType>;
+
   public abstract sendBootNotification(
     chargePointModel: string,
     chargePointVendor: string,