Initial cleanup at loading JSON schemas
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20ResponseService.ts
index d4636f6f3ca25319ef5ad020bd4b29425733e0d8..f9d8fde9feb7d39d944bb1f5d38cf27b1f474f42 100644 (file)
@@ -8,8 +8,16 @@ import type { JSONSchemaType } from 'ajv';
 
 import OCPPError from '../../../exception/OCPPError';
 import type { JsonObject, JsonType } from '../../../types/JsonType';
-import { OCPP20RequestCommand } from '../../../types/ocpp/2.0/Requests';
-import type { OCPP20BootNotificationResponse } from '../../../types/ocpp/2.0/Responses';
+import {
+  OCPP20IncomingRequestCommand,
+  OCPP20RequestCommand,
+} from '../../../types/ocpp/2.0/Requests';
+import type {
+  OCPP20BootNotificationResponse,
+  OCPP20ClearCacheResponse,
+  OCPP20HeartbeatResponse,
+  OCPP20StatusNotificationResponse,
+} from '../../../types/ocpp/2.0/Responses';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
 import { RegistrationStatusEnumType, ResponseHandler } from '../../../types/ocpp/Responses';
@@ -21,6 +29,11 @@ import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
 const moduleName = 'OCPP20ResponseService';
 
 export default class OCPP20ResponseService extends OCPPResponseService {
+  public jsonIncomingRequestResponseSchemas: Map<
+    OCPP20IncomingRequestCommand,
+    JSONSchemaType<JsonObject>
+  >;
+
   private responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>;
   private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
 
@@ -31,19 +44,35 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     super(OCPPVersion.VERSION_20);
     this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([
       [OCPP20RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)],
+      [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)],
+      [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)],
     ]);
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20BootNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
+        ),
+      ],
+      [
+        OCPP20RequestCommand.HEARTBEAT,
+        this.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
+        ),
+      ],
+      [
+        OCPP20RequestCommand.STATUS_NOTIFICATION,
+        this.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
+        ),
+      ],
+    ]);
+    this.jsonIncomingRequestResponseSchemas = new Map([
+      [
+        OCPP20IncomingRequestCommand.CLEAR_CACHE,
+        this.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
+        ),
       ],
     ]);
     this.validatePayload.bind(this);
@@ -105,7 +134,7 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     commandName: OCPP20RequestCommand,
     payload: JsonType
   ): boolean {
-    if (this.jsonSchemas.has(commandName)) {
+    if (this.jsonSchemas.has(commandName) === true) {
       return this.validateResponsePayload(
         chargingStation,
         commandName,
@@ -114,7 +143,7 @@ export default class OCPP20ResponseService extends OCPPResponseService {
       );
     }
     logger.warn(
-      `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
+      `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation`
     );
     return false;
   }
@@ -157,4 +186,13 @@ export default class OCPP20ResponseService extends OCPPResponseService {
       );
     }
   }
+
+  private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
+    return JSON.parse(
+      fs.readFileSync(
+        path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
+        'utf8'
+      )
+    ) as JSONSchemaType<T>;
+  }
 }