X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPServiceUtils.ts;h=f7a5d61ceb772f23d4da6b7fc5e394848ca52d8f;hb=a7bb184512dd064b5da4dc125d652b6b40ceffee;hp=451870862ad1c5630a68fa34da323594e1e27fa7;hpb=130783a74f495abcb198b7f01abe19dab4f7fb47;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 45187086..f7a5d61c 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,28 +1,30 @@ -import type { DefinedError, ErrorObject } from 'ajv'; +import fs from 'node:fs'; -import BaseError from '../../exception/BaseError'; -import type { JsonObject, JsonType } from '../../types/JsonType'; -import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates'; -import type { OCPP16StatusNotificationRequest } from '../../types/ocpp/1.6/Requests'; -import type { OCPP20StatusNotificationRequest } from '../../types/ocpp/2.0/Requests'; -import { ChargePointErrorCode } from '../../types/ocpp/ChargePointErrorCode'; -import { StandardParametersKey } from '../../types/ocpp/Configuration'; -import type { ConnectorStatusEnum } from '../../types/ocpp/ConnectorStatusEnum'; -import { ErrorType } from '../../types/ocpp/ErrorType'; -import { MessageType } from '../../types/ocpp/MessageType'; -import { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues'; -import { OCPPVersion } from '../../types/ocpp/OCPPVersion'; +import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; + +import { type ChargingStation, ChargingStationConfigurationUtils } from '../../charging-station'; +import { BaseError } from '../../exception'; import { + ChargePointErrorCode, + type ConnectorStatusEnum, + ErrorType, + FileType, IncomingRequestCommand, + type JsonObject, + type JsonType, MessageTrigger, + MessageType, + MeterValueMeasurand, + type MeterValuePhase, + type OCPP16StatusNotificationRequest, + type OCPP20StatusNotificationRequest, + OCPPVersion, RequestCommand, + type SampledValueTemplate, + StandardParametersKey, type StatusNotificationRequest, -} from '../../types/ocpp/Requests'; -import Constants from '../../utils/Constants'; -import logger from '../../utils/Logger'; -import Utils from '../../utils/Utils'; -import type ChargingStation from '../ChargingStation'; -import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; +} from '../../types'; +import { Constants, FileUtils, Utils, logger } from '../../utils'; export class OCPPServiceUtils { protected constructor() { @@ -134,7 +136,7 @@ export class OCPPServiceUtils { if (obj[key] instanceof Date) { (obj as JsonObject)[key] = (obj[key] as Date).toISOString(); } else if (obj[key] !== null && typeof obj[key] === 'object') { - this.convertDateToISOString(obj[key] as T); + OCPPServiceUtils.convertDateToISOString(obj[key] as T); } } } @@ -164,8 +166,31 @@ export class OCPPServiceUtils { } } - protected static logPrefix(ocppVersion: OCPPVersion): string { - return Utils.logPrefix(` OCPP ${ocppVersion} |`); + public static startHeartbeatInterval(chargingStation: ChargingStation, interval: number): void { + if (!chargingStation.heartbeatSetInterval) { + chargingStation.startHeartbeat(); + } else if (chargingStation.getHeartbeatInterval() !== interval) { + chargingStation.restartHeartbeat(); + } + } + + protected static parseJsonSchemaFile( + filePath: string, + ocppVersion: OCPPVersion, + moduleName?: string, + methodName?: string + ): 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, moduleName, methodName), + { throwError: false } + ); + } } protected static getSampledValueTemplate( @@ -199,7 +224,7 @@ export class OCPPServiceUtils { chargingStation.getConnectorStatus(connectorId)?.MeterValues; for ( let index = 0; - Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length; + Utils.isNotEmptyArray(sampledValueTemplates) === true && index < sampledValueTemplates.length; index++ ) { if ( @@ -265,4 +290,16 @@ export class OCPPServiceUtils { ? Math.min(numberValue * options.unitMultiplier, limit) : numberValue * options.unitMultiplier; } + + private static logPrefix = ( + ocppVersion: OCPPVersion, + moduleName?: string, + methodName?: string + ): string => { + const logMsg = + Utils.isNotEmptyString(moduleName) && Utils.isNotEmptyString(methodName) + ? ` OCPP ${ocppVersion} | ${moduleName}.${methodName}:` + : ` OCPP ${ocppVersion} |`; + return Utils.logPrefix(logMsg); + }; }