1 import type { DefinedError
, ErrorObject
} from
'ajv';
3 import BaseError from
'../../exception/BaseError';
4 import type { JsonObject
, JsonType
} from
'../../types/JsonType';
5 import type { SampledValueTemplate
} from
'../../types/MeasurandPerPhaseSampledValueTemplates';
6 import type { OCPP16StatusNotificationRequest
} from
'../../types/ocpp/1.6/Requests';
7 import type { OCPP20StatusNotificationRequest
} from
'../../types/ocpp/2.0/Requests';
8 import { ChargePointErrorCode
} from
'../../types/ocpp/ChargePointErrorCode';
9 import { StandardParametersKey
} from
'../../types/ocpp/Configuration';
10 import type { ConnectorStatusEnum
} from
'../../types/ocpp/ConnectorStatusEnum';
11 import { ErrorType
} from
'../../types/ocpp/ErrorType';
12 import { MessageType
} from
'../../types/ocpp/MessageType';
13 import { MeterValueMeasurand
, type MeterValuePhase
} from
'../../types/ocpp/MeterValues';
14 import { OCPPVersion
} from
'../../types/ocpp/OCPPVersion';
16 IncomingRequestCommand
,
19 type StatusNotificationRequest
,
20 } from
'../../types/ocpp/Requests';
21 import Constants from
'../../utils/Constants';
22 import logger from
'../../utils/Logger';
23 import Utils from
'../../utils/Utils';
24 import type ChargingStation from
'../ChargingStation';
25 import { ChargingStationConfigurationUtils
} from
'../ChargingStationConfigurationUtils';
27 export class OCPPServiceUtils
{
28 protected constructor() {
29 // This is intentional
32 public static ajvErrorsToErrorType(errors
: ErrorObject
[]): ErrorType
{
33 for (const error
of errors
as DefinedError
[]) {
34 switch (error
.keyword
) {
36 return ErrorType
.TYPE_CONSTRAINT_VIOLATION
;
39 return ErrorType
.OCCURRENCE_CONSTRAINT_VIOLATION
;
42 return ErrorType
.PROPERTY_CONSTRAINT_VIOLATION
;
45 return ErrorType
.FORMAT_VIOLATION
;
48 public static getMessageTypeString(messageType
: MessageType
): string {
49 switch (messageType
) {
50 case MessageType
.CALL_MESSAGE
:
52 case MessageType
.CALL_RESULT_MESSAGE
:
54 case MessageType
.CALL_ERROR_MESSAGE
:
59 public static isRequestCommandSupported(
60 chargingStation
: ChargingStation
,
61 command
: RequestCommand
63 const isRequestCommand
= Object.values
<RequestCommand
>(RequestCommand
).includes(command
);
65 isRequestCommand
=== true &&
66 !chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
70 isRequestCommand
=== true &&
71 chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
73 return chargingStation
.stationInfo
?.commandsSupport
?.outgoingCommands
[command
] ?? false;
75 logger
.error(`${chargingStation.logPrefix()} Unknown outgoing OCPP command '${command}'`);
79 public static isIncomingRequestCommandSupported(
80 chargingStation
: ChargingStation
,
81 command
: IncomingRequestCommand
83 const isIncomingRequestCommand
=
84 Object.values
<IncomingRequestCommand
>(IncomingRequestCommand
).includes(command
);
86 isIncomingRequestCommand
=== true &&
87 !chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
91 isIncomingRequestCommand
=== true &&
92 chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
94 return chargingStation
.stationInfo
?.commandsSupport
?.incomingCommands
[command
] ?? false;
96 logger
.error(`${chargingStation.logPrefix()} Unknown incoming OCPP command '${command}'`);
100 public static isMessageTriggerSupported(
101 chargingStation
: ChargingStation
,
102 messageTrigger
: MessageTrigger
104 const isMessageTrigger
= Object.values(MessageTrigger
).includes(messageTrigger
);
105 if (isMessageTrigger
=== true && !chargingStation
.stationInfo
?.messageTriggerSupport
) {
107 } else if (isMessageTrigger
=== true && chargingStation
.stationInfo
?.messageTriggerSupport
) {
108 return chargingStation
.stationInfo
?.messageTriggerSupport
[messageTrigger
] ?? false;
111 `${chargingStation.logPrefix()} Unknown incoming OCPP message trigger '${messageTrigger}'`
116 public static isConnectorIdValid(
117 chargingStation
: ChargingStation
,
118 ocppCommand
: IncomingRequestCommand
,
121 if (connectorId
< 0) {
123 `${chargingStation.logPrefix()} ${ocppCommand} incoming request received with invalid connector Id ${connectorId}`
130 public static convertDateToISOString
<T
extends JsonType
>(obj
: T
): void {
131 for (const key
in obj
) {
132 if (obj
[key
] instanceof Date) {
133 (obj
as JsonObject
)[key
] = (obj
[key
] as Date).toISOString();
134 } else if (obj
[key
] !== null && typeof obj
[key
] === 'object') {
135 this.convertDateToISOString
<T
>(obj
[key
] as T
);
140 public static buildStatusNotificationRequest(
141 chargingStation
: ChargingStation
,
143 status: ConnectorStatusEnum
144 ): StatusNotificationRequest
{
145 switch (chargingStation
.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
) {
146 case OCPPVersion
.VERSION_16
:
150 errorCode
: ChargePointErrorCode
.NO_ERROR
,
151 } as OCPP16StatusNotificationRequest
;
152 case OCPPVersion
.VERSION_20
:
153 case OCPPVersion
.VERSION_201
:
155 timestamp
: new Date(),
156 connectorStatus
: status,
159 } as OCPP20StatusNotificationRequest
;
161 throw new BaseError('Cannot build status notification payload: OCPP version not supported');
165 protected static getSampledValueTemplate(
166 chargingStation
: ChargingStation
,
168 measurand
: MeterValueMeasurand
= MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
169 phase
?: MeterValuePhase
170 ): SampledValueTemplate
| undefined {
171 const onPhaseStr
= phase
? `on phase ${phase} ` : '';
172 if (Constants
.SUPPORTED_MEASURANDS
.includes(measurand
) === false) {
174 `${chargingStation.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
179 measurand
!== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
180 ChargingStationConfigurationUtils
.getConfigurationKey(
182 StandardParametersKey
.MeterValuesSampledData
183 )?.value
.includes(measurand
) === false
186 `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
187 StandardParametersKey.MeterValuesSampledData
192 const sampledValueTemplates
: SampledValueTemplate
[] =
193 chargingStation
.getConnectorStatus(connectorId
).MeterValues
;
196 Utils
.isEmptyArray(sampledValueTemplates
) === false && index
< sampledValueTemplates
.length
;
200 Constants
.SUPPORTED_MEASURANDS
.includes(
201 sampledValueTemplates
[index
]?.measurand
??
202 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
206 `${chargingStation.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
210 sampledValueTemplates
[index
]?.phase
=== phase
&&
211 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
212 ChargingStationConfigurationUtils
.getConfigurationKey(
214 StandardParametersKey
.MeterValuesSampledData
215 )?.value
.includes(measurand
) === true
217 return sampledValueTemplates
[index
];
220 !sampledValueTemplates
[index
].phase
&&
221 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
222 ChargingStationConfigurationUtils
.getConfigurationKey(
224 StandardParametersKey
.MeterValuesSampledData
225 )?.value
.includes(measurand
) === true
227 return sampledValueTemplates
[index
];
229 measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
230 (!sampledValueTemplates
[index
].measurand
||
231 sampledValueTemplates
[index
].measurand
=== measurand
)
233 return sampledValueTemplates
[index
];
236 if (measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
) {
237 const errorMsg
= `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`;
238 logger
.error(`${chargingStation.logPrefix()} ${errorMsg}`);
239 throw new BaseError(errorMsg
);
242 `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
246 protected static getLimitFromSampledValueTemplateCustomValue(
249 options
: { limitationEnabled
?: boolean; unitMultiplier
?: number } = {
250 limitationEnabled
: true,
254 options
.limitationEnabled
= options
?.limitationEnabled
?? true;
255 options
.unitMultiplier
= options
?.unitMultiplier
?? 1;
256 const parsedInt
= parseInt(value
);
257 const numberValue
= isNaN(parsedInt
) ? Infinity : parsedInt
;
258 return options
?.limitationEnabled
259 ? Math.min(numberValue
* options
.unitMultiplier
, limit
)
260 : numberValue
* options
.unitMultiplier
;