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