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
, 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 protected static getSampledValueTemplate(
77 chargingStation
: ChargingStation
,
79 measurand
: MeterValueMeasurand
= MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
80 phase
?: MeterValuePhase
81 ): SampledValueTemplate
| undefined {
82 const onPhaseStr
= phase
? `on phase ${phase} ` : '';
83 if (Constants
.SUPPORTED_MEASURANDS
.includes(measurand
) === false) {
85 `${chargingStation.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
90 measurand
!== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
91 !ChargingStationConfigurationUtils
.getConfigurationKey(
93 StandardParametersKey
.MeterValuesSampledData
94 )?.value
.includes(measurand
)
97 `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
98 StandardParametersKey.MeterValuesSampledData
103 const sampledValueTemplates
: SampledValueTemplate
[] =
104 chargingStation
.getConnectorStatus(connectorId
).MeterValues
;
107 !Utils
.isEmptyArray(sampledValueTemplates
) && index
< sampledValueTemplates
.length
;
111 Constants
.SUPPORTED_MEASURANDS
.includes(
112 sampledValueTemplates
[index
]?.measurand
??
113 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
117 `${chargingStation.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
121 sampledValueTemplates
[index
]?.phase
=== phase
&&
122 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
123 ChargingStationConfigurationUtils
.getConfigurationKey(
125 StandardParametersKey
.MeterValuesSampledData
126 )?.value
.includes(measurand
) === true
128 return sampledValueTemplates
[index
];
131 !sampledValueTemplates
[index
].phase
&&
132 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
133 ChargingStationConfigurationUtils
.getConfigurationKey(
135 StandardParametersKey
.MeterValuesSampledData
136 )?.value
.includes(measurand
) === true
138 return sampledValueTemplates
[index
];
140 measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
141 (!sampledValueTemplates
[index
].measurand
||
142 sampledValueTemplates
[index
].measurand
=== measurand
)
144 return sampledValueTemplates
[index
];
147 if (measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
) {
148 const errorMsg
= `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`;
149 logger
.error(`${chargingStation.logPrefix()} ${errorMsg}`);
150 throw new BaseError(errorMsg
);
153 `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
157 protected static getLimitFromSampledValueTemplateCustomValue(
160 options
: { limitationEnabled
?: boolean; unitMultiplier
?: number } = {
161 limitationEnabled
: true,
165 options
.limitationEnabled
= options
?.limitationEnabled
?? true;
166 options
.unitMultiplier
= options
?.unitMultiplier
?? 1;
167 const numberValue
= isNaN(parseInt(value
)) ? Infinity : parseInt(value
);
168 return options
?.limitationEnabled
169 ? Math.min(numberValue
* options
.unitMultiplier
, limit
)
170 : numberValue
* options
.unitMultiplier
;