| 1 | import type { DefinedError, ErrorObject } from 'ajv'; |
| 2 | |
| 3 | import BaseError from '../../exception/BaseError'; |
| 4 | import type { JsonObject, JsonType } from '../../types/JsonType'; |
| 5 | import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates'; |
| 6 | import { StandardParametersKey } from '../../types/ocpp/Configuration'; |
| 7 | import { ErrorType } from '../../types/ocpp/ErrorType'; |
| 8 | import { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues'; |
| 9 | import { IncomingRequestCommand, MessageTrigger, RequestCommand } from '../../types/ocpp/Requests'; |
| 10 | import Constants from '../../utils/Constants'; |
| 11 | import logger from '../../utils/Logger'; |
| 12 | import Utils from '../../utils/Utils'; |
| 13 | import type ChargingStation from '../ChargingStation'; |
| 14 | import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; |
| 15 | |
| 16 | export class OCPPServiceUtils { |
| 17 | protected constructor() { |
| 18 | // This is intentional |
| 19 | } |
| 20 | |
| 21 | public static ajvErrorsToErrorType(errors: ErrorObject[]): ErrorType { |
| 22 | for (const error of errors as DefinedError[]) { |
| 23 | switch (error.keyword) { |
| 24 | case 'type': |
| 25 | return ErrorType.TYPE_CONSTRAINT_VIOLATION; |
| 26 | case 'dependencies': |
| 27 | case 'required': |
| 28 | return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION; |
| 29 | case 'pattern': |
| 30 | case 'format': |
| 31 | return ErrorType.PROPERTY_CONSTRAINT_VIOLATION; |
| 32 | } |
| 33 | } |
| 34 | return ErrorType.FORMAT_VIOLATION; |
| 35 | } |
| 36 | |
| 37 | public static isRequestCommandSupported( |
| 38 | chargingStation: ChargingStation, |
| 39 | command: RequestCommand |
| 40 | ): boolean { |
| 41 | const isRequestCommand = Object.values<RequestCommand>(RequestCommand).includes(command); |
| 42 | if ( |
| 43 | isRequestCommand === true && |
| 44 | !chargingStation.stationInfo?.commandsSupport?.outgoingCommands |
| 45 | ) { |
| 46 | return true; |
| 47 | } else if ( |
| 48 | isRequestCommand === true && |
| 49 | chargingStation.stationInfo?.commandsSupport?.outgoingCommands |
| 50 | ) { |
| 51 | return chargingStation.stationInfo?.commandsSupport?.outgoingCommands[command] ?? false; |
| 52 | } |
| 53 | logger.error(`${chargingStation.logPrefix()} Unknown outgoing OCPP command '${command}'`); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | public static isIncomingRequestCommandSupported( |
| 58 | chargingStation: ChargingStation, |
| 59 | command: IncomingRequestCommand |
| 60 | ): boolean { |
| 61 | const isIncomingRequestCommand = |
| 62 | Object.values<IncomingRequestCommand>(IncomingRequestCommand).includes(command); |
| 63 | if ( |
| 64 | isIncomingRequestCommand === true && |
| 65 | !chargingStation.stationInfo?.commandsSupport?.incomingCommands |
| 66 | ) { |
| 67 | return true; |
| 68 | } else if ( |
| 69 | isIncomingRequestCommand === true && |
| 70 | chargingStation.stationInfo?.commandsSupport?.incomingCommands |
| 71 | ) { |
| 72 | return chargingStation.stationInfo?.commandsSupport?.incomingCommands[command] ?? false; |
| 73 | } |
| 74 | logger.error(`${chargingStation.logPrefix()} Unknown incoming OCPP command '${command}'`); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | public static isMessageTriggerSupported( |
| 79 | chargingStation: ChargingStation, |
| 80 | messageTrigger: MessageTrigger |
| 81 | ): boolean { |
| 82 | const isMessageTrigger = Object.values(MessageTrigger).includes(messageTrigger); |
| 83 | if (isMessageTrigger === true && !chargingStation.stationInfo?.messageTriggerSupport) { |
| 84 | return true; |
| 85 | } else if (isMessageTrigger === true && chargingStation.stationInfo?.messageTriggerSupport) { |
| 86 | return chargingStation.stationInfo?.messageTriggerSupport[messageTrigger] ?? false; |
| 87 | } |
| 88 | logger.error( |
| 89 | `${chargingStation.logPrefix()} Unknown incoming OCPP message trigger '${messageTrigger}'` |
| 90 | ); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | public static isConnectorIdValid( |
| 95 | chargingStation: ChargingStation, |
| 96 | ocppCommand: IncomingRequestCommand, |
| 97 | connectorId: number |
| 98 | ): boolean { |
| 99 | if (connectorId < 0) { |
| 100 | logger.error( |
| 101 | `${chargingStation.logPrefix()} ${ocppCommand} incoming request received with invalid connector Id ${connectorId}` |
| 102 | ); |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | public static convertDateToISOString<T extends JsonType>(obj: T): void { |
| 109 | for (const k in obj) { |
| 110 | if (obj[k] instanceof Date) { |
| 111 | (obj as JsonObject)[k] = (obj[k] as Date).toISOString(); |
| 112 | } else if (obj[k] !== null && typeof obj[k] === 'object') { |
| 113 | this.convertDateToISOString<T>(obj[k] as T); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected static getSampledValueTemplate( |
| 119 | chargingStation: ChargingStation, |
| 120 | connectorId: number, |
| 121 | measurand: MeterValueMeasurand = MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER, |
| 122 | phase?: MeterValuePhase |
| 123 | ): SampledValueTemplate | undefined { |
| 124 | const onPhaseStr = phase ? `on phase ${phase} ` : ''; |
| 125 | if (Constants.SUPPORTED_MEASURANDS.includes(measurand) === false) { |
| 126 | logger.warn( |
| 127 | `${chargingStation.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` |
| 128 | ); |
| 129 | return; |
| 130 | } |
| 131 | if ( |
| 132 | measurand !== MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && |
| 133 | ChargingStationConfigurationUtils.getConfigurationKey( |
| 134 | chargingStation, |
| 135 | StandardParametersKey.MeterValuesSampledData |
| 136 | )?.value.includes(measurand) === false |
| 137 | ) { |
| 138 | logger.debug( |
| 139 | `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${ |
| 140 | StandardParametersKey.MeterValuesSampledData |
| 141 | }' OCPP parameter` |
| 142 | ); |
| 143 | return; |
| 144 | } |
| 145 | const sampledValueTemplates: SampledValueTemplate[] = |
| 146 | chargingStation.getConnectorStatus(connectorId).MeterValues; |
| 147 | for ( |
| 148 | let index = 0; |
| 149 | Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length; |
| 150 | index++ |
| 151 | ) { |
| 152 | if ( |
| 153 | Constants.SUPPORTED_MEASURANDS.includes( |
| 154 | sampledValueTemplates[index]?.measurand ?? |
| 155 | MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER |
| 156 | ) === false |
| 157 | ) { |
| 158 | logger.warn( |
| 159 | `${chargingStation.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` |
| 160 | ); |
| 161 | } else if ( |
| 162 | phase && |
| 163 | sampledValueTemplates[index]?.phase === phase && |
| 164 | sampledValueTemplates[index]?.measurand === measurand && |
| 165 | ChargingStationConfigurationUtils.getConfigurationKey( |
| 166 | chargingStation, |
| 167 | StandardParametersKey.MeterValuesSampledData |
| 168 | )?.value.includes(measurand) === true |
| 169 | ) { |
| 170 | return sampledValueTemplates[index]; |
| 171 | } else if ( |
| 172 | !phase && |
| 173 | !sampledValueTemplates[index].phase && |
| 174 | sampledValueTemplates[index]?.measurand === measurand && |
| 175 | ChargingStationConfigurationUtils.getConfigurationKey( |
| 176 | chargingStation, |
| 177 | StandardParametersKey.MeterValuesSampledData |
| 178 | )?.value.includes(measurand) === true |
| 179 | ) { |
| 180 | return sampledValueTemplates[index]; |
| 181 | } else if ( |
| 182 | measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && |
| 183 | (!sampledValueTemplates[index].measurand || |
| 184 | sampledValueTemplates[index].measurand === measurand) |
| 185 | ) { |
| 186 | return sampledValueTemplates[index]; |
| 187 | } |
| 188 | } |
| 189 | if (measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER) { |
| 190 | const errorMsg = `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`; |
| 191 | logger.error(`${chargingStation.logPrefix()} ${errorMsg}`); |
| 192 | throw new BaseError(errorMsg); |
| 193 | } |
| 194 | logger.debug( |
| 195 | `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | protected static getLimitFromSampledValueTemplateCustomValue( |
| 200 | value: string, |
| 201 | limit: number, |
| 202 | options: { limitationEnabled?: boolean; unitMultiplier?: number } = { |
| 203 | limitationEnabled: true, |
| 204 | unitMultiplier: 1, |
| 205 | } |
| 206 | ): number { |
| 207 | options.limitationEnabled = options?.limitationEnabled ?? true; |
| 208 | options.unitMultiplier = options?.unitMultiplier ?? 1; |
| 209 | const parsedInt = parseInt(value); |
| 210 | const numberValue = isNaN(parsedInt) ? Infinity : parsedInt; |
| 211 | return options?.limitationEnabled |
| 212 | ? Math.min(numberValue * options.unitMultiplier, limit) |
| 213 | : numberValue * options.unitMultiplier; |
| 214 | } |
| 215 | } |