2c7d3df3191f2246eb3086e4bb02d003ef241663
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20ServiceUtils.ts
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import fs from 'node:fs';
4 import path from 'node:path';
5 import { fileURLToPath } from 'node:url';
6
7 import type { JSONSchemaType } from 'ajv';
8
9 import { FileType } from '../../../types/FileType';
10 import type { JsonType } from '../../../types/JsonType';
11 import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
12 import FileUtils from '../../../utils/FileUtils';
13 import { OCPPServiceUtils } from '../OCPPServiceUtils';
14
15 export class OCPP20ServiceUtils extends OCPPServiceUtils {
16 public static parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
17 const filePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath);
18 try {
19 return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
20 } catch (error) {
21 FileUtils.handleFileException(
22 OCPPServiceUtils.logPrefix(OCPPVersion.VERSION_20),
23 FileType.JsonSchema,
24 filePath,
25 error as NodeJS.ErrnoException,
26 { throwError: false }
27 );
28 }
29 }
30 }