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 {
this.deleteFSWatcher(file);
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix(file),
- FileType.Authorization,
file,
+ FileType.Authorization,
error as NodeJS.ErrnoException,
+ this.logPrefix(file),
{
throwError: false,
}
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 {
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 {
}
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix(),
- FileType.ChargingStationTemplate,
this.templateFile,
- error as NodeJS.ErrnoException
+ FileType.ChargingStationTemplate,
+ error as NodeJS.ErrnoException,
+ this.logPrefix()
);
}
return template;
}
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix(),
- FileType.ChargingStationConfiguration,
this.configurationFile,
- error as NodeJS.ErrnoException
+ FileType.ChargingStationConfiguration,
+ error as NodeJS.ErrnoException,
+ this.logPrefix()
);
}
}
}
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix(),
- FileType.ChargingStationConfiguration,
this.configurationFile,
- error as NodeJS.ErrnoException
+ FileType.ChargingStationConfiguration,
+ error as NodeJS.ErrnoException,
+ this.logPrefix()
);
}
} else {
// 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 OCPPError from '../../../exception/OCPPError';
import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate';
-import { FileType } from '../../../types/FileType';
import type { JsonType } from '../../../types/JsonType';
import type {
MeasurandPerPhaseSampledValueTemplates,
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';
}
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 }
- );
- }
+ return super.parseJsonSchemaFile<T>(
+ path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
+ OCPPVersion.VERSION_16
+ );
}
private static buildSampledValue(
// 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<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 }
- );
- }
+ return super.parseJsonSchemaFile<T>(
+ path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
+ OCPPVersion.VERSION_20
+ );
}
}
-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';
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';
}
}
- protected static logPrefix(ocppVersion: OCPPVersion): string {
- return Utils.logPrefix(` OCPP ${ocppVersion} |`);
+ protected static parseJsonSchemaFile<T extends JsonType>(
+ filePath: string,
+ ocppVersion: OCPPVersion
+ ): JSONSchemaType<T> {
+ try {
+ return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+ } catch (error) {
+ FileUtils.handleFileException(
+ filePath,
+ FileType.JsonSchema,
+ error as NodeJS.ErrnoException,
+ OCPPServiceUtils.logPrefix(ocppVersion),
+ { throwError: false }
+ );
+ }
}
protected static getSampledValueTemplate(
? Math.min(numberValue * options.unitMultiplier, limit)
: numberValue * options.unitMultiplier;
}
+
+ private static logPrefix(ocppVersion: OCPPVersion): string {
+ return Utils.logPrefix(` OCPP ${ocppVersion} |`);
+ }
}
);
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix,
- FileType.PerformanceRecords,
this.dbName,
- error as NodeJS.ErrnoException
+ FileType.PerformanceRecords,
+ error as NodeJS.ErrnoException,
+ this.logPrefix
);
}
await release();
}
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix,
- FileType.PerformanceRecords,
this.dbName,
- error as NodeJS.ErrnoException
+ FileType.PerformanceRecords,
+ error as NodeJS.ErrnoException,
+ this.logPrefix
);
}
}
}
} catch (error) {
FileUtils.handleFileException(
- this.logPrefix,
- FileType.PerformanceRecords,
this.dbName,
- error as NodeJS.ErrnoException
+ FileType.PerformanceRecords,
+ error as NodeJS.ErrnoException,
+ this.logPrefix
);
}
}
) 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) {
});
} catch (error) {
Configuration.handleFileException(
- Configuration.logPrefix(),
- FileType.Configuration,
Configuration.configurationFile,
- error as NodeJS.ErrnoException
+ FileType.Configuration,
+ error as NodeJS.ErrnoException,
+ Configuration.logPrefix()
);
}
}
}
private static handleFileException(
- logPrefix: string,
- fileType: FileType,
filePath: string,
+ fileType: FileType,
error: NodeJS.ErrnoException,
+ logPrefix: string,
params: HandleErrorParams<EmptyObject> = { throwError: true }
): void {
const prefix = logPrefix.trim().length !== 0 ? `${logPrefix} ` : '';
}
public static watchJsonFile<T extends JsonType>(
- logPrefix: string,
- fileType: FileType,
file: string,
+ fileType: FileType,
+ logPrefix: string,
refreshedVariable?: T,
listener: fs.WatchListener<string> = (event, filename) => {
if (!Utils.isEmptyString(filename) && event === 'change') {
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,
});
}
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,
});
}
}
public static handleFileException(
- logPrefix: string,
- fileType: FileType,
file: string,
+ fileType: FileType,
error: NodeJS.ErrnoException,
+ logPrefix: string,
params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
): void {
const prefix = !Utils.isEmptyString(logPrefix) ? `${logPrefix} ` : '';