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 { ChargingProfile
, ChargingSchedulePeriod
} from
'../types/ocpp/ChargingProfile';
17 import type { BootNotificationRequest
} from
'../types/ocpp/Requests';
18 import { WorkerProcessType
} from
'../types/Worker';
19 import Configuration from
'../utils/Configuration';
20 import Constants from
'../utils/Constants';
21 import logger from
'../utils/Logger';
22 import Utils from
'../utils/Utils';
24 const moduleName
= 'ChargingStationUtils';
26 export class ChargingStationUtils
{
27 private constructor() {
28 // This is intentional
31 public static getChargingStationId(
33 stationTemplate
: ChargingStationTemplate
35 // In case of multiple instances: add instance index to charging station id
36 const instanceIndex
= process
.env
.CF_INSTANCE_INDEX
?? 0;
37 const idSuffix
= stationTemplate
.nameSuffix
?? '';
38 const idStr
= '000000000' + index
.toString();
39 return stationTemplate
?.fixedName
40 ? stationTemplate
.baseName
41 : stationTemplate
.baseName
+
43 instanceIndex
.toString() +
44 idStr
.substring(idStr
.length
- 4) +
48 public static getHashId(index
: number, stationTemplate
: ChargingStationTemplate
): string {
49 const hashBootNotificationRequest
= {
50 chargePointModel
: stationTemplate
.chargePointModel
,
51 chargePointVendor
: stationTemplate
.chargePointVendor
,
52 ...(!Utils
.isUndefined(stationTemplate
.chargeBoxSerialNumberPrefix
) && {
53 chargeBoxSerialNumber
: stationTemplate
.chargeBoxSerialNumberPrefix
,
55 ...(!Utils
.isUndefined(stationTemplate
.chargePointSerialNumberPrefix
) && {
56 chargePointSerialNumber
: stationTemplate
.chargePointSerialNumberPrefix
,
58 // FIXME?: Should a firmware version change always reference a new configuration file?
59 ...(!Utils
.isUndefined(stationTemplate
.firmwareVersion
) && {
60 firmwareVersion
: stationTemplate
.firmwareVersion
,
62 ...(!Utils
.isUndefined(stationTemplate
.iccid
) && { iccid
: stationTemplate
.iccid
}),
63 ...(!Utils
.isUndefined(stationTemplate
.imsi
) && { imsi
: stationTemplate
.imsi
}),
64 ...(!Utils
.isUndefined(stationTemplate
.meterSerialNumberPrefix
) && {
65 meterSerialNumber
: stationTemplate
.meterSerialNumberPrefix
,
67 ...(!Utils
.isUndefined(stationTemplate
.meterType
) && {
68 meterType
: stationTemplate
.meterType
,
72 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
74 JSON
.stringify(hashBootNotificationRequest
) +
75 ChargingStationUtils
.getChargingStationId(index
, stationTemplate
)
80 public static getTemplateMaxNumberOfConnectors(stationTemplate
: ChargingStationTemplate
): number {
81 const templateConnectors
= stationTemplate
?.Connectors
;
82 if (!templateConnectors
) {
85 return Object.keys(templateConnectors
).length
;
88 public static checkTemplateMaxConnectors(
89 templateMaxConnectors
: number,
93 if (templateMaxConnectors
=== 0) {
95 `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration`
97 } else if (templateMaxConnectors
< 0) {
99 `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined`
104 public static getConfiguredNumberOfConnectors(stationTemplate
: ChargingStationTemplate
): number {
105 let configuredMaxConnectors
: number;
106 if (Utils
.isEmptyArray(stationTemplate
.numberOfConnectors
) === false) {
107 const numberOfConnectors
= stationTemplate
.numberOfConnectors
as number[];
108 configuredMaxConnectors
=
109 numberOfConnectors
[Math.floor(Utils
.secureRandom() * numberOfConnectors
.length
)];
110 } else if (Utils
.isUndefined(stationTemplate
.numberOfConnectors
) === false) {
111 configuredMaxConnectors
= stationTemplate
.numberOfConnectors
as number;
113 configuredMaxConnectors
= stationTemplate
?.Connectors
[0]
114 ? ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
) - 1
115 : ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
117 return configuredMaxConnectors
;
120 public static checkConfiguredMaxConnectors(
121 configuredMaxConnectors
: number,
122 templateFile
: string,
125 if (configuredMaxConnectors
<= 0) {
127 `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors`
132 public static createBootNotificationRequest(
133 stationInfo
: ChargingStationInfo
134 ): BootNotificationRequest
{
136 chargePointModel
: stationInfo
.chargePointModel
,
137 chargePointVendor
: stationInfo
.chargePointVendor
,
138 ...(!Utils
.isUndefined(stationInfo
.chargeBoxSerialNumber
) && {
139 chargeBoxSerialNumber
: stationInfo
.chargeBoxSerialNumber
,
141 ...(!Utils
.isUndefined(stationInfo
.chargePointSerialNumber
) && {
142 chargePointSerialNumber
: stationInfo
.chargePointSerialNumber
,
144 ...(!Utils
.isUndefined(stationInfo
.firmwareVersion
) && {
145 firmwareVersion
: stationInfo
.firmwareVersion
,
147 ...(!Utils
.isUndefined(stationInfo
.iccid
) && { iccid
: stationInfo
.iccid
}),
148 ...(!Utils
.isUndefined(stationInfo
.imsi
) && { imsi
: stationInfo
.imsi
}),
149 ...(!Utils
.isUndefined(stationInfo
.meterSerialNumber
) && {
150 meterSerialNumber
: stationInfo
.meterSerialNumber
,
152 ...(!Utils
.isUndefined(stationInfo
.meterType
) && {
153 meterType
: stationInfo
.meterType
,
158 public static workerPoolInUse(): boolean {
159 return [WorkerProcessType
.DYNAMIC_POOL
, WorkerProcessType
.STATIC_POOL
].includes(
160 Configuration
.getWorker().processType
164 public static workerDynamicPoolInUse(): boolean {
165 return Configuration
.getWorker().processType
=== WorkerProcessType
.DYNAMIC_POOL
;
168 public static warnDeprecatedTemplateKey(
169 template
: ChargingStationTemplate
,
171 templateFile
: string,
175 if (!Utils
.isUndefined(template
[key
])) {
177 `${logPrefix} Deprecated template key '${key}' usage in file '${templateFile}'${
178 logMsgToAppend && '. ' + logMsgToAppend
184 public static convertDeprecatedTemplateKey(
185 template
: ChargingStationTemplate
,
186 deprecatedKey
: string,
189 if (!Utils
.isUndefined(template
[deprecatedKey
])) {
190 template
[key
] = template
[deprecatedKey
] as unknown
;
191 delete template
[deprecatedKey
];
195 public static stationTemplateToStationInfo(
196 stationTemplate
: ChargingStationTemplate
197 ): ChargingStationInfo
{
198 stationTemplate
= Utils
.cloneObject(stationTemplate
);
199 delete stationTemplate
.power
;
200 delete stationTemplate
.powerUnit
;
201 delete stationTemplate
.Configuration
;
202 delete stationTemplate
.AutomaticTransactionGenerator
;
203 delete stationTemplate
.chargeBoxSerialNumberPrefix
;
204 delete stationTemplate
.chargePointSerialNumberPrefix
;
205 delete stationTemplate
.meterSerialNumberPrefix
;
206 return stationTemplate
as unknown
as ChargingStationInfo
;
209 public static createStationInfoHash(stationInfo
: ChargingStationInfo
): void {
210 delete stationInfo
.infoHash
;
211 stationInfo
.infoHash
= crypto
212 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
213 .update(JSON
.stringify(stationInfo
))
217 public static createSerialNumber(
218 stationTemplate
: ChargingStationTemplate
,
219 stationInfo
: ChargingStationInfo
= {} as ChargingStationInfo
,
221 randomSerialNumberUpperCase
?: boolean;
222 randomSerialNumber
?: boolean;
224 randomSerialNumberUpperCase
: true,
225 randomSerialNumber
: true,
228 params
= params
?? {};
229 params
.randomSerialNumberUpperCase
= params
?.randomSerialNumberUpperCase
?? true;
230 params
.randomSerialNumber
= params
?.randomSerialNumber
?? true;
231 const serialNumberSuffix
= params
?.randomSerialNumber
232 ? ChargingStationUtils
.getRandomSerialNumberSuffix({
233 upperCase
: params
.randomSerialNumberUpperCase
,
236 stationInfo
.chargePointSerialNumber
=
237 stationTemplate
?.chargePointSerialNumberPrefix
&&
238 stationTemplate
.chargePointSerialNumberPrefix
+ serialNumberSuffix
;
239 stationInfo
.chargeBoxSerialNumber
=
240 stationTemplate
?.chargeBoxSerialNumberPrefix
&&
241 stationTemplate
.chargeBoxSerialNumberPrefix
+ serialNumberSuffix
;
242 stationInfo
.meterSerialNumber
=
243 stationTemplate
?.meterSerialNumberPrefix
&&
244 stationTemplate
.meterSerialNumberPrefix
+ serialNumberSuffix
;
247 public static propagateSerialNumber(
248 stationTemplate
: ChargingStationTemplate
,
249 stationInfoSrc
: ChargingStationInfo
,
250 stationInfoDst
: ChargingStationInfo
= {} as ChargingStationInfo
252 if (!stationInfoSrc
|| !stationTemplate
) {
254 'Missing charging station template or existing configuration to propagate serial number'
257 stationTemplate
?.chargePointSerialNumberPrefix
&& stationInfoSrc
?.chargePointSerialNumber
258 ? (stationInfoDst
.chargePointSerialNumber
= stationInfoSrc
.chargePointSerialNumber
)
259 : stationInfoDst
?.chargePointSerialNumber
&& delete stationInfoDst
.chargePointSerialNumber
;
260 stationTemplate
?.chargeBoxSerialNumberPrefix
&& stationInfoSrc
?.chargeBoxSerialNumber
261 ? (stationInfoDst
.chargeBoxSerialNumber
= stationInfoSrc
.chargeBoxSerialNumber
)
262 : stationInfoDst
?.chargeBoxSerialNumber
&& delete stationInfoDst
.chargeBoxSerialNumber
;
263 stationTemplate
?.meterSerialNumberPrefix
&& stationInfoSrc
?.meterSerialNumber
264 ? (stationInfoDst
.meterSerialNumber
= stationInfoSrc
.meterSerialNumber
)
265 : stationInfoDst
?.meterSerialNumber
&& delete stationInfoDst
.meterSerialNumber
;
268 public static getAmperageLimitationUnitDivider(stationInfo
: ChargingStationInfo
): number {
270 switch (stationInfo
.amperageLimitationUnit
) {
271 case AmpereUnits
.DECI_AMPERE
:
274 case AmpereUnits
.CENTI_AMPERE
:
277 case AmpereUnits
.MILLI_AMPERE
:
285 * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority)
287 * @param chargingProfiles -
291 public static getLimitFromChargingProfiles(
292 chargingProfiles
: ChargingProfile
[],
296 matchingChargingProfile
: ChargingProfile
;
298 const debugLogMsg
= `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
299 for (const chargingProfile
of chargingProfiles
) {
301 const currentMoment
= moment();
302 const chargingSchedule
= chargingProfile
.chargingSchedule
;
303 // Check type (recurring) and if it is already active
304 // Adjust the daily recurring schedule to today
306 chargingProfile
.chargingProfileKind
=== ChargingProfileKindType
.RECURRING
&&
307 chargingProfile
.recurrencyKind
=== RecurrencyKindType
.DAILY
&&
308 currentMoment
.isAfter(chargingSchedule
.startSchedule
)
310 const currentDate
= new Date();
311 chargingSchedule
.startSchedule
= new Date(chargingSchedule
.startSchedule
);
312 chargingSchedule
.startSchedule
.setFullYear(
313 currentDate
.getFullYear(),
314 currentDate
.getMonth(),
315 currentDate
.getDate()
317 // Check if the start of the schedule is yesterday
318 if (moment(chargingSchedule
.startSchedule
).isAfter(currentMoment
)) {
319 chargingSchedule
.startSchedule
.setDate(currentDate
.getDate() - 1);
321 } else if (moment(chargingSchedule
.startSchedule
).isAfter(currentMoment
)) {
324 // Check if the charging profile is active
326 moment(chargingSchedule
.startSchedule
)
327 .add(chargingSchedule
.duration
, 's')
328 .isAfter(currentMoment
)
330 let lastButOneSchedule
: ChargingSchedulePeriod
;
331 // Search the right schedule period
332 for (const schedulePeriod
of chargingSchedule
.chargingSchedulePeriod
) {
333 // Handling of only one period
335 chargingSchedule
.chargingSchedulePeriod
.length
=== 1 &&
336 schedulePeriod
.startPeriod
=== 0
339 limit
: schedulePeriod
.limit
,
340 matchingChargingProfile
: chargingProfile
,
342 logger
.debug(debugLogMsg
, result
);
345 // Find the right schedule period
347 moment(chargingSchedule
.startSchedule
)
348 .add(schedulePeriod
.startPeriod
, 's')
349 .isAfter(currentMoment
)
351 // Found the schedule: last but one is the correct one
353 limit
: lastButOneSchedule
.limit
,
354 matchingChargingProfile
: chargingProfile
,
356 logger
.debug(debugLogMsg
, result
);
360 lastButOneSchedule
= schedulePeriod
;
361 // Handle the last schedule period
363 schedulePeriod
.startPeriod
===
364 chargingSchedule
.chargingSchedulePeriod
[
365 chargingSchedule
.chargingSchedulePeriod
.length
- 1
369 limit
: lastButOneSchedule
.limit
,
370 matchingChargingProfile
: chargingProfile
,
372 logger
.debug(debugLogMsg
, result
);
381 public static getDefaultVoltageOut(
382 currentType
: CurrentType
,
383 templateFile
: string,
386 const errMsg
= `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`;
387 let defaultVoltageOut
: number;
388 switch (currentType
) {
390 defaultVoltageOut
= Voltage
.VOLTAGE_230
;
393 defaultVoltageOut
= Voltage
.VOLTAGE_400
;
396 logger
.error(`${logPrefix} ${errMsg}`);
397 throw new BaseError(errMsg
);
399 return defaultVoltageOut
;
402 public static getAuthorizationFile(stationInfo
: ChargingStationInfo
): string | undefined {
404 stationInfo
.authorizationFile
&&
406 path
.resolve(path
.dirname(fileURLToPath(import.meta
.url
)), '../'),
408 path
.basename(stationInfo
.authorizationFile
)
413 private static getRandomSerialNumberSuffix(params
?: {
414 randomBytesLength
?: number;
417 const randomSerialNumberSuffix
= crypto
418 .randomBytes(params
?.randomBytesLength
?? 16)
420 if (params
?.upperCase
) {
421 return randomSerialNumberSuffix
.toUpperCase();
423 return randomSerialNumberSuffix
;