Factor out feature profile check at OCPP command handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index c4dc0cbf396fafc986ccf921712d125c6da7582b..ed3a8da7461d06bdeee98bbc6a8ba834de6f72aa 100644 (file)
@@ -34,6 +34,8 @@ export default abstract class OCPPRequestService {
     this.chargingStation = chargingStation;
     this.ocppResponseService = ocppResponseService;
     this.sendMessageHandler.bind(this);
+    this.sendResult.bind(this);
+    this.sendError.bind(this);
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -41,13 +43,13 @@ export default abstract class OCPPRequestService {
     chargingStation: ChargingStation,
     ocppResponseService: OCPPResponseService
   ): T {
-    if (!OCPPRequestService.instances.has(chargingStation.id)) {
+    if (!OCPPRequestService.instances.has(chargingStation.hashId)) {
       OCPPRequestService.instances.set(
-        chargingStation.id,
+        chargingStation.hashId,
         new this(chargingStation, ocppResponseService)
       );
     }
-    return OCPPRequestService.instances.get(chargingStation.id) as T;
+    return OCPPRequestService.instances.get(chargingStation.hashId) as T;
   }
 
   public async sendResult(
@@ -152,6 +154,21 @@ export default abstract class OCPPRequestService {
             // FIXME: Handle sending error
             this.chargingStation.wsConnection.send(messageToSend);
             PerformanceStatistics.endMeasure(commandName, beginId);
+            let msgTypeStr: string;
+            switch (messageType) {
+              case MessageType.CALL_MESSAGE:
+                msgTypeStr = 'request';
+                break;
+              case MessageType.CALL_RESULT_MESSAGE:
+                msgTypeStr = 'response';
+                break;
+              case MessageType.CALL_ERROR_MESSAGE:
+                msgTypeStr = 'error';
+                break;
+            }
+            logger.debug(
+              `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${msgTypeStr} payload: ${messageToSend}`
+            );
           } else if (!params.skipBufferingOnError) {
             // Buffer it
             this.chargingStation.bufferMessage(messageToSend);
@@ -315,9 +332,10 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendMessageHandler(
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  public abstract sendMessageHandler<Request extends JsonType, Response extends JsonType>(
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: SendParams
-  ): Promise<ResponseType>;
+  ): Promise<Response>;
 }