X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPServiceUtils.ts;h=b16d5f0d4a8970df1259fe9b9ef74850d178c7e8;hb=6625c5ec9c5260260aeb17e86ddd4ca5a2d15d10;hp=08618941d3308dfe7602d92df1726c599efb6552;hpb=cda962601b73f2de23fa089ee102160dd055af2e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 08618941..b16d5f0d 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,27 +1,32 @@ -import type { DefinedError, ErrorObject } from 'ajv'; - -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 { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues'; -import { OCPPVersion } from '../../types/ocpp/OCPPVersion'; +import fs from 'node:fs'; + +import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; + +import { OCPP16Constants, OCPP20Constants } from './internal'; +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'; + type StatusNotificationResponse, +} from '../../types'; +import { Constants, FileUtils, Utils, logger } from '../../utils'; export class OCPPServiceUtils { protected constructor() { @@ -44,6 +49,19 @@ 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 @@ -108,7 +126,7 @@ export class OCPPServiceUtils { ): boolean { if (connectorId < 0) { logger.error( - `${chargingStation.logPrefix()} ${ocppCommand} incoming request received with invalid connector Id ${connectorId}` + `${chargingStation.logPrefix()} ${ocppCommand} incoming request received with invalid connector id ${connectorId}` ); return false; } @@ -120,7 +138,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); } } } @@ -150,6 +168,109 @@ export class OCPPServiceUtils { } } + public static startHeartbeatInterval(chargingStation: ChargingStation, interval: number): void { + if (!chargingStation.heartbeatSetInterval) { + chargingStation.startHeartbeat(); + } else if (chargingStation.getHeartbeatInterval() !== interval) { + chargingStation.restartHeartbeat(); + } + } + + public static async sendAndSetConnectorStatus( + chargingStation: ChargingStation, + connectorId: number, + status: ConnectorStatusEnum + ) { + OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status); + await chargingStation.ocppRequestService.requestHandler< + StatusNotificationRequest, + StatusNotificationResponse + >( + chargingStation, + RequestCommand.STATUS_NOTIFICATION, + OCPPServiceUtils.buildStatusNotificationRequest(chargingStation, connectorId, status) + ); + chargingStation.getConnectorStatus(connectorId).status = status; + } + + protected static checkConnectorStatusTransition( + chargingStation: ChargingStation, + connectorId: number, + status: ConnectorStatusEnum + ): boolean { + const fromStatus = chargingStation.getConnectorStatus(connectorId).status; + let transitionAllowed = false; + switch (chargingStation.stationInfo.ocppVersion) { + case OCPPVersion.VERSION_16: + if ( + connectorId === 0 && + OCPP16Constants.ChargePointStatusChargingStationTransitions.findIndex( + (transition) => transition.from === fromStatus && transition.to === status + ) !== -1 + ) { + transitionAllowed = true; + } else if ( + OCPP16Constants.ChargePointStatusConnectorTransitions.findIndex( + (transition) => transition.from === fromStatus && transition.to === status + ) !== -1 + ) { + transitionAllowed = true; + } + break; + case OCPPVersion.VERSION_20: + case OCPPVersion.VERSION_201: + if ( + connectorId === 0 && + OCPP20Constants.ChargingStationStatusTransitions.findIndex( + (transition) => transition.from === fromStatus && transition.to === status + ) !== -1 + ) { + transitionAllowed = true; + } else if ( + OCPP20Constants.ConnectorStatusTransitions.findIndex( + (transition) => transition.from === fromStatus && transition.to === status + ) !== -1 + ) { + transitionAllowed = true; + } + break; + default: + throw new BaseError( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Cannot check connector status transition: OCPP version ${chargingStation.stationInfo.ocppVersion} not supported` + ); + } + if (transitionAllowed === false) { + logger.warn( + `${chargingStation.logPrefix()} OCPP ${ + chargingStation.stationInfo.ocppVersion + } connector ${connectorId} status transition from '${ + chargingStation.getConnectorStatus(connectorId).status + }' to '${status}' is not allowed` + ); + } + return transitionAllowed; + } + + 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, @@ -168,7 +289,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 '${ @@ -178,10 +299,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 ( @@ -200,7 +321,7 @@ export class OCPPServiceUtils { ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === true + )?.value?.includes(measurand) === true ) { return sampledValueTemplates[index]; } else if ( @@ -210,7 +331,7 @@ export class OCPPServiceUtils { ChargingStationConfigurationUtils.getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === true + )?.value?.includes(measurand) === true ) { return sampledValueTemplates[index]; } else if ( @@ -247,4 +368,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); + }; }