1 import type { DefinedError
, ErrorObject
} from
'ajv';
3 import BaseError from
'../../exception/BaseError';
4 import type { SampledValueTemplate
} from
'../../types/MeasurandPerPhaseSampledValueTemplates';
5 import { StandardParametersKey
} from
'../../types/ocpp/Configuration';
6 import { ErrorType
} from
'../../types/ocpp/ErrorType';
7 import { MeterValueMeasurand
, type MeterValuePhase
} from
'../../types/ocpp/MeterValues';
8 import { IncomingRequestCommand
, MessageTrigger
, RequestCommand
} from
'../../types/ocpp/Requests';
9 import Constants from
'../../utils/Constants';
10 import logger from
'../../utils/Logger';
11 import Utils from
'../../utils/Utils';
12 import type ChargingStation from
'../ChargingStation';
13 import { ChargingStationConfigurationUtils
} from
'../ChargingStationConfigurationUtils';
15 export class OCPPServiceUtils
{
16 protected constructor() {
17 // This is intentional
20 public static ajvErrorsToErrorType(errors
: ErrorObject
[]): ErrorType
{
21 for (const error
of errors
as DefinedError
[]) {
22 switch (error
.keyword
) {
24 return ErrorType
.TYPE_CONSTRAINT_VIOLATION
;
27 return ErrorType
.OCCURRENCE_CONSTRAINT_VIOLATION
;
30 return ErrorType
.PROPERTY_CONSTRAINT_VIOLATION
;
33 return ErrorType
.FORMAT_VIOLATION
;
36 public static isRequestCommandSupported(
37 chargingStation
: ChargingStation
,
38 command
: RequestCommand
40 const isRequestCommand
= Object.values(RequestCommand
).includes(command
);
42 isRequestCommand
=== true &&
43 !chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
47 isRequestCommand
=== true &&
48 chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
50 return chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
[command
] ?? false;
52 logger
.error(`${chargingStation.logPrefix()} Unknown outgoing OCPP command '${command}'`);
56 public static isIncomingRequestCommandSupported(
57 chargingStation
: ChargingStation
,
58 command
: IncomingRequestCommand
60 const isIncomingRequestCommand
= Object.values(IncomingRequestCommand
).includes(command
);
62 isIncomingRequestCommand
=== true &&
63 !chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
67 isIncomingRequestCommand
=== true &&
68 chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
70 return chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
[command
] ?? false;
72 logger
.error(`${chargingStation.logPrefix()} Unknown incoming OCPP command '${command}'`);
76 public static isMessageTriggerSupported(
77 chargingStation
: ChargingStation
,
78 messageTrigger
: MessageTrigger
80 const isMessageTrigger
= Object.values(MessageTrigger
).includes(messageTrigger
);
81 if (isMessageTrigger
=== true && !chargingStation
.stationInfo
?.messageTriggerSupport
) {
83 } else if (isMessageTrigger
=== true && chargingStation
.stationInfo
?.messageTriggerSupport
) {
84 return chargingStation
.stationInfo
?.messageTriggerSupport
[messageTrigger
] ?? false;
87 `${chargingStation.logPrefix()} Unknown incoming OCPP message trigger '${messageTrigger}'`
92 public static isConnectorIdValid(
93 chargingStation
: ChargingStation
,
94 ocppCommand
: IncomingRequestCommand
,
97 if (connectorId
< 0) {
99 `${chargingStation.logPrefix()} ${ocppCommand} incoming request received with invalid connector Id ${connectorId}`
106 protected static getSampledValueTemplate(
107 chargingStation
: ChargingStation
,
109 measurand
: MeterValueMeasurand
= MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
110 phase
?: MeterValuePhase
111 ): SampledValueTemplate
| undefined {
112 const onPhaseStr
= phase
? `on phase ${phase} ` : '';
113 if (Constants
.SUPPORTED_MEASURANDS
.includes(measurand
) === false) {
115 `${chargingStation.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
120 measurand
!== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
121 ChargingStationConfigurationUtils
.getConfigurationKey(
123 StandardParametersKey
.MeterValuesSampledData
124 )?.value
.includes(measurand
) === false
127 `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
128 StandardParametersKey.MeterValuesSampledData
133 const sampledValueTemplates
: SampledValueTemplate
[] =
134 chargingStation
.getConnectorStatus(connectorId
).MeterValues
;
137 Utils
.isEmptyArray(sampledValueTemplates
) === false && index
< sampledValueTemplates
.length
;
141 Constants
.SUPPORTED_MEASURANDS
.includes(
142 sampledValueTemplates
[index
]?.measurand
??
143 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
147 `${chargingStation.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
151 sampledValueTemplates
[index
]?.phase
=== phase
&&
152 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
153 ChargingStationConfigurationUtils
.getConfigurationKey(
155 StandardParametersKey
.MeterValuesSampledData
156 )?.value
.includes(measurand
) === true
158 return sampledValueTemplates
[index
];
161 !sampledValueTemplates
[index
].phase
&&
162 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
163 ChargingStationConfigurationUtils
.getConfigurationKey(
165 StandardParametersKey
.MeterValuesSampledData
166 )?.value
.includes(measurand
) === true
168 return sampledValueTemplates
[index
];
170 measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
171 (!sampledValueTemplates
[index
].measurand
||
172 sampledValueTemplates
[index
].measurand
=== measurand
)
174 return sampledValueTemplates
[index
];
177 if (measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
) {
178 const errorMsg
= `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`;
179 logger
.error(`${chargingStation.logPrefix()} ${errorMsg}`);
180 throw new BaseError(errorMsg
);
183 `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
187 protected static getLimitFromSampledValueTemplateCustomValue(
190 options
: { limitationEnabled
?: boolean; unitMultiplier
?: number } = {
191 limitationEnabled
: true,
195 options
.limitationEnabled
= options
?.limitationEnabled
?? true;
196 options
.unitMultiplier
= options
?.unitMultiplier
?? 1;
197 const parsedInt
= parseInt(value
);
198 const numberValue
= isNaN(parsedInt
) ? Infinity : parsedInt
;
199 return options
?.limitationEnabled
200 ? Math.min(numberValue
* options
.unitMultiplier
, limit
)
201 : numberValue
* options
.unitMultiplier
;