Commit | Line | Data |
---|---|---|
d972af76 JB |
1 | import { createHash, randomBytes } from 'node:crypto'; |
2 | import type { EventEmitter } from 'node:events'; | |
3 | import { basename, dirname, join } from 'node:path'; | |
130783a7 | 4 | import { fileURLToPath } from 'node:url'; |
8114d10e | 5 | |
e302df1d | 6 | import chalk from 'chalk'; |
f924d466 JB |
7 | import { |
8 | addDays, | |
f924d466 JB |
9 | addSeconds, |
10 | addWeeks, | |
497588ef | 11 | differenceInDays, |
d476bc1b | 12 | differenceInSeconds, |
497588ef | 13 | differenceInWeeks, |
f924d466 JB |
14 | isAfter, |
15 | isBefore, | |
252a7d22 | 16 | isWithinInterval, |
522e4b05 | 17 | toDate, |
f924d466 | 18 | } from 'date-fns'; |
8114d10e | 19 | |
4c3c0d59 | 20 | import type { ChargingStation } from './ChargingStation'; |
268a74bb | 21 | import { BaseError } from '../exception'; |
981ebfbe | 22 | import { |
492cf6ab | 23 | AmpereUnits, |
04b1261c | 24 | AvailabilityType, |
268a74bb JB |
25 | type BootNotificationRequest, |
26 | BootReasonEnumType, | |
15068be9 | 27 | type ChargingProfile, |
268a74bb | 28 | ChargingProfileKindType, |
15068be9 JB |
29 | ChargingRateUnitType, |
30 | type ChargingSchedulePeriod, | |
268a74bb JB |
31 | type ChargingStationInfo, |
32 | type ChargingStationTemplate, | |
b1f1b0f6 | 33 | ChargingStationWorkerMessageEvents, |
dd08d43d | 34 | ConnectorPhaseRotation, |
a78ef5ed | 35 | type ConnectorStatus, |
c3b83130 | 36 | ConnectorStatusEnum, |
268a74bb | 37 | CurrentType, |
ae25f265 | 38 | type EvseTemplate, |
268a74bb JB |
39 | type OCPP16BootNotificationRequest, |
40 | type OCPP20BootNotificationRequest, | |
41 | OCPPVersion, | |
42 | RecurrencyKindType, | |
43 | Voltage, | |
44 | } from '../types'; | |
9bf0ef23 JB |
45 | import { |
46 | ACElectricUtils, | |
47 | Constants, | |
48 | DCElectricUtils, | |
49 | cloneObject, | |
b85cef4c | 50 | convertToDate, |
9bf0ef23 | 51 | convertToInt, |
80c58041 | 52 | isArraySorted, |
9bf0ef23 JB |
53 | isEmptyObject, |
54 | isEmptyString, | |
55 | isNotEmptyArray, | |
56 | isNotEmptyString, | |
57 | isNullOrUndefined, | |
58 | isUndefined, | |
76dab5a9 | 59 | isValidDate, |
9bf0ef23 JB |
60 | logger, |
61 | secureRandom, | |
62 | } from '../utils'; | |
17ac262c | 63 | |
91a4f151 JB |
64 | const moduleName = 'ChargingStationUtils'; |
65 | ||
fba11dc6 JB |
66 | export const getChargingStationId = ( |
67 | index: number, | |
5edd8ba0 | 68 | stationTemplate: ChargingStationTemplate, |
fba11dc6 JB |
69 | ): string => { |
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( | |
5edd8ba0 | 77 | idStr.length - 4, |
fba11dc6 JB |
78 | )}${idSuffix}`; |
79 | }; | |
80 | ||
81 | export const countReservableConnectors = (connectors: Map<number, ConnectorStatus>) => { | |
82 | let reservableConnectors = 0; | |
83 | for (const [connectorId, connectorStatus] of connectors) { | |
84 | if (connectorId === 0) { | |
85 | continue; | |
3fa7f799 | 86 | } |
fba11dc6 JB |
87 | if (connectorStatus.status === ConnectorStatusEnum.Available) { |
88 | ++reservableConnectors; | |
1bf29f5b | 89 | } |
1bf29f5b | 90 | } |
fba11dc6 JB |
91 | return reservableConnectors; |
92 | }; | |
93 | ||
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, | |
100 | }), | |
101 | ...(!isUndefined(stationTemplate.chargePointSerialNumberPrefix) && { | |
102 | chargePointSerialNumber: stationTemplate.chargePointSerialNumberPrefix, | |
103 | }), | |
104 | ...(!isUndefined(stationTemplate.meterSerialNumberPrefix) && { | |
105 | meterSerialNumber: stationTemplate.meterSerialNumberPrefix, | |
106 | }), | |
107 | ...(!isUndefined(stationTemplate.meterType) && { | |
108 | meterType: stationTemplate.meterType, | |
109 | }), | |
110 | }; | |
111 | return createHash(Constants.DEFAULT_HASH_ALGORITHM) | |
112 | .update(`${JSON.stringify(chargingStationInfo)}${getChargingStationId(index, stationTemplate)}`) | |
113 | .digest('hex'); | |
114 | }; | |
115 | ||
116 | export const checkChargingStation = ( | |
117 | chargingStation: ChargingStation, | |
5edd8ba0 | 118 | logPrefix: string, |
fba11dc6 JB |
119 | ): boolean => { |
120 | if (chargingStation.started === false && chargingStation.starting === false) { | |
121 | logger.warn(`${logPrefix} charging station is stopped, cannot proceed`); | |
122 | return false; | |
123 | } | |
124 | return true; | |
125 | }; | |
126 | ||
127 | export const getPhaseRotationValue = ( | |
128 | connectorId: number, | |
5edd8ba0 | 129 | numberOfPhases: number, |
fba11dc6 JB |
130 | ): string | undefined => { |
131 | // AC/DC | |
132 | if (connectorId === 0 && numberOfPhases === 0) { | |
133 | return `${connectorId}.${ConnectorPhaseRotation.RST}`; | |
134 | } else if (connectorId > 0 && numberOfPhases === 0) { | |
135 | return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`; | |
136 | // AC | |
137 | } else if (connectorId > 0 && numberOfPhases === 1) { | |
138 | return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`; | |
139 | } else if (connectorId > 0 && numberOfPhases === 3) { | |
140 | return `${connectorId}.${ConnectorPhaseRotation.RST}`; | |
141 | } | |
142 | }; | |
143 | ||
144 | export const getMaxNumberOfEvses = (evses: Record<string, EvseTemplate>): number => { | |
145 | if (!evses) { | |
146 | return -1; | |
147 | } | |
148 | return Object.keys(evses).length; | |
149 | }; | |
150 | ||
151 | const getMaxNumberOfConnectors = (connectors: Record<string, ConnectorStatus>): number => { | |
152 | if (!connectors) { | |
153 | return -1; | |
154 | } | |
155 | return Object.keys(connectors).length; | |
156 | }; | |
157 | ||
158 | export const getBootConnectorStatus = ( | |
159 | chargingStation: ChargingStation, | |
160 | connectorId: number, | |
5edd8ba0 | 161 | connectorStatus: ConnectorStatus, |
fba11dc6 JB |
162 | ): ConnectorStatusEnum => { |
163 | let connectorBootStatus: ConnectorStatusEnum; | |
164 | if ( | |
165 | !connectorStatus?.status && | |
166 | (chargingStation.isChargingStationAvailable() === false || | |
167 | chargingStation.isConnectorAvailable(connectorId) === false) | |
168 | ) { | |
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; | |
176 | } else { | |
177 | // Set default status | |
178 | connectorBootStatus = ConnectorStatusEnum.Available; | |
179 | } | |
180 | return connectorBootStatus; | |
181 | }; | |
182 | ||
183 | export const checkTemplate = ( | |
184 | stationTemplate: ChargingStationTemplate, | |
185 | logPrefix: string, | |
5edd8ba0 | 186 | templateFile: string, |
fba11dc6 JB |
187 | ): void => { |
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); | |
192 | } | |
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); | |
197 | } | |
e1d9a0f4 | 198 | if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator!)) { |
fba11dc6 JB |
199 | stationTemplate.AutomaticTransactionGenerator = Constants.DEFAULT_ATG_CONFIGURATION; |
200 | logger.warn( | |
201 | `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`, | |
5edd8ba0 | 202 | Constants.DEFAULT_ATG_CONFIGURATION, |
fba11dc6 | 203 | ); |
fa7bccf4 | 204 | } |
fba11dc6 JB |
205 | if (isNullOrUndefined(stationTemplate.idTagsFile) || isEmptyString(stationTemplate.idTagsFile)) { |
206 | logger.warn( | |
5edd8ba0 | 207 | `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator`, |
fba11dc6 | 208 | ); |
c3b83130 | 209 | } |
fba11dc6 JB |
210 | }; |
211 | ||
212 | export const checkConnectorsConfiguration = ( | |
213 | stationTemplate: ChargingStationTemplate, | |
214 | logPrefix: string, | |
5edd8ba0 | 215 | templateFile: string, |
fba11dc6 JB |
216 | ): { |
217 | configuredMaxConnectors: number; | |
218 | templateMaxConnectors: number; | |
219 | templateMaxAvailableConnectors: number; | |
220 | } => { | |
221 | const configuredMaxConnectors = getConfiguredNumberOfConnectors(stationTemplate); | |
222 | checkConfiguredMaxConnectors(configuredMaxConnectors, logPrefix, templateFile); | |
e1d9a0f4 | 223 | const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors!); |
fba11dc6 | 224 | checkTemplateMaxConnectors(templateMaxConnectors, logPrefix, templateFile); |
e1d9a0f4 | 225 | const templateMaxAvailableConnectors = stationTemplate.Connectors![0] |
fba11dc6 JB |
226 | ? templateMaxConnectors - 1 |
227 | : templateMaxConnectors; | |
228 | if ( | |
229 | configuredMaxConnectors > templateMaxAvailableConnectors && | |
230 | !stationTemplate?.randomConnectors | |
8a133cc8 | 231 | ) { |
fba11dc6 | 232 | logger.warn( |
5edd8ba0 | 233 | `${logPrefix} Number of connectors exceeds the number of connector configurations in template ${templateFile}, forcing random connector configurations affectation`, |
cda5d0fb | 234 | ); |
fba11dc6 JB |
235 | stationTemplate.randomConnectors = true; |
236 | } | |
237 | return { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors }; | |
238 | }; | |
239 | ||
240 | export const checkStationInfoConnectorStatus = ( | |
241 | connectorId: number, | |
242 | connectorStatus: ConnectorStatus, | |
243 | logPrefix: string, | |
5edd8ba0 | 244 | templateFile: string, |
fba11dc6 JB |
245 | ): void => { |
246 | if (!isNullOrUndefined(connectorStatus?.status)) { | |
247 | logger.warn( | |
5edd8ba0 | 248 | `${logPrefix} Charging station information from template ${templateFile} with connector id ${connectorId} status configuration defined, undefine it`, |
cda5d0fb | 249 | ); |
fba11dc6 JB |
250 | delete connectorStatus.status; |
251 | } | |
252 | }; | |
253 | ||
254 | export const buildConnectorsMap = ( | |
255 | connectors: Record<string, ConnectorStatus>, | |
256 | logPrefix: string, | |
5edd8ba0 | 257 | templateFile: string, |
fba11dc6 JB |
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)); | |
fa7bccf4 | 266 | } |
fba11dc6 JB |
267 | } else { |
268 | logger.warn( | |
5edd8ba0 | 269 | `${logPrefix} Charging station information from template ${templateFile} with no connectors, cannot build connectors map`, |
fba11dc6 | 270 | ); |
fa7bccf4 | 271 | } |
fba11dc6 JB |
272 | return connectorsMap; |
273 | }; | |
fa7bccf4 | 274 | |
fba11dc6 JB |
275 | export const initializeConnectorsMapStatus = ( |
276 | connectors: Map<number, ConnectorStatus>, | |
5edd8ba0 | 277 | logPrefix: string, |
fba11dc6 JB |
278 | ): void => { |
279 | for (const connectorId of connectors.keys()) { | |
280 | if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted === true) { | |
04b1261c | 281 | logger.warn( |
5edd8ba0 JB |
282 | `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${connectors.get( |
283 | connectorId, | |
284 | )?.transactionId}`, | |
04b1261c | 285 | ); |
04b1261c | 286 | } |
fba11dc6 | 287 | if (connectorId === 0) { |
e1d9a0f4 | 288 | connectors.get(connectorId)!.availability = AvailabilityType.Operative; |
fba11dc6 | 289 | if (isUndefined(connectors.get(connectorId)?.chargingProfiles)) { |
e1d9a0f4 | 290 | connectors.get(connectorId)!.chargingProfiles = []; |
04b1261c | 291 | } |
fba11dc6 JB |
292 | } else if ( |
293 | connectorId > 0 && | |
294 | isNullOrUndefined(connectors.get(connectorId)?.transactionStarted) | |
295 | ) { | |
e1d9a0f4 | 296 | initializeConnectorStatus(connectors.get(connectorId)!); |
04b1261c JB |
297 | } |
298 | } | |
fba11dc6 JB |
299 | }; |
300 | ||
301 | export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => { | |
302 | connectorStatus.idTagLocalAuthorized = false; | |
303 | connectorStatus.idTagAuthorized = false; | |
304 | connectorStatus.transactionRemoteStarted = false; | |
305 | connectorStatus.transactionStarted = false; | |
a71d4e70 JB |
306 | delete connectorStatus?.transactionStart; |
307 | delete connectorStatus?.transactionId; | |
fba11dc6 JB |
308 | delete connectorStatus?.localAuthorizeIdTag; |
309 | delete connectorStatus?.authorizeIdTag; | |
fba11dc6 JB |
310 | delete connectorStatus?.transactionIdTag; |
311 | connectorStatus.transactionEnergyActiveImportRegisterValue = 0; | |
312 | delete connectorStatus?.transactionBeginMeterValue; | |
313 | }; | |
314 | ||
315 | export const createBootNotificationRequest = ( | |
316 | stationInfo: ChargingStationInfo, | |
5edd8ba0 | 317 | bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp, |
fba11dc6 JB |
318 | ): BootNotificationRequest => { |
319 | const ocppVersion = stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; | |
320 | switch (ocppVersion) { | |
321 | case OCPPVersion.VERSION_16: | |
322 | return { | |
323 | chargePointModel: stationInfo.chargePointModel, | |
324 | chargePointVendor: stationInfo.chargePointVendor, | |
325 | ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && { | |
326 | chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber, | |
327 | }), | |
328 | ...(!isUndefined(stationInfo.chargePointSerialNumber) && { | |
329 | chargePointSerialNumber: stationInfo.chargePointSerialNumber, | |
330 | }), | |
331 | ...(!isUndefined(stationInfo.firmwareVersion) && { | |
332 | firmwareVersion: stationInfo.firmwareVersion, | |
333 | }), | |
334 | ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }), | |
335 | ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }), | |
336 | ...(!isUndefined(stationInfo.meterSerialNumber) && { | |
337 | meterSerialNumber: stationInfo.meterSerialNumber, | |
338 | }), | |
339 | ...(!isUndefined(stationInfo.meterType) && { | |
340 | meterType: stationInfo.meterType, | |
341 | }), | |
342 | } as OCPP16BootNotificationRequest; | |
343 | case OCPPVersion.VERSION_20: | |
344 | case OCPPVersion.VERSION_201: | |
345 | return { | |
346 | reason: bootReason, | |
347 | chargingStation: { | |
348 | model: stationInfo.chargePointModel, | |
349 | vendorName: stationInfo.chargePointVendor, | |
9bf0ef23 | 350 | ...(!isUndefined(stationInfo.firmwareVersion) && { |
d270cc87 JB |
351 | firmwareVersion: stationInfo.firmwareVersion, |
352 | }), | |
fba11dc6 JB |
353 | ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && { |
354 | serialNumber: stationInfo.chargeBoxSerialNumber, | |
d270cc87 | 355 | }), |
fba11dc6 JB |
356 | ...((!isUndefined(stationInfo.iccid) || !isUndefined(stationInfo.imsi)) && { |
357 | modem: { | |
358 | ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }), | |
359 | ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }), | |
360 | }, | |
d270cc87 | 361 | }), |
fba11dc6 JB |
362 | }, |
363 | } as OCPP20BootNotificationRequest; | |
364 | } | |
365 | }; | |
366 | ||
367 | export const warnTemplateKeysDeprecation = ( | |
368 | stationTemplate: ChargingStationTemplate, | |
369 | logPrefix: string, | |
5edd8ba0 | 370 | templateFile: string, |
fba11dc6 | 371 | ) => { |
e4c6cf05 JB |
372 | const templateKeys: { deprecatedKey: string; key?: string }[] = [ |
373 | { deprecatedKey: 'supervisionUrl', key: 'supervisionUrls' }, | |
374 | { deprecatedKey: 'authorizationFile', key: 'idTagsFile' }, | |
375 | { deprecatedKey: 'payloadSchemaValidation', key: 'ocppStrictCompliance' }, | |
fba11dc6 JB |
376 | ]; |
377 | for (const templateKey of templateKeys) { | |
378 | warnDeprecatedTemplateKey( | |
379 | stationTemplate, | |
380 | templateKey.deprecatedKey, | |
381 | logPrefix, | |
382 | templateFile, | |
e1d9a0f4 | 383 | !isUndefined(templateKey.key) ? `Use '${templateKey.key}' instead` : undefined, |
fba11dc6 JB |
384 | ); |
385 | convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key); | |
386 | } | |
387 | }; | |
388 | ||
389 | export const stationTemplateToStationInfo = ( | |
5edd8ba0 | 390 | stationTemplate: ChargingStationTemplate, |
fba11dc6 JB |
391 | ): ChargingStationInfo => { |
392 | stationTemplate = cloneObject<ChargingStationTemplate>(stationTemplate); | |
393 | delete stationTemplate.power; | |
394 | delete stationTemplate.powerUnit; | |
e1d9a0f4 JB |
395 | delete stationTemplate.Connectors; |
396 | delete stationTemplate.Evses; | |
fba11dc6 JB |
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; | |
403 | }; | |
404 | ||
405 | export const createSerialNumber = ( | |
406 | stationTemplate: ChargingStationTemplate, | |
407 | stationInfo: ChargingStationInfo, | |
408 | params: { | |
409 | randomSerialNumberUpperCase?: boolean; | |
410 | randomSerialNumber?: boolean; | |
411 | } = { | |
412 | randomSerialNumberUpperCase: true, | |
413 | randomSerialNumber: true, | |
5edd8ba0 | 414 | }, |
fba11dc6 JB |
415 | ): void => { |
416 | params = { ...{ randomSerialNumberUpperCase: true, randomSerialNumber: true }, ...params }; | |
417 | const serialNumberSuffix = params?.randomSerialNumber | |
418 | ? getRandomSerialNumberSuffix({ | |
419 | upperCase: params.randomSerialNumberUpperCase, | |
420 | }) | |
421 | : ''; | |
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}`); | |
428 | }; | |
429 | ||
430 | export const propagateSerialNumber = ( | |
431 | stationTemplate: ChargingStationTemplate, | |
432 | stationInfoSrc: ChargingStationInfo, | |
5edd8ba0 | 433 | stationInfoDst: ChargingStationInfo, |
fba11dc6 JB |
434 | ) => { |
435 | if (!stationInfoSrc || !stationTemplate) { | |
436 | throw new BaseError( | |
5edd8ba0 | 437 | 'Missing charging station template or existing configuration to propagate serial number', |
fba11dc6 | 438 | ); |
17ac262c | 439 | } |
fba11dc6 JB |
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; | |
449 | }; | |
450 | ||
451 | export const getAmperageLimitationUnitDivider = (stationInfo: ChargingStationInfo): number => { | |
452 | let unitDivider = 1; | |
453 | switch (stationInfo.amperageLimitationUnit) { | |
454 | case AmpereUnits.DECI_AMPERE: | |
455 | unitDivider = 10; | |
456 | break; | |
457 | case AmpereUnits.CENTI_AMPERE: | |
458 | unitDivider = 100; | |
459 | break; | |
460 | case AmpereUnits.MILLI_AMPERE: | |
461 | unitDivider = 1000; | |
462 | break; | |
463 | } | |
464 | return unitDivider; | |
465 | }; | |
466 | ||
467 | export const getChargingStationConnectorChargingProfilesPowerLimit = ( | |
468 | chargingStation: ChargingStation, | |
5edd8ba0 | 469 | connectorId: number, |
fba11dc6 | 470 | ): number | undefined => { |
e1d9a0f4 | 471 | let limit: number | undefined, matchingChargingProfile: ChargingProfile | undefined; |
252a7d22 | 472 | // Get charging profiles for connector id and sort by stack level |
fba11dc6 JB |
473 | const chargingProfiles = |
474 | cloneObject<ChargingProfile[]>( | |
e1d9a0f4 | 475 | chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!, |
fba11dc6 | 476 | )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? []; |
252a7d22 | 477 | // Get charging profiles on connector 0 and sort by stack level |
710d50eb | 478 | if (isNotEmptyArray(chargingStation.getConnectorStatus(0)?.chargingProfiles)) { |
fba11dc6 JB |
479 | chargingProfiles.push( |
480 | ...cloneObject<ChargingProfile[]>( | |
e1d9a0f4 | 481 | chargingStation.getConnectorStatus(0)!.chargingProfiles!, |
5edd8ba0 | 482 | ).sort((a, b) => b.stackLevel - a.stackLevel), |
fba11dc6 | 483 | ); |
17ac262c | 484 | } |
fba11dc6 | 485 | if (isNotEmptyArray(chargingProfiles)) { |
a71d4e70 JB |
486 | const result = getLimitFromChargingProfiles( |
487 | chargingStation, | |
488 | connectorId, | |
489 | chargingProfiles, | |
490 | chargingStation.logPrefix(), | |
491 | ); | |
fba11dc6 JB |
492 | if (!isNullOrUndefined(result)) { |
493 | limit = result?.limit; | |
494 | matchingChargingProfile = result?.matchingChargingProfile; | |
495 | switch (chargingStation.getCurrentOutType()) { | |
496 | case CurrentType.AC: | |
497 | limit = | |
e1d9a0f4 JB |
498 | matchingChargingProfile?.chargingSchedule?.chargingRateUnit === |
499 | ChargingRateUnitType.WATT | |
fba11dc6 JB |
500 | ? limit |
501 | : ACElectricUtils.powerTotal( | |
502 | chargingStation.getNumberOfPhases(), | |
503 | chargingStation.getVoltageOut(), | |
e1d9a0f4 | 504 | limit!, |
fba11dc6 JB |
505 | ); |
506 | break; | |
507 | case CurrentType.DC: | |
508 | limit = | |
e1d9a0f4 JB |
509 | matchingChargingProfile?.chargingSchedule?.chargingRateUnit === |
510 | ChargingRateUnitType.WATT | |
fba11dc6 | 511 | ? limit |
e1d9a0f4 | 512 | : DCElectricUtils.power(chargingStation.getVoltageOut(), limit!); |
fba11dc6 JB |
513 | } |
514 | const connectorMaximumPower = | |
515 | chargingStation.getMaximumPower() / chargingStation.powerDivider; | |
e1d9a0f4 | 516 | if (limit! > connectorMaximumPower) { |
fba11dc6 | 517 | logger.error( |
aa5c5ad4 | 518 | `${chargingStation.logPrefix()} ${moduleName}.getChargingStationConnectorChargingProfilesPowerLimit: Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`, |
5edd8ba0 | 519 | result, |
fba11dc6 JB |
520 | ); |
521 | limit = connectorMaximumPower; | |
15068be9 JB |
522 | } |
523 | } | |
15068be9 | 524 | } |
fba11dc6 JB |
525 | return limit; |
526 | }; | |
527 | ||
528 | export const getDefaultVoltageOut = ( | |
529 | currentType: CurrentType, | |
530 | logPrefix: string, | |
5edd8ba0 | 531 | templateFile: string, |
fba11dc6 JB |
532 | ): Voltage => { |
533 | const errorMsg = `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`; | |
534 | let defaultVoltageOut: number; | |
535 | switch (currentType) { | |
536 | case CurrentType.AC: | |
537 | defaultVoltageOut = Voltage.VOLTAGE_230; | |
538 | break; | |
539 | case CurrentType.DC: | |
540 | defaultVoltageOut = Voltage.VOLTAGE_400; | |
541 | break; | |
542 | default: | |
543 | logger.error(`${logPrefix} ${errorMsg}`); | |
544 | throw new BaseError(errorMsg); | |
15068be9 | 545 | } |
fba11dc6 JB |
546 | return defaultVoltageOut; |
547 | }; | |
548 | ||
549 | export const getIdTagsFile = (stationInfo: ChargingStationInfo): string | undefined => { | |
550 | return ( | |
551 | stationInfo.idTagsFile && | |
552 | join(dirname(fileURLToPath(import.meta.url)), 'assets', basename(stationInfo.idTagsFile)) | |
553 | ); | |
554 | }; | |
555 | ||
b2b60626 | 556 | export const waitChargingStationEvents = async ( |
fba11dc6 JB |
557 | emitter: EventEmitter, |
558 | event: ChargingStationWorkerMessageEvents, | |
5edd8ba0 | 559 | eventsToWait: number, |
fba11dc6 | 560 | ): Promise<number> => { |
474d4ffc | 561 | return new Promise<number>((resolve) => { |
fba11dc6 JB |
562 | let events = 0; |
563 | if (eventsToWait === 0) { | |
564 | resolve(events); | |
565 | } | |
566 | emitter.on(event, () => { | |
567 | ++events; | |
568 | if (events === eventsToWait) { | |
b1f1b0f6 JB |
569 | resolve(events); |
570 | } | |
b1f1b0f6 | 571 | }); |
fba11dc6 JB |
572 | }); |
573 | }; | |
574 | ||
575 | const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => { | |
e1d9a0f4 | 576 | let configuredMaxConnectors = 0; |
fba11dc6 JB |
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) { | |
e1d9a0f4 | 584 | configuredMaxConnectors = stationTemplate.Connectors[0] |
fba11dc6 JB |
585 | ? getMaxNumberOfConnectors(stationTemplate.Connectors) - 1 |
586 | : getMaxNumberOfConnectors(stationTemplate.Connectors); | |
587 | } else if (stationTemplate.Evses && !stationTemplate.Connectors) { | |
fba11dc6 JB |
588 | for (const evse in stationTemplate.Evses) { |
589 | if (evse === '0') { | |
590 | continue; | |
cda5d0fb | 591 | } |
fba11dc6 | 592 | configuredMaxConnectors += getMaxNumberOfConnectors(stationTemplate.Evses[evse].Connectors); |
cda5d0fb | 593 | } |
cda5d0fb | 594 | } |
fba11dc6 JB |
595 | return configuredMaxConnectors; |
596 | }; | |
597 | ||
598 | const checkConfiguredMaxConnectors = ( | |
599 | configuredMaxConnectors: number, | |
600 | logPrefix: string, | |
5edd8ba0 | 601 | templateFile: string, |
fba11dc6 JB |
602 | ): void => { |
603 | if (configuredMaxConnectors <= 0) { | |
604 | logger.warn( | |
5edd8ba0 | 605 | `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors`, |
fba11dc6 | 606 | ); |
cda5d0fb | 607 | } |
fba11dc6 | 608 | }; |
cda5d0fb | 609 | |
fba11dc6 JB |
610 | const checkTemplateMaxConnectors = ( |
611 | templateMaxConnectors: number, | |
612 | logPrefix: string, | |
5edd8ba0 | 613 | templateFile: string, |
fba11dc6 JB |
614 | ): void => { |
615 | if (templateMaxConnectors === 0) { | |
616 | logger.warn( | |
5edd8ba0 | 617 | `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration`, |
fba11dc6 JB |
618 | ); |
619 | } else if (templateMaxConnectors < 0) { | |
620 | logger.error( | |
5edd8ba0 | 621 | `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined`, |
fba11dc6 JB |
622 | ); |
623 | } | |
624 | }; | |
625 | ||
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 = []; | |
636 | } | |
637 | }; | |
638 | ||
639 | const warnDeprecatedTemplateKey = ( | |
640 | template: ChargingStationTemplate, | |
641 | key: string, | |
642 | logPrefix: string, | |
643 | templateFile: string, | |
5edd8ba0 | 644 | logMsgToAppend = '', |
fba11dc6 | 645 | ): void => { |
a37fc6dc | 646 | if (!isUndefined(template[key as keyof ChargingStationTemplate])) { |
fba11dc6 JB |
647 | const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ |
648 | isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' | |
649 | }`; | |
650 | logger.warn(`${logPrefix} ${logMsg}`); | |
651 | console.warn(chalk.yellow(`${logMsg}`)); | |
652 | } | |
653 | }; | |
654 | ||
655 | const convertDeprecatedTemplateKey = ( | |
656 | template: ChargingStationTemplate, | |
657 | deprecatedKey: string, | |
e1d9a0f4 | 658 | key?: string, |
fba11dc6 | 659 | ): void => { |
a37fc6dc | 660 | if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) { |
e1d9a0f4 | 661 | if (!isUndefined(key)) { |
a37fc6dc JB |
662 | (template as unknown as Record<string, unknown>)[key!] = |
663 | template[deprecatedKey as keyof ChargingStationTemplate]; | |
e1d9a0f4 | 664 | } |
a37fc6dc | 665 | delete template[deprecatedKey as keyof ChargingStationTemplate]; |
fba11dc6 JB |
666 | } |
667 | }; | |
668 | ||
947f048a JB |
669 | interface ChargingProfilesLimit { |
670 | limit: number; | |
671 | matchingChargingProfile: ChargingProfile; | |
672 | } | |
673 | ||
fba11dc6 JB |
674 | /** |
675 | * Charging profiles should already be sorted by connector id and stack level (highest stack level has priority) | |
676 | * | |
677 | * @param chargingProfiles - | |
678 | * @param logPrefix - | |
947f048a | 679 | * @returns ChargingProfilesLimit |
fba11dc6 JB |
680 | */ |
681 | const getLimitFromChargingProfiles = ( | |
a71d4e70 JB |
682 | chargingStation: ChargingStation, |
683 | connectorId: number, | |
fba11dc6 | 684 | chargingProfiles: ChargingProfile[], |
5edd8ba0 | 685 | logPrefix: string, |
947f048a | 686 | ): ChargingProfilesLimit | undefined => { |
fba11dc6 | 687 | const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`; |
fba11dc6 | 688 | const currentDate = new Date(); |
b5c19509 | 689 | const connectorStatus = chargingStation.getConnectorStatus(connectorId); |
fba11dc6 | 690 | for (const chargingProfile of chargingProfiles) { |
252a7d22 | 691 | if ( |
aa5c5ad4 JB |
692 | (isValidDate(chargingProfile.validFrom) && |
693 | isBefore(currentDate, chargingProfile.validFrom!)) || | |
694 | (isValidDate(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo!)) | |
252a7d22 JB |
695 | ) { |
696 | logger.debug( | |
697 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${ | |
698 | chargingProfile.chargingProfileId | |
699 | } is not valid for the current date ${currentDate.toISOString()}`, | |
700 | ); | |
701 | continue; | |
702 | } | |
fba11dc6 | 703 | const chargingSchedule = chargingProfile.chargingSchedule; |
b5c19509 | 704 | if (connectorStatus?.transactionStarted && !chargingSchedule?.startSchedule) { |
109c677a | 705 | logger.debug( |
a71d4e70 | 706 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}. Trying to set it to the connector transaction start date`, |
cda5d0fb | 707 | ); |
a71d4e70 | 708 | // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction |
b5c19509 | 709 | chargingSchedule.startSchedule = connectorStatus?.transactionStart; |
52952bf8 | 710 | } |
8d75a403 JB |
711 | if (!(chargingSchedule?.startSchedule instanceof Date)) { |
712 | logger.warn( | |
713 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`, | |
714 | ); | |
715 | chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; | |
716 | } | |
252a7d22 JB |
717 | if ( |
718 | chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && | |
719 | isNullOrUndefined(chargingProfile.recurrencyKind) | |
720 | ) { | |
721 | logger.error( | |
722 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`, | |
723 | ); | |
724 | continue; | |
725 | } | |
f924d466 | 726 | if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { |
76dab5a9 | 727 | prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); |
b5c19509 JB |
728 | } else if ( |
729 | chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE && | |
730 | connectorStatus?.transactionStarted | |
731 | ) { | |
732 | chargingSchedule.startSchedule = connectorStatus?.transactionStart; | |
fba11dc6 JB |
733 | } |
734 | // Check if the charging profile is active | |
735 | if ( | |
b5c19509 | 736 | isValidDate(chargingSchedule.startSchedule) && |
975e18ec JB |
737 | isWithinInterval(currentDate, { |
738 | start: chargingSchedule.startSchedule!, | |
739 | end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), | |
740 | }) | |
fba11dc6 | 741 | ) { |
252a7d22 | 742 | if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { |
80c58041 JB |
743 | const chargingSchedulePeriodCompareFn = ( |
744 | a: ChargingSchedulePeriod, | |
745 | b: ChargingSchedulePeriod, | |
746 | ) => a.startPeriod - b.startPeriod; | |
747 | if ( | |
748 | isArraySorted<ChargingSchedulePeriod>( | |
749 | chargingSchedule.chargingSchedulePeriod, | |
750 | chargingSchedulePeriodCompareFn, | |
751 | ) === false | |
752 | ) { | |
753 | logger.warn( | |
754 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} schedule periods are not sorted by start period`, | |
755 | ); | |
756 | chargingSchedule.chargingSchedulePeriod.sort(chargingSchedulePeriodCompareFn); | |
757 | } | |
975e18ec | 758 | // Check if the first schedule period start period is equal to 0 |
55f2ab60 JB |
759 | if (chargingSchedule.chargingSchedulePeriod[0].startPeriod !== 0) { |
760 | logger.error( | |
761 | `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} first schedule period start period ${chargingSchedule.chargingSchedulePeriod[0].startPeriod} is not equal to 0`, | |
762 | ); | |
975e18ec | 763 | continue; |
55f2ab60 | 764 | } |
975e18ec JB |
765 | // Handling of only one schedule period |
766 | if (chargingSchedule.chargingSchedulePeriod.length === 1) { | |
252a7d22 JB |
767 | const result: ChargingProfilesLimit = { |
768 | limit: chargingSchedule.chargingSchedulePeriod[0].limit, | |
fba11dc6 JB |
769 | matchingChargingProfile: chargingProfile, |
770 | }; | |
771 | logger.debug(debugLogMsg, result); | |
772 | return result; | |
41189456 | 773 | } |
252a7d22 JB |
774 | let lastButOneSchedule: ChargingSchedulePeriod | undefined; |
775 | // Search for the right schedule period | |
975e18ec | 776 | for (const [index, schedulePeriod] of chargingSchedule.chargingSchedulePeriod.entries()) { |
252a7d22 JB |
777 | // Find the right schedule period |
778 | if ( | |
779 | isAfter( | |
780 | addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), | |
781 | currentDate, | |
782 | ) | |
783 | ) { | |
b5c19509 | 784 | // Found the schedule period: last but one is the correct one |
252a7d22 JB |
785 | const result: ChargingProfilesLimit = { |
786 | limit: lastButOneSchedule!.limit, | |
787 | matchingChargingProfile: chargingProfile, | |
788 | }; | |
789 | logger.debug(debugLogMsg, result); | |
790 | return result; | |
791 | } | |
792 | // Keep it | |
793 | lastButOneSchedule = schedulePeriod; | |
975e18ec | 794 | // Handle the last schedule period within the charging profile duration |
252a7d22 | 795 | if ( |
975e18ec JB |
796 | index === chargingSchedule.chargingSchedulePeriod.length - 1 || |
797 | (index < chargingSchedule.chargingSchedulePeriod.length - 1 && | |
798 | chargingSchedule.duration! > | |
799 | differenceInSeconds( | |
975e18ec JB |
800 | addSeconds( |
801 | chargingSchedule.startSchedule!, | |
802 | chargingSchedule.chargingSchedulePeriod[index + 1].startPeriod, | |
803 | ), | |
d9dc6292 | 804 | chargingSchedule.startSchedule!, |
975e18ec | 805 | )) |
252a7d22 JB |
806 | ) { |
807 | const result: ChargingProfilesLimit = { | |
808 | limit: lastButOneSchedule.limit, | |
809 | matchingChargingProfile: chargingProfile, | |
810 | }; | |
811 | logger.debug(debugLogMsg, result); | |
812 | return result; | |
813 | } | |
17ac262c JB |
814 | } |
815 | } | |
816 | } | |
17ac262c | 817 | } |
fba11dc6 | 818 | }; |
17ac262c | 819 | |
522e4b05 JB |
820 | /** |
821 | * Adjust recurring charging profile startSchedule to the current recurrency time interval if needed | |
822 | * | |
823 | * @param chargingProfile - | |
824 | * @param currentDate - | |
825 | * @param logPrefix - | |
826 | */ | |
76dab5a9 JB |
827 | const prepareRecurringChargingProfile = ( |
828 | chargingProfile: ChargingProfile, | |
829 | currentDate: Date, | |
830 | logPrefix: string, | |
831 | ) => { | |
832 | const chargingSchedule = chargingProfile.chargingSchedule; | |
522e4b05 | 833 | let recurringInterval: Interval; |
76dab5a9 JB |
834 | switch (chargingProfile.recurrencyKind) { |
835 | case RecurrencyKindType.DAILY: | |
522e4b05 JB |
836 | recurringInterval = { |
837 | start: chargingSchedule.startSchedule!, | |
838 | end: addDays(chargingSchedule.startSchedule!, 1), | |
839 | }; | |
d476bc1b | 840 | checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix); |
522e4b05 JB |
841 | if ( |
842 | !isWithinInterval(currentDate, recurringInterval) && | |
843 | isBefore(chargingSchedule.startSchedule!, currentDate) | |
844 | ) { | |
845 | chargingSchedule.startSchedule = addDays( | |
76dab5a9 | 846 | chargingSchedule.startSchedule!, |
05b52716 | 847 | differenceInDays(currentDate, recurringInterval.start), |
76dab5a9 | 848 | ); |
522e4b05 JB |
849 | recurringInterval = { |
850 | start: chargingSchedule.startSchedule, | |
851 | end: addDays(chargingSchedule.startSchedule, 1), | |
852 | }; | |
76dab5a9 JB |
853 | } |
854 | break; | |
855 | case RecurrencyKindType.WEEKLY: | |
522e4b05 JB |
856 | recurringInterval = { |
857 | start: chargingSchedule.startSchedule!, | |
858 | end: addWeeks(chargingSchedule.startSchedule!, 1), | |
859 | }; | |
d476bc1b | 860 | checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix); |
522e4b05 JB |
861 | if ( |
862 | !isWithinInterval(currentDate, recurringInterval) && | |
863 | isBefore(chargingSchedule.startSchedule!, currentDate) | |
864 | ) { | |
865 | chargingSchedule.startSchedule = addWeeks( | |
76dab5a9 | 866 | chargingSchedule.startSchedule!, |
05b52716 | 867 | differenceInWeeks(currentDate, recurringInterval.start), |
76dab5a9 | 868 | ); |
522e4b05 JB |
869 | recurringInterval = { |
870 | start: chargingSchedule.startSchedule, | |
871 | end: addWeeks(chargingSchedule.startSchedule, 1), | |
872 | }; | |
76dab5a9 JB |
873 | } |
874 | break; | |
875 | } | |
522e4b05 JB |
876 | if (!isWithinInterval(currentDate, recurringInterval!)) { |
877 | logger.error( | |
aa5c5ad4 | 878 | `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${ |
522e4b05 JB |
879 | chargingProfile.recurrencyKind |
880 | } charging profile id ${ | |
881 | chargingProfile.chargingProfileId | |
882 | } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate( | |
883 | recurringInterval!.start, | |
884 | ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`, | |
885 | ); | |
886 | } | |
76dab5a9 JB |
887 | }; |
888 | ||
d476bc1b JB |
889 | const checkRecurringChargingProfileDuration = ( |
890 | chargingProfile: ChargingProfile, | |
891 | interval: Interval, | |
892 | logPrefix: string, | |
893 | ) => { | |
894 | if ( | |
895 | chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start) | |
896 | ) { | |
897 | logger.warn( | |
aa5c5ad4 | 898 | `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${ |
d476bc1b JB |
899 | chargingProfile.chargingProfileKind |
900 | } charging profile id ${chargingProfile.chargingProfileId} duration ${ | |
901 | chargingProfile.chargingSchedule.duration | |
710d50eb | 902 | } is greater than the recurrency time interval duration ${differenceInSeconds( |
d476bc1b JB |
903 | interval.end, |
904 | interval.start, | |
710d50eb | 905 | )}`, |
d476bc1b | 906 | ); |
55f2ab60 | 907 | chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start); |
d476bc1b JB |
908 | } |
909 | }; | |
910 | ||
fba11dc6 JB |
911 | const getRandomSerialNumberSuffix = (params?: { |
912 | randomBytesLength?: number; | |
913 | upperCase?: boolean; | |
914 | }): string => { | |
915 | const randomSerialNumberSuffix = randomBytes(params?.randomBytesLength ?? 16).toString('hex'); | |
916 | if (params?.upperCase) { | |
917 | return randomSerialNumberSuffix.toUpperCase(); | |
17ac262c | 918 | } |
fba11dc6 JB |
919 | return randomSerialNumberSuffix; |
920 | }; |