Migrate all JSON schemas to draft-06 or higher
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
index 509eebd57b16bd8beacb924aafba3b5e2c5e8423..6015d4d9b8037956e1434ae23b934fd4f98bad6e 100644 (file)
@@ -1,10 +1,12 @@
-import type { JSONSchemaType } from 'ajv';
-import Ajv from 'ajv-draft-04';
+import { AsyncResource } from 'async_hooks';
+
+import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
 import OCPPError from '../../exception/OCPPError';
 import type { HandleErrorParams } from '../../types/Error';
 import type { JsonType } from '../../types/JsonType';
+import type { OCPPVersion } from '../../types/ocpp/OCPPVersion';
 import type { IncomingRequestCommand } from '../../types/ocpp/Requests';
 import logger from '../../utils/Logger';
 import type ChargingStation from '../ChargingStation';
@@ -14,11 +16,15 @@ const moduleName = 'OCPPIncomingRequestService';
 
 export default abstract class OCPPIncomingRequestService {
   private static instance: OCPPIncomingRequestService | null = null;
-  private ajv: Ajv;
+  protected asyncResource: AsyncResource;
+  private readonly version: OCPPVersion;
+  private readonly ajv: Ajv;
 
-  protected constructor() {
+  protected constructor(version: OCPPVersion) {
+    this.version = version;
     this.ajv = new Ajv();
     ajvFormats(this.ajv);
+    this.asyncResource = new AsyncResource(moduleName);
     this.incomingRequestHandler.bind(this);
     this.validateIncomingRequestPayload.bind(this);
   }
@@ -35,7 +41,7 @@ export default abstract class OCPPIncomingRequestService {
     commandName: IncomingRequestCommand,
     error: Error,
     params: HandleErrorParams<T> = { throwError: true }
-  ): T {
+  ): T | undefined {
     logger.error(
       `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
       error