Add error handling to JSON schemas file reading
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
index ccacc0fa541a06efba86a58f763229297623e837..902aa094306cd07f8cfe07580314af910b13e8ad 100644 (file)
@@ -1,9 +1,5 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
-
 import type { JSONSchemaType } from 'ajv';
 
 import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
@@ -37,19 +33,19 @@ export default class OCPP20RequestService extends OCPPRequestService {
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
           '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
         ),
       ],
       [
         OCPP20RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
           '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
         ),
       ],
       [
         OCPP20RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
           '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
         ),
       ],
@@ -108,13 +104,4 @@ export default class OCPP20RequestService extends OCPPRequestService {
         );
     }
   }
-
-  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>;
-  }
 }