From: Jérôme Benoit Date: Thu, 26 Jan 2023 00:33:34 +0000 (+0100) Subject: Add error handling to JSON schemas file reading X-Git-Tag: v1.1.92~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=130783a74f495abcb198b7f01abe19dab4f7fb47;p=e-mobility-charging-stations-simulator.git Add error handling to JSON schemas file reading Close #369 Signed-off-by: Jérôme Benoit --- diff --git a/mikro-orm.config-template.ts b/mikro-orm.config-template.ts index cd705071..eea71b03 100644 --- a/mikro-orm.config-template.ts +++ b/mikro-orm.config-template.ts @@ -1,4 +1,4 @@ -import path from 'path'; +import path from 'node:path'; import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; diff --git a/rollup.config.mjs b/rollup.config.mjs index 8a311828..6b113491 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -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', diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index a2b180e3..8c95660d 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import { FileType } from '../types/FileType'; import FileUtils from '../utils/FileUtils'; diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index bbf20f44..abd3093a 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -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'; diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 276c8368..c9806f21 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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'; diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index e73f4d50..c40a1476 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -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'; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index b9f8368b..6e31a43b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -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.RESET, - this.parseJsonSchemaFile('../../../assets/json-schemas/ocpp/1.6/Reset.json'), + OCPP16ServiceUtils.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/Reset.json' + ), ], [ OCPP16IncomingRequestCommand.CLEAR_CACHE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ClearCache.json' ), ], [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json' ), ], [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json' ), ], [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json' ), ], [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json' ), ], [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json' ), ], [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json' ), ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json' ), ], [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json' ), ], [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json' ), ], [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json' ), ], [ OCPP16IncomingRequestCommand.DATA_TRANSFER, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' ), ], [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json' ), ], @@ -1300,13 +1302,4 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 15b2f2dd..84550695 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -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.AUTHORIZE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/Authorize.json' ), ], [ OCPP16RequestCommand.BOOT_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/BootNotification.json' ), ], [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json' ), ], [ OCPP16RequestCommand.HEARTBEAT, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json' ), ], [ OCPP16RequestCommand.METER_VALUES, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/MeterValues.json' ), ], [ OCPP16RequestCommand.STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json' ), ], [ OCPP16RequestCommand.START_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json' ), ], [ OCPP16RequestCommand.STOP_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json' ), ], [ OCPP16RequestCommand.DATA_TRANSFER, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' ), ], [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json' ), ], @@ -202,13 +198,4 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index aae1d065..188618ca 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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.BOOT_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json' ), ], [ OCPP16RequestCommand.HEARTBEAT, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json' ), ], [ OCPP16RequestCommand.AUTHORIZE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json' ), ], [ OCPP16RequestCommand.START_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json' ), ], [ OCPP16RequestCommand.STOP_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json' ), ], [ OCPP16RequestCommand.STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json' ), ], [ OCPP16RequestCommand.METER_VALUES, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json' ), ], [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json' ), ], [ OCPP16RequestCommand.DATA_TRANSFER, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json' ), ], [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../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( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json' ), ], [ OCPP16IncomingRequestCommand.CLEAR_CACHE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json' ), ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json' ), ], [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json' ), ], [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json' ), ], [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json' ), ], [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json' ), ], [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json' ), ], [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json' ), ], [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json' ), ], [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json' ), ], [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json' ), ], [ OCPP16IncomingRequestCommand.DATA_TRANSFER, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json' ), ], [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, - this.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json' ), ], @@ -647,13 +643,4 @@ export default class OCPP16ResponseService extends OCPPResponseService { logger.warn(logMsg); } } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 65fc51c5..d09a8548 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -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(relativePath: string): JSONSchemaType { + const filePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath); + try { + return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType; + } 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, diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 7e92a9ca..03c3cb04 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -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.CLEAR_CACHE, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json' ), ], @@ -145,13 +141,4 @@ export default class OCPP20IncomingRequestService extends OCPPIncomingRequestSer ); return false; } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index ccacc0fa..902aa094 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -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.BOOT_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json' ), ], [ OCPP20RequestCommand.HEARTBEAT, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json' ), ], [ OCPP20RequestCommand.STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json' ), ], @@ -108,13 +104,4 @@ export default class OCPP20RequestService extends OCPPRequestService { ); } } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 442c6c1d..6e63c8a4 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -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.BOOT_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json' ), ], [ OCPP20RequestCommand.HEARTBEAT, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json' ), ], [ OCPP20RequestCommand.STATUS_NOTIFICATION, - this.parseJsonSchemaFile( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../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( + OCPP20ServiceUtils.parseJsonSchemaFile( '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json' ), ], @@ -185,13 +181,4 @@ export default class OCPP20ResponseService extends OCPPResponseService { ); } } - - private parseJsonSchemaFile(relativePath: string): JSONSchemaType { - return JSON.parse( - fs.readFileSync( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), - 'utf8' - ) - ) as JSONSchemaType; - } } diff --git a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts index 63e43115..2c7d3df3 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts @@ -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(relativePath: string): JSONSchemaType { + const filePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath); + try { + return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType; + } catch (error) { + FileUtils.handleFileException( + OCPPServiceUtils.logPrefix(OCPPVersion.VERSION_20), + FileType.JsonSchema, + filePath, + error as NodeJS.ErrnoException, + { throwError: false } + ); + } + } +} diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 15edd6df..45187086 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -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, diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 6ac54aa4..ad1f85ba 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -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'; diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index d5eb848c..1a1ebeba 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -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'; diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index 5fb0fb61..4b8f4c7c 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -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'; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index b1926e16..263fa048 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -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'; diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 5bb22e7d..19150af1 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import chalk from 'chalk'; diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index aeb1b86a..8588691d 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import WorkerConstants from './WorkerConstants'; import type { WorkerData, WorkerOptions } from '../types/Worker';