1 import crypto from
'crypto';
2 import path from
'path';
3 import { fileURLToPath
} from
'url';
5 import moment from
'moment';
7 import BaseError from
'../exception/BaseError';
8 import type { ChargingStationInfo
} from
'../types/ChargingStationInfo';
11 type ChargingStationTemplate
,
14 } from
'../types/ChargingStationTemplate';
15 import { ChargingProfileKindType
, RecurrencyKindType
} from
'../types/ocpp/1.6/ChargingProfile';
16 import type { OCPP16BootNotificationRequest
} from
'../types/ocpp/1.6/Requests';
17 import { BootReasonEnumType
, OCPP20BootNotificationRequest
} from
'../types/ocpp/2.0/Requests';
18 import type { ChargingProfile
, ChargingSchedulePeriod
} from
'../types/ocpp/ChargingProfile';
19 import { OCPPVersion
} from
'../types/ocpp/OCPPVersion';
20 import type { BootNotificationRequest
} from
'../types/ocpp/Requests';
21 import { WorkerProcessType
} from
'../types/Worker';
22 import Configuration from
'../utils/Configuration';
23 import Constants from
'../utils/Constants';
24 import logger from
'../utils/Logger';
25 import Utils from
'../utils/Utils';
27 const moduleName
= 'ChargingStationUtils';
29 export class ChargingStationUtils
{
30 private constructor() {
31 // This is intentional
34 public static getChargingStationId(
36 stationTemplate
: ChargingStationTemplate
38 // In case of multiple instances: add instance index to charging station id
39 const instanceIndex
= process
.env
.CF_INSTANCE_INDEX
?? 0;
40 const idSuffix
= stationTemplate
.nameSuffix
?? '';
41 const idStr
= '000000000' + index
.toString();
42 return stationTemplate
?.fixedName
43 ? stationTemplate
.baseName
44 : stationTemplate
.baseName
+
46 instanceIndex
.toString() +
47 idStr
.substring(idStr
.length
- 4) +
51 public static getHashId(index
: number, stationTemplate
: ChargingStationTemplate
): string {
52 const hashBootNotificationRequest
= {
53 chargePointModel
: stationTemplate
.chargePointModel
,
54 chargePointVendor
: stationTemplate
.chargePointVendor
,
55 ...(!Utils
.isUndefined(stationTemplate
.chargeBoxSerialNumberPrefix
) && {
56 chargeBoxSerialNumber
: stationTemplate
.chargeBoxSerialNumberPrefix
,
58 ...(!Utils
.isUndefined(stationTemplate
.chargePointSerialNumberPrefix
) && {
59 chargePointSerialNumber
: stationTemplate
.chargePointSerialNumberPrefix
,
61 // FIXME?: Should a firmware version change always reference a new configuration file?
62 ...(!Utils
.isUndefined(stationTemplate
.firmwareVersion
) && {
63 firmwareVersion
: stationTemplate
.firmwareVersion
,
65 ...(!Utils
.isUndefined(stationTemplate
.iccid
) && { iccid
: stationTemplate
.iccid
}),
66 ...(!Utils
.isUndefined(stationTemplate
.imsi
) && { imsi
: stationTemplate
.imsi
}),
67 ...(!Utils
.isUndefined(stationTemplate
.meterSerialNumberPrefix
) && {
68 meterSerialNumber
: stationTemplate
.meterSerialNumberPrefix
,
70 ...(!Utils
.isUndefined(stationTemplate
.meterType
) && {
71 meterType
: stationTemplate
.meterType
,
75 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
77 JSON
.stringify(hashBootNotificationRequest
) +
78 ChargingStationUtils
.getChargingStationId(index
, stationTemplate
)
83 public static getTemplateMaxNumberOfConnectors(stationTemplate
: ChargingStationTemplate
): number {
84 const templateConnectors
= stationTemplate
?.Connectors
;
85 if (!templateConnectors
) {
88 return Object.keys(templateConnectors
).length
;
91 public static checkTemplateMaxConnectors(
92 templateMaxConnectors
: number,
96 if (templateMaxConnectors
=== 0) {
98 `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration`
100 } else if (templateMaxConnectors
< 0) {
102 `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined`
107 public static getConfiguredNumberOfConnectors(stationTemplate
: ChargingStationTemplate
): number {
108 let configuredMaxConnectors
: number;
109 if (Utils
.isEmptyArray(stationTemplate
.numberOfConnectors
) === false) {
110 const numberOfConnectors
= stationTemplate
.numberOfConnectors
as number[];
111 configuredMaxConnectors
=
112 numberOfConnectors
[Math.floor(Utils
.secureRandom() * numberOfConnectors
.length
)];
113 } else if (Utils
.isUndefined(stationTemplate
.numberOfConnectors
) === false) {
114 configuredMaxConnectors
= stationTemplate
.numberOfConnectors
as number;
116 configuredMaxConnectors
= stationTemplate
?.Connectors
[0]
117 ? ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
) - 1
118 : ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
120 return configuredMaxConnectors
;
123 public static checkConfiguredMaxConnectors(
124 configuredMaxConnectors
: number,
125 templateFile
: string,
128 if (configuredMaxConnectors
<= 0) {
130 `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors`
135 public static createBootNotificationRequest(
136 stationInfo
: ChargingStationInfo
137 ): BootNotificationRequest
{
138 const ocppVersion
= stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
139 switch (ocppVersion
) {
140 case OCPPVersion
.VERSION_16
:
142 chargePointModel
: stationInfo
.chargePointModel
,
143 chargePointVendor
: stationInfo
.chargePointVendor
,
144 ...(!Utils
.isUndefined(stationInfo
.chargeBoxSerialNumber
) && {
145 chargeBoxSerialNumber
: stationInfo
.chargeBoxSerialNumber
,
147 ...(!Utils
.isUndefined(stationInfo
.chargePointSerialNumber
) && {
148 chargePointSerialNumber
: stationInfo
.chargePointSerialNumber
,
150 ...(!Utils
.isUndefined(stationInfo
.firmwareVersion
) && {
151 firmwareVersion
: stationInfo
.firmwareVersion
,
153 ...(!Utils
.isUndefined(stationInfo
.iccid
) && { iccid
: stationInfo
.iccid
}),
154 ...(!Utils
.isUndefined(stationInfo
.imsi
) && { imsi
: stationInfo
.imsi
}),
155 ...(!Utils
.isUndefined(stationInfo
.meterSerialNumber
) && {
156 meterSerialNumber
: stationInfo
.meterSerialNumber
,
158 ...(!Utils
.isUndefined(stationInfo
.meterType
) && {
159 meterType
: stationInfo
.meterType
,
161 } as OCPP16BootNotificationRequest
;
162 case OCPPVersion
.VERSION_20
:
163 case OCPPVersion
.VERSION_201
:
165 reason
: BootReasonEnumType
.PowerUp
,
167 model
: stationInfo
.chargePointModel
,
168 vendorName
: stationInfo
.chargePointVendor
,
169 ...(!Utils
.isUndefined(stationInfo
.firmwareVersion
) && {
170 firmwareVersion
: stationInfo
.firmwareVersion
,
172 ...(!Utils
.isUndefined(stationInfo
.chargeBoxSerialNumber
) && {
173 serialNumber
: stationInfo
.chargeBoxSerialNumber
,
176 ...(!Utils
.isUndefined(stationInfo
.iccid
) && { iccid
: stationInfo
.iccid
}),
177 ...(!Utils
.isUndefined(stationInfo
.imsi
) && { imsi
: stationInfo
.imsi
}),
180 } as OCPP20BootNotificationRequest
;
184 public static workerPoolInUse(): boolean {
185 return [WorkerProcessType
.DYNAMIC_POOL
, WorkerProcessType
.STATIC_POOL
].includes(
186 Configuration
.getWorker().processType
190 public static workerDynamicPoolInUse(): boolean {
191 return Configuration
.getWorker().processType
=== WorkerProcessType
.DYNAMIC_POOL
;
194 public static warnDeprecatedTemplateKey(
195 template
: ChargingStationTemplate
,
197 templateFile
: string,
201 if (!Utils
.isUndefined(template
[key
])) {
203 `${logPrefix} Deprecated template key '${key}' usage in file '${templateFile}'${
204 logMsgToAppend && '. ' + logMsgToAppend
210 public static convertDeprecatedTemplateKey(
211 template
: ChargingStationTemplate
,
212 deprecatedKey
: string,
215 if (!Utils
.isUndefined(template
[deprecatedKey
])) {
216 template
[key
] = template
[deprecatedKey
] as unknown
;
217 delete template
[deprecatedKey
];
221 public static stationTemplateToStationInfo(
222 stationTemplate
: ChargingStationTemplate
223 ): ChargingStationInfo
{
224 stationTemplate
= Utils
.cloneObject(stationTemplate
);
225 delete stationTemplate
.power
;
226 delete stationTemplate
.powerUnit
;
227 delete stationTemplate
.Configuration
;
228 delete stationTemplate
.AutomaticTransactionGenerator
;
229 delete stationTemplate
.chargeBoxSerialNumberPrefix
;
230 delete stationTemplate
.chargePointSerialNumberPrefix
;
231 delete stationTemplate
.meterSerialNumberPrefix
;
232 return stationTemplate
as unknown
as ChargingStationInfo
;
235 public static createStationInfoHash(stationInfo
: ChargingStationInfo
): void {
236 delete stationInfo
.infoHash
;
237 stationInfo
.infoHash
= crypto
238 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
239 .update(JSON
.stringify(stationInfo
))
243 public static createSerialNumber(
244 stationTemplate
: ChargingStationTemplate
,
245 stationInfo
: ChargingStationInfo
= {} as ChargingStationInfo
,
247 randomSerialNumberUpperCase
?: boolean;
248 randomSerialNumber
?: boolean;
250 randomSerialNumberUpperCase
: true,
251 randomSerialNumber
: true,
254 params
= params
?? {};
255 params
.randomSerialNumberUpperCase
= params
?.randomSerialNumberUpperCase
?? true;
256 params
.randomSerialNumber
= params
?.randomSerialNumber
?? true;
257 const serialNumberSuffix
= params
?.randomSerialNumber
258 ? ChargingStationUtils
.getRandomSerialNumberSuffix({
259 upperCase
: params
.randomSerialNumberUpperCase
,
262 stationInfo
.chargePointSerialNumber
=
263 stationTemplate
?.chargePointSerialNumberPrefix
&&
264 stationTemplate
.chargePointSerialNumberPrefix
+ serialNumberSuffix
;
265 stationInfo
.chargeBoxSerialNumber
=
266 stationTemplate
?.chargeBoxSerialNumberPrefix
&&
267 stationTemplate
.chargeBoxSerialNumberPrefix
+ serialNumberSuffix
;
268 stationInfo
.meterSerialNumber
=
269 stationTemplate
?.meterSerialNumberPrefix
&&
270 stationTemplate
.meterSerialNumberPrefix
+ serialNumberSuffix
;
273 public static propagateSerialNumber(
274 stationTemplate
: ChargingStationTemplate
,
275 stationInfoSrc
: ChargingStationInfo
,
276 stationInfoDst
: ChargingStationInfo
= {} as ChargingStationInfo
278 if (!stationInfoSrc
|| !stationTemplate
) {
280 'Missing charging station template or existing configuration to propagate serial number'
283 stationTemplate
?.chargePointSerialNumberPrefix
&& stationInfoSrc
?.chargePointSerialNumber
284 ? (stationInfoDst
.chargePointSerialNumber
= stationInfoSrc
.chargePointSerialNumber
)
285 : stationInfoDst
?.chargePointSerialNumber
&& delete stationInfoDst
.chargePointSerialNumber
;
286 stationTemplate
?.chargeBoxSerialNumberPrefix
&& stationInfoSrc
?.chargeBoxSerialNumber
287 ? (stationInfoDst
.chargeBoxSerialNumber
= stationInfoSrc
.chargeBoxSerialNumber
)
288 : stationInfoDst
?.chargeBoxSerialNumber
&& delete stationInfoDst
.chargeBoxSerialNumber
;
289 stationTemplate
?.meterSerialNumberPrefix
&& stationInfoSrc
?.meterSerialNumber
290 ? (stationInfoDst
.meterSerialNumber
= stationInfoSrc
.meterSerialNumber
)
291 : stationInfoDst
?.meterSerialNumber
&& delete stationInfoDst
.meterSerialNumber
;
294 public static getAmperageLimitationUnitDivider(stationInfo
: ChargingStationInfo
): number {
296 switch (stationInfo
.amperageLimitationUnit
) {
297 case AmpereUnits
.DECI_AMPERE
:
300 case AmpereUnits
.CENTI_AMPERE
:
303 case AmpereUnits
.MILLI_AMPERE
:
311 * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority)
313 * @param chargingProfiles -
317 public static getLimitFromChargingProfiles(
318 chargingProfiles
: ChargingProfile
[],
322 matchingChargingProfile
: ChargingProfile
;
324 const debugLogMsg
= `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
325 for (const chargingProfile
of chargingProfiles
) {
327 const currentMoment
= moment();
328 const chargingSchedule
= chargingProfile
.chargingSchedule
;
329 // Check type (recurring) and if it is already active
330 // Adjust the daily recurring schedule to today
332 chargingProfile
.chargingProfileKind
=== ChargingProfileKindType
.RECURRING
&&
333 chargingProfile
.recurrencyKind
=== RecurrencyKindType
.DAILY
&&
334 currentMoment
.isAfter(chargingSchedule
.startSchedule
)
336 const currentDate
= new Date();
337 chargingSchedule
.startSchedule
= new Date(chargingSchedule
.startSchedule
);
338 chargingSchedule
.startSchedule
.setFullYear(
339 currentDate
.getFullYear(),
340 currentDate
.getMonth(),
341 currentDate
.getDate()
343 // Check if the start of the schedule is yesterday
344 if (moment(chargingSchedule
.startSchedule
).isAfter(currentMoment
)) {
345 chargingSchedule
.startSchedule
.setDate(currentDate
.getDate() - 1);
347 } else if (moment(chargingSchedule
.startSchedule
).isAfter(currentMoment
)) {
350 // Check if the charging profile is active
352 moment(chargingSchedule
.startSchedule
)
353 .add(chargingSchedule
.duration
, 's')
354 .isAfter(currentMoment
)
356 let lastButOneSchedule
: ChargingSchedulePeriod
;
357 // Search the right schedule period
358 for (const schedulePeriod
of chargingSchedule
.chargingSchedulePeriod
) {
359 // Handling of only one period
361 chargingSchedule
.chargingSchedulePeriod
.length
=== 1 &&
362 schedulePeriod
.startPeriod
=== 0
365 limit
: schedulePeriod
.limit
,
366 matchingChargingProfile
: chargingProfile
,
368 logger
.debug(debugLogMsg
, result
);
371 // Find the right schedule period
373 moment(chargingSchedule
.startSchedule
)
374 .add(schedulePeriod
.startPeriod
, 's')
375 .isAfter(currentMoment
)
377 // Found the schedule: last but one is the correct one
379 limit
: lastButOneSchedule
.limit
,
380 matchingChargingProfile
: chargingProfile
,
382 logger
.debug(debugLogMsg
, result
);
386 lastButOneSchedule
= schedulePeriod
;
387 // Handle the last schedule period
389 schedulePeriod
.startPeriod
===
390 chargingSchedule
.chargingSchedulePeriod
[
391 chargingSchedule
.chargingSchedulePeriod
.length
- 1
395 limit
: lastButOneSchedule
.limit
,
396 matchingChargingProfile
: chargingProfile
,
398 logger
.debug(debugLogMsg
, result
);
407 public static getDefaultVoltageOut(
408 currentType
: CurrentType
,
409 templateFile
: string,
412 const errMsg
= `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`;
413 let defaultVoltageOut
: number;
414 switch (currentType
) {
416 defaultVoltageOut
= Voltage
.VOLTAGE_230
;
419 defaultVoltageOut
= Voltage
.VOLTAGE_400
;
422 logger
.error(`${logPrefix} ${errMsg}`);
423 throw new BaseError(errMsg
);
425 return defaultVoltageOut
;
428 public static getAuthorizationFile(stationInfo
: ChargingStationInfo
): string | undefined {
430 stationInfo
.authorizationFile
&&
432 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
434 path
.basename(stationInfo
.authorizationFile
)
439 private static getRandomSerialNumberSuffix(params
?: {
440 randomBytesLength
?: number;
443 const randomSerialNumberSuffix
= crypto
444 .randomBytes(params
?.randomBytesLength
?? 16)
446 if (params
?.upperCase
) {
447 return randomSerialNumberSuffix
.toUpperCase();
449 return randomSerialNumberSuffix
;