Add error handling to JSON schemas file reading
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 26 Jan 2023 00:33:34 +0000 (01:33 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 26 Jan 2023 00:33:34 +0000 (01:33 +0100)
Close #369

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
21 files changed:
mikro-orm.config-template.ts
rollup.config.mjs
src/charging-station/AuthorizedTagsCache.ts
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts
src/charging-station/ocpp/OCPPServiceUtils.ts
src/performance/PerformanceStatistics.ts
src/performance/storage/JsonFileStorage.ts
src/performance/storage/Storage.ts
src/utils/Configuration.ts
src/utils/FileUtils.ts
src/worker/WorkerAbstract.ts

index cd7050712406aff6d0d4d7c1b27bb9bd5b6c97da..eea71b03e7474a4b50f6f9f646faa2ad75250b80 100644 (file)
@@ -1,4 +1,4 @@
-import path from 'path';
+import path from 'node:path';
 
 import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
 
index 8a3118285ef9bb4d4637e1bc927f651a71b1355d..6b113491dc7bc668d70e8eba682e9d32a50a544d 100644 (file)
@@ -39,7 +39,6 @@ export default {
     'async_hooks',
     'basic-ftp',
     'chalk',
-    'fs',
     'http',
     'http-status-codes',
     'just-clone',
@@ -48,13 +47,14 @@ export default {
     'moment',
     'mongodb',
     'node:crypto',
+    'node:fs',
+    'node:path',
+    'node:url',
     'node:util',
-    'path',
     'perf_hooks',
     'poolifier',
     'proper-lockfile',
     'tar',
-    'url',
     'winston',
     'winston-daily-rotate-file',
     'winston/lib/winston/transports',
index a2b180e330600b37873d7c37b0e01c52edf2e3c6..8c95660d07130155f495ec3d1fa7d017dbab1ca6 100644 (file)
@@ -1,4 +1,4 @@
-import fs from 'fs';
+import fs from 'node:fs';
 
 import { FileType } from '../types/FileType';
 import FileUtils from '../utils/FileUtils';
index bbf20f443ee67685ac0aa8ab62e06ccdec98dc2d..abd3093a54d97b8a149ec89b1aded96b5c2996a0 100644 (file)
@@ -1,7 +1,7 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import path from 'path';
-import { fileURLToPath } from 'url';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
 import { type Worker, isMainThread } from 'worker_threads';
 
 import chalk from 'chalk';
index 276c836866970585c2cef6923c6dfd1dcb89a746..c9806f21e6b614574b457daeb3f24f02df359ea8 100644 (file)
@@ -1,9 +1,9 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
 import crypto from 'node:crypto';
-import path from 'path';
-import { URL } from 'url';
+import fs from 'node:fs';
+import path from 'node:path';
+import { URL } from 'node:url';
 import { parentPort } from 'worker_threads';
 
 import merge from 'just-merge';
index e73f4d50670628cd61dfc82a2604ef7f9ae2d9fe..c40a1476b1e79b97b0e84afc2364174db13bfac4 100644 (file)
@@ -1,6 +1,6 @@
 import crypto from 'node:crypto';
-import path from 'path';
-import { fileURLToPath } from 'url';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
 
 import moment from 'moment';
 
index b9f8368b36aa4bf882154efe0fd2dc6caef29031..6e31a43b80095f9900fce1695ae57b2d16761265 100644 (file)
@@ -1,8 +1,8 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
-import path from 'path';
-import { URL, fileURLToPath } from 'url';
+import fs from 'node:fs';
+import path from 'node:path';
+import { URL, fileURLToPath } from 'node:url';
 
 import type { JSONSchemaType } from 'ajv';
 import { Client, type FTPResponse } from 'basic-ftp';
@@ -140,83 +140,85 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16IncomingRequestCommand.RESET,
-        this.parseJsonSchemaFile<ResetRequest>('../../../assets/json-schemas/ocpp/1.6/Reset.json'),
+        OCPP16ServiceUtils.parseJsonSchemaFile<ResetRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/Reset.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<OCPP16ClearCacheRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16ClearCacheRequest>(
           '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR,
-        this.parseJsonSchemaFile<UnlockConnectorRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<UnlockConnectorRequest>(
           '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_CONFIGURATION,
-        this.parseJsonSchemaFile<GetConfigurationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetConfigurationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
-        this.parseJsonSchemaFile<ChangeConfigurationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeConfigurationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
-        this.parseJsonSchemaFile<GetDiagnosticsRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetDiagnosticsRequest>(
           '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<SetChargingProfileRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<SetChargingProfileRequest>(
           '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<ClearChargingProfileRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ClearChargingProfileRequest>(
           '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        this.parseJsonSchemaFile<ChangeAvailabilityRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityRequest>(
           '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-        this.parseJsonSchemaFile<RemoteStartTransactionRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<RemoteStartTransactionRequest>(
           '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
-        this.parseJsonSchemaFile<RemoteStopTransactionRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<RemoteStopTransactionRequest>(
           '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        this.parseJsonSchemaFile<OCPP16TriggerMessageRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16TriggerMessageRequest>(
           '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferRequest>(
           '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UPDATE_FIRMWARE,
-        this.parseJsonSchemaFile<OCPP16UpdateFirmwareRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16UpdateFirmwareRequest>(
           '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json'
         ),
       ],
@@ -1300,13 +1302,4 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       );
     }
   }
-
-  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>;
-  }
 }
index 15b2f2dd64e8ba130d8a31268d04ff82fa501f19..845506958fb633500504a04ca3eff118df2382cb 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 { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
@@ -47,61 +43,61 @@ export default class OCPP16RequestService extends OCPPRequestService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.AUTHORIZE,
-        this.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
           '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
         ),
       ],
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
         ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
         ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
           '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
         ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        this.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
           '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
         ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
         ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
           '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
         ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
           '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
         ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferRequest>(
           '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
         ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
           '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
         ),
       ],
@@ -202,13 +198,4 @@ export default class OCPP16RequestService 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>;
-  }
 }
index aae1d0655a39fd52f1d38d61c3043975974aee61..188618ca0e4cb70f5a0e6cd632a2114af1ab1529 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 { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
@@ -93,61 +89,61 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP16HeartbeatResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16HeartbeatResponse>(
           '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.AUTHORIZE,
-        this.parseJsonSchemaFile<OCPP16AuthorizeResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeResponse>(
           '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StartTransactionResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StartTransactionResponse>(
           '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StopTransactionResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StopTransactionResponse>(
           '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16StatusNotificationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StatusNotificationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        this.parseJsonSchemaFile<OCPP16MeterValuesResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16MeterValuesResponse>(
           '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferResponse>(
           '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
         ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json'
         ),
       ],
@@ -155,85 +151,85 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP16IncomingRequestCommand.RESET,
-        this.parseJsonSchemaFile<GenericResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
           '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<GenericResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
           '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        this.parseJsonSchemaFile<ChangeAvailabilityResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityResponse>(
           '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR,
-        this.parseJsonSchemaFile<UnlockConnectorResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<UnlockConnectorResponse>(
           '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_CONFIGURATION,
-        this.parseJsonSchemaFile<GetConfigurationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetConfigurationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
-        this.parseJsonSchemaFile<ChangeConfigurationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeConfigurationResponse>(
           '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<SetChargingProfileResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<SetChargingProfileResponse>(
           '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<ClearChargingProfileResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<ClearChargingProfileResponse>(
           '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-        this.parseJsonSchemaFile<GenericResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
           '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
-        this.parseJsonSchemaFile<GenericResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
           '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
-        this.parseJsonSchemaFile<GetDiagnosticsResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetDiagnosticsResponse>(
           '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        this.parseJsonSchemaFile<OCPP16TriggerMessageResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16TriggerMessageResponse>(
           '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferResponse>(
           '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UPDATE_FIRMWARE,
-        this.parseJsonSchemaFile<OCPP16UpdateFirmwareResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16UpdateFirmwareResponse>(
           '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json'
         ),
       ],
@@ -647,13 +643,4 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       logger.warn(logMsg);
     }
   }
-
-  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>;
-  }
 }
index 65fc51c527302969e3335b61533390ae415a4737..d09a8548d28e0545e949fd7c9913637e7ee0dd69 100644 (file)
@@ -1,7 +1,15 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+import type { JSONSchemaType } from 'ajv';
+
 import OCPPError from '../../../exception/OCPPError';
 import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate';
+import { FileType } from '../../../types/FileType';
+import type { JsonType } from '../../../types/JsonType';
 import type {
   MeasurandPerPhaseSampledValueTemplates,
   SampledValueTemplate,
@@ -26,8 +34,10 @@ import {
   OCPP16RequestCommand,
 } from '../../../types/ocpp/1.6/Requests';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
+import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
 import Constants from '../../../utils/Constants';
 import { ACElectricUtils, DCElectricUtils } from '../../../utils/ElectricUtils';
+import FileUtils from '../../../utils/FileUtils';
 import logger from '../../../utils/Logger';
 import Utils from '../../../utils/Utils';
 import type ChargingStation from '../../ChargingStation';
@@ -779,6 +789,21 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     !cpReplaced && chargingStation.getConnectorStatus(connectorId)?.chargingProfiles?.push(cp);
   }
 
+  public static parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
+    const filePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath);
+    try {
+      return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+    } catch (error) {
+      FileUtils.handleFileException(
+        OCPPServiceUtils.logPrefix(OCPPVersion.VERSION_16),
+        FileType.JsonSchema,
+        filePath,
+        error as NodeJS.ErrnoException,
+        { throwError: false }
+      );
+    }
+  }
+
   private static buildSampledValue(
     sampledValueTemplate: SampledValueTemplate,
     value: number,
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>;
-  }
 }
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>;
-  }
 }
index 442c6c1d6d488086d999be08e2616fb906cf1b98..6e63c8a45d2d6666771ea156b62ea653782d954b 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';
@@ -50,19 +46,19 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
           '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
         ),
       ],
       [
         OCPP20RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
           '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
         ),
       ],
       [
         OCPP20RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
           '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
         ),
       ],
@@ -70,7 +66,7 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
           '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
         ),
       ],
@@ -185,13 +181,4 @@ 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>;
-  }
 }
index 63e43115dabaf6db2ccdba3c3dee653b91948bb9..2c7d3df3191f2246eb3086e4bb02d003ef241663 100644 (file)
@@ -1,5 +1,30 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+import type { JSONSchemaType } from 'ajv';
+
+import { FileType } from '../../../types/FileType';
+import type { JsonType } from '../../../types/JsonType';
+import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
+import FileUtils from '../../../utils/FileUtils';
 import { OCPPServiceUtils } from '../OCPPServiceUtils';
 
-export class OCPP20ServiceUtils extends OCPPServiceUtils {}
+export class OCPP20ServiceUtils extends OCPPServiceUtils {
+  public static parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
+    const filePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath);
+    try {
+      return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+    } catch (error) {
+      FileUtils.handleFileException(
+        OCPPServiceUtils.logPrefix(OCPPVersion.VERSION_20),
+        FileType.JsonSchema,
+        filePath,
+        error as NodeJS.ErrnoException,
+        { throwError: false }
+      );
+    }
+  }
+}
index 15edd6dfef63a731370d62dfa76e466abeeeabed..451870862ad1c5630a68fa34da323594e1e27fa7 100644 (file)
@@ -164,6 +164,10 @@ export class OCPPServiceUtils {
     }
   }
 
+  protected static logPrefix(ocppVersion: OCPPVersion): string {
+    return Utils.logPrefix(` OCPP ${ocppVersion} |`);
+  }
+
   protected static getSampledValueTemplate(
     chargingStation: ChargingStation,
     connectorId: number,
index 6ac54aa42c451813f22c8d2cd1fe1d6e73979064..ad1f85ba7098cb13db9acb87a34bba2344f16de8 100644 (file)
@@ -1,7 +1,7 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import type { URL } from 'node:url';
 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
-import type { URL } from 'url';
 import { parentPort } from 'worker_threads';
 
 import { MessageChannelUtils } from '../charging-station/MessageChannelUtils';
index d5eb848c7c90d5dca20c09623496b71d44b36ef5..1a1ebeba19b625c0640ac95905f89e627c0cf388 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
+import fs from 'node:fs';
 
 import lockfile from 'proper-lockfile';
 
index 5fb0fb61beb4190ec9cdc98a7252bf4c8f8115c5..4b8f4c7cbb1c7f3c9cba6ceacfa709209565ac09 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { URL } from 'url';
+import { URL } from 'node:url';
 
 import type { EmptyObject } from '../../types/EmptyObject';
 import type { HandleErrorParams } from '../../types/Error';
index b1926e16dad7425d3cd7ea8a257163c41a4a7e14..263fa04876d92d90080aae3946f4a36864a9e7d6 100644 (file)
@@ -1,6 +1,6 @@
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
 import merge from 'just-merge';
index 5bb22e7d47939c5234ae97105ce7e68ed24ca7da..19150af19fbc31d8a6d1a9a7d5554094f569efe4 100644 (file)
@@ -1,4 +1,4 @@
-import fs from 'fs';
+import fs from 'node:fs';
 
 import chalk from 'chalk';
 
index aeb1b86afec93810ed825243e738b96bf05d1700..8588691dc3911e1e48f9aabe6f854dada52912fa 100644 (file)
@@ -1,4 +1,4 @@
-import fs from 'fs';
+import fs from 'node:fs';
 
 import WorkerConstants from './WorkerConstants';
 import type { WorkerData, WorkerOptions } from '../types/Worker';