X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPServiceUtils.ts;h=4b232033f70b1f71509e8034b4b6978a3a8194d8;hb=17bc43d765c22c8d8c132484f8dc9c3edd370d91;hp=6b7f709f29351bec1bdbc0b9b199bccc8eb8b1e7;hpb=231d1ecd25f002a28380985f53726d9c8bdd2106;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 6b7f709f..4b232033 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,15 +1,33 @@ -import type { DefinedError, ErrorObject } from 'ajv'; +import fs from 'node:fs'; -import BaseError from '../../exception/BaseError'; -import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates'; -import { StandardParametersKey } from '../../types/ocpp/Configuration'; -import { ErrorType } from '../../types/ocpp/ErrorType'; -import { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues'; -import { IncomingRequestCommand, MessageTrigger, RequestCommand } 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 type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; + +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'; +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'; import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; export class OCPPServiceUtils { @@ -33,11 +51,24 @@ export class OCPPServiceUtils { return ErrorType.FORMAT_VIOLATION; } + public static getMessageTypeString(messageType: MessageType): string { + switch (messageType) { + case MessageType.CALL_MESSAGE: + return 'request'; + case MessageType.CALL_RESULT_MESSAGE: + return 'response'; + case MessageType.CALL_ERROR_MESSAGE: + return 'error'; + default: + return 'unknown'; + } + } + public static isRequestCommandSupported( chargingStation: ChargingStation, command: RequestCommand ): boolean { - const isRequestCommand = Object.values(RequestCommand).includes(command); + const isRequestCommand = Object.values(RequestCommand).includes(command); if ( isRequestCommand === true && !chargingStation.stationInfo?.commandsSupport?.outgoingCommands @@ -57,7 +88,8 @@ export class OCPPServiceUtils { chargingStation: ChargingStation, command: IncomingRequestCommand ): boolean { - const isIncomingRequestCommand = Object.values(IncomingRequestCommand).includes(command); + const isIncomingRequestCommand = + Object.values(IncomingRequestCommand).includes(command); if ( isIncomingRequestCommand === true && !chargingStation.stationInfo?.commandsSupport?.incomingCommands @@ -103,6 +135,60 @@ export class OCPPServiceUtils { return true; } + public static convertDateToISOString(obj: T): void { + for (const key in obj) { + 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); + } + } + } + + public static buildStatusNotificationRequest( + chargingStation: ChargingStation, + connectorId: number, + status: ConnectorStatusEnum + ): StatusNotificationRequest { + switch (chargingStation.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16) { + case OCPPVersion.VERSION_16: + return { + connectorId, + status, + errorCode: ChargePointErrorCode.NO_ERROR, + } as OCPP16StatusNotificationRequest; + case OCPPVersion.VERSION_20: + case OCPPVersion.VERSION_201: + return { + timestamp: new Date(), + connectorStatus: status, + connectorId, + evseId: connectorId, + } as OCPP20StatusNotificationRequest; + default: + throw new BaseError('Cannot build status notification payload: OCPP version not supported'); + } + } + + 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( chargingStation: ChargingStation, connectorId: number, @@ -121,7 +207,7 @@ export class OCPPServiceUtils { ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === false + )?.value?.includes(measurand) === false ) { logger.debug( `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${ @@ -131,10 +217,10 @@ export class OCPPServiceUtils { return; } const sampledValueTemplates: SampledValueTemplate[] = - chargingStation.getConnectorStatus(connectorId).MeterValues; + chargingStation.getConnectorStatus(connectorId)?.MeterValues; for ( let index = 0; - Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length; + Utils.isNotEmptyArray(sampledValueTemplates) === true && index < sampledValueTemplates.length; index++ ) { if ( @@ -153,7 +239,7 @@ export class OCPPServiceUtils { ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === true + )?.value?.includes(measurand) === true ) { return sampledValueTemplates[index]; } else if ( @@ -163,7 +249,7 @@ export class OCPPServiceUtils { ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === true + )?.value?.includes(measurand) === true ) { return sampledValueTemplates[index]; } else if ( @@ -200,4 +286,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); + }; }