Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
b4d34251 | 2 | |
d972af76 | 3 | import { createHash } from 'node:crypto'; |
db54d2e0 | 4 | import { EventEmitter } from 'node:events'; |
0ebf7c2e | 5 | import { type FSWatcher, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
d972af76 | 6 | import { dirname, join } from 'node:path'; |
130783a7 | 7 | import { URL } from 'node:url'; |
01f4001e | 8 | import { parentPort } from 'node:worker_threads'; |
8114d10e | 9 | |
be4c6702 | 10 | import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns'; |
5d280aae | 11 | import merge from 'just-merge'; |
d972af76 | 12 | import { type RawData, WebSocket } from 'ws'; |
8114d10e | 13 | |
4c3c0d59 | 14 | import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator'; |
7671fa0b | 15 | import { ChargingStationWorkerBroadcastChannel } from './broadcast-channel/ChargingStationWorkerBroadcastChannel'; |
f2d5e3d9 JB |
16 | import { |
17 | addConfigurationKey, | |
18 | deleteConfigurationKey, | |
19 | getConfigurationKey, | |
20 | setConfigurationKeyValue, | |
357a5553 | 21 | } from './ConfigurationKeyUtils'; |
fba11dc6 JB |
22 | import { |
23 | buildConnectorsMap, | |
e6a33233 | 24 | checkChargingStation, |
fba11dc6 JB |
25 | checkConnectorsConfiguration, |
26 | checkStationInfoConnectorStatus, | |
27 | checkTemplate, | |
fba11dc6 JB |
28 | createBootNotificationRequest, |
29 | createSerialNumber, | |
30 | getAmperageLimitationUnitDivider, | |
31 | getBootConnectorStatus, | |
32 | getChargingStationConnectorChargingProfilesPowerLimit, | |
33 | getChargingStationId, | |
34 | getDefaultVoltageOut, | |
35 | getHashId, | |
36 | getIdTagsFile, | |
37 | getMaxNumberOfEvses, | |
90aceaf6 | 38 | getNumberOfReservableConnectors, |
fba11dc6 | 39 | getPhaseRotationValue, |
d8093be1 | 40 | hasFeatureProfile, |
90aceaf6 | 41 | hasReservationExpired, |
fba11dc6 JB |
42 | initializeConnectorsMapStatus, |
43 | propagateSerialNumber, | |
90aceaf6 | 44 | removeExpiredReservations, |
fba11dc6 JB |
45 | stationTemplateToStationInfo, |
46 | warnTemplateKeysDeprecation, | |
08b58f00 JB |
47 | } from './Helpers'; |
48 | import { IdTagsCache } from './IdTagsCache'; | |
49 | import { | |
50 | OCPP16IncomingRequestService, | |
51 | OCPP16RequestService, | |
52 | OCPP16ResponseService, | |
53 | OCPP16ServiceUtils, | |
54 | OCPP20IncomingRequestService, | |
55 | OCPP20RequestService, | |
56 | OCPP20ResponseService, | |
57 | type OCPPIncomingRequestService, | |
58 | type OCPPRequestService, | |
59 | OCPPServiceUtils, | |
60 | } from './ocpp'; | |
61 | import { SharedLRUCache } from './SharedLRUCache'; | |
268a74bb | 62 | import { BaseError, OCPPError } from '../exception'; |
b84bca85 | 63 | import { PerformanceStatistics } from '../performance'; |
e7aeea18 | 64 | import { |
268a74bb | 65 | type AutomaticTransactionGeneratorConfiguration, |
e7aeea18 | 66 | AvailabilityType, |
e0b0ee21 | 67 | type BootNotificationRequest, |
268a74bb | 68 | type BootNotificationResponse, |
e0b0ee21 | 69 | type CachedRequest, |
268a74bb | 70 | type ChargingStationConfiguration, |
db54d2e0 | 71 | ChargingStationEvents, |
268a74bb JB |
72 | type ChargingStationInfo, |
73 | type ChargingStationOcppConfiguration, | |
74 | type ChargingStationTemplate, | |
c8aafe0d | 75 | type ConnectorStatus, |
268a74bb JB |
76 | ConnectorStatusEnum, |
77 | CurrentType, | |
e0b0ee21 | 78 | type ErrorCallback, |
268a74bb JB |
79 | type ErrorResponse, |
80 | ErrorType, | |
2585c6e9 | 81 | type EvseStatus, |
52952bf8 | 82 | type EvseStatusConfiguration, |
268a74bb | 83 | FileType, |
c9a4f9ea JB |
84 | FirmwareStatus, |
85 | type FirmwareStatusNotificationRequest, | |
268a74bb JB |
86 | type FirmwareStatusNotificationResponse, |
87 | type FirmwareUpgrade, | |
e0b0ee21 | 88 | type HeartbeatRequest, |
268a74bb | 89 | type HeartbeatResponse, |
e0b0ee21 | 90 | type IncomingRequest, |
268a74bb JB |
91 | type IncomingRequestCommand, |
92 | type JsonType, | |
93 | MessageType, | |
94 | type MeterValue, | |
95 | MeterValueMeasurand, | |
e0b0ee21 | 96 | type MeterValuesRequest, |
268a74bb JB |
97 | type MeterValuesResponse, |
98 | OCPPVersion, | |
8ca6874c | 99 | type OutgoingRequest, |
268a74bb JB |
100 | PowerUnits, |
101 | RegistrationStatusEnumType, | |
e7aeea18 | 102 | RequestCommand, |
66dd3447 | 103 | type Reservation, |
366f75f6 | 104 | type ReservationKey, |
66dd3447 | 105 | ReservationTerminationReason, |
268a74bb | 106 | type Response, |
268a74bb | 107 | StandardParametersKey, |
5ced7e80 | 108 | type Status, |
e0b0ee21 | 109 | type StatusNotificationRequest, |
e0b0ee21 | 110 | type StatusNotificationResponse, |
ef6fa3fb | 111 | StopTransactionReason, |
e0b0ee21 JB |
112 | type StopTransactionRequest, |
113 | type StopTransactionResponse, | |
268a74bb JB |
114 | SupervisionUrlDistribution, |
115 | SupportedFeatureProfiles, | |
6dad8e21 | 116 | VendorParametersKey, |
268a74bb JB |
117 | type WSError, |
118 | WebSocketCloseEventStatusCode, | |
119 | type WsOptions, | |
120 | } from '../types'; | |
60a74391 JB |
121 | import { |
122 | ACElectricUtils, | |
1227a6f1 JB |
123 | AsyncLock, |
124 | AsyncLockType, | |
60a74391 JB |
125 | Configuration, |
126 | Constants, | |
127 | DCElectricUtils, | |
179ed367 JB |
128 | buildChargingStationAutomaticTransactionGeneratorConfiguration, |
129 | buildConnectorsStatus, | |
130 | buildEvsesStatus, | |
c8faabc8 JB |
131 | buildStartedMessage, |
132 | buildStoppedMessage, | |
133 | buildUpdatedMessage, | |
9bf0ef23 JB |
134 | cloneObject, |
135 | convertToBoolean, | |
136 | convertToInt, | |
137 | exponentialDelay, | |
138 | formatDurationMilliSeconds, | |
139 | formatDurationSeconds, | |
140 | getRandomInteger, | |
141 | getWebSocketCloseEventStatusString, | |
fa5995d6 | 142 | handleFileException, |
9bf0ef23 JB |
143 | isNotEmptyArray, |
144 | isNotEmptyString, | |
145 | isNullOrUndefined, | |
146 | isUndefined, | |
147 | logPrefix, | |
60a74391 | 148 | logger, |
5adf6ca4 | 149 | min, |
5f742aac | 150 | once, |
9bf0ef23 JB |
151 | roundTo, |
152 | secureRandom, | |
153 | sleep, | |
1f8f6332 | 154 | watchJsonFile, |
60a74391 | 155 | } from '../utils'; |
3f40bc9c | 156 | |
db54d2e0 | 157 | export class ChargingStation extends EventEmitter { |
c72f6634 | 158 | public readonly index: number; |
2484ac1e | 159 | public readonly templateFile: string; |
452a82ca | 160 | public started: boolean; |
cbf9b878 | 161 | public starting: boolean; |
f911a4af | 162 | public idTagsCache: IdTagsCache; |
551e477c JB |
163 | public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined; |
164 | public ocppConfiguration!: ChargingStationOcppConfiguration | undefined; | |
4ed03b6e | 165 | public wsConnection: WebSocket | null; |
8df5ae48 | 166 | public readonly connectors: Map<number, ConnectorStatus>; |
2585c6e9 | 167 | public readonly evses: Map<number, EvseStatus>; |
9e23580d | 168 | public readonly requests: Map<string, CachedRequest>; |
551e477c | 169 | public performanceStatistics!: PerformanceStatistics | undefined; |
e1d9a0f4 | 170 | public heartbeatSetInterval?: NodeJS.Timeout; |
6e0964c8 | 171 | public ocppRequestService!: OCPPRequestService; |
0a03f36c | 172 | public bootNotificationRequest!: BootNotificationRequest; |
1895299d | 173 | public bootNotificationResponse!: BootNotificationResponse | undefined; |
fa7bccf4 | 174 | public powerDivider!: number; |
5398cecf | 175 | private internalStationInfo!: ChargingStationInfo; |
950b1349 | 176 | private stopping: boolean; |
073bd098 | 177 | private configurationFile!: string; |
7c72977b | 178 | private configurationFileHash!: string; |
6e0964c8 | 179 | private connectorsConfigurationHash!: string; |
2585c6e9 | 180 | private evsesConfigurationHash!: string; |
c7db8ecb | 181 | private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration; |
a472cf2b | 182 | private ocppIncomingRequestService!: OCPPIncomingRequestService; |
8e242273 | 183 | private readonly messageBuffer: Set<string>; |
fa7bccf4 | 184 | private configuredSupervisionUrl!: URL; |
265e4266 | 185 | private wsConnectionRestarted: boolean; |
ad2f27c3 | 186 | private autoReconnectRetryCount: number; |
d972af76 | 187 | private templateFileWatcher!: FSWatcher | undefined; |
cda5d0fb | 188 | private templateFileHash!: string; |
57adbebc | 189 | private readonly sharedLRUCache: SharedLRUCache; |
e1d9a0f4 | 190 | private webSocketPingSetInterval?: NodeJS.Timeout; |
89b7a234 | 191 | private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel; |
178956d8 | 192 | private reservationExpirationSetInterval?: NodeJS.Timeout; |
6af9012e | 193 | |
2484ac1e | 194 | constructor(index: number, templateFile: string) { |
db54d2e0 | 195 | super(); |
aa428a31 | 196 | this.started = false; |
950b1349 JB |
197 | this.starting = false; |
198 | this.stopping = false; | |
4ed03b6e | 199 | this.wsConnection = null; |
aa428a31 JB |
200 | this.wsConnectionRestarted = false; |
201 | this.autoReconnectRetryCount = 0; | |
ad2f27c3 | 202 | this.index = index; |
2484ac1e | 203 | this.templateFile = templateFile; |
9f2e3130 | 204 | this.connectors = new Map<number, ConnectorStatus>(); |
2585c6e9 | 205 | this.evses = new Map<number, EvseStatus>(); |
32b02249 | 206 | this.requests = new Map<string, CachedRequest>(); |
8e242273 | 207 | this.messageBuffer = new Set<string>(); |
b44b779a | 208 | this.sharedLRUCache = SharedLRUCache.getInstance(); |
f911a4af | 209 | this.idTagsCache = IdTagsCache.getInstance(); |
89b7a234 | 210 | this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this); |
32de5a57 | 211 | |
db54d2e0 JB |
212 | this.on(ChargingStationEvents.started, () => { |
213 | parentPort?.postMessage(buildStartedMessage(this)); | |
214 | }); | |
215 | this.on(ChargingStationEvents.stopped, () => { | |
216 | parentPort?.postMessage(buildStoppedMessage(this)); | |
217 | }); | |
218 | this.on(ChargingStationEvents.updated, () => { | |
219 | parentPort?.postMessage(buildUpdatedMessage(this)); | |
220 | }); | |
221 | ||
9f2e3130 | 222 | this.initialize(); |
c0560973 JB |
223 | } |
224 | ||
a14022a2 JB |
225 | public get hasEvses(): boolean { |
226 | return this.connectors.size === 0 && this.evses.size > 0; | |
227 | } | |
228 | ||
5398cecf JB |
229 | public get stationInfo(): ChargingStationInfo { |
230 | return { | |
5398cecf JB |
231 | ...{ |
232 | enableStatistics: false, | |
233 | remoteAuthorization: true, | |
234 | currentOutType: CurrentType.AC, | |
235 | ocppStrictCompliance: true, | |
236 | outOfOrderEndMeterValues: false, | |
237 | beginEndMeterValues: false, | |
238 | meteringPerTransaction: true, | |
239 | transactionDataMeterValues: false, | |
240 | mainVoltageMeterValues: true, | |
241 | phaseLineToLineVoltageMeterValues: false, | |
242 | customValueLimitationMeterValues: true, | |
243 | supervisionUrlOcppConfiguration: false, | |
244 | supervisionUrlOcppKey: VendorParametersKey.ConnectionUrl, | |
245 | ocppVersion: OCPPVersion.VERSION_16, | |
246 | ocppPersistentConfiguration: true, | |
247 | stationInfoPersistentConfiguration: true, | |
248 | automaticTransactionGeneratorPersistentConfiguration: true, | |
249 | autoReconnectMaxRetries: -1, | |
250 | registrationMaxRetries: -1, | |
251 | reconnectExponentialDelay: false, | |
252 | stopTransactionsOnStopped: true, | |
253 | }, | |
73de8f02 | 254 | ...this.internalStationInfo, |
5398cecf JB |
255 | }; |
256 | } | |
257 | ||
25f5a959 | 258 | private get wsConnectionUrl(): URL { |
fa7bccf4 | 259 | return new URL( |
44eb6026 | 260 | `${ |
5398cecf JB |
261 | this.stationInfo?.supervisionUrlOcppConfiguration && |
262 | isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && | |
263 | isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value) | |
264 | ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value | |
44eb6026 | 265 | : this.configuredSupervisionUrl.href |
5edd8ba0 | 266 | }/${this.stationInfo.chargingStationId}`, |
fa7bccf4 | 267 | ); |
12fc74d6 JB |
268 | } |
269 | ||
8b7072dc | 270 | public logPrefix = (): string => { |
9bf0ef23 | 271 | return logPrefix( |
ccb1d6e9 | 272 | ` ${ |
9bf0ef23 | 273 | (isNotEmptyString(this?.stationInfo?.chargingStationId) |
e302df1d | 274 | ? this?.stationInfo?.chargingStationId |
e1d9a0f4 | 275 | : getChargingStationId(this.index, this.getTemplateFromFile()!)) ?? |
e302df1d | 276 | 'Error at building log prefix' |
5edd8ba0 | 277 | } |`, |
ccb1d6e9 | 278 | ); |
8b7072dc | 279 | }; |
c0560973 | 280 | |
f911a4af | 281 | public hasIdTags(): boolean { |
e1d9a0f4 | 282 | return isNotEmptyArray(this.idTagsCache.getIdTags(getIdTagsFile(this.stationInfo)!)); |
c0560973 JB |
283 | } |
284 | ||
e1d9a0f4 | 285 | public getNumberOfPhases(stationInfo?: ChargingStationInfo): number { |
fa7bccf4 JB |
286 | const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo; |
287 | switch (this.getCurrentOutType(stationInfo)) { | |
4c2b4904 | 288 | case CurrentType.AC: |
e1d9a0f4 | 289 | return !isUndefined(localStationInfo.numberOfPhases) ? localStationInfo.numberOfPhases! : 3; |
4c2b4904 | 290 | case CurrentType.DC: |
c0560973 JB |
291 | return 0; |
292 | } | |
293 | } | |
294 | ||
d5bff457 | 295 | public isWebSocketConnectionOpened(): boolean { |
0d8140bd | 296 | return this?.wsConnection?.readyState === WebSocket.OPEN; |
c0560973 JB |
297 | } |
298 | ||
1895299d | 299 | public getRegistrationStatus(): RegistrationStatusEnumType | undefined { |
672fed6e JB |
300 | return this?.bootNotificationResponse?.status; |
301 | } | |
302 | ||
f7c2994d | 303 | public inUnknownState(): boolean { |
9bf0ef23 | 304 | return isNullOrUndefined(this?.bootNotificationResponse?.status); |
73c4266d JB |
305 | } |
306 | ||
f7c2994d | 307 | public inPendingState(): boolean { |
d270cc87 | 308 | return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING; |
16cd35ad JB |
309 | } |
310 | ||
f7c2994d | 311 | public inAcceptedState(): boolean { |
d270cc87 | 312 | return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED; |
c0560973 JB |
313 | } |
314 | ||
f7c2994d | 315 | public inRejectedState(): boolean { |
d270cc87 | 316 | return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED; |
16cd35ad JB |
317 | } |
318 | ||
319 | public isRegistered(): boolean { | |
ed6cfcff | 320 | return ( |
f7c2994d JB |
321 | this.inUnknownState() === false && |
322 | (this.inAcceptedState() === true || this.inPendingState() === true) | |
ed6cfcff | 323 | ); |
16cd35ad JB |
324 | } |
325 | ||
c0560973 | 326 | public isChargingStationAvailable(): boolean { |
0d6f335f | 327 | return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative; |
c0560973 JB |
328 | } |
329 | ||
a14022a2 JB |
330 | public hasConnector(connectorId: number): boolean { |
331 | if (this.hasEvses) { | |
332 | for (const evseStatus of this.evses.values()) { | |
333 | if (evseStatus.connectors.has(connectorId)) { | |
334 | return true; | |
335 | } | |
336 | } | |
337 | return false; | |
338 | } | |
339 | return this.connectors.has(connectorId); | |
340 | } | |
341 | ||
28e78158 JB |
342 | public isConnectorAvailable(connectorId: number): boolean { |
343 | return ( | |
344 | connectorId > 0 && | |
345 | this.getConnectorStatus(connectorId)?.availability === AvailabilityType.Operative | |
346 | ); | |
c0560973 JB |
347 | } |
348 | ||
54544ef1 | 349 | public getNumberOfConnectors(): number { |
28e78158 JB |
350 | if (this.hasEvses) { |
351 | let numberOfConnectors = 0; | |
352 | for (const [evseId, evseStatus] of this.evses) { | |
4334db72 JB |
353 | if (evseId > 0) { |
354 | numberOfConnectors += evseStatus.connectors.size; | |
28e78158 | 355 | } |
28e78158 JB |
356 | } |
357 | return numberOfConnectors; | |
358 | } | |
007b5bde | 359 | return this.connectors.has(0) ? this.connectors.size - 1 : this.connectors.size; |
54544ef1 JB |
360 | } |
361 | ||
28e78158 | 362 | public getNumberOfEvses(): number { |
007b5bde | 363 | return this.evses.has(0) ? this.evses.size - 1 : this.evses.size; |
28e78158 JB |
364 | } |
365 | ||
366 | public getConnectorStatus(connectorId: number): ConnectorStatus | undefined { | |
367 | if (this.hasEvses) { | |
368 | for (const evseStatus of this.evses.values()) { | |
369 | if (evseStatus.connectors.has(connectorId)) { | |
370 | return evseStatus.connectors.get(connectorId); | |
371 | } | |
372 | } | |
5e128725 | 373 | return undefined; |
28e78158 JB |
374 | } |
375 | return this.connectors.get(connectorId); | |
c0560973 JB |
376 | } |
377 | ||
ad8537a7 | 378 | public getConnectorMaximumAvailablePower(connectorId: number): number { |
e1d9a0f4 | 379 | let connectorAmperageLimitationPowerLimit: number | undefined; |
b47d68d7 | 380 | if ( |
9bf0ef23 | 381 | !isNullOrUndefined(this.getAmperageLimitation()) && |
e1d9a0f4 | 382 | this.getAmperageLimitation()! < this.stationInfo.maximumAmperage! |
b47d68d7 | 383 | ) { |
4160ae28 | 384 | connectorAmperageLimitationPowerLimit = |
5398cecf | 385 | (this.stationInfo?.currentOutType === CurrentType.AC |
cc6e8ab5 JB |
386 | ? ACElectricUtils.powerTotal( |
387 | this.getNumberOfPhases(), | |
5398cecf | 388 | this.stationInfo.voltageOut!, |
e1d9a0f4 | 389 | this.getAmperageLimitation()! * |
5edd8ba0 | 390 | (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()), |
cc6e8ab5 | 391 | ) |
5398cecf | 392 | : DCElectricUtils.power(this.stationInfo.voltageOut!, this.getAmperageLimitation()!)) / |
fa7bccf4 | 393 | this.powerDivider; |
cc6e8ab5 | 394 | } |
5398cecf | 395 | const connectorMaximumPower = this.stationInfo.maximumPower! / this.powerDivider; |
15068be9 | 396 | const connectorChargingProfilesPowerLimit = |
fba11dc6 | 397 | getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId); |
5adf6ca4 | 398 | return min( |
ad8537a7 | 399 | isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, |
e1d9a0f4 | 400 | isNaN(connectorAmperageLimitationPowerLimit!) |
ad8537a7 | 401 | ? Infinity |
e1d9a0f4 JB |
402 | : connectorAmperageLimitationPowerLimit!, |
403 | isNaN(connectorChargingProfilesPowerLimit!) ? Infinity : connectorChargingProfilesPowerLimit!, | |
ad8537a7 | 404 | ); |
cc6e8ab5 JB |
405 | } |
406 | ||
6e0964c8 | 407 | public getTransactionIdTag(transactionId: number): string | undefined { |
28e78158 JB |
408 | if (this.hasEvses) { |
409 | for (const evseStatus of this.evses.values()) { | |
410 | for (const connectorStatus of evseStatus.connectors.values()) { | |
411 | if (connectorStatus.transactionId === transactionId) { | |
412 | return connectorStatus.transactionIdTag; | |
413 | } | |
414 | } | |
415 | } | |
416 | } else { | |
417 | for (const connectorId of this.connectors.keys()) { | |
3fa7f799 | 418 | if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) { |
28e78158 JB |
419 | return this.getConnectorStatus(connectorId)?.transactionIdTag; |
420 | } | |
c0560973 JB |
421 | } |
422 | } | |
423 | } | |
424 | ||
ded57f02 | 425 | public getNumberOfRunningTransactions(): number { |
cfc9875a | 426 | let numberOfRunningTransactions = 0; |
ded57f02 | 427 | if (this.hasEvses) { |
3fa7f799 JB |
428 | for (const [evseId, evseStatus] of this.evses) { |
429 | if (evseId === 0) { | |
430 | continue; | |
431 | } | |
ded57f02 JB |
432 | for (const connectorStatus of evseStatus.connectors.values()) { |
433 | if (connectorStatus.transactionStarted === true) { | |
cfc9875a | 434 | ++numberOfRunningTransactions; |
ded57f02 JB |
435 | } |
436 | } | |
437 | } | |
438 | } else { | |
439 | for (const connectorId of this.connectors.keys()) { | |
440 | if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { | |
cfc9875a | 441 | ++numberOfRunningTransactions; |
ded57f02 JB |
442 | } |
443 | } | |
444 | } | |
cfc9875a | 445 | return numberOfRunningTransactions; |
ded57f02 JB |
446 | } |
447 | ||
f479a792 | 448 | public getConnectorIdByTransactionId(transactionId: number): number | undefined { |
28e78158 JB |
449 | if (this.hasEvses) { |
450 | for (const evseStatus of this.evses.values()) { | |
451 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
452 | if (connectorStatus.transactionId === transactionId) { | |
453 | return connectorId; | |
454 | } | |
455 | } | |
456 | } | |
457 | } else { | |
458 | for (const connectorId of this.connectors.keys()) { | |
3fa7f799 | 459 | if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) { |
28e78158 JB |
460 | return connectorId; |
461 | } | |
c0560973 JB |
462 | } |
463 | } | |
464 | } | |
465 | ||
07989fad JB |
466 | public getEnergyActiveImportRegisterByTransactionId( |
467 | transactionId: number, | |
5edd8ba0 | 468 | rounded = false, |
07989fad JB |
469 | ): number { |
470 | return this.getEnergyActiveImportRegister( | |
e1d9a0f4 | 471 | this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)!)!, |
5edd8ba0 | 472 | rounded, |
cbad1217 | 473 | ); |
cbad1217 JB |
474 | } |
475 | ||
18bf8274 | 476 | public getEnergyActiveImportRegisterByConnectorId(connectorId: number, rounded = false): number { |
e1d9a0f4 | 477 | return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId)!, rounded); |
6ed92bc1 JB |
478 | } |
479 | ||
c0560973 | 480 | public getAuthorizeRemoteTxRequests(): boolean { |
f2d5e3d9 | 481 | const authorizeRemoteTxRequests = getConfigurationKey( |
17ac262c | 482 | this, |
5edd8ba0 | 483 | StandardParametersKey.AuthorizeRemoteTxRequests, |
e7aeea18 | 484 | ); |
9bf0ef23 | 485 | return authorizeRemoteTxRequests ? convertToBoolean(authorizeRemoteTxRequests.value) : false; |
c0560973 JB |
486 | } |
487 | ||
488 | public getLocalAuthListEnabled(): boolean { | |
f2d5e3d9 | 489 | const localAuthListEnabled = getConfigurationKey( |
17ac262c | 490 | this, |
5edd8ba0 | 491 | StandardParametersKey.LocalAuthListEnabled, |
e7aeea18 | 492 | ); |
9bf0ef23 | 493 | return localAuthListEnabled ? convertToBoolean(localAuthListEnabled.value) : false; |
c0560973 JB |
494 | } |
495 | ||
8f953431 | 496 | public getHeartbeatInterval(): number { |
f2d5e3d9 | 497 | const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval); |
8f953431 | 498 | if (HeartbeatInterval) { |
be4c6702 | 499 | return secondsToMilliseconds(convertToInt(HeartbeatInterval.value)); |
8f953431 | 500 | } |
f2d5e3d9 | 501 | const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval); |
8f953431 | 502 | if (HeartBeatInterval) { |
be4c6702 | 503 | return secondsToMilliseconds(convertToInt(HeartBeatInterval.value)); |
8f953431 JB |
504 | } |
505 | this.stationInfo?.autoRegister === false && | |
506 | logger.warn( | |
507 | `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${ | |
508 | Constants.DEFAULT_HEARTBEAT_INTERVAL | |
5edd8ba0 | 509 | }`, |
8f953431 JB |
510 | ); |
511 | return Constants.DEFAULT_HEARTBEAT_INTERVAL; | |
512 | } | |
513 | ||
269de583 JB |
514 | public setSupervisionUrl(url: string): void { |
515 | if ( | |
5398cecf JB |
516 | this.stationInfo?.supervisionUrlOcppConfiguration && |
517 | isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) | |
269de583 | 518 | ) { |
5398cecf | 519 | setConfigurationKeyValue(this, this.stationInfo.supervisionUrlOcppKey!, url); |
269de583 JB |
520 | } else { |
521 | this.stationInfo.supervisionUrls = url; | |
522 | this.saveStationInfo(); | |
523 | this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl(); | |
524 | } | |
525 | } | |
526 | ||
c0560973 | 527 | public startHeartbeat(): void { |
5b43123f | 528 | if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval) { |
6a8329b4 JB |
529 | this.heartbeatSetInterval = setInterval(() => { |
530 | this.ocppRequestService | |
531 | .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT) | |
72092cfc | 532 | .catch((error) => { |
6a8329b4 JB |
533 | logger.error( |
534 | `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`, | |
5edd8ba0 | 535 | error, |
6a8329b4 JB |
536 | ); |
537 | }); | |
c0560973 | 538 | }, this.getHeartbeatInterval()); |
e7aeea18 | 539 | logger.info( |
9bf0ef23 | 540 | `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds( |
5edd8ba0 JB |
541 | this.getHeartbeatInterval(), |
542 | )}`, | |
e7aeea18 | 543 | ); |
c0560973 | 544 | } else if (this.heartbeatSetInterval) { |
e7aeea18 | 545 | logger.info( |
9bf0ef23 | 546 | `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds( |
5edd8ba0 JB |
547 | this.getHeartbeatInterval(), |
548 | )}`, | |
e7aeea18 | 549 | ); |
c0560973 | 550 | } else { |
e7aeea18 | 551 | logger.error( |
944d4529 | 552 | `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`, |
e7aeea18 | 553 | ); |
c0560973 JB |
554 | } |
555 | } | |
556 | ||
557 | public restartHeartbeat(): void { | |
558 | // Stop heartbeat | |
559 | this.stopHeartbeat(); | |
560 | // Start heartbeat | |
561 | this.startHeartbeat(); | |
562 | } | |
563 | ||
17ac262c JB |
564 | public restartWebSocketPing(): void { |
565 | // Stop WebSocket ping | |
566 | this.stopWebSocketPing(); | |
567 | // Start WebSocket ping | |
568 | this.startWebSocketPing(); | |
569 | } | |
570 | ||
c0560973 JB |
571 | public startMeterValues(connectorId: number, interval: number): void { |
572 | if (connectorId === 0) { | |
e7aeea18 | 573 | logger.error( |
04c32a95 | 574 | `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`, |
e7aeea18 | 575 | ); |
c0560973 JB |
576 | return; |
577 | } | |
734d790d | 578 | if (!this.getConnectorStatus(connectorId)) { |
e7aeea18 | 579 | logger.error( |
66dd3447 | 580 | `${this.logPrefix()} Trying to start MeterValues on non existing connector id |
04c32a95 | 581 | ${connectorId}`, |
e7aeea18 | 582 | ); |
c0560973 JB |
583 | return; |
584 | } | |
5e3cb728 | 585 | if (this.getConnectorStatus(connectorId)?.transactionStarted === false) { |
e7aeea18 | 586 | logger.error( |
944d4529 | 587 | `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`, |
e7aeea18 | 588 | ); |
c0560973 | 589 | return; |
e7aeea18 | 590 | } else if ( |
5e3cb728 | 591 | this.getConnectorStatus(connectorId)?.transactionStarted === true && |
9bf0ef23 | 592 | isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId) |
e7aeea18 JB |
593 | ) { |
594 | logger.error( | |
944d4529 | 595 | `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`, |
e7aeea18 | 596 | ); |
c0560973 JB |
597 | return; |
598 | } | |
599 | if (interval > 0) { | |
e1d9a0f4 | 600 | this.getConnectorStatus(connectorId)!.transactionSetInterval = setInterval(() => { |
6a8329b4 JB |
601 | // FIXME: Implement OCPP version agnostic helpers |
602 | const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue( | |
603 | this, | |
604 | connectorId, | |
e1d9a0f4 | 605 | this.getConnectorStatus(connectorId)!.transactionId!, |
5edd8ba0 | 606 | interval, |
6a8329b4 JB |
607 | ); |
608 | this.ocppRequestService | |
609 | .requestHandler<MeterValuesRequest, MeterValuesResponse>( | |
08f130a0 | 610 | this, |
f22266fd JB |
611 | RequestCommand.METER_VALUES, |
612 | { | |
613 | connectorId, | |
72092cfc | 614 | transactionId: this.getConnectorStatus(connectorId)?.transactionId, |
f22266fd | 615 | meterValue: [meterValue], |
5edd8ba0 | 616 | }, |
6a8329b4 | 617 | ) |
72092cfc | 618 | .catch((error) => { |
6a8329b4 JB |
619 | logger.error( |
620 | `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`, | |
5edd8ba0 | 621 | error, |
6a8329b4 JB |
622 | ); |
623 | }); | |
624 | }, interval); | |
c0560973 | 625 | } else { |
e7aeea18 JB |
626 | logger.error( |
627 | `${this.logPrefix()} Charging station ${ | |
628 | StandardParametersKey.MeterValueSampleInterval | |
5edd8ba0 | 629 | } configuration set to ${interval}, not sending MeterValues`, |
e7aeea18 | 630 | ); |
c0560973 JB |
631 | } |
632 | } | |
633 | ||
04b1261c JB |
634 | public stopMeterValues(connectorId: number) { |
635 | if (this.getConnectorStatus(connectorId)?.transactionSetInterval) { | |
636 | clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval); | |
637 | } | |
638 | } | |
639 | ||
c0560973 | 640 | public start(): void { |
0d8852a5 JB |
641 | if (this.started === false) { |
642 | if (this.starting === false) { | |
643 | this.starting = true; | |
5398cecf | 644 | if (this.stationInfo?.enableStatistics === true) { |
551e477c | 645 | this.performanceStatistics?.start(); |
0d8852a5 | 646 | } |
d8093be1 | 647 | if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { |
178956d8 | 648 | this.startReservationExpirationSetInterval(); |
d193a949 | 649 | } |
0d8852a5 JB |
650 | this.openWSConnection(); |
651 | // Monitor charging station template file | |
1f8f6332 JB |
652 | this.templateFileWatcher = watchJsonFile( |
653 | this.templateFile, | |
654 | FileType.ChargingStationTemplate, | |
655 | this.logPrefix(), | |
656 | undefined, | |
657 | (event, filename): void => { | |
658 | if (isNotEmptyString(filename) && event === 'change') { | |
659 | try { | |
660 | logger.debug( | |
661 | `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${ | |
662 | this.templateFile | |
663 | } file have changed, reload`, | |
664 | ); | |
665 | this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash); | |
666 | // Initialize | |
667 | this.initialize(); | |
668 | this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!); | |
669 | // Restart the ATG | |
670 | this.stopAutomaticTransactionGenerator(); | |
671 | delete this.automaticTransactionGeneratorConfiguration; | |
61854f7c | 672 | if (this.getAutomaticTransactionGeneratorConfiguration().enable === true) { |
1f8f6332 JB |
673 | this.startAutomaticTransactionGenerator(); |
674 | } | |
5398cecf | 675 | if (this.stationInfo?.enableStatistics === true) { |
1f8f6332 JB |
676 | this.performanceStatistics?.restart(); |
677 | } else { | |
678 | this.performanceStatistics?.stop(); | |
679 | } | |
680 | // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed | |
681 | } catch (error) { | |
682 | logger.error( | |
683 | `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`, | |
684 | error, | |
685 | ); | |
686 | } | |
687 | } | |
688 | }, | |
689 | ); | |
56eb297e | 690 | this.started = true; |
db54d2e0 | 691 | this.emit(ChargingStationEvents.started); |
0d8852a5 JB |
692 | this.starting = false; |
693 | } else { | |
694 | logger.warn(`${this.logPrefix()} Charging station is already starting...`); | |
695 | } | |
950b1349 | 696 | } else { |
0d8852a5 | 697 | logger.warn(`${this.logPrefix()} Charging station is already started...`); |
950b1349 | 698 | } |
c0560973 JB |
699 | } |
700 | ||
9ff486f4 | 701 | public async stop(reason?: StopTransactionReason, stopTransactions?: boolean): Promise<void> { |
0d8852a5 JB |
702 | if (this.started === true) { |
703 | if (this.stopping === false) { | |
704 | this.stopping = true; | |
9ff486f4 | 705 | await this.stopMessageSequence(reason, stopTransactions); |
0d8852a5 | 706 | this.closeWSConnection(); |
5398cecf | 707 | if (this.stationInfo?.enableStatistics === true) { |
551e477c | 708 | this.performanceStatistics?.stop(); |
0d8852a5 | 709 | } |
d8093be1 | 710 | if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { |
5543b88d JB |
711 | this.stopReservationExpirationSetInterval(); |
712 | } | |
0d8852a5 | 713 | this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); |
72092cfc | 714 | this.templateFileWatcher?.close(); |
cda5d0fb | 715 | this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash); |
cdde2cfe | 716 | delete this.bootNotificationResponse; |
0d8852a5 | 717 | this.started = false; |
ac7f79af | 718 | this.saveConfiguration(); |
db54d2e0 | 719 | this.emit(ChargingStationEvents.stopped); |
0d8852a5 JB |
720 | this.stopping = false; |
721 | } else { | |
722 | logger.warn(`${this.logPrefix()} Charging station is already stopping...`); | |
c0560973 | 723 | } |
950b1349 | 724 | } else { |
0d8852a5 | 725 | logger.warn(`${this.logPrefix()} Charging station is already stopped...`); |
c0560973 | 726 | } |
c0560973 JB |
727 | } |
728 | ||
60ddad53 JB |
729 | public async reset(reason?: StopTransactionReason): Promise<void> { |
730 | await this.stop(reason); | |
e1d9a0f4 | 731 | await sleep(this.stationInfo.resetTime!); |
fa7bccf4 | 732 | this.initialize(); |
94ec7e96 JB |
733 | this.start(); |
734 | } | |
735 | ||
17ac262c | 736 | public saveOcppConfiguration(): void { |
5398cecf | 737 | if (this.stationInfo?.ocppPersistentConfiguration === true) { |
b1bbdae5 | 738 | this.saveConfiguration(); |
e6895390 JB |
739 | } |
740 | } | |
741 | ||
8e242273 JB |
742 | public bufferMessage(message: string): void { |
743 | this.messageBuffer.add(message); | |
3ba2381e JB |
744 | } |
745 | ||
db2336d9 | 746 | public openWSConnection( |
7f3decca JB |
747 | options?: WsOptions, |
748 | params?: { closeOpened?: boolean; terminateOpened?: boolean }, | |
db2336d9 | 749 | ): void { |
7f3decca JB |
750 | options = { |
751 | handshakeTimeout: secondsToMilliseconds(this.getConnectionTimeout()), | |
e6a33233 | 752 | ...this.stationInfo?.wsOptions, |
7f3decca JB |
753 | ...options, |
754 | }; | |
b1bbdae5 | 755 | params = { ...{ closeOpened: false, terminateOpened: false }, ...params }; |
e6a33233 | 756 | if (!checkChargingStation(this, this.logPrefix())) { |
d1c6c833 JB |
757 | return; |
758 | } | |
db2336d9 | 759 | if ( |
9bf0ef23 JB |
760 | !isNullOrUndefined(this.stationInfo.supervisionUser) && |
761 | !isNullOrUndefined(this.stationInfo.supervisionPassword) | |
db2336d9 JB |
762 | ) { |
763 | options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`; | |
764 | } | |
765 | if (params?.closeOpened) { | |
766 | this.closeWSConnection(); | |
767 | } | |
768 | if (params?.terminateOpened) { | |
769 | this.terminateWSConnection(); | |
770 | } | |
db2336d9 | 771 | |
56eb297e | 772 | if (this.isWebSocketConnectionOpened() === true) { |
0a03f36c | 773 | logger.warn( |
944d4529 | 774 | `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`, |
0a03f36c JB |
775 | ); |
776 | return; | |
777 | } | |
778 | ||
db2336d9 | 779 | logger.info( |
5edd8ba0 | 780 | `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`, |
db2336d9 JB |
781 | ); |
782 | ||
feff11ec JB |
783 | this.wsConnection = new WebSocket( |
784 | this.wsConnectionUrl, | |
9a77cc07 | 785 | `ocpp${this.stationInfo?.ocppVersion}`, |
5edd8ba0 | 786 | options, |
feff11ec | 787 | ); |
db2336d9 JB |
788 | |
789 | // Handle WebSocket message | |
790 | this.wsConnection.on( | |
791 | 'message', | |
5edd8ba0 | 792 | this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void, |
db2336d9 JB |
793 | ); |
794 | // Handle WebSocket error | |
795 | this.wsConnection.on( | |
796 | 'error', | |
5edd8ba0 | 797 | this.onError.bind(this) as (this: WebSocket, error: Error) => void, |
db2336d9 JB |
798 | ); |
799 | // Handle WebSocket close | |
800 | this.wsConnection.on( | |
801 | 'close', | |
5edd8ba0 | 802 | this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void, |
db2336d9 JB |
803 | ); |
804 | // Handle WebSocket open | |
805 | this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void); | |
806 | // Handle WebSocket ping | |
807 | this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void); | |
808 | // Handle WebSocket pong | |
809 | this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void); | |
810 | } | |
811 | ||
812 | public closeWSConnection(): void { | |
56eb297e | 813 | if (this.isWebSocketConnectionOpened() === true) { |
72092cfc | 814 | this.wsConnection?.close(); |
db2336d9 JB |
815 | this.wsConnection = null; |
816 | } | |
817 | } | |
818 | ||
e1d9a0f4 | 819 | public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration { |
c7db8ecb JB |
820 | if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration)) { |
821 | let automaticTransactionGeneratorConfiguration: | |
822 | | AutomaticTransactionGeneratorConfiguration | |
823 | | undefined; | |
61854f7c JB |
824 | const stationTemplate = this.getTemplateFromFile(); |
825 | const stationConfiguration = this.getConfigurationFromFile(); | |
c7db8ecb | 826 | if ( |
5398cecf | 827 | this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true && |
61854f7c JB |
828 | stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash && |
829 | stationConfiguration?.automaticTransactionGenerator | |
c7db8ecb JB |
830 | ) { |
831 | automaticTransactionGeneratorConfiguration = | |
61854f7c | 832 | stationConfiguration?.automaticTransactionGenerator; |
c7db8ecb | 833 | } else { |
61854f7c | 834 | automaticTransactionGeneratorConfiguration = stationTemplate?.AutomaticTransactionGenerator; |
c7db8ecb JB |
835 | } |
836 | this.automaticTransactionGeneratorConfiguration = { | |
837 | ...Constants.DEFAULT_ATG_CONFIGURATION, | |
838 | ...automaticTransactionGeneratorConfiguration, | |
839 | }; | |
ac7f79af | 840 | } |
c7db8ecb | 841 | return this.automaticTransactionGeneratorConfiguration!; |
ac7f79af JB |
842 | } |
843 | ||
5ced7e80 JB |
844 | public getAutomaticTransactionGeneratorStatuses(): Status[] | undefined { |
845 | return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses; | |
846 | } | |
847 | ||
ac7f79af JB |
848 | public startAutomaticTransactionGenerator(connectorIds?: number[]): void { |
849 | this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this); | |
9bf0ef23 | 850 | if (isNotEmptyArray(connectorIds)) { |
e1d9a0f4 | 851 | for (const connectorId of connectorIds!) { |
551e477c | 852 | this.automaticTransactionGenerator?.startConnector(connectorId); |
a5e9befc JB |
853 | } |
854 | } else { | |
551e477c | 855 | this.automaticTransactionGenerator?.start(); |
4f69be04 | 856 | } |
cb60061f | 857 | this.saveAutomaticTransactionGeneratorConfiguration(); |
db54d2e0 | 858 | this.emit(ChargingStationEvents.updated); |
4f69be04 JB |
859 | } |
860 | ||
9ff486f4 | 861 | public stopAutomaticTransactionGenerator(connectorIds?: number[]): void { |
9bf0ef23 | 862 | if (isNotEmptyArray(connectorIds)) { |
e1d9a0f4 | 863 | for (const connectorId of connectorIds!) { |
9ff486f4 | 864 | this.automaticTransactionGenerator?.stopConnector(connectorId); |
a5e9befc JB |
865 | } |
866 | } else { | |
9ff486f4 | 867 | this.automaticTransactionGenerator?.stop(); |
4f69be04 | 868 | } |
cb60061f | 869 | this.saveAutomaticTransactionGeneratorConfiguration(); |
db54d2e0 | 870 | this.emit(ChargingStationEvents.updated); |
4f69be04 JB |
871 | } |
872 | ||
5e3cb728 JB |
873 | public async stopTransactionOnConnector( |
874 | connectorId: number, | |
9ff486f4 | 875 | reason?: StopTransactionReason, |
5e3cb728 | 876 | ): Promise<StopTransactionResponse> { |
72092cfc | 877 | const transactionId = this.getConnectorStatus(connectorId)?.transactionId; |
5e3cb728 | 878 | if ( |
5398cecf JB |
879 | this.stationInfo?.beginEndMeterValues === true && |
880 | this.stationInfo?.ocppStrictCompliance === true && | |
881 | this.stationInfo?.outOfOrderEndMeterValues === false | |
5e3cb728 JB |
882 | ) { |
883 | // FIXME: Implement OCPP version agnostic helpers | |
884 | const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( | |
885 | this, | |
886 | connectorId, | |
e1d9a0f4 | 887 | this.getEnergyActiveImportRegisterByTransactionId(transactionId!), |
5e3cb728 JB |
888 | ); |
889 | await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>( | |
890 | this, | |
891 | RequestCommand.METER_VALUES, | |
892 | { | |
893 | connectorId, | |
894 | transactionId, | |
895 | meterValue: [transactionEndMeterValue], | |
5edd8ba0 | 896 | }, |
5e3cb728 JB |
897 | ); |
898 | } | |
899 | return this.ocppRequestService.requestHandler<StopTransactionRequest, StopTransactionResponse>( | |
900 | this, | |
901 | RequestCommand.STOP_TRANSACTION, | |
902 | { | |
903 | transactionId, | |
e1d9a0f4 | 904 | meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId!, true), |
9ff486f4 | 905 | ...(isNullOrUndefined(reason) && { reason }), |
5edd8ba0 | 906 | }, |
5e3cb728 JB |
907 | ); |
908 | } | |
909 | ||
10e8c3e1 | 910 | public getReserveConnectorZeroSupported(): boolean { |
9bf0ef23 | 911 | return convertToBoolean( |
f2d5e3d9 | 912 | getConfigurationKey(this, StandardParametersKey.ReserveConnectorZeroSupported)!.value, |
24578c31 JB |
913 | ); |
914 | } | |
915 | ||
d193a949 | 916 | public async addReservation(reservation: Reservation): Promise<void> { |
2ca0ea90 | 917 | const reservationFound = this.getReservationBy('reservationId', reservation.reservationId); |
530e5fbb JB |
918 | if (!isUndefined(reservationFound)) { |
919 | await this.removeReservation( | |
920 | reservationFound!, | |
921 | ReservationTerminationReason.REPLACE_EXISTING, | |
922 | ); | |
d193a949 | 923 | } |
e1d9a0f4 | 924 | this.getConnectorStatus(reservation.connectorId)!.reservation = reservation; |
ec94a3cf | 925 | await OCPPServiceUtils.sendAndSetConnectorStatus( |
d193a949 | 926 | this, |
ec94a3cf JB |
927 | reservation.connectorId, |
928 | ConnectorStatusEnum.Reserved, | |
e1d9a0f4 | 929 | undefined, |
5edd8ba0 | 930 | { send: reservation.connectorId !== 0 }, |
d193a949 | 931 | ); |
24578c31 JB |
932 | } |
933 | ||
d193a949 JB |
934 | public async removeReservation( |
935 | reservation: Reservation, | |
90aceaf6 | 936 | reason: ReservationTerminationReason, |
d193a949 | 937 | ): Promise<void> { |
e1d9a0f4 | 938 | const connector = this.getConnectorStatus(reservation.connectorId)!; |
d193a949 | 939 | switch (reason) { |
96d96b12 | 940 | case ReservationTerminationReason.CONNECTOR_STATE_CHANGED: |
ec94a3cf | 941 | case ReservationTerminationReason.TRANSACTION_STARTED: |
ec9f36cc JB |
942 | delete connector.reservation; |
943 | break; | |
e74bc549 JB |
944 | case ReservationTerminationReason.RESERVATION_CANCELED: |
945 | case ReservationTerminationReason.REPLACE_EXISTING: | |
946 | case ReservationTerminationReason.EXPIRED: | |
ec94a3cf | 947 | await OCPPServiceUtils.sendAndSetConnectorStatus( |
d193a949 | 948 | this, |
ec94a3cf JB |
949 | reservation.connectorId, |
950 | ConnectorStatusEnum.Available, | |
e1d9a0f4 | 951 | undefined, |
5edd8ba0 | 952 | { send: reservation.connectorId !== 0 }, |
d193a949 | 953 | ); |
ec94a3cf | 954 | delete connector.reservation; |
d193a949 | 955 | break; |
b029e74e | 956 | default: |
90aceaf6 | 957 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
56563a3c | 958 | throw new BaseError(`Unknown reservation termination reason '${reason}'`); |
d193a949 | 959 | } |
24578c31 JB |
960 | } |
961 | ||
3fa7f799 | 962 | public getReservationBy( |
366f75f6 | 963 | filterKey: ReservationKey, |
5edd8ba0 | 964 | value: number | string, |
3fa7f799 | 965 | ): Reservation | undefined { |
66dd3447 | 966 | if (this.hasEvses) { |
3fa7f799 JB |
967 | for (const evseStatus of this.evses.values()) { |
968 | for (const connectorStatus of evseStatus.connectors.values()) { | |
2ca0ea90 | 969 | if (connectorStatus?.reservation?.[filterKey] === value) { |
3fa7f799 | 970 | return connectorStatus.reservation; |
66dd3447 JB |
971 | } |
972 | } | |
973 | } | |
974 | } else { | |
3fa7f799 | 975 | for (const connectorStatus of this.connectors.values()) { |
2ca0ea90 | 976 | if (connectorStatus?.reservation?.[filterKey] === value) { |
3fa7f799 | 977 | return connectorStatus.reservation; |
66dd3447 JB |
978 | } |
979 | } | |
980 | } | |
d193a949 JB |
981 | } |
982 | ||
e6948a57 JB |
983 | public isConnectorReservable( |
984 | reservationId: number, | |
985 | idTag?: string, | |
986 | connectorId?: number, | |
987 | ): boolean { | |
90aceaf6 JB |
988 | const reservation = this.getReservationBy('reservationId', reservationId); |
989 | const reservationExists = !isUndefined(reservation) && !hasReservationExpired(reservation!); | |
e6948a57 JB |
990 | if (arguments.length === 1) { |
991 | return !reservationExists; | |
992 | } else if (arguments.length > 1) { | |
90aceaf6 JB |
993 | const userReservation = !isUndefined(idTag) |
994 | ? this.getReservationBy('idTag', idTag!) | |
995 | : undefined; | |
e6948a57 | 996 | const userReservationExists = |
90aceaf6 | 997 | !isUndefined(userReservation) && !hasReservationExpired(userReservation!); |
e6948a57 JB |
998 | const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0; |
999 | const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0; | |
1000 | return ( | |
1001 | !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable | |
1002 | ); | |
1003 | } | |
1004 | return false; | |
1005 | } | |
1006 | ||
1007 | private startReservationExpirationSetInterval(customInterval?: number): void { | |
2035255d | 1008 | const interval = customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_INTERVAL; |
42371a2e | 1009 | if (interval > 0) { |
04c32a95 JB |
1010 | logger.info( |
1011 | `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds( | |
1012 | interval, | |
1013 | )}`, | |
1014 | ); | |
37aa4e56 | 1015 | this.reservationExpirationSetInterval = setInterval((): void => { |
90aceaf6 | 1016 | removeExpiredReservations(this).catch(Constants.EMPTY_FUNCTION); |
42371a2e JB |
1017 | }, interval); |
1018 | } | |
d193a949 JB |
1019 | } |
1020 | ||
e6948a57 | 1021 | private stopReservationExpirationSetInterval(): void { |
d116b9b5 | 1022 | if (!isNullOrUndefined(this.reservationExpirationSetInterval)) { |
e6948a57 JB |
1023 | clearInterval(this.reservationExpirationSetInterval); |
1024 | } | |
d193a949 JB |
1025 | } |
1026 | ||
366f75f6 JB |
1027 | // private restartReservationExpiryDateSetInterval(): void { |
1028 | // this.stopReservationExpirationSetInterval(); | |
1029 | // this.startReservationExpirationSetInterval(); | |
1030 | // } | |
d193a949 JB |
1031 | |
1032 | private getNumberOfReservableConnectors(): number { | |
90aceaf6 | 1033 | let numberOfReservableConnectors = 0; |
66dd3447 | 1034 | if (this.hasEvses) { |
3fa7f799 | 1035 | for (const evseStatus of this.evses.values()) { |
90aceaf6 | 1036 | numberOfReservableConnectors += getNumberOfReservableConnectors(evseStatus.connectors); |
66dd3447 JB |
1037 | } |
1038 | } else { | |
90aceaf6 | 1039 | numberOfReservableConnectors = getNumberOfReservableConnectors(this.connectors); |
66dd3447 | 1040 | } |
90aceaf6 | 1041 | return numberOfReservableConnectors - this.getNumberOfReservationsOnConnectorZero(); |
66dd3447 JB |
1042 | } |
1043 | ||
d193a949 | 1044 | private getNumberOfReservationsOnConnectorZero(): number { |
6913d568 JB |
1045 | if ( |
1046 | // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | |
1047 | (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation) || | |
1048 | (!this.hasEvses && this.connectors.get(0)?.reservation) | |
1049 | ) { | |
cfc9875a | 1050 | return 1; |
66dd3447 | 1051 | } |
cfc9875a | 1052 | return 0; |
24578c31 JB |
1053 | } |
1054 | ||
f90c1757 | 1055 | private flushMessageBuffer(): void { |
8e242273 | 1056 | if (this.messageBuffer.size > 0) { |
7d3b0f64 | 1057 | for (const message of this.messageBuffer.values()) { |
e1d9a0f4 JB |
1058 | let beginId: string | undefined; |
1059 | let commandName: RequestCommand | undefined; | |
8ca6874c | 1060 | const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse; |
1431af78 JB |
1061 | const isRequest = messageType === MessageType.CALL_MESSAGE; |
1062 | if (isRequest) { | |
1063 | [, , commandName] = JSON.parse(message) as OutgoingRequest; | |
1064 | beginId = PerformanceStatistics.beginMeasure(commandName); | |
1065 | } | |
72092cfc | 1066 | this.wsConnection?.send(message); |
e1d9a0f4 | 1067 | isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!); |
8ca6874c JB |
1068 | logger.debug( |
1069 | `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString( | |
5edd8ba0 JB |
1070 | messageType, |
1071 | )} payload sent: ${message}`, | |
8ca6874c | 1072 | ); |
8e242273 | 1073 | this.messageBuffer.delete(message); |
7d3b0f64 | 1074 | } |
77f00f84 JB |
1075 | } |
1076 | } | |
1077 | ||
72092cfc | 1078 | private getTemplateFromFile(): ChargingStationTemplate | undefined { |
e1d9a0f4 | 1079 | let template: ChargingStationTemplate | undefined; |
5ad8570f | 1080 | try { |
cda5d0fb JB |
1081 | if (this.sharedLRUCache.hasChargingStationTemplate(this.templateFileHash)) { |
1082 | template = this.sharedLRUCache.getChargingStationTemplate(this.templateFileHash); | |
7c72977b JB |
1083 | } else { |
1084 | const measureId = `${FileType.ChargingStationTemplate} read`; | |
1085 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
d972af76 | 1086 | template = JSON.parse(readFileSync(this.templateFile, 'utf8')) as ChargingStationTemplate; |
7c72977b | 1087 | PerformanceStatistics.endMeasure(measureId, beginId); |
d972af76 | 1088 | template.templateHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
7c72977b JB |
1089 | .update(JSON.stringify(template)) |
1090 | .digest('hex'); | |
57adbebc | 1091 | this.sharedLRUCache.setChargingStationTemplate(template); |
cda5d0fb | 1092 | this.templateFileHash = template.templateHash; |
7c72977b | 1093 | } |
5ad8570f | 1094 | } catch (error) { |
fa5995d6 | 1095 | handleFileException( |
2484ac1e | 1096 | this.templateFile, |
7164966d JB |
1097 | FileType.ChargingStationTemplate, |
1098 | error as NodeJS.ErrnoException, | |
5edd8ba0 | 1099 | this.logPrefix(), |
e7aeea18 | 1100 | ); |
5ad8570f | 1101 | } |
2484ac1e JB |
1102 | return template; |
1103 | } | |
1104 | ||
7a3a2ebb | 1105 | private getStationInfoFromTemplate(): ChargingStationInfo { |
e1d9a0f4 | 1106 | const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile()!; |
fba11dc6 | 1107 | checkTemplate(stationTemplate, this.logPrefix(), this.templateFile); |
5f742aac JB |
1108 | const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this); |
1109 | warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile); | |
8a133cc8 | 1110 | if (stationTemplate?.Connectors) { |
fba11dc6 | 1111 | checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile); |
8a133cc8 | 1112 | } |
fba11dc6 JB |
1113 | const stationInfo: ChargingStationInfo = stationTemplateToStationInfo(stationTemplate); |
1114 | stationInfo.hashId = getHashId(this.index, stationTemplate); | |
1115 | stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate); | |
72092cfc | 1116 | stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16; |
fba11dc6 | 1117 | createSerialNumber(stationTemplate, stationInfo); |
9bf0ef23 | 1118 | if (isNotEmptyArray(stationTemplate?.power)) { |
551e477c | 1119 | stationTemplate.power = stationTemplate.power as number[]; |
9bf0ef23 | 1120 | const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length); |
cc6e8ab5 | 1121 | stationInfo.maximumPower = |
72092cfc | 1122 | stationTemplate?.powerUnit === PowerUnits.KILO_WATT |
fa7bccf4 JB |
1123 | ? stationTemplate.power[powerArrayRandomIndex] * 1000 |
1124 | : stationTemplate.power[powerArrayRandomIndex]; | |
5ad8570f | 1125 | } else { |
551e477c | 1126 | stationTemplate.power = stationTemplate?.power as number; |
cc6e8ab5 | 1127 | stationInfo.maximumPower = |
72092cfc | 1128 | stationTemplate?.powerUnit === PowerUnits.KILO_WATT |
fa7bccf4 JB |
1129 | ? stationTemplate.power * 1000 |
1130 | : stationTemplate.power; | |
1131 | } | |
73de8f02 | 1132 | stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo); |
3637ca2c | 1133 | stationInfo.firmwareVersionPattern = |
72092cfc | 1134 | stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN; |
3637ca2c | 1135 | if ( |
9bf0ef23 | 1136 | isNotEmptyString(stationInfo.firmwareVersion) && |
e1d9a0f4 | 1137 | new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion!) === false |
3637ca2c JB |
1138 | ) { |
1139 | logger.warn( | |
1140 | `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${ | |
1141 | this.templateFile | |
5edd8ba0 | 1142 | } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`, |
3637ca2c JB |
1143 | ); |
1144 | } | |
598c886d | 1145 | stationInfo.firmwareUpgrade = merge<FirmwareUpgrade>( |
15748260 | 1146 | { |
598c886d JB |
1147 | versionUpgrade: { |
1148 | step: 1, | |
1149 | }, | |
15748260 JB |
1150 | reset: true, |
1151 | }, | |
5edd8ba0 | 1152 | stationTemplate?.firmwareUpgrade ?? {}, |
15748260 | 1153 | ); |
9bf0ef23 | 1154 | stationInfo.resetTime = !isNullOrUndefined(stationTemplate?.resetTime) |
be4c6702 | 1155 | ? secondsToMilliseconds(stationTemplate.resetTime!) |
e7aeea18 | 1156 | : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; |
9ac86a7e | 1157 | return stationInfo; |
5ad8570f JB |
1158 | } |
1159 | ||
551e477c JB |
1160 | private getStationInfoFromFile(): ChargingStationInfo | undefined { |
1161 | let stationInfo: ChargingStationInfo | undefined; | |
5398cecf | 1162 | if (this.stationInfo?.stationInfoPersistentConfiguration === true) { |
f832e5df JB |
1163 | stationInfo = this.getConfigurationFromFile()?.stationInfo; |
1164 | if (stationInfo) { | |
1165 | delete stationInfo?.infoHash; | |
1166 | } | |
1167 | } | |
f765beaa | 1168 | return stationInfo; |
2484ac1e JB |
1169 | } |
1170 | ||
1171 | private getStationInfo(): ChargingStationInfo { | |
1172 | const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate(); | |
551e477c | 1173 | const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile(); |
6b90dcca JB |
1174 | // Priority: |
1175 | // 1. charging station info from template | |
1176 | // 2. charging station info from configuration file | |
f765beaa | 1177 | if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) { |
e1d9a0f4 | 1178 | return stationInfoFromFile!; |
f765beaa | 1179 | } |
fec4d204 | 1180 | stationInfoFromFile && |
fba11dc6 | 1181 | propagateSerialNumber( |
e1d9a0f4 | 1182 | this.getTemplateFromFile()!, |
fec4d204 | 1183 | stationInfoFromFile, |
5edd8ba0 | 1184 | stationInfoFromTemplate, |
fec4d204 | 1185 | ); |
01efc60a | 1186 | return stationInfoFromTemplate; |
2484ac1e JB |
1187 | } |
1188 | ||
1189 | private saveStationInfo(): void { | |
5398cecf | 1190 | if (this.stationInfo?.stationInfoPersistentConfiguration === true) { |
b1bbdae5 | 1191 | this.saveConfiguration(); |
ccb1d6e9 | 1192 | } |
2484ac1e JB |
1193 | } |
1194 | ||
c0560973 | 1195 | private handleUnsupportedVersion(version: OCPPVersion) { |
944d4529 | 1196 | const errorMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`; |
ded57f02 JB |
1197 | logger.error(`${this.logPrefix()} ${errorMsg}`); |
1198 | throw new BaseError(errorMsg); | |
c0560973 JB |
1199 | } |
1200 | ||
2484ac1e | 1201 | private initialize(): void { |
e1d9a0f4 | 1202 | const stationTemplate = this.getTemplateFromFile()!; |
fba11dc6 | 1203 | checkTemplate(stationTemplate, this.logPrefix(), this.templateFile); |
d972af76 JB |
1204 | this.configurationFile = join( |
1205 | dirname(this.templateFile.replace('station-templates', 'configurations')), | |
5edd8ba0 | 1206 | `${getHashId(this.index, stationTemplate)}.json`, |
0642c3d2 | 1207 | ); |
61854f7c | 1208 | const stationConfiguration = this.getConfigurationFromFile(); |
a4f7c75f | 1209 | if ( |
61854f7c | 1210 | stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash && |
e1d9a0f4 | 1211 | // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing |
61854f7c | 1212 | (stationConfiguration?.connectorsStatus || stationConfiguration?.evsesStatus) |
a4f7c75f | 1213 | ) { |
61854f7c | 1214 | this.initializeConnectorsOrEvsesFromFile(stationConfiguration); |
a4f7c75f JB |
1215 | } else { |
1216 | this.initializeConnectorsOrEvsesFromTemplate(stationTemplate); | |
1217 | } | |
5398cecf | 1218 | this.internalStationInfo = this.getStationInfo(); |
3637ca2c JB |
1219 | if ( |
1220 | this.stationInfo.firmwareStatus === FirmwareStatus.Installing && | |
9bf0ef23 JB |
1221 | isNotEmptyString(this.stationInfo.firmwareVersion) && |
1222 | isNotEmptyString(this.stationInfo.firmwareVersionPattern) | |
3637ca2c | 1223 | ) { |
d812bdcb | 1224 | const patternGroup: number | undefined = |
15748260 | 1225 | this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ?? |
d812bdcb | 1226 | this.stationInfo.firmwareVersion?.split('.').length; |
e1d9a0f4 JB |
1227 | const match = this.stationInfo |
1228 | .firmwareVersion!.match(new RegExp(this.stationInfo.firmwareVersionPattern!))! | |
1229 | .slice(1, patternGroup! + 1); | |
3637ca2c | 1230 | const patchLevelIndex = match.length - 1; |
5d280aae | 1231 | match[patchLevelIndex] = ( |
9bf0ef23 | 1232 | convertToInt(match[patchLevelIndex]) + |
e1d9a0f4 | 1233 | this.stationInfo.firmwareUpgrade!.versionUpgrade!.step! |
5d280aae | 1234 | ).toString(); |
72092cfc | 1235 | this.stationInfo.firmwareVersion = match?.join('.'); |
3637ca2c | 1236 | } |
6bccfcbc | 1237 | this.saveStationInfo(); |
6bccfcbc | 1238 | this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl(); |
5398cecf | 1239 | if (this.stationInfo?.enableStatistics === true) { |
6bccfcbc JB |
1240 | this.performanceStatistics = PerformanceStatistics.getInstance( |
1241 | this.stationInfo.hashId, | |
e1d9a0f4 | 1242 | this.stationInfo.chargingStationId!, |
5edd8ba0 | 1243 | this.configuredSupervisionUrl, |
6bccfcbc JB |
1244 | ); |
1245 | } | |
fba11dc6 | 1246 | this.bootNotificationRequest = createBootNotificationRequest(this.stationInfo); |
692f2f64 JB |
1247 | this.powerDivider = this.getPowerDivider(); |
1248 | // OCPP configuration | |
1249 | this.ocppConfiguration = this.getOcppConfiguration(); | |
1250 | this.initializeOcppConfiguration(); | |
1251 | this.initializeOcppServices(); | |
1252 | if (this.stationInfo?.autoRegister === true) { | |
1253 | this.bootNotificationResponse = { | |
1254 | currentTime: new Date(), | |
be4c6702 | 1255 | interval: millisecondsToSeconds(this.getHeartbeatInterval()), |
692f2f64 JB |
1256 | status: RegistrationStatusEnumType.ACCEPTED, |
1257 | }; | |
1258 | } | |
147d0e0f JB |
1259 | } |
1260 | ||
feff11ec | 1261 | private initializeOcppServices(): void { |
9a77cc07 | 1262 | const ocppVersion = this.stationInfo?.ocppVersion; |
feff11ec JB |
1263 | switch (ocppVersion) { |
1264 | case OCPPVersion.VERSION_16: | |
1265 | this.ocppIncomingRequestService = | |
1266 | OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>(); | |
1267 | this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>( | |
5edd8ba0 | 1268 | OCPP16ResponseService.getInstance<OCPP16ResponseService>(), |
feff11ec JB |
1269 | ); |
1270 | break; | |
1271 | case OCPPVersion.VERSION_20: | |
1272 | case OCPPVersion.VERSION_201: | |
1273 | this.ocppIncomingRequestService = | |
1274 | OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>(); | |
1275 | this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>( | |
5edd8ba0 | 1276 | OCPP20ResponseService.getInstance<OCPP20ResponseService>(), |
feff11ec JB |
1277 | ); |
1278 | break; | |
1279 | default: | |
1280 | this.handleUnsupportedVersion(ocppVersion); | |
1281 | break; | |
1282 | } | |
1283 | } | |
1284 | ||
2484ac1e | 1285 | private initializeOcppConfiguration(): void { |
f2d5e3d9 JB |
1286 | if (!getConfigurationKey(this, StandardParametersKey.HeartbeatInterval)) { |
1287 | addConfigurationKey(this, StandardParametersKey.HeartbeatInterval, '0'); | |
f0f65a62 | 1288 | } |
f2d5e3d9 JB |
1289 | if (!getConfigurationKey(this, StandardParametersKey.HeartBeatInterval)) { |
1290 | addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { visible: false }); | |
f0f65a62 | 1291 | } |
e7aeea18 | 1292 | if ( |
5398cecf JB |
1293 | this.stationInfo?.supervisionUrlOcppConfiguration && |
1294 | isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && | |
1295 | !getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) | |
e7aeea18 | 1296 | ) { |
f2d5e3d9 | 1297 | addConfigurationKey( |
17ac262c | 1298 | this, |
5398cecf | 1299 | this.stationInfo.supervisionUrlOcppKey!, |
fa7bccf4 | 1300 | this.configuredSupervisionUrl.href, |
5edd8ba0 | 1301 | { reboot: true }, |
e7aeea18 | 1302 | ); |
e6895390 | 1303 | } else if ( |
5398cecf JB |
1304 | !this.stationInfo?.supervisionUrlOcppConfiguration && |
1305 | isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) && | |
1306 | getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!) | |
e6895390 | 1307 | ) { |
5398cecf | 1308 | deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!, { save: false }); |
12fc74d6 | 1309 | } |
cc6e8ab5 | 1310 | if ( |
9bf0ef23 | 1311 | isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && |
f2d5e3d9 | 1312 | !getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) |
cc6e8ab5 | 1313 | ) { |
f2d5e3d9 | 1314 | addConfigurationKey( |
17ac262c | 1315 | this, |
e1d9a0f4 | 1316 | this.stationInfo.amperageLimitationOcppKey!, |
17ac262c | 1317 | ( |
e1d9a0f4 | 1318 | this.stationInfo.maximumAmperage! * getAmperageLimitationUnitDivider(this.stationInfo) |
5edd8ba0 | 1319 | ).toString(), |
cc6e8ab5 JB |
1320 | ); |
1321 | } | |
f2d5e3d9 JB |
1322 | if (!getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)) { |
1323 | addConfigurationKey( | |
17ac262c | 1324 | this, |
e7aeea18 | 1325 | StandardParametersKey.SupportedFeatureProfiles, |
5edd8ba0 | 1326 | `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`, |
e7aeea18 JB |
1327 | ); |
1328 | } | |
f2d5e3d9 | 1329 | addConfigurationKey( |
17ac262c | 1330 | this, |
e7aeea18 JB |
1331 | StandardParametersKey.NumberOfConnectors, |
1332 | this.getNumberOfConnectors().toString(), | |
a95873d8 | 1333 | { readonly: true }, |
5edd8ba0 | 1334 | { overwrite: true }, |
e7aeea18 | 1335 | ); |
f2d5e3d9 JB |
1336 | if (!getConfigurationKey(this, StandardParametersKey.MeterValuesSampledData)) { |
1337 | addConfigurationKey( | |
17ac262c | 1338 | this, |
e7aeea18 | 1339 | StandardParametersKey.MeterValuesSampledData, |
5edd8ba0 | 1340 | MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER, |
e7aeea18 | 1341 | ); |
7abfea5f | 1342 | } |
f2d5e3d9 | 1343 | if (!getConfigurationKey(this, StandardParametersKey.ConnectorPhaseRotation)) { |
dd08d43d | 1344 | const connectorsPhaseRotation: string[] = []; |
28e78158 JB |
1345 | if (this.hasEvses) { |
1346 | for (const evseStatus of this.evses.values()) { | |
1347 | for (const connectorId of evseStatus.connectors.keys()) { | |
dd08d43d | 1348 | connectorsPhaseRotation.push( |
e1d9a0f4 | 1349 | getPhaseRotationValue(connectorId, this.getNumberOfPhases())!, |
dd08d43d | 1350 | ); |
28e78158 JB |
1351 | } |
1352 | } | |
1353 | } else { | |
1354 | for (const connectorId of this.connectors.keys()) { | |
dd08d43d | 1355 | connectorsPhaseRotation.push( |
e1d9a0f4 | 1356 | getPhaseRotationValue(connectorId, this.getNumberOfPhases())!, |
dd08d43d | 1357 | ); |
7e1dc878 JB |
1358 | } |
1359 | } | |
f2d5e3d9 | 1360 | addConfigurationKey( |
17ac262c | 1361 | this, |
e7aeea18 | 1362 | StandardParametersKey.ConnectorPhaseRotation, |
5edd8ba0 | 1363 | connectorsPhaseRotation.toString(), |
e7aeea18 | 1364 | ); |
7e1dc878 | 1365 | } |
f2d5e3d9 JB |
1366 | if (!getConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests)) { |
1367 | addConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests, 'true'); | |
36f6a92e | 1368 | } |
17ac262c | 1369 | if ( |
f2d5e3d9 JB |
1370 | !getConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled) && |
1371 | getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)?.value?.includes( | |
1372 | SupportedFeatureProfiles.LocalAuthListManagement, | |
17ac262c JB |
1373 | ) |
1374 | ) { | |
f2d5e3d9 JB |
1375 | addConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled, 'false'); |
1376 | } | |
1377 | if (!getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) { | |
1378 | addConfigurationKey( | |
17ac262c | 1379 | this, |
e7aeea18 | 1380 | StandardParametersKey.ConnectionTimeOut, |
5edd8ba0 | 1381 | Constants.DEFAULT_CONNECTION_TIMEOUT.toString(), |
e7aeea18 | 1382 | ); |
8bce55bf | 1383 | } |
2484ac1e | 1384 | this.saveOcppConfiguration(); |
073bd098 JB |
1385 | } |
1386 | ||
a4f7c75f JB |
1387 | private initializeConnectorsOrEvsesFromFile(configuration: ChargingStationConfiguration): void { |
1388 | if (configuration?.connectorsStatus && !configuration?.evsesStatus) { | |
8df5ae48 | 1389 | for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) { |
9bf0ef23 | 1390 | this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus)); |
8df5ae48 | 1391 | } |
a4f7c75f JB |
1392 | } else if (configuration?.evsesStatus && !configuration?.connectorsStatus) { |
1393 | for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) { | |
9bf0ef23 | 1394 | const evseStatus = cloneObject<EvseStatusConfiguration>(evseStatusConfiguration); |
a4f7c75f JB |
1395 | delete evseStatus.connectorsStatus; |
1396 | this.evses.set(evseId, { | |
8df5ae48 | 1397 | ...(evseStatus as EvseStatus), |
a4f7c75f | 1398 | connectors: new Map<number, ConnectorStatus>( |
e1d9a0f4 | 1399 | evseStatusConfiguration.connectorsStatus!.map((connectorStatus, connectorId) => [ |
a4f7c75f JB |
1400 | connectorId, |
1401 | connectorStatus, | |
5edd8ba0 | 1402 | ]), |
a4f7c75f JB |
1403 | ), |
1404 | }); | |
1405 | } | |
1406 | } else if (configuration?.evsesStatus && configuration?.connectorsStatus) { | |
1407 | const errorMsg = `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`; | |
1408 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1409 | throw new BaseError(errorMsg); | |
1410 | } else { | |
1411 | const errorMsg = `No connectors or evses defined in configuration file ${this.configurationFile}`; | |
1412 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1413 | throw new BaseError(errorMsg); | |
1414 | } | |
1415 | } | |
1416 | ||
34eeb1fb | 1417 | private initializeConnectorsOrEvsesFromTemplate(stationTemplate: ChargingStationTemplate) { |
cda5d0fb | 1418 | if (stationTemplate?.Connectors && !stationTemplate?.Evses) { |
34eeb1fb | 1419 | this.initializeConnectorsFromTemplate(stationTemplate); |
cda5d0fb | 1420 | } else if (stationTemplate?.Evses && !stationTemplate?.Connectors) { |
34eeb1fb | 1421 | this.initializeEvsesFromTemplate(stationTemplate); |
cda5d0fb | 1422 | } else if (stationTemplate?.Evses && stationTemplate?.Connectors) { |
ae25f265 JB |
1423 | const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}`; |
1424 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1425 | throw new BaseError(errorMsg); | |
1426 | } else { | |
1427 | const errorMsg = `No connectors or evses defined in template file ${this.templateFile}`; | |
1428 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1429 | throw new BaseError(errorMsg); | |
1430 | } | |
1431 | } | |
1432 | ||
34eeb1fb | 1433 | private initializeConnectorsFromTemplate(stationTemplate: ChargingStationTemplate): void { |
cda5d0fb | 1434 | if (!stationTemplate?.Connectors && this.connectors.size === 0) { |
ded57f02 JB |
1435 | const errorMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`; |
1436 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1437 | throw new BaseError(errorMsg); | |
3d25cc86 | 1438 | } |
e1d9a0f4 | 1439 | if (!stationTemplate?.Connectors?.[0]) { |
3d25cc86 JB |
1440 | logger.warn( |
1441 | `${this.logPrefix()} Charging station information from template ${ | |
1442 | this.templateFile | |
5edd8ba0 | 1443 | } with no connector id 0 configuration`, |
3d25cc86 JB |
1444 | ); |
1445 | } | |
cda5d0fb JB |
1446 | if (stationTemplate?.Connectors) { |
1447 | const { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors } = | |
fba11dc6 | 1448 | checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile); |
d972af76 | 1449 | const connectorsConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
cda5d0fb | 1450 | .update( |
5edd8ba0 | 1451 | `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`, |
cda5d0fb | 1452 | ) |
3d25cc86 JB |
1453 | .digest('hex'); |
1454 | const connectorsConfigChanged = | |
1455 | this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash; | |
1456 | if (this.connectors?.size === 0 || connectorsConfigChanged) { | |
1457 | connectorsConfigChanged && this.connectors.clear(); | |
1458 | this.connectorsConfigurationHash = connectorsConfigHash; | |
269196a8 JB |
1459 | if (templateMaxConnectors > 0) { |
1460 | for (let connectorId = 0; connectorId <= configuredMaxConnectors; connectorId++) { | |
1461 | if ( | |
1462 | connectorId === 0 && | |
1c9de2b9 | 1463 | (!stationTemplate?.Connectors?.[connectorId] || |
cda5d0fb | 1464 | this.getUseConnectorId0(stationTemplate) === false) |
269196a8 JB |
1465 | ) { |
1466 | continue; | |
1467 | } | |
1468 | const templateConnectorId = | |
cda5d0fb | 1469 | connectorId > 0 && stationTemplate?.randomConnectors |
9bf0ef23 | 1470 | ? getRandomInteger(templateMaxAvailableConnectors, 1) |
269196a8 | 1471 | : connectorId; |
cda5d0fb | 1472 | const connectorStatus = stationTemplate?.Connectors[templateConnectorId]; |
fba11dc6 | 1473 | checkStationInfoConnectorStatus( |
ae25f265 | 1474 | templateConnectorId, |
04b1261c JB |
1475 | connectorStatus, |
1476 | this.logPrefix(), | |
5edd8ba0 | 1477 | this.templateFile, |
04b1261c | 1478 | ); |
9bf0ef23 | 1479 | this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus)); |
3d25cc86 | 1480 | } |
fba11dc6 | 1481 | initializeConnectorsMapStatus(this.connectors, this.logPrefix()); |
52952bf8 | 1482 | this.saveConnectorsStatus(); |
ae25f265 JB |
1483 | } else { |
1484 | logger.warn( | |
1485 | `${this.logPrefix()} Charging station information from template ${ | |
1486 | this.templateFile | |
5edd8ba0 | 1487 | } with no connectors configuration defined, cannot create connectors`, |
ae25f265 | 1488 | ); |
3d25cc86 JB |
1489 | } |
1490 | } | |
1491 | } else { | |
1492 | logger.warn( | |
1493 | `${this.logPrefix()} Charging station information from template ${ | |
1494 | this.templateFile | |
5edd8ba0 | 1495 | } with no connectors configuration defined, using already defined connectors`, |
3d25cc86 JB |
1496 | ); |
1497 | } | |
3d25cc86 JB |
1498 | } |
1499 | ||
34eeb1fb | 1500 | private initializeEvsesFromTemplate(stationTemplate: ChargingStationTemplate): void { |
cda5d0fb | 1501 | if (!stationTemplate?.Evses && this.evses.size === 0) { |
ded57f02 JB |
1502 | const errorMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`; |
1503 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1504 | throw new BaseError(errorMsg); | |
2585c6e9 | 1505 | } |
e1d9a0f4 | 1506 | if (!stationTemplate?.Evses?.[0]) { |
2585c6e9 JB |
1507 | logger.warn( |
1508 | `${this.logPrefix()} Charging station information from template ${ | |
1509 | this.templateFile | |
5edd8ba0 | 1510 | } with no evse id 0 configuration`, |
2585c6e9 JB |
1511 | ); |
1512 | } | |
e1d9a0f4 | 1513 | if (!stationTemplate?.Evses?.[0]?.Connectors?.[0]) { |
59a0f26d JB |
1514 | logger.warn( |
1515 | `${this.logPrefix()} Charging station information from template ${ | |
1516 | this.templateFile | |
5edd8ba0 | 1517 | } with evse id 0 with no connector id 0 configuration`, |
59a0f26d JB |
1518 | ); |
1519 | } | |
491dad29 JB |
1520 | if (Object.keys(stationTemplate?.Evses?.[0]?.Connectors as object).length > 1) { |
1521 | logger.warn( | |
1522 | `${this.logPrefix()} Charging station information from template ${ | |
1523 | this.templateFile | |
1524 | } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`, | |
1525 | ); | |
1526 | } | |
cda5d0fb | 1527 | if (stationTemplate?.Evses) { |
d972af76 | 1528 | const evsesConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
ba01a213 | 1529 | .update(JSON.stringify(stationTemplate?.Evses)) |
2585c6e9 JB |
1530 | .digest('hex'); |
1531 | const evsesConfigChanged = | |
1532 | this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash; | |
1533 | if (this.evses?.size === 0 || evsesConfigChanged) { | |
1534 | evsesConfigChanged && this.evses.clear(); | |
1535 | this.evsesConfigurationHash = evsesConfigHash; | |
fba11dc6 | 1536 | const templateMaxEvses = getMaxNumberOfEvses(stationTemplate?.Evses); |
ae25f265 | 1537 | if (templateMaxEvses > 0) { |
eb979012 JB |
1538 | for (const evseKey in stationTemplate.Evses) { |
1539 | const evseId = convertToInt(evseKey); | |
52952bf8 | 1540 | this.evses.set(evseId, { |
fba11dc6 | 1541 | connectors: buildConnectorsMap( |
eb979012 | 1542 | stationTemplate?.Evses[evseKey]?.Connectors, |
ae25f265 | 1543 | this.logPrefix(), |
5edd8ba0 | 1544 | this.templateFile, |
ae25f265 JB |
1545 | ), |
1546 | availability: AvailabilityType.Operative, | |
1547 | }); | |
e1d9a0f4 | 1548 | initializeConnectorsMapStatus(this.evses.get(evseId)!.connectors, this.logPrefix()); |
ae25f265 | 1549 | } |
52952bf8 | 1550 | this.saveEvsesStatus(); |
ae25f265 JB |
1551 | } else { |
1552 | logger.warn( | |
1553 | `${this.logPrefix()} Charging station information from template ${ | |
04b1261c | 1554 | this.templateFile |
5edd8ba0 | 1555 | } with no evses configuration defined, cannot create evses`, |
04b1261c | 1556 | ); |
2585c6e9 JB |
1557 | } |
1558 | } | |
513db108 JB |
1559 | } else { |
1560 | logger.warn( | |
1561 | `${this.logPrefix()} Charging station information from template ${ | |
1562 | this.templateFile | |
5edd8ba0 | 1563 | } with no evses configuration defined, using already defined evses`, |
513db108 | 1564 | ); |
2585c6e9 JB |
1565 | } |
1566 | } | |
1567 | ||
551e477c JB |
1568 | private getConfigurationFromFile(): ChargingStationConfiguration | undefined { |
1569 | let configuration: ChargingStationConfiguration | undefined; | |
9bf0ef23 | 1570 | if (isNotEmptyString(this.configurationFile) && existsSync(this.configurationFile)) { |
073bd098 | 1571 | try { |
57adbebc JB |
1572 | if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { |
1573 | configuration = this.sharedLRUCache.getChargingStationConfiguration( | |
5edd8ba0 | 1574 | this.configurationFileHash, |
57adbebc | 1575 | ); |
7c72977b JB |
1576 | } else { |
1577 | const measureId = `${FileType.ChargingStationConfiguration} read`; | |
1578 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
1579 | configuration = JSON.parse( | |
5edd8ba0 | 1580 | readFileSync(this.configurationFile, 'utf8'), |
7c72977b JB |
1581 | ) as ChargingStationConfiguration; |
1582 | PerformanceStatistics.endMeasure(measureId, beginId); | |
57adbebc | 1583 | this.sharedLRUCache.setChargingStationConfiguration(configuration); |
e1d9a0f4 | 1584 | this.configurationFileHash = configuration.configurationHash!; |
7c72977b | 1585 | } |
073bd098 | 1586 | } catch (error) { |
fa5995d6 | 1587 | handleFileException( |
073bd098 | 1588 | this.configurationFile, |
7164966d JB |
1589 | FileType.ChargingStationConfiguration, |
1590 | error as NodeJS.ErrnoException, | |
5edd8ba0 | 1591 | this.logPrefix(), |
073bd098 JB |
1592 | ); |
1593 | } | |
1594 | } | |
1595 | return configuration; | |
1596 | } | |
1597 | ||
cb60061f | 1598 | private saveAutomaticTransactionGeneratorConfiguration(): void { |
5398cecf | 1599 | if (this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true) { |
5ced7e80 JB |
1600 | this.saveConfiguration(); |
1601 | } | |
ac7f79af JB |
1602 | } |
1603 | ||
52952bf8 | 1604 | private saveConnectorsStatus() { |
7446de3b | 1605 | this.saveConfiguration(); |
52952bf8 JB |
1606 | } |
1607 | ||
1608 | private saveEvsesStatus() { | |
7446de3b | 1609 | this.saveConfiguration(); |
52952bf8 JB |
1610 | } |
1611 | ||
179ed367 | 1612 | private saveConfiguration(): void { |
9bf0ef23 | 1613 | if (isNotEmptyString(this.configurationFile)) { |
2484ac1e | 1614 | try { |
d972af76 JB |
1615 | if (!existsSync(dirname(this.configurationFile))) { |
1616 | mkdirSync(dirname(this.configurationFile), { recursive: true }); | |
073bd098 | 1617 | } |
ae8ceef3 JB |
1618 | let configurationData: ChargingStationConfiguration = this.getConfigurationFromFile() |
1619 | ? cloneObject<ChargingStationConfiguration>(this.getConfigurationFromFile()!) | |
1620 | : {}; | |
5398cecf | 1621 | if (this.stationInfo?.stationInfoPersistentConfiguration === true && this.stationInfo) { |
52952bf8 | 1622 | configurationData.stationInfo = this.stationInfo; |
5ced7e80 JB |
1623 | } else { |
1624 | delete configurationData.stationInfo; | |
52952bf8 | 1625 | } |
5398cecf JB |
1626 | if ( |
1627 | this.stationInfo?.ocppPersistentConfiguration === true && | |
1628 | this.ocppConfiguration?.configurationKey | |
1629 | ) { | |
52952bf8 | 1630 | configurationData.configurationKey = this.ocppConfiguration.configurationKey; |
5ced7e80 JB |
1631 | } else { |
1632 | delete configurationData.configurationKey; | |
52952bf8 | 1633 | } |
179ed367 JB |
1634 | configurationData = merge<ChargingStationConfiguration>( |
1635 | configurationData, | |
5edd8ba0 | 1636 | buildChargingStationAutomaticTransactionGeneratorConfiguration(this), |
179ed367 | 1637 | ); |
5ced7e80 | 1638 | if ( |
5398cecf | 1639 | !this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration || |
5ced7e80 JB |
1640 | !this.getAutomaticTransactionGeneratorConfiguration() |
1641 | ) { | |
1642 | delete configurationData.automaticTransactionGenerator; | |
1643 | } | |
b1bbdae5 | 1644 | if (this.connectors.size > 0) { |
179ed367 | 1645 | configurationData.connectorsStatus = buildConnectorsStatus(this); |
5ced7e80 JB |
1646 | } else { |
1647 | delete configurationData.connectorsStatus; | |
52952bf8 | 1648 | } |
b1bbdae5 | 1649 | if (this.evses.size > 0) { |
179ed367 | 1650 | configurationData.evsesStatus = buildEvsesStatus(this); |
5ced7e80 JB |
1651 | } else { |
1652 | delete configurationData.evsesStatus; | |
52952bf8 | 1653 | } |
7c72977b | 1654 | delete configurationData.configurationHash; |
d972af76 | 1655 | const configurationHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
5ced7e80 JB |
1656 | .update( |
1657 | JSON.stringify({ | |
1658 | stationInfo: configurationData.stationInfo, | |
1659 | configurationKey: configurationData.configurationKey, | |
1660 | automaticTransactionGenerator: configurationData.automaticTransactionGenerator, | |
8ab96efb JB |
1661 | ...(this.connectors.size > 0 && { |
1662 | connectorsStatus: configurationData.connectorsStatus, | |
1663 | }), | |
1664 | ...(this.evses.size > 0 && { evsesStatus: configurationData.evsesStatus }), | |
5edd8ba0 | 1665 | } as ChargingStationConfiguration), |
5ced7e80 | 1666 | ) |
7c72977b JB |
1667 | .digest('hex'); |
1668 | if (this.configurationFileHash !== configurationHash) { | |
0ebf7c2e JB |
1669 | AsyncLock.runExclusive(AsyncLockType.configuration, () => { |
1670 | configurationData.configurationHash = configurationHash; | |
1671 | const measureId = `${FileType.ChargingStationConfiguration} write`; | |
1672 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
1673 | writeFileSync( | |
1674 | this.configurationFile, | |
4ed03b6e | 1675 | JSON.stringify(configurationData, undefined, 2), |
0ebf7c2e JB |
1676 | 'utf8', |
1677 | ); | |
1678 | PerformanceStatistics.endMeasure(measureId, beginId); | |
1679 | this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); | |
1680 | this.sharedLRUCache.setChargingStationConfiguration(configurationData); | |
1681 | this.configurationFileHash = configurationHash; | |
1682 | }).catch((error) => { | |
1683 | handleFileException( | |
1684 | this.configurationFile, | |
1685 | FileType.ChargingStationConfiguration, | |
1686 | error as NodeJS.ErrnoException, | |
1687 | this.logPrefix(), | |
1688 | ); | |
1689 | }); | |
7c72977b JB |
1690 | } else { |
1691 | logger.debug( | |
1692 | `${this.logPrefix()} Not saving unchanged charging station configuration file ${ | |
1693 | this.configurationFile | |
5edd8ba0 | 1694 | }`, |
7c72977b | 1695 | ); |
2484ac1e | 1696 | } |
2484ac1e | 1697 | } catch (error) { |
fa5995d6 | 1698 | handleFileException( |
2484ac1e | 1699 | this.configurationFile, |
7164966d JB |
1700 | FileType.ChargingStationConfiguration, |
1701 | error as NodeJS.ErrnoException, | |
5edd8ba0 | 1702 | this.logPrefix(), |
073bd098 JB |
1703 | ); |
1704 | } | |
2484ac1e JB |
1705 | } else { |
1706 | logger.error( | |
5edd8ba0 | 1707 | `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`, |
2484ac1e | 1708 | ); |
073bd098 JB |
1709 | } |
1710 | } | |
1711 | ||
551e477c JB |
1712 | private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | undefined { |
1713 | return this.getTemplateFromFile()?.Configuration; | |
2484ac1e JB |
1714 | } |
1715 | ||
551e477c | 1716 | private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | undefined { |
60655b26 | 1717 | const configurationKey = this.getConfigurationFromFile()?.configurationKey; |
5398cecf | 1718 | if (this.stationInfo?.ocppPersistentConfiguration === true && configurationKey) { |
60655b26 | 1719 | return { configurationKey }; |
648512ce | 1720 | } |
60655b26 | 1721 | return undefined; |
7dde0b73 JB |
1722 | } |
1723 | ||
551e477c JB |
1724 | private getOcppConfiguration(): ChargingStationOcppConfiguration | undefined { |
1725 | let ocppConfiguration: ChargingStationOcppConfiguration | undefined = | |
72092cfc | 1726 | this.getOcppConfigurationFromFile(); |
2484ac1e JB |
1727 | if (!ocppConfiguration) { |
1728 | ocppConfiguration = this.getOcppConfigurationFromTemplate(); | |
1729 | } | |
1730 | return ocppConfiguration; | |
1731 | } | |
1732 | ||
c0560973 | 1733 | private async onOpen(): Promise<void> { |
56eb297e | 1734 | if (this.isWebSocketConnectionOpened() === true) { |
5144f4d1 | 1735 | logger.info( |
5edd8ba0 | 1736 | `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`, |
5144f4d1 | 1737 | ); |
f23be6aa | 1738 | let registrationRetryCount = 0; |
ed6cfcff | 1739 | if (this.isRegistered() === false) { |
5144f4d1 | 1740 | // Send BootNotification |
5144f4d1 | 1741 | do { |
f7f98c68 | 1742 | this.bootNotificationResponse = await this.ocppRequestService.requestHandler< |
5144f4d1 JB |
1743 | BootNotificationRequest, |
1744 | BootNotificationResponse | |
8bfbc743 JB |
1745 | >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { |
1746 | skipBufferingOnError: true, | |
1747 | }); | |
ed6cfcff | 1748 | if (this.isRegistered() === false) { |
5398cecf | 1749 | this.stationInfo?.registrationMaxRetries !== -1 && ++registrationRetryCount; |
9bf0ef23 | 1750 | await sleep( |
1895299d | 1751 | this?.bootNotificationResponse?.interval |
be4c6702 | 1752 | ? secondsToMilliseconds(this.bootNotificationResponse.interval) |
5edd8ba0 | 1753 | : Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL, |
5144f4d1 JB |
1754 | ); |
1755 | } | |
1756 | } while ( | |
ed6cfcff | 1757 | this.isRegistered() === false && |
5398cecf | 1758 | (registrationRetryCount <= this.stationInfo.registrationMaxRetries! || |
9a77cc07 | 1759 | this.stationInfo?.registrationMaxRetries === -1) |
5144f4d1 JB |
1760 | ); |
1761 | } | |
ed6cfcff | 1762 | if (this.isRegistered() === true) { |
db54d2e0 | 1763 | this.emit(ChargingStationEvents.registered); |
f7c2994d | 1764 | if (this.inAcceptedState() === true) { |
db54d2e0 | 1765 | this.emit(ChargingStationEvents.accepted); |
94bb24d5 | 1766 | await this.startMessageSequence(); |
c0560973 | 1767 | } |
5144f4d1 JB |
1768 | } else { |
1769 | logger.error( | |
f23be6aa | 1770 | `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this |
5398cecf | 1771 | .stationInfo?.registrationMaxRetries})`, |
5144f4d1 | 1772 | ); |
caad9d6b | 1773 | } |
5144f4d1 | 1774 | this.wsConnectionRestarted = false; |
aa428a31 | 1775 | this.autoReconnectRetryCount = 0; |
db54d2e0 | 1776 | this.emit(ChargingStationEvents.updated); |
2e6f5966 | 1777 | } else { |
5144f4d1 | 1778 | logger.warn( |
5edd8ba0 | 1779 | `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`, |
e7aeea18 | 1780 | ); |
2e6f5966 | 1781 | } |
2e6f5966 JB |
1782 | } |
1783 | ||
ef7d8c21 | 1784 | private async onClose(code: number, reason: Buffer): Promise<void> { |
d09085e9 | 1785 | switch (code) { |
6c65a295 JB |
1786 | // Normal close |
1787 | case WebSocketCloseEventStatusCode.CLOSE_NORMAL: | |
c0560973 | 1788 | case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS: |
e7aeea18 | 1789 | logger.info( |
9bf0ef23 | 1790 | `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString( |
5edd8ba0 JB |
1791 | code, |
1792 | )}' and reason '${reason.toString()}'`, | |
e7aeea18 | 1793 | ); |
c0560973 JB |
1794 | this.autoReconnectRetryCount = 0; |
1795 | break; | |
6c65a295 JB |
1796 | // Abnormal close |
1797 | default: | |
e7aeea18 | 1798 | logger.error( |
9bf0ef23 | 1799 | `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString( |
5edd8ba0 JB |
1800 | code, |
1801 | )}' and reason '${reason.toString()}'`, | |
e7aeea18 | 1802 | ); |
56eb297e | 1803 | this.started === true && (await this.reconnect()); |
c0560973 JB |
1804 | break; |
1805 | } | |
db54d2e0 | 1806 | this.emit(ChargingStationEvents.updated); |
2e6f5966 JB |
1807 | } |
1808 | ||
56d09fd7 JB |
1809 | private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined { |
1810 | const cachedRequest = this.requests.get(messageId); | |
1811 | if (Array.isArray(cachedRequest) === true) { | |
1812 | return cachedRequest; | |
1813 | } | |
1814 | throw new OCPPError( | |
1815 | ErrorType.PROTOCOL_ERROR, | |
1816 | `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString( | |
5edd8ba0 | 1817 | messageType, |
56d09fd7 JB |
1818 | )} is not an array`, |
1819 | undefined, | |
5edd8ba0 | 1820 | cachedRequest as JsonType, |
56d09fd7 JB |
1821 | ); |
1822 | } | |
1823 | ||
1824 | private async handleIncomingMessage(request: IncomingRequest): Promise<void> { | |
1825 | const [messageType, messageId, commandName, commandPayload] = request; | |
9a77cc07 | 1826 | if (this.stationInfo?.enableStatistics === true) { |
56d09fd7 JB |
1827 | this.performanceStatistics?.addRequestStatistic(commandName, messageType); |
1828 | } | |
1829 | logger.debug( | |
1830 | `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify( | |
5edd8ba0 JB |
1831 | request, |
1832 | )}`, | |
56d09fd7 JB |
1833 | ); |
1834 | // Process the message | |
1835 | await this.ocppIncomingRequestService.incomingRequestHandler( | |
1836 | this, | |
1837 | messageId, | |
1838 | commandName, | |
5edd8ba0 | 1839 | commandPayload, |
56d09fd7 JB |
1840 | ); |
1841 | } | |
1842 | ||
1843 | private handleResponseMessage(response: Response): void { | |
1844 | const [messageType, messageId, commandPayload] = response; | |
1845 | if (this.requests.has(messageId) === false) { | |
1846 | // Error | |
1847 | throw new OCPPError( | |
1848 | ErrorType.INTERNAL_ERROR, | |
1849 | `Response for unknown message id ${messageId}`, | |
1850 | undefined, | |
5edd8ba0 | 1851 | commandPayload, |
56d09fd7 JB |
1852 | ); |
1853 | } | |
1854 | // Respond | |
1855 | const [responseCallback, , requestCommandName, requestPayload] = this.getCachedRequest( | |
1856 | messageType, | |
5edd8ba0 | 1857 | messageId, |
e1d9a0f4 | 1858 | )!; |
56d09fd7 JB |
1859 | logger.debug( |
1860 | `${this.logPrefix()} << Command '${ | |
1861 | requestCommandName ?? Constants.UNKNOWN_COMMAND | |
5edd8ba0 | 1862 | }' received response payload: ${JSON.stringify(response)}`, |
56d09fd7 JB |
1863 | ); |
1864 | responseCallback(commandPayload, requestPayload); | |
1865 | } | |
1866 | ||
1867 | private handleErrorMessage(errorResponse: ErrorResponse): void { | |
1868 | const [messageType, messageId, errorType, errorMessage, errorDetails] = errorResponse; | |
1869 | if (this.requests.has(messageId) === false) { | |
1870 | // Error | |
1871 | throw new OCPPError( | |
1872 | ErrorType.INTERNAL_ERROR, | |
1873 | `Error response for unknown message id ${messageId}`, | |
1874 | undefined, | |
5edd8ba0 | 1875 | { errorType, errorMessage, errorDetails }, |
56d09fd7 JB |
1876 | ); |
1877 | } | |
e1d9a0f4 | 1878 | const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)!; |
56d09fd7 JB |
1879 | logger.debug( |
1880 | `${this.logPrefix()} << Command '${ | |
1881 | requestCommandName ?? Constants.UNKNOWN_COMMAND | |
5edd8ba0 | 1882 | }' received error response payload: ${JSON.stringify(errorResponse)}`, |
56d09fd7 JB |
1883 | ); |
1884 | errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)); | |
1885 | } | |
1886 | ||
ef7d8c21 | 1887 | private async onMessage(data: RawData): Promise<void> { |
e1d9a0f4 JB |
1888 | let request: IncomingRequest | Response | ErrorResponse | undefined; |
1889 | let messageType: number | undefined; | |
ded57f02 | 1890 | let errorMsg: string; |
c0560973 | 1891 | try { |
e1d9a0f4 | 1892 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
56d09fd7 | 1893 | request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse; |
53e5fd67 | 1894 | if (Array.isArray(request) === true) { |
56d09fd7 | 1895 | [messageType] = request; |
b3ec7bc1 JB |
1896 | // Check the type of message |
1897 | switch (messageType) { | |
1898 | // Incoming Message | |
1899 | case MessageType.CALL_MESSAGE: | |
56d09fd7 | 1900 | await this.handleIncomingMessage(request as IncomingRequest); |
b3ec7bc1 | 1901 | break; |
56d09fd7 | 1902 | // Response Message |
b3ec7bc1 | 1903 | case MessageType.CALL_RESULT_MESSAGE: |
56d09fd7 | 1904 | this.handleResponseMessage(request as Response); |
a2d1c0f1 JB |
1905 | break; |
1906 | // Error Message | |
1907 | case MessageType.CALL_ERROR_MESSAGE: | |
56d09fd7 | 1908 | this.handleErrorMessage(request as ErrorResponse); |
b3ec7bc1 | 1909 | break; |
56d09fd7 | 1910 | // Unknown Message |
b3ec7bc1 JB |
1911 | default: |
1912 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
ded57f02 JB |
1913 | errorMsg = `Wrong message type ${messageType}`; |
1914 | logger.error(`${this.logPrefix()} ${errorMsg}`); | |
1915 | throw new OCPPError(ErrorType.PROTOCOL_ERROR, errorMsg); | |
b3ec7bc1 | 1916 | } |
db54d2e0 | 1917 | this.emit(ChargingStationEvents.updated); |
47e22477 | 1918 | } else { |
e1d9a0f4 JB |
1919 | throw new OCPPError( |
1920 | ErrorType.PROTOCOL_ERROR, | |
1921 | 'Incoming message is not an array', | |
1922 | undefined, | |
1923 | { | |
1924 | request, | |
1925 | }, | |
1926 | ); | |
47e22477 | 1927 | } |
c0560973 | 1928 | } catch (error) { |
e1d9a0f4 JB |
1929 | let commandName: IncomingRequestCommand | undefined; |
1930 | let requestCommandName: RequestCommand | IncomingRequestCommand | undefined; | |
56d09fd7 | 1931 | let errorCallback: ErrorCallback; |
e1d9a0f4 | 1932 | const [, messageId] = request!; |
13701f69 JB |
1933 | switch (messageType) { |
1934 | case MessageType.CALL_MESSAGE: | |
56d09fd7 | 1935 | [, , commandName] = request as IncomingRequest; |
13701f69 | 1936 | // Send error |
56d09fd7 | 1937 | await this.ocppRequestService.sendError(this, messageId, error as OCPPError, commandName); |
13701f69 JB |
1938 | break; |
1939 | case MessageType.CALL_RESULT_MESSAGE: | |
1940 | case MessageType.CALL_ERROR_MESSAGE: | |
56d09fd7 | 1941 | if (this.requests.has(messageId) === true) { |
e1d9a0f4 | 1942 | [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)!; |
13701f69 JB |
1943 | // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op) |
1944 | errorCallback(error as OCPPError, false); | |
1945 | } else { | |
1946 | // Remove the request from the cache in case of error at response handling | |
1947 | this.requests.delete(messageId); | |
1948 | } | |
de4cb8b6 | 1949 | break; |
ba7965c4 | 1950 | } |
56d09fd7 JB |
1951 | if (error instanceof OCPPError === false) { |
1952 | logger.warn( | |
1953 | `${this.logPrefix()} Error thrown at incoming OCPP command '${ | |
1954 | commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND | |
e1d9a0f4 | 1955 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
56d09fd7 | 1956 | }' message '${data.toString()}' handling is not an OCPPError:`, |
5edd8ba0 | 1957 | error, |
56d09fd7 JB |
1958 | ); |
1959 | } | |
1960 | logger.error( | |
1961 | `${this.logPrefix()} Incoming OCPP command '${ | |
1962 | commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND | |
e1d9a0f4 | 1963 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
56d09fd7 JB |
1964 | }' message '${data.toString()}'${ |
1965 | messageType !== MessageType.CALL_MESSAGE | |
1966 | ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'` | |
1967 | : '' | |
1968 | } processing error:`, | |
5edd8ba0 | 1969 | error, |
56d09fd7 | 1970 | ); |
c0560973 | 1971 | } |
2328be1e JB |
1972 | } |
1973 | ||
c0560973 | 1974 | private onPing(): void { |
44eb6026 | 1975 | logger.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`); |
c0560973 JB |
1976 | } |
1977 | ||
1978 | private onPong(): void { | |
44eb6026 | 1979 | logger.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`); |
c0560973 JB |
1980 | } |
1981 | ||
9534e74e | 1982 | private onError(error: WSError): void { |
bcc9c3c0 | 1983 | this.closeWSConnection(); |
44eb6026 | 1984 | logger.error(`${this.logPrefix()} WebSocket error:`, error); |
c0560973 JB |
1985 | } |
1986 | ||
18bf8274 | 1987 | private getEnergyActiveImportRegister(connectorStatus: ConnectorStatus, rounded = false): number { |
5398cecf | 1988 | if (this.stationInfo?.meteringPerTransaction === true) { |
07989fad | 1989 | return ( |
18bf8274 | 1990 | (rounded === true |
e1d9a0f4 | 1991 | ? Math.round(connectorStatus.transactionEnergyActiveImportRegisterValue!) |
07989fad JB |
1992 | : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0 |
1993 | ); | |
1994 | } | |
1995 | return ( | |
18bf8274 | 1996 | (rounded === true |
e1d9a0f4 | 1997 | ? Math.round(connectorStatus.energyActiveImportRegisterValue!) |
07989fad JB |
1998 | : connectorStatus?.energyActiveImportRegisterValue) ?? 0 |
1999 | ); | |
2000 | } | |
2001 | ||
cda5d0fb JB |
2002 | private getUseConnectorId0(stationTemplate?: ChargingStationTemplate): boolean { |
2003 | return stationTemplate?.useConnectorId0 ?? true; | |
8bce55bf JB |
2004 | } |
2005 | ||
9ff486f4 | 2006 | private async stopRunningTransactions(reason?: StopTransactionReason): Promise<void> { |
28e78158 | 2007 | if (this.hasEvses) { |
3fa7f799 JB |
2008 | for (const [evseId, evseStatus] of this.evses) { |
2009 | if (evseId === 0) { | |
2010 | continue; | |
2011 | } | |
28e78158 JB |
2012 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { |
2013 | if (connectorStatus.transactionStarted === true) { | |
2014 | await this.stopTransactionOnConnector(connectorId, reason); | |
2015 | } | |
2016 | } | |
2017 | } | |
2018 | } else { | |
2019 | for (const connectorId of this.connectors.keys()) { | |
2020 | if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { | |
2021 | await this.stopTransactionOnConnector(connectorId, reason); | |
2022 | } | |
60ddad53 JB |
2023 | } |
2024 | } | |
2025 | } | |
2026 | ||
1f761b9a | 2027 | // 0 for disabling |
c72f6634 | 2028 | private getConnectionTimeout(): number { |
f2d5e3d9 | 2029 | if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) { |
e7aeea18 | 2030 | return ( |
f2d5e3d9 JB |
2031 | parseInt(getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)!.value!) ?? |
2032 | Constants.DEFAULT_CONNECTION_TIMEOUT | |
e7aeea18 | 2033 | ); |
291cb255 | 2034 | } |
291cb255 | 2035 | return Constants.DEFAULT_CONNECTION_TIMEOUT; |
3574dfd3 JB |
2036 | } |
2037 | ||
c0560973 | 2038 | private getPowerDivider(): number { |
b1bbdae5 | 2039 | let powerDivider = this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors(); |
be1e907c | 2040 | if (this.stationInfo?.powerSharedByConnectors === true) { |
c0560973 | 2041 | powerDivider = this.getNumberOfRunningTransactions(); |
6ecb15e4 JB |
2042 | } |
2043 | return powerDivider; | |
2044 | } | |
2045 | ||
fa7bccf4 JB |
2046 | private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined { |
2047 | const maximumPower = this.getMaximumPower(stationInfo); | |
2048 | switch (this.getCurrentOutType(stationInfo)) { | |
cc6e8ab5 JB |
2049 | case CurrentType.AC: |
2050 | return ACElectricUtils.amperagePerPhaseFromPower( | |
fa7bccf4 | 2051 | this.getNumberOfPhases(stationInfo), |
b1bbdae5 | 2052 | maximumPower / (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()), |
5edd8ba0 | 2053 | this.getVoltageOut(stationInfo), |
cc6e8ab5 JB |
2054 | ); |
2055 | case CurrentType.DC: | |
fa7bccf4 | 2056 | return DCElectricUtils.amperage(maximumPower, this.getVoltageOut(stationInfo)); |
cc6e8ab5 JB |
2057 | } |
2058 | } | |
2059 | ||
5398cecf JB |
2060 | private getMaximumPower(stationInfo?: ChargingStationInfo): number { |
2061 | return (stationInfo ?? this.stationInfo).maximumPower!; | |
2062 | } | |
2063 | ||
2064 | private getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType { | |
73de8f02 | 2065 | return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC; |
5398cecf JB |
2066 | } |
2067 | ||
2068 | private getVoltageOut(stationInfo?: ChargingStationInfo): number { | |
2069 | const defaultVoltageOut = getDefaultVoltageOut( | |
2070 | this.getCurrentOutType(stationInfo), | |
2071 | this.logPrefix(), | |
2072 | this.templateFile, | |
2073 | ); | |
2074 | return (stationInfo ?? this.stationInfo).voltageOut ?? defaultVoltageOut; | |
2075 | } | |
2076 | ||
cc6e8ab5 JB |
2077 | private getAmperageLimitation(): number | undefined { |
2078 | if ( | |
9bf0ef23 | 2079 | isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && |
f2d5e3d9 | 2080 | getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!) |
cc6e8ab5 JB |
2081 | ) { |
2082 | return ( | |
9bf0ef23 | 2083 | convertToInt( |
f2d5e3d9 | 2084 | getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)?.value, |
fba11dc6 | 2085 | ) / getAmperageLimitationUnitDivider(this.stationInfo) |
cc6e8ab5 JB |
2086 | ); |
2087 | } | |
2088 | } | |
2089 | ||
c0560973 | 2090 | private async startMessageSequence(): Promise<void> { |
b7f9e41d | 2091 | if (this.stationInfo?.autoRegister === true) { |
f7f98c68 | 2092 | await this.ocppRequestService.requestHandler< |
ef6fa3fb JB |
2093 | BootNotificationRequest, |
2094 | BootNotificationResponse | |
8bfbc743 JB |
2095 | >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { |
2096 | skipBufferingOnError: true, | |
2097 | }); | |
6114e6f1 | 2098 | } |
136c90ba | 2099 | // Start WebSocket ping |
c0560973 | 2100 | this.startWebSocketPing(); |
5ad8570f | 2101 | // Start heartbeat |
c0560973 | 2102 | this.startHeartbeat(); |
0a60c33c | 2103 | // Initialize connectors status |
c3b83130 JB |
2104 | if (this.hasEvses) { |
2105 | for (const [evseId, evseStatus] of this.evses) { | |
4334db72 JB |
2106 | if (evseId > 0) { |
2107 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
fba11dc6 | 2108 | const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus); |
4334db72 JB |
2109 | await OCPPServiceUtils.sendAndSetConnectorStatus( |
2110 | this, | |
2111 | connectorId, | |
12f26d4a | 2112 | connectorBootStatus, |
5edd8ba0 | 2113 | evseId, |
4334db72 JB |
2114 | ); |
2115 | } | |
c3b83130 | 2116 | } |
4334db72 JB |
2117 | } |
2118 | } else { | |
2119 | for (const connectorId of this.connectors.keys()) { | |
2120 | if (connectorId > 0) { | |
fba11dc6 | 2121 | const connectorBootStatus = getBootConnectorStatus( |
c3b83130 JB |
2122 | this, |
2123 | connectorId, | |
e1d9a0f4 | 2124 | this.getConnectorStatus(connectorId)!, |
c3b83130 JB |
2125 | ); |
2126 | await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorBootStatus); | |
2127 | } | |
2128 | } | |
5ad8570f | 2129 | } |
c9a4f9ea JB |
2130 | if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) { |
2131 | await this.ocppRequestService.requestHandler< | |
2132 | FirmwareStatusNotificationRequest, | |
2133 | FirmwareStatusNotificationResponse | |
2134 | >(this, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { | |
2135 | status: FirmwareStatus.Installed, | |
2136 | }); | |
2137 | this.stationInfo.firmwareStatus = FirmwareStatus.Installed; | |
c9a4f9ea | 2138 | } |
3637ca2c | 2139 | |
0a60c33c | 2140 | // Start the ATG |
61854f7c | 2141 | if (this.getAutomaticTransactionGeneratorConfiguration().enable === true) { |
4f69be04 | 2142 | this.startAutomaticTransactionGenerator(); |
fa7bccf4 | 2143 | } |
aa428a31 | 2144 | this.wsConnectionRestarted === true && this.flushMessageBuffer(); |
fa7bccf4 JB |
2145 | } |
2146 | ||
e7aeea18 | 2147 | private async stopMessageSequence( |
9ff486f4 | 2148 | reason?: StopTransactionReason, |
5398cecf | 2149 | stopTransactions = this.stationInfo?.stopTransactionsOnStopped, |
e7aeea18 | 2150 | ): Promise<void> { |
136c90ba | 2151 | // Stop WebSocket ping |
c0560973 | 2152 | this.stopWebSocketPing(); |
79411696 | 2153 | // Stop heartbeat |
c0560973 | 2154 | this.stopHeartbeat(); |
fa7bccf4 | 2155 | // Stop ongoing transactions |
9ff486f4 JB |
2156 | stopTransactions && (await this.stopRunningTransactions(reason)); |
2157 | // Stop the ATG | |
b20eb107 | 2158 | if (this.automaticTransactionGenerator?.started === true) { |
9ff486f4 | 2159 | this.stopAutomaticTransactionGenerator(); |
79411696 | 2160 | } |
039211f9 JB |
2161 | if (this.hasEvses) { |
2162 | for (const [evseId, evseStatus] of this.evses) { | |
2163 | if (evseId > 0) { | |
2164 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
2165 | await this.ocppRequestService.requestHandler< | |
2166 | StatusNotificationRequest, | |
2167 | StatusNotificationResponse | |
2168 | >( | |
2169 | this, | |
2170 | RequestCommand.STATUS_NOTIFICATION, | |
2171 | OCPPServiceUtils.buildStatusNotificationRequest( | |
2172 | this, | |
2173 | connectorId, | |
12f26d4a | 2174 | ConnectorStatusEnum.Unavailable, |
5edd8ba0 JB |
2175 | evseId, |
2176 | ), | |
039211f9 JB |
2177 | ); |
2178 | delete connectorStatus?.status; | |
2179 | } | |
2180 | } | |
2181 | } | |
2182 | } else { | |
2183 | for (const connectorId of this.connectors.keys()) { | |
2184 | if (connectorId > 0) { | |
2185 | await this.ocppRequestService.requestHandler< | |
2186 | StatusNotificationRequest, | |
2187 | StatusNotificationResponse | |
2188 | >( | |
6e939d9e | 2189 | this, |
039211f9 JB |
2190 | RequestCommand.STATUS_NOTIFICATION, |
2191 | OCPPServiceUtils.buildStatusNotificationRequest( | |
2192 | this, | |
2193 | connectorId, | |
5edd8ba0 JB |
2194 | ConnectorStatusEnum.Unavailable, |
2195 | ), | |
039211f9 JB |
2196 | ); |
2197 | delete this.getConnectorStatus(connectorId)?.status; | |
2198 | } | |
45c0ae82 JB |
2199 | } |
2200 | } | |
79411696 JB |
2201 | } |
2202 | ||
c0560973 | 2203 | private startWebSocketPing(): void { |
f2d5e3d9 | 2204 | const webSocketPingInterval: number = getConfigurationKey( |
17ac262c | 2205 | this, |
5edd8ba0 | 2206 | StandardParametersKey.WebSocketPingInterval, |
e7aeea18 | 2207 | ) |
f2d5e3d9 | 2208 | ? convertToInt(getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value) |
9cd3dfb0 | 2209 | : 0; |
ad2f27c3 JB |
2210 | if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) { |
2211 | this.webSocketPingSetInterval = setInterval(() => { | |
56eb297e | 2212 | if (this.isWebSocketConnectionOpened() === true) { |
72092cfc | 2213 | this.wsConnection?.ping(); |
136c90ba | 2214 | } |
be4c6702 | 2215 | }, secondsToMilliseconds(webSocketPingInterval)); |
e7aeea18 | 2216 | logger.info( |
9bf0ef23 | 2217 | `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds( |
5edd8ba0 JB |
2218 | webSocketPingInterval, |
2219 | )}`, | |
e7aeea18 | 2220 | ); |
ad2f27c3 | 2221 | } else if (this.webSocketPingSetInterval) { |
e7aeea18 | 2222 | logger.info( |
9bf0ef23 | 2223 | `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds( |
5edd8ba0 JB |
2224 | webSocketPingInterval, |
2225 | )}`, | |
e7aeea18 | 2226 | ); |
136c90ba | 2227 | } else { |
e7aeea18 | 2228 | logger.error( |
5edd8ba0 | 2229 | `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`, |
e7aeea18 | 2230 | ); |
136c90ba JB |
2231 | } |
2232 | } | |
2233 | ||
c0560973 | 2234 | private stopWebSocketPing(): void { |
ad2f27c3 JB |
2235 | if (this.webSocketPingSetInterval) { |
2236 | clearInterval(this.webSocketPingSetInterval); | |
dfe81c8f | 2237 | delete this.webSocketPingSetInterval; |
136c90ba JB |
2238 | } |
2239 | } | |
2240 | ||
1f5df42a | 2241 | private getConfiguredSupervisionUrl(): URL { |
d5c3df49 | 2242 | let configuredSupervisionUrl: string; |
72092cfc | 2243 | const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls(); |
9bf0ef23 | 2244 | if (isNotEmptyArray(supervisionUrls)) { |
269de583 | 2245 | let configuredSupervisionUrlIndex: number; |
2dcfe98e | 2246 | switch (Configuration.getSupervisionUrlDistribution()) { |
2dcfe98e | 2247 | case SupervisionUrlDistribution.RANDOM: |
e1d9a0f4 JB |
2248 | configuredSupervisionUrlIndex = Math.floor( |
2249 | secureRandom() * (supervisionUrls as string[]).length, | |
2250 | ); | |
2dcfe98e | 2251 | break; |
a52a6446 | 2252 | case SupervisionUrlDistribution.ROUND_ROBIN: |
c72f6634 | 2253 | case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY: |
2dcfe98e | 2254 | default: |
a52a6446 | 2255 | Object.values(SupervisionUrlDistribution).includes( |
e1d9a0f4 | 2256 | Configuration.getSupervisionUrlDistribution()!, |
a52a6446 JB |
2257 | ) === false && |
2258 | logger.error( | |
e1d9a0f4 | 2259 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
a52a6446 JB |
2260 | `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${ |
2261 | SupervisionUrlDistribution.CHARGING_STATION_AFFINITY | |
5edd8ba0 | 2262 | }`, |
a52a6446 | 2263 | ); |
e1d9a0f4 | 2264 | configuredSupervisionUrlIndex = (this.index - 1) % (supervisionUrls as string[]).length; |
2dcfe98e | 2265 | break; |
c0560973 | 2266 | } |
e1d9a0f4 | 2267 | configuredSupervisionUrl = (supervisionUrls as string[])[configuredSupervisionUrlIndex]; |
d5c3df49 JB |
2268 | } else { |
2269 | configuredSupervisionUrl = supervisionUrls as string; | |
2270 | } | |
9bf0ef23 | 2271 | if (isNotEmptyString(configuredSupervisionUrl)) { |
d5c3df49 | 2272 | return new URL(configuredSupervisionUrl); |
c0560973 | 2273 | } |
49c508b0 | 2274 | const errorMsg = 'No supervision url(s) configured'; |
7f77d16f JB |
2275 | logger.error(`${this.logPrefix()} ${errorMsg}`); |
2276 | throw new BaseError(`${errorMsg}`); | |
136c90ba JB |
2277 | } |
2278 | ||
c0560973 | 2279 | private stopHeartbeat(): void { |
ad2f27c3 JB |
2280 | if (this.heartbeatSetInterval) { |
2281 | clearInterval(this.heartbeatSetInterval); | |
dfe81c8f | 2282 | delete this.heartbeatSetInterval; |
7dde0b73 | 2283 | } |
5ad8570f JB |
2284 | } |
2285 | ||
55516218 | 2286 | private terminateWSConnection(): void { |
56eb297e | 2287 | if (this.isWebSocketConnectionOpened() === true) { |
72092cfc | 2288 | this.wsConnection?.terminate(); |
55516218 JB |
2289 | this.wsConnection = null; |
2290 | } | |
2291 | } | |
2292 | ||
aa428a31 | 2293 | private async reconnect(): Promise<void> { |
7874b0b1 JB |
2294 | // Stop WebSocket ping |
2295 | this.stopWebSocketPing(); | |
136c90ba | 2296 | // Stop heartbeat |
c0560973 | 2297 | this.stopHeartbeat(); |
5ad8570f | 2298 | // Stop the ATG if needed |
ac7f79af | 2299 | if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure === true) { |
9ff486f4 | 2300 | this.stopAutomaticTransactionGenerator(); |
ad2f27c3 | 2301 | } |
e7aeea18 | 2302 | if ( |
5398cecf JB |
2303 | this.autoReconnectRetryCount < this.stationInfo.autoReconnectMaxRetries! || |
2304 | this.stationInfo?.autoReconnectMaxRetries === -1 | |
e7aeea18 | 2305 | ) { |
1fe0632a | 2306 | ++this.autoReconnectRetryCount; |
5398cecf JB |
2307 | const reconnectDelay = |
2308 | this.stationInfo?.reconnectExponentialDelay === true | |
2309 | ? exponentialDelay(this.autoReconnectRetryCount) | |
2310 | : secondsToMilliseconds(this.getConnectionTimeout()); | |
1e080116 JB |
2311 | const reconnectDelayWithdraw = 1000; |
2312 | const reconnectTimeout = | |
2313 | reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0 | |
2314 | ? reconnectDelay - reconnectDelayWithdraw | |
2315 | : 0; | |
e7aeea18 | 2316 | logger.error( |
9bf0ef23 | 2317 | `${this.logPrefix()} WebSocket connection retry in ${roundTo( |
e7aeea18 | 2318 | reconnectDelay, |
5edd8ba0 JB |
2319 | 2, |
2320 | )}ms, timeout ${reconnectTimeout}ms`, | |
e7aeea18 | 2321 | ); |
9bf0ef23 | 2322 | await sleep(reconnectDelay); |
e7aeea18 | 2323 | logger.error( |
5edd8ba0 | 2324 | `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`, |
e7aeea18 JB |
2325 | ); |
2326 | this.openWSConnection( | |
59b6ed8d | 2327 | { |
59b6ed8d JB |
2328 | handshakeTimeout: reconnectTimeout, |
2329 | }, | |
5edd8ba0 | 2330 | { closeOpened: true }, |
e7aeea18 | 2331 | ); |
265e4266 | 2332 | this.wsConnectionRestarted = true; |
5398cecf | 2333 | } else if (this.stationInfo?.autoReconnectMaxRetries !== -1) { |
e7aeea18 | 2334 | logger.error( |
d56ea27c | 2335 | `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${ |
e7aeea18 | 2336 | this.autoReconnectRetryCount |
5398cecf | 2337 | }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`, |
e7aeea18 | 2338 | ); |
5ad8570f JB |
2339 | } |
2340 | } | |
7dde0b73 | 2341 | } |