1 import { createHash
, randomBytes
} from
'node:crypto';
2 import type { EventEmitter
} from
'node:events';
3 import { basename
, dirname
, join
} from
'node:path';
4 import { fileURLToPath
} from
'node:url';
6 import chalk from
'chalk';
22 import type { ChargingStation
} from
'./ChargingStation';
23 import { BaseError
} from
'../exception';
27 type BootNotificationRequest
,
30 ChargingProfileKindType
,
32 type ChargingSchedulePeriod
,
33 type ChargingStationInfo
,
34 type ChargingStationTemplate
,
35 ChargingStationWorkerMessageEvents
,
36 ConnectorPhaseRotation
,
41 type OCPP16BootNotificationRequest
,
42 type OCPP20BootNotificationRequest
,
64 const moduleName
= 'ChargingStationUtils';
66 export const getChargingStationId
= (
68 stationTemplate
: ChargingStationTemplate
,
70 // In case of multiple instances: add instance index to charging station id
71 const instanceIndex
= process
.env
.CF_INSTANCE_INDEX
?? 0;
72 const idSuffix
= stationTemplate
?.nameSuffix
?? '';
73 const idStr
= `000000000${index.toString()}`;
74 return stationTemplate
?.fixedName
75 ? stationTemplate
.baseName
76 : `${stationTemplate.baseName}-${instanceIndex.toString()}${idStr.substring(
81 export const countReservableConnectors
= (connectors
: Map
<number, ConnectorStatus
>) => {
82 let reservableConnectors
= 0;
83 for (const [connectorId
, connectorStatus
] of connectors
) {
84 if (connectorId
=== 0) {
87 if (connectorStatus
.status === ConnectorStatusEnum
.Available
) {
88 ++reservableConnectors
;
91 return reservableConnectors
;
94 export const getHashId
= (index
: number, stationTemplate
: ChargingStationTemplate
): string => {
95 const chargingStationInfo
= {
96 chargePointModel
: stationTemplate
.chargePointModel
,
97 chargePointVendor
: stationTemplate
.chargePointVendor
,
98 ...(!isUndefined(stationTemplate
.chargeBoxSerialNumberPrefix
) && {
99 chargeBoxSerialNumber
: stationTemplate
.chargeBoxSerialNumberPrefix
,
101 ...(!isUndefined(stationTemplate
.chargePointSerialNumberPrefix
) && {
102 chargePointSerialNumber
: stationTemplate
.chargePointSerialNumberPrefix
,
104 ...(!isUndefined(stationTemplate
.meterSerialNumberPrefix
) && {
105 meterSerialNumber
: stationTemplate
.meterSerialNumberPrefix
,
107 ...(!isUndefined(stationTemplate
.meterType
) && {
108 meterType
: stationTemplate
.meterType
,
111 return createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
112 .update(`${JSON.stringify(chargingStationInfo)}${getChargingStationId(index, stationTemplate)}`)
116 export const checkChargingStation
= (
117 chargingStation
: ChargingStation
,
120 if (chargingStation
.started
=== false && chargingStation
.starting
=== false) {
121 logger
.warn(`${logPrefix} charging station is stopped, cannot proceed`);
127 export const getPhaseRotationValue
= (
129 numberOfPhases
: number,
130 ): string | undefined => {
132 if (connectorId
=== 0 && numberOfPhases
=== 0) {
133 return `${connectorId}.${ConnectorPhaseRotation.RST}`;
134 } else if (connectorId
> 0 && numberOfPhases
=== 0) {
135 return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`;
137 } else if (connectorId
> 0 && numberOfPhases
=== 1) {
138 return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`;
139 } else if (connectorId
> 0 && numberOfPhases
=== 3) {
140 return `${connectorId}.${ConnectorPhaseRotation.RST}`;
144 export const getMaxNumberOfEvses
= (evses
: Record
<string, EvseTemplate
>): number => {
148 return Object.keys(evses
).length
;
151 const getMaxNumberOfConnectors
= (connectors
: Record
<string, ConnectorStatus
>): number => {
155 return Object.keys(connectors
).length
;
158 export const getBootConnectorStatus
= (
159 chargingStation
: ChargingStation
,
161 connectorStatus
: ConnectorStatus
,
162 ): ConnectorStatusEnum
=> {
163 let connectorBootStatus
: ConnectorStatusEnum
;
165 !connectorStatus
?.status &&
166 (chargingStation
.isChargingStationAvailable() === false ||
167 chargingStation
.isConnectorAvailable(connectorId
) === false)
169 connectorBootStatus
= ConnectorStatusEnum
.Unavailable
;
170 } else if (!connectorStatus
?.status && connectorStatus
?.bootStatus
) {
171 // Set boot status in template at startup
172 connectorBootStatus
= connectorStatus
?.bootStatus
;
173 } else if (connectorStatus
?.status) {
174 // Set previous status at startup
175 connectorBootStatus
= connectorStatus
?.status;
177 // Set default status
178 connectorBootStatus
= ConnectorStatusEnum
.Available
;
180 return connectorBootStatus
;
183 export const checkTemplate
= (
184 stationTemplate
: ChargingStationTemplate
,
186 templateFile
: string,
188 if (isNullOrUndefined(stationTemplate
)) {
189 const errorMsg
= `Failed to read charging station template file ${templateFile}`;
190 logger
.error(`${logPrefix} ${errorMsg}`);
191 throw new BaseError(errorMsg
);
193 if (isEmptyObject(stationTemplate
)) {
194 const errorMsg
= `Empty charging station information from template file ${templateFile}`;
195 logger
.error(`${logPrefix} ${errorMsg}`);
196 throw new BaseError(errorMsg
);
198 if (isEmptyObject(stationTemplate
.AutomaticTransactionGenerator
!)) {
199 stationTemplate
.AutomaticTransactionGenerator
= Constants
.DEFAULT_ATG_CONFIGURATION
;
201 `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`,
202 Constants
.DEFAULT_ATG_CONFIGURATION
,
205 if (isNullOrUndefined(stationTemplate
.idTagsFile
) || isEmptyString(stationTemplate
.idTagsFile
)) {
207 `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator`,
212 export const checkConnectorsConfiguration
= (
213 stationTemplate
: ChargingStationTemplate
,
215 templateFile
: string,
217 configuredMaxConnectors
: number;
218 templateMaxConnectors
: number;
219 templateMaxAvailableConnectors
: number;
221 const configuredMaxConnectors
= getConfiguredNumberOfConnectors(stationTemplate
);
222 checkConfiguredMaxConnectors(configuredMaxConnectors
, logPrefix
, templateFile
);
223 const templateMaxConnectors
= getMaxNumberOfConnectors(stationTemplate
.Connectors
!);
224 checkTemplateMaxConnectors(templateMaxConnectors
, logPrefix
, templateFile
);
225 const templateMaxAvailableConnectors
= stationTemplate
.Connectors
![0]
226 ? templateMaxConnectors
- 1
227 : templateMaxConnectors
;
229 configuredMaxConnectors
> templateMaxAvailableConnectors
&&
230 !stationTemplate
?.randomConnectors
233 `${logPrefix} Number of connectors exceeds the number of connector configurations in template ${templateFile}, forcing random connector configurations affectation`,
235 stationTemplate
.randomConnectors
= true;
237 return { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
};
240 export const checkStationInfoConnectorStatus
= (
242 connectorStatus
: ConnectorStatus
,
244 templateFile
: string,
246 if (!isNullOrUndefined(connectorStatus
?.status)) {
248 `${logPrefix} Charging station information from template ${templateFile} with connector id ${connectorId} status configuration defined, undefine it`,
250 delete connectorStatus
.status;
254 export const buildConnectorsMap
= (
255 connectors
: Record
<string, ConnectorStatus
>,
257 templateFile
: string,
258 ): Map
<number, ConnectorStatus
> => {
259 const connectorsMap
= new Map
<number, ConnectorStatus
>();
260 if (getMaxNumberOfConnectors(connectors
) > 0) {
261 for (const connector
in connectors
) {
262 const connectorStatus
= connectors
[connector
];
263 const connectorId
= convertToInt(connector
);
264 checkStationInfoConnectorStatus(connectorId
, connectorStatus
, logPrefix
, templateFile
);
265 connectorsMap
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
269 `${logPrefix} Charging station information from template ${templateFile} with no connectors, cannot build connectors map`,
272 return connectorsMap
;
275 export const initializeConnectorsMapStatus
= (
276 connectors
: Map
<number, ConnectorStatus
>,
279 for (const connectorId
of connectors
.keys()) {
280 if (connectorId
> 0 && connectors
.get(connectorId
)?.transactionStarted
=== true) {
282 `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${connectors.get(
287 if (connectorId
=== 0) {
288 connectors
.get(connectorId
)!.availability
= AvailabilityType
.Operative
;
289 if (isUndefined(connectors
.get(connectorId
)?.chargingProfiles
)) {
290 connectors
.get(connectorId
)!.chargingProfiles
= [];
294 isNullOrUndefined(connectors
.get(connectorId
)?.transactionStarted
)
296 initializeConnectorStatus(connectors
.get(connectorId
)!);
301 export const resetConnectorStatus
= (connectorStatus
: ConnectorStatus
): void => {
302 connectorStatus
.idTagLocalAuthorized
= false;
303 connectorStatus
.idTagAuthorized
= false;
304 connectorStatus
.transactionRemoteStarted
= false;
305 connectorStatus
.transactionStarted
= false;
306 delete connectorStatus
?.transactionStart
;
307 delete connectorStatus
?.transactionId
;
308 delete connectorStatus
?.localAuthorizeIdTag
;
309 delete connectorStatus
?.authorizeIdTag
;
310 delete connectorStatus
?.transactionIdTag
;
311 connectorStatus
.transactionEnergyActiveImportRegisterValue
= 0;
312 delete connectorStatus
?.transactionBeginMeterValue
;
315 export const createBootNotificationRequest
= (
316 stationInfo
: ChargingStationInfo
,
317 bootReason
: BootReasonEnumType
= BootReasonEnumType
.PowerUp
,
318 ): BootNotificationRequest
=> {
319 const ocppVersion
= stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
320 switch (ocppVersion
) {
321 case OCPPVersion
.VERSION_16
:
323 chargePointModel
: stationInfo
.chargePointModel
,
324 chargePointVendor
: stationInfo
.chargePointVendor
,
325 ...(!isUndefined(stationInfo
.chargeBoxSerialNumber
) && {
326 chargeBoxSerialNumber
: stationInfo
.chargeBoxSerialNumber
,
328 ...(!isUndefined(stationInfo
.chargePointSerialNumber
) && {
329 chargePointSerialNumber
: stationInfo
.chargePointSerialNumber
,
331 ...(!isUndefined(stationInfo
.firmwareVersion
) && {
332 firmwareVersion
: stationInfo
.firmwareVersion
,
334 ...(!isUndefined(stationInfo
.iccid
) && { iccid
: stationInfo
.iccid
}),
335 ...(!isUndefined(stationInfo
.imsi
) && { imsi
: stationInfo
.imsi
}),
336 ...(!isUndefined(stationInfo
.meterSerialNumber
) && {
337 meterSerialNumber
: stationInfo
.meterSerialNumber
,
339 ...(!isUndefined(stationInfo
.meterType
) && {
340 meterType
: stationInfo
.meterType
,
342 } as OCPP16BootNotificationRequest
;
343 case OCPPVersion
.VERSION_20
:
344 case OCPPVersion
.VERSION_201
:
348 model
: stationInfo
.chargePointModel
,
349 vendorName
: stationInfo
.chargePointVendor
,
350 ...(!isUndefined(stationInfo
.firmwareVersion
) && {
351 firmwareVersion
: stationInfo
.firmwareVersion
,
353 ...(!isUndefined(stationInfo
.chargeBoxSerialNumber
) && {
354 serialNumber
: stationInfo
.chargeBoxSerialNumber
,
356 ...((!isUndefined(stationInfo
.iccid
) || !isUndefined(stationInfo
.imsi
)) && {
358 ...(!isUndefined(stationInfo
.iccid
) && { iccid
: stationInfo
.iccid
}),
359 ...(!isUndefined(stationInfo
.imsi
) && { imsi
: stationInfo
.imsi
}),
363 } as OCPP20BootNotificationRequest
;
367 export const warnTemplateKeysDeprecation
= (
368 stationTemplate
: ChargingStationTemplate
,
370 templateFile
: string,
372 const templateKeys
: { deprecatedKey
: string; key
?: string }[] = [
373 { deprecatedKey
: 'supervisionUrl', key
: 'supervisionUrls' },
374 { deprecatedKey
: 'authorizationFile', key
: 'idTagsFile' },
375 { deprecatedKey
: 'payloadSchemaValidation', key
: 'ocppStrictCompliance' },
377 for (const templateKey
of templateKeys
) {
378 warnDeprecatedTemplateKey(
380 templateKey
.deprecatedKey
,
383 !isUndefined(templateKey
.key
) ? `Use '${templateKey.key}' instead` : undefined,
385 convertDeprecatedTemplateKey(stationTemplate
, templateKey
.deprecatedKey
, templateKey
.key
);
389 export const stationTemplateToStationInfo
= (
390 stationTemplate
: ChargingStationTemplate
,
391 ): ChargingStationInfo
=> {
392 stationTemplate
= cloneObject
<ChargingStationTemplate
>(stationTemplate
);
393 delete stationTemplate
.power
;
394 delete stationTemplate
.powerUnit
;
395 delete stationTemplate
.Connectors
;
396 delete stationTemplate
.Evses
;
397 delete stationTemplate
.Configuration
;
398 delete stationTemplate
.AutomaticTransactionGenerator
;
399 delete stationTemplate
.chargeBoxSerialNumberPrefix
;
400 delete stationTemplate
.chargePointSerialNumberPrefix
;
401 delete stationTemplate
.meterSerialNumberPrefix
;
402 return stationTemplate
as unknown
as ChargingStationInfo
;
405 export const createSerialNumber
= (
406 stationTemplate
: ChargingStationTemplate
,
407 stationInfo
: ChargingStationInfo
,
409 randomSerialNumberUpperCase
?: boolean;
410 randomSerialNumber
?: boolean;
412 randomSerialNumberUpperCase
: true,
413 randomSerialNumber
: true,
416 params
= { ...{ randomSerialNumberUpperCase
: true, randomSerialNumber
: true }, ...params
};
417 const serialNumberSuffix
= params
?.randomSerialNumber
418 ? getRandomSerialNumberSuffix({
419 upperCase
: params
.randomSerialNumberUpperCase
,
422 isNotEmptyString(stationTemplate
?.chargePointSerialNumberPrefix
) &&
423 (stationInfo
.chargePointSerialNumber
= `${stationTemplate.chargePointSerialNumberPrefix}${serialNumberSuffix}`);
424 isNotEmptyString(stationTemplate
?.chargeBoxSerialNumberPrefix
) &&
425 (stationInfo
.chargeBoxSerialNumber
= `${stationTemplate.chargeBoxSerialNumberPrefix}${serialNumberSuffix}`);
426 isNotEmptyString(stationTemplate
?.meterSerialNumberPrefix
) &&
427 (stationInfo
.meterSerialNumber
= `${stationTemplate.meterSerialNumberPrefix}${serialNumberSuffix}`);
430 export const propagateSerialNumber
= (
431 stationTemplate
: ChargingStationTemplate
,
432 stationInfoSrc
: ChargingStationInfo
,
433 stationInfoDst
: ChargingStationInfo
,
435 if (!stationInfoSrc
|| !stationTemplate
) {
437 'Missing charging station template or existing configuration to propagate serial number',
440 stationTemplate
?.chargePointSerialNumberPrefix
&& stationInfoSrc
?.chargePointSerialNumber
441 ? (stationInfoDst
.chargePointSerialNumber
= stationInfoSrc
.chargePointSerialNumber
)
442 : stationInfoDst
?.chargePointSerialNumber
&& delete stationInfoDst
.chargePointSerialNumber
;
443 stationTemplate
?.chargeBoxSerialNumberPrefix
&& stationInfoSrc
?.chargeBoxSerialNumber
444 ? (stationInfoDst
.chargeBoxSerialNumber
= stationInfoSrc
.chargeBoxSerialNumber
)
445 : stationInfoDst
?.chargeBoxSerialNumber
&& delete stationInfoDst
.chargeBoxSerialNumber
;
446 stationTemplate
?.meterSerialNumberPrefix
&& stationInfoSrc
?.meterSerialNumber
447 ? (stationInfoDst
.meterSerialNumber
= stationInfoSrc
.meterSerialNumber
)
448 : stationInfoDst
?.meterSerialNumber
&& delete stationInfoDst
.meterSerialNumber
;
451 export const getAmperageLimitationUnitDivider
= (stationInfo
: ChargingStationInfo
): number => {
453 switch (stationInfo
.amperageLimitationUnit
) {
454 case AmpereUnits
.DECI_AMPERE
:
457 case AmpereUnits
.CENTI_AMPERE
:
460 case AmpereUnits
.MILLI_AMPERE
:
467 export const getChargingStationConnectorChargingProfilesPowerLimit
= (
468 chargingStation
: ChargingStation
,
470 ): number | undefined => {
471 let limit
: number | undefined, matchingChargingProfile
: ChargingProfile
| undefined;
472 // Get charging profiles for connector id and sort by stack level
473 const chargingProfiles
=
474 cloneObject
<ChargingProfile
[]>(
475 chargingStation
.getConnectorStatus(connectorId
)!.chargingProfiles
!,
476 )?.sort((a
, b
) => b
.stackLevel
- a
.stackLevel
) ?? [];
477 // Get charging profiles on connector 0 and sort by stack level
478 if (chargingStation
.getConnectorStatus(0)?.chargingProfiles
) {
479 chargingProfiles
.push(
480 ...cloneObject
<ChargingProfile
[]>(
481 chargingStation
.getConnectorStatus(0)!.chargingProfiles
!,
482 ).sort((a
, b
) => b
.stackLevel
- a
.stackLevel
),
485 if (isNotEmptyArray(chargingProfiles
)) {
486 const result
= getLimitFromChargingProfiles(
490 chargingStation
.logPrefix(),
492 if (!isNullOrUndefined(result
)) {
493 limit
= result
?.limit
;
494 matchingChargingProfile
= result
?.matchingChargingProfile
;
495 switch (chargingStation
.getCurrentOutType()) {
498 matchingChargingProfile
?.chargingSchedule
?.chargingRateUnit
===
499 ChargingRateUnitType
.WATT
501 : ACElectricUtils
.powerTotal(
502 chargingStation
.getNumberOfPhases(),
503 chargingStation
.getVoltageOut(),
509 matchingChargingProfile
?.chargingSchedule
?.chargingRateUnit
===
510 ChargingRateUnitType
.WATT
512 : DCElectricUtils
.power(chargingStation
.getVoltageOut(), limit
!);
514 const connectorMaximumPower
=
515 chargingStation
.getMaximumPower() / chargingStation
.powerDivider
;
516 if (limit
! > connectorMaximumPower
) {
518 `${chargingStation.logPrefix()} Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`,
521 limit
= connectorMaximumPower
;
528 export const getDefaultVoltageOut
= (
529 currentType
: CurrentType
,
531 templateFile
: string,
533 const errorMsg
= `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`;
534 let defaultVoltageOut
: number;
535 switch (currentType
) {
537 defaultVoltageOut
= Voltage
.VOLTAGE_230
;
540 defaultVoltageOut
= Voltage
.VOLTAGE_400
;
543 logger
.error(`${logPrefix} ${errorMsg}`);
544 throw new BaseError(errorMsg
);
546 return defaultVoltageOut
;
549 export const getIdTagsFile
= (stationInfo
: ChargingStationInfo
): string | undefined => {
551 stationInfo
.idTagsFile
&&
552 join(dirname(fileURLToPath(import.meta
.url
)), 'assets', basename(stationInfo
.idTagsFile
))
556 export const waitChargingStationEvents
= async (
557 emitter
: EventEmitter
,
558 event
: ChargingStationWorkerMessageEvents
,
559 eventsToWait
: number,
560 ): Promise
<number> => {
561 return new Promise
<number>((resolve
) => {
563 if (eventsToWait
=== 0) {
566 emitter
.on(event
, () => {
568 if (events
=== eventsToWait
) {
575 const getConfiguredNumberOfConnectors
= (stationTemplate
: ChargingStationTemplate
): number => {
576 let configuredMaxConnectors
= 0;
577 if (isNotEmptyArray(stationTemplate
.numberOfConnectors
) === true) {
578 const numberOfConnectors
= stationTemplate
.numberOfConnectors
as number[];
579 configuredMaxConnectors
=
580 numberOfConnectors
[Math.floor(secureRandom() * numberOfConnectors
.length
)];
581 } else if (isUndefined(stationTemplate
.numberOfConnectors
) === false) {
582 configuredMaxConnectors
= stationTemplate
.numberOfConnectors
as number;
583 } else if (stationTemplate
.Connectors
&& !stationTemplate
.Evses
) {
584 configuredMaxConnectors
= stationTemplate
.Connectors
[0]
585 ? getMaxNumberOfConnectors(stationTemplate
.Connectors
) - 1
586 : getMaxNumberOfConnectors(stationTemplate
.Connectors
);
587 } else if (stationTemplate
.Evses
&& !stationTemplate
.Connectors
) {
588 for (const evse
in stationTemplate
.Evses
) {
592 configuredMaxConnectors
+= getMaxNumberOfConnectors(stationTemplate
.Evses
[evse
].Connectors
);
595 return configuredMaxConnectors
;
598 const checkConfiguredMaxConnectors
= (
599 configuredMaxConnectors
: number,
601 templateFile
: string,
603 if (configuredMaxConnectors
<= 0) {
605 `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors`,
610 const checkTemplateMaxConnectors
= (
611 templateMaxConnectors
: number,
613 templateFile
: string,
615 if (templateMaxConnectors
=== 0) {
617 `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration`,
619 } else if (templateMaxConnectors
< 0) {
621 `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined`,
626 const initializeConnectorStatus
= (connectorStatus
: ConnectorStatus
): void => {
627 connectorStatus
.availability
= AvailabilityType
.Operative
;
628 connectorStatus
.idTagLocalAuthorized
= false;
629 connectorStatus
.idTagAuthorized
= false;
630 connectorStatus
.transactionRemoteStarted
= false;
631 connectorStatus
.transactionStarted
= false;
632 connectorStatus
.energyActiveImportRegisterValue
= 0;
633 connectorStatus
.transactionEnergyActiveImportRegisterValue
= 0;
634 if (isUndefined(connectorStatus
.chargingProfiles
)) {
635 connectorStatus
.chargingProfiles
= [];
639 const warnDeprecatedTemplateKey
= (
640 template
: ChargingStationTemplate
,
643 templateFile
: string,
646 if (!isUndefined(template
[key
as keyof ChargingStationTemplate
])) {
647 const logMsg
= `Deprecated template key '${key}' usage in file '${templateFile}'${
648 isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}
` : ''
650 logger
.warn(`${logPrefix} ${logMsg}`);
651 console
.warn(chalk
.yellow(`${logMsg}`));
655 const convertDeprecatedTemplateKey
= (
656 template
: ChargingStationTemplate
,
657 deprecatedKey
: string,
660 if (!isUndefined(template
[deprecatedKey
as keyof ChargingStationTemplate
])) {
661 if (!isUndefined(key
)) {
662 (template
as unknown
as Record
<string, unknown
>)[key
!] =
663 template
[deprecatedKey
as keyof ChargingStationTemplate
];
665 delete template
[deprecatedKey
as keyof ChargingStationTemplate
];
669 interface ChargingProfilesLimit
{
671 matchingChargingProfile
: ChargingProfile
;
675 * Charging profiles should already be sorted by connector id and stack level (highest stack level has priority)
677 * @param chargingProfiles -
679 * @returns ChargingProfilesLimit
681 const getLimitFromChargingProfiles
= (
682 chargingStation
: ChargingStation
,
684 chargingProfiles
: ChargingProfile
[],
686 ): ChargingProfilesLimit
| undefined => {
687 const debugLogMsg
= `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
688 const currentDate
= new Date();
689 for (const chargingProfile
of chargingProfiles
) {
691 chargingProfile
.validFrom
&&
692 chargingProfile
.validTo
&&
693 !isWithinInterval(currentDate
, {
694 start
: chargingProfile
.validFrom
,
695 end
: chargingProfile
.validTo
,
699 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${
700 chargingProfile.chargingProfileId
701 } is not valid for the current date ${currentDate.toISOString()}`,
705 const chargingSchedule
= chargingProfile
.chargingSchedule
;
706 if (!chargingSchedule
?.startSchedule
) {
708 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}. Trying to set it to the connector transaction start date`,
710 // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
711 chargingSchedule
.startSchedule
=
712 chargingStation
.getConnectorStatus(connectorId
)?.transactionStart
;
714 if (!(chargingSchedule
?.startSchedule
instanceof Date)) {
716 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`,
718 chargingSchedule
.startSchedule
= convertToDate(chargingSchedule
.startSchedule
)!;
721 chargingProfile
.chargingProfileKind
=== ChargingProfileKindType
.RECURRING
&&
722 isNullOrUndefined(chargingProfile
.recurrencyKind
)
725 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`,
729 // Adjust recurring start schedule
730 if (chargingProfile
.chargingProfileKind
=== ChargingProfileKindType
.RECURRING
) {
731 switch (chargingProfile
.recurrencyKind
) {
732 case RecurrencyKindType
.DAILY
:
733 if (isBefore(chargingSchedule
.startSchedule
, startOfDay(currentDate
))) {
735 chargingSchedule
.startSchedule
,
736 differenceInDays(chargingSchedule
.startSchedule
, endOfDay(currentDate
)),
739 isBefore(chargingSchedule
.startSchedule
, startOfDay(currentDate
)) ||
740 isAfter(chargingSchedule
.startSchedule
, endOfDay(currentDate
))
743 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
744 chargingProfile.recurrencyKind
745 } charging profile id ${
746 chargingProfile.chargingProfileId
747 } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current day`,
752 case RecurrencyKindType
.WEEKLY
:
753 if (isBefore(chargingSchedule
.startSchedule
, startOfWeek(currentDate
))) {
755 chargingSchedule
.startSchedule
,
756 differenceInWeeks(chargingSchedule
.startSchedule
, endOfWeek(currentDate
)),
759 isBefore(chargingSchedule
.startSchedule
, startOfWeek(currentDate
)) ||
760 isAfter(chargingSchedule
.startSchedule
, endOfWeek(currentDate
))
763 `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
764 chargingProfile.recurrencyKind
765 } charging profile id ${
766 chargingProfile.chargingProfileId
767 } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current week`,
773 } else if (chargingProfile
.chargingProfileKind
=== ChargingProfileKindType
.RELATIVE
) {
774 chargingSchedule
.startSchedule
=
775 chargingStation
.getConnectorStatus(connectorId
)?.transactionStart
;
777 // Check if the charging profile is active
779 isAfter(addSeconds(chargingSchedule
.startSchedule
!, chargingSchedule
.duration
!), currentDate
)
781 if (isNotEmptyArray(chargingSchedule
.chargingSchedulePeriod
)) {
782 // Handling of only one period
784 chargingSchedule
.chargingSchedulePeriod
.length
=== 1 &&
785 chargingSchedule
.chargingSchedulePeriod
[0].startPeriod
=== 0
787 const result
: ChargingProfilesLimit
= {
788 limit
: chargingSchedule
.chargingSchedulePeriod
[0].limit
,
789 matchingChargingProfile
: chargingProfile
,
791 logger
.debug(debugLogMsg
, result
);
794 let lastButOneSchedule
: ChargingSchedulePeriod
| undefined;
795 // Search for the right schedule period
796 for (const schedulePeriod
of chargingSchedule
.chargingSchedulePeriod
) {
797 // Find the right schedule period
800 addSeconds(chargingSchedule
.startSchedule
!, schedulePeriod
.startPeriod
),
804 // Found the schedule: last but one is the correct one
805 const result
: ChargingProfilesLimit
= {
806 limit
: lastButOneSchedule
!.limit
,
807 matchingChargingProfile
: chargingProfile
,
809 logger
.debug(debugLogMsg
, result
);
813 lastButOneSchedule
= schedulePeriod
;
814 // Handle the last schedule period
816 schedulePeriod
.startPeriod
===
817 chargingSchedule
.chargingSchedulePeriod
[
818 chargingSchedule
.chargingSchedulePeriod
.length
- 1
821 const result
: ChargingProfilesLimit
= {
822 limit
: lastButOneSchedule
.limit
,
823 matchingChargingProfile
: chargingProfile
,
825 logger
.debug(debugLogMsg
, result
);
834 const getRandomSerialNumberSuffix
= (params
?: {
835 randomBytesLength
?: number;
838 const randomSerialNumberSuffix
= randomBytes(params
?.randomBytesLength
?? 16).toString('hex');
839 if (params
?.upperCase
) {
840 return randomSerialNumberSuffix
.toUpperCase();
842 return randomSerialNumberSuffix
;