Add BootNotification and ClearCache OCPP 2.0.1 commands support
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 839e39955f9ec59d724f2615374d8c0e1cee52e8..c9f100d48f2af32a9a4277e4671ea756efb045b3 100644 (file)
@@ -1,5 +1,5 @@
-import type { JSONSchemaType } from 'ajv';
-import Ajv from 'ajv-draft-04';
+import Ajv, { type JSONSchemaType } from 'ajv';
+import AjvDraft04 from 'ajv-draft-04';
 import ajvFormats from 'ajv-formats';
 
 import OCPPError from '../../exception/OCPPError';
@@ -9,6 +9,7 @@ import type { HandleErrorParams } from '../../types/Error';
 import type { JsonObject, JsonType } from '../../types/JsonType';
 import { ErrorType } from '../../types/ocpp/ErrorType';
 import { MessageType } from '../../types/ocpp/MessageType';
+import { OCPPVersion } from '../../types/ocpp/OCPPVersion';
 import {
   type ErrorCallback,
   type IncomingRequestCommand,
@@ -16,7 +17,7 @@ import {
   RequestCommand,
   type RequestParams,
   type ResponseCallback,
-  ResponseType,
+  type ResponseType,
 } from '../../types/ocpp/Requests';
 import type { ErrorResponse, Response } from '../../types/ocpp/Responses';
 import Constants from '../../utils/Constants';
@@ -30,14 +31,24 @@ const moduleName = 'OCPPRequestService';
 
 export default abstract class OCPPRequestService {
   private static instance: OCPPRequestService | null = null;
+  private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
 
   private readonly ocppResponseService: OCPPResponseService;
 
-  protected constructor(ocppResponseService: OCPPResponseService) {
-    this.ocppResponseService = ocppResponseService;
-    this.ajv = new Ajv();
+  protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) {
+    this.version = version;
+    switch (this.version) {
+      case OCPPVersion.VERSION_16:
+        this.ajv = new AjvDraft04();
+        break;
+      case OCPPVersion.VERSION_20:
+      case OCPPVersion.VERSION_201:
+        this.ajv = new Ajv();
+        break;
+    }
     ajvFormats(this.ajv);
+    this.ocppResponseService = ocppResponseService;
     this.requestHandler.bind(this);
     this.sendMessage.bind(this);
     this.sendResponse.bind(this);
@@ -189,10 +200,10 @@ export default abstract class OCPPRequestService {
           // Check if wsConnection opened
           if (chargingStation.isWebSocketConnectionOpened() === true) {
             // Yes: Send Message
-            const beginId = PerformanceStatistics.beginMeasure(commandName);
+            const beginId = PerformanceStatistics.beginMeasure(commandName as string);
             // FIXME: Handle sending error
             chargingStation.wsConnection.send(messageToSend);
-            PerformanceStatistics.endMeasure(commandName, beginId);
+            PerformanceStatistics.endMeasure(commandName as string, beginId);
             logger.debug(
               `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
                 messageType
@@ -376,10 +387,10 @@ export default abstract class OCPPRequestService {
   }
 
   // eslint-disable-next-line @typescript-eslint/no-unused-vars
-  public abstract requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
+  public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: RequestParams
-  ): Promise<ResponseType>;
+  ): Promise<ResType>;
 }