From 7164966d863b4539243b473c5e2e9d22fb9b5fd1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 26 Jan 2023 16:34:07 +0100 Subject: [PATCH] Factor out JSON schemas parsing with per OCPP stack version specialisation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/AuthorizedTagsCache.ts | 16 +++++------ src/charging-station/ChargingStation.ts | 24 ++++++++--------- .../ocpp/1.6/OCPP16ServiceUtils.ts | 19 +++---------- .../ocpp/2.0/OCPP20ServiceUtils.ts | 19 +++---------- src/charging-station/ocpp/OCPPServiceUtils.ts | 27 ++++++++++++++++--- src/performance/storage/JsonFileStorage.ts | 18 ++++++------- src/utils/Configuration.ts | 16 +++++------ src/utils/FileUtils.ts | 12 ++++----- 8 files changed, 75 insertions(+), 76 deletions(-) diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index 8c95660d..b5058291 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -30,10 +30,10 @@ export default class AuthorizedTagsCache { this.FSWatchers.set( file, FileUtils.watchJsonFile( - this.logPrefix(file), - FileType.Authorization, file, - null, + FileType.Authorization, + this.logPrefix(file), + undefined, (event, filename) => { if (!Utils.isEmptyString(filename) && event === 'change') { try { @@ -44,10 +44,10 @@ export default class AuthorizedTagsCache { this.deleteFSWatcher(file); } catch (error) { FileUtils.handleFileException( - this.logPrefix(file), - FileType.Authorization, file, + FileType.Authorization, error as NodeJS.ErrnoException, + this.logPrefix(file), { throwError: false, } @@ -94,10 +94,10 @@ export default class AuthorizedTagsCache { authorizedTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; } catch (error) { FileUtils.handleFileException( - this.logPrefix(file), - FileType.Authorization, file, - error as NodeJS.ErrnoException + FileType.Authorization, + error as NodeJS.ErrnoException, + this.logPrefix(file) ); } } else { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index c9806f21..514d50ce 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -506,10 +506,10 @@ export default class ChargingStation { this.openWSConnection(); // Monitor charging station template file this.templateFileWatcher = FileUtils.watchJsonFile( - this.logPrefix(), - FileType.ChargingStationTemplate, this.templateFile, - null, + FileType.ChargingStationTemplate, + this.logPrefix(), + undefined, (event, filename): void => { if (!Utils.isEmptyString(filename) && event === 'change') { try { @@ -819,10 +819,10 @@ export default class ChargingStation { } } catch (error) { FileUtils.handleFileException( - this.logPrefix(), - FileType.ChargingStationTemplate, this.templateFile, - error as NodeJS.ErrnoException + FileType.ChargingStationTemplate, + error as NodeJS.ErrnoException, + this.logPrefix() ); } return template; @@ -1335,10 +1335,10 @@ export default class ChargingStation { } } catch (error) { FileUtils.handleFileException( - this.logPrefix(), - FileType.ChargingStationConfiguration, this.configurationFile, - error as NodeJS.ErrnoException + FileType.ChargingStationConfiguration, + error as NodeJS.ErrnoException, + this.logPrefix() ); } } @@ -1381,10 +1381,10 @@ export default class ChargingStation { } } catch (error) { FileUtils.handleFileException( - this.logPrefix(), - FileType.ChargingStationConfiguration, this.configurationFile, - error as NodeJS.ErrnoException + FileType.ChargingStationConfiguration, + error as NodeJS.ErrnoException, + this.logPrefix() ); } } else { diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index d09a8548..de9c8548 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -1,6 +1,5 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -8,7 +7,6 @@ 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, @@ -37,7 +35,6 @@ 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'; @@ -790,18 +787,10 @@ export class OCPP16ServiceUtils 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_16), - FileType.JsonSchema, - filePath, - error as NodeJS.ErrnoException, - { throwError: false } - ); - } + return super.parseJsonSchemaFile( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + OCPPVersion.VERSION_16 + ); } private static buildSampledValue( diff --git a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts index 2c7d3df3..e6f5c7ad 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts @@ -1,30 +1,19 @@ // 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 { 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 } - ); - } + return super.parseJsonSchemaFile( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + OCPPVersion.VERSION_20 + ); } } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 45187086..c85ccf2f 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,6 +1,9 @@ -import type { DefinedError, ErrorObject } from 'ajv'; +import fs from 'node:fs'; + +import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; import BaseError from '../../exception/BaseError'; +import { FileType } from '../../types/FileType'; import type { JsonObject, JsonType } from '../../types/JsonType'; import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates'; import type { OCPP16StatusNotificationRequest } from '../../types/ocpp/1.6/Requests'; @@ -19,6 +22,7 @@ import { type StatusNotificationRequest, } from '../../types/ocpp/Requests'; import Constants from '../../utils/Constants'; +import FileUtils from '../../utils/FileUtils'; import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import type ChargingStation from '../ChargingStation'; @@ -164,8 +168,21 @@ export class OCPPServiceUtils { } } - protected static logPrefix(ocppVersion: OCPPVersion): string { - return Utils.logPrefix(` OCPP ${ocppVersion} |`); + protected static parseJsonSchemaFile( + filePath: string, + ocppVersion: OCPPVersion + ): JSONSchemaType { + try { + return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType; + } catch (error) { + FileUtils.handleFileException( + filePath, + FileType.JsonSchema, + error as NodeJS.ErrnoException, + OCPPServiceUtils.logPrefix(ocppVersion), + { throwError: false } + ); + } } protected static getSampledValueTemplate( @@ -265,4 +282,8 @@ export class OCPPServiceUtils { ? Math.min(numberValue * options.unitMultiplier, limit) : numberValue * options.unitMultiplier; } + + private static logPrefix(ocppVersion: OCPPVersion): string { + return Utils.logPrefix(` OCPP ${ocppVersion} |`); + } } diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 1a1ebeba..bacc8d21 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -36,10 +36,10 @@ export class JsonFileStorage extends Storage { ); } catch (error) { FileUtils.handleFileException( - this.logPrefix, - FileType.PerformanceRecords, this.dbName, - error as NodeJS.ErrnoException + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix ); } await release(); @@ -56,10 +56,10 @@ export class JsonFileStorage extends Storage { } } catch (error) { FileUtils.handleFileException( - this.logPrefix, - FileType.PerformanceRecords, this.dbName, - error as NodeJS.ErrnoException + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix ); } } @@ -72,10 +72,10 @@ export class JsonFileStorage extends Storage { } } catch (error) { FileUtils.handleFileException( - this.logPrefix, - FileType.PerformanceRecords, this.dbName, - error as NodeJS.ErrnoException + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix ); } } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 263fa048..8490be4e 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -357,10 +357,10 @@ export default class Configuration { ) as ConfigurationData; } catch (error) { Configuration.handleFileException( - Configuration.logPrefix(), - FileType.Configuration, Configuration.configurationFile, - error as NodeJS.ErrnoException + FileType.Configuration, + error as NodeJS.ErrnoException, + Configuration.logPrefix() ); } if (!Configuration.configurationFileWatcher) { @@ -385,10 +385,10 @@ export default class Configuration { }); } catch (error) { Configuration.handleFileException( - Configuration.logPrefix(), - FileType.Configuration, Configuration.configurationFile, - error as NodeJS.ErrnoException + FileType.Configuration, + error as NodeJS.ErrnoException, + Configuration.logPrefix() ); } } @@ -423,10 +423,10 @@ export default class Configuration { } private static handleFileException( - logPrefix: string, - fileType: FileType, filePath: string, + fileType: FileType, error: NodeJS.ErrnoException, + logPrefix: string, params: HandleErrorParams = { throwError: true } ): void { const prefix = logPrefix.trim().length !== 0 ? `${logPrefix} ` : ''; diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 19150af1..8ef81680 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -15,9 +15,9 @@ export default class FileUtils { } public static watchJsonFile( - logPrefix: string, - fileType: FileType, file: string, + fileType: FileType, + logPrefix: string, refreshedVariable?: T, listener: fs.WatchListener = (event, filename) => { if (!Utils.isEmptyString(filename) && event === 'change') { @@ -25,7 +25,7 @@ export default class FileUtils { logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`); refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); } catch (error) { - FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { + FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { throwError: false, }); } @@ -36,7 +36,7 @@ export default class FileUtils { try { return fs.watch(file, listener); } catch (error) { - FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { + FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { throwError: false, }); } @@ -46,10 +46,10 @@ export default class FileUtils { } public static handleFileException( - logPrefix: string, - fileType: FileType, file: string, + fileType: FileType, error: NodeJS.ErrnoException, + logPrefix: string, params: HandleErrorParams = { throwError: true, consoleOut: false } ): void { const prefix = !Utils.isEmptyString(logPrefix) ? `${logPrefix} ` : ''; -- 2.34.1