Add error handling to JSON schemas file reading
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20IncomingRequestService.ts
index 7e92a9ca6517e374d8de7ab88170cd04b1be8726..03c3cb04428e8e0d880a3f1c7799e9657c4c8557 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,7 +33,7 @@ export default class OCPP20IncomingRequestService extends OCPPIncomingRequestSer
     this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
           '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json'
         ),
       ],
@@ -145,13 +141,4 @@ export default class OCPP20IncomingRequestService extends OCPPIncomingRequestSer
     );
     return false;
   }
-
-  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>;
-  }
 }