1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { createHash
, randomInt
} from
'node:crypto'
4 import { EventEmitter
} from
'node:events'
5 import { existsSync
, type FSWatcher
, mkdirSync
, readFileSync
, rmSync
, writeFileSync
} from
'node:fs'
6 import { dirname
, join
} from
'node:path'
7 import { URL
} from
'node:url'
8 import { parentPort
} from
'node:worker_threads'
10 import { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns'
11 import { mergeDeepRight
, once
} from
'rambda'
12 import { type RawData
, WebSocket
} from
'ws'
14 import { BaseError
, OCPPError
} from
'../exception/index.js'
15 import { PerformanceStatistics
} from
'../performance/index.js'
17 type AutomaticTransactionGeneratorConfiguration
,
19 type BootNotificationRequest
,
20 type BootNotificationResponse
,
22 type ChargingStationConfiguration
,
23 ChargingStationEvents
,
24 type ChargingStationInfo
,
25 type ChargingStationOcppConfiguration
,
26 type ChargingStationOptions
,
27 type ChargingStationTemplate
,
35 type EvseStatusConfiguration
,
38 type FirmwareStatusNotificationRequest
,
39 type FirmwareStatusNotificationResponse
,
40 type HeartbeatRequest
,
41 type HeartbeatResponse
,
43 type IncomingRequestCommand
,
46 type MeterValuesRequest
,
47 type MeterValuesResponse
,
51 RegistrationStatusEnumType
,
55 ReservationTerminationReason
,
57 StandardParametersKey
,
59 type StopTransactionReason
,
60 type StopTransactionRequest
,
61 type StopTransactionResponse
,
62 SupervisionUrlDistribution
,
63 SupportedFeatureProfiles
,
65 WebSocketCloseEventStatusCode
,
68 } from
'../types/index.js'
74 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
75 buildConnectorsStatus
,
89 formatDurationMilliSeconds
,
90 formatDurationSeconds
,
91 getWebSocketCloseEventStatusString
,
102 } from
'../utils/index.js'
103 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator.js'
104 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel.js'
107 deleteConfigurationKey
,
109 setConfigurationKeyValue
110 } from
'./ConfigurationKeyUtils.js'
114 checkChargingStation
,
116 checkConnectorsConfiguration
,
117 checkStationInfoConnectorStatus
,
119 createBootNotificationRequest
,
121 getAmperageLimitationUnitDivider
,
122 getBootConnectorStatus
,
123 getChargingStationConnectorChargingProfilesPowerLimit
,
124 getChargingStationId
,
125 getDefaultVoltageOut
,
129 getNumberOfReservableConnectors
,
130 getPhaseRotationValue
,
132 hasReservationExpired
,
133 initializeConnectorsMapStatus
,
134 propagateSerialNumber
,
135 setChargingStationOptions
,
136 stationTemplateToStationInfo
,
137 warnTemplateKeysDeprecation
138 } from
'./Helpers.js'
139 import { IdTagsCache
} from
'./IdTagsCache.js'
142 buildTransactionEndMeterValue
,
143 getMessageTypeString
,
144 OCPP16IncomingRequestService
,
145 OCPP16RequestService
,
146 OCPP16ResponseService
,
147 OCPP20IncomingRequestService
,
148 OCPP20RequestService
,
149 OCPP20ResponseService
,
150 type OCPPIncomingRequestService
,
151 type OCPPRequestService
,
152 sendAndSetConnectorStatus
153 } from
'./ocpp/index.js'
154 import { SharedLRUCache
} from
'./SharedLRUCache.js'
156 export class ChargingStation
extends EventEmitter
{
157 public readonly index
: number
158 public readonly templateFile
: string
159 public stationInfo
?: ChargingStationInfo
160 public started
: boolean
161 public starting
: boolean
162 public idTagsCache
: IdTagsCache
163 public automaticTransactionGenerator
?: AutomaticTransactionGenerator
164 public ocppConfiguration
?: ChargingStationOcppConfiguration
165 public wsConnection
: WebSocket
| null
166 public readonly connectors
: Map
<number, ConnectorStatus
>
167 public readonly evses
: Map
<number, EvseStatus
>
168 public readonly requests
: Map
<string, CachedRequest
>
169 public performanceStatistics
?: PerformanceStatistics
170 public heartbeatSetInterval
?: NodeJS
.Timeout
171 public ocppRequestService
!: OCPPRequestService
172 public bootNotificationRequest
?: BootNotificationRequest
173 public bootNotificationResponse
?: BootNotificationResponse
174 public powerDivider
?: number
175 private stopping
: boolean
176 private configurationFile
!: string
177 private configurationFileHash
!: string
178 private connectorsConfigurationHash
!: string
179 private evsesConfigurationHash
!: string
180 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
181 private ocppIncomingRequestService
!: OCPPIncomingRequestService
182 private readonly messageBuffer
: Set
<string>
183 private configuredSupervisionUrl
!: URL
184 private wsConnectionRetried
: boolean
185 private wsConnectionRetryCount
: number
186 private templateFileWatcher
?: FSWatcher
187 private templateFileHash
!: string
188 private readonly sharedLRUCache
: SharedLRUCache
189 private wsPingSetInterval
?: NodeJS
.Timeout
190 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
191 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
193 constructor (index
: number, templateFile
: string, options
?: ChargingStationOptions
) {
196 this.starting
= false
197 this.stopping
= false
198 this.wsConnection
= null
199 this.wsConnectionRetried
= false
200 this.wsConnectionRetryCount
= 0
202 this.templateFile
= templateFile
203 this.connectors
= new Map
<number, ConnectorStatus
>()
204 this.evses
= new Map
<number, EvseStatus
>()
205 this.requests
= new Map
<string, CachedRequest
>()
206 this.messageBuffer
= new Set
<string>()
207 this.sharedLRUCache
= SharedLRUCache
.getInstance()
208 this.idTagsCache
= IdTagsCache
.getInstance()
209 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this)
211 this.on(ChargingStationEvents
.added
, () => {
212 parentPort
?.postMessage(buildAddedMessage(this))
214 this.on(ChargingStationEvents
.deleted
, () => {
215 parentPort
?.postMessage(buildDeletedMessage(this))
217 this.on(ChargingStationEvents
.started
, () => {
218 parentPort
?.postMessage(buildStartedMessage(this))
220 this.on(ChargingStationEvents
.stopped
, () => {
221 parentPort
?.postMessage(buildStoppedMessage(this))
223 this.on(ChargingStationEvents
.updated
, () => {
224 parentPort
?.postMessage(buildUpdatedMessage(this))
226 this.on(ChargingStationEvents
.accepted
, () => {
227 this.startMessageSequence(
228 this.wsConnectionRetried
230 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
231 ).catch((error
: unknown
) => {
232 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
234 this.wsConnectionRetried
= false
236 this.on(ChargingStationEvents
.rejected
, () => {
237 this.wsConnectionRetried
= false
239 this.on(ChargingStationEvents
.disconnected
, () => {
241 this.internalStopMessageSequence()
244 `${this.logPrefix()} Error while stopping the internal message sequence:`,
250 this.initialize(options
)
254 if (this.stationInfo
?.autoStart
=== true) {
259 public get
hasEvses (): boolean {
260 return this.connectors
.size
=== 0 && this.evses
.size
> 0
263 public get
wsConnectionUrl (): URL
{
266 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
267 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
268 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
269 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
270 : this.configuredSupervisionUrl.href
271 }/${this.stationInfo?.chargingStationId}`
275 public logPrefix
= (): string => {
277 this instanceof ChargingStation
&&
278 this.stationInfo
!= null &&
279 isNotEmptyString(this.stationInfo
.chargingStationId
)
281 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
283 let stationTemplate
: ChargingStationTemplate
| undefined
285 stationTemplate
= JSON
.parse(
286 readFileSync(this.templateFile
, 'utf8')
287 ) as ChargingStationTemplate
291 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
294 public hasIdTags (): boolean {
295 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
296 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
299 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
300 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
301 const localStationInfo
= stationInfo
?? this.stationInfo
!
302 switch (this.getCurrentOutType(stationInfo
)) {
304 return localStationInfo
.numberOfPhases
?? 3
310 public isWebSocketConnectionOpened (): boolean {
311 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
314 public inUnknownState (): boolean {
315 return this.bootNotificationResponse
?.status == null
318 public inPendingState (): boolean {
319 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
322 public inAcceptedState (): boolean {
323 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
326 public inRejectedState (): boolean {
327 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
330 public isRegistered (): boolean {
331 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
334 public isChargingStationAvailable (): boolean {
335 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
338 public hasConnector (connectorId
: number): boolean {
340 for (const evseStatus
of this.evses
.values()) {
341 if (evseStatus
.connectors
.has(connectorId
)) {
347 return this.connectors
.has(connectorId
)
350 public isConnectorAvailable (connectorId
: number): boolean {
353 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
357 public getNumberOfConnectors (): number {
359 let numberOfConnectors
= 0
360 for (const [evseId
, evseStatus
] of this.evses
) {
362 numberOfConnectors
+= evseStatus
.connectors
.size
365 return numberOfConnectors
367 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
370 public getNumberOfEvses (): number {
371 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
374 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
376 for (const evseStatus
of this.evses
.values()) {
377 if (evseStatus
.connectors
.has(connectorId
)) {
378 return evseStatus
.connectors
.get(connectorId
)
383 return this.connectors
.get(connectorId
)
386 public getConnectorMaximumAvailablePower (connectorId
: number): number {
387 let connectorAmperageLimitationPowerLimit
: number | undefined
388 const amperageLimitation
= this.getAmperageLimitation()
390 amperageLimitation
!= null &&
391 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
392 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
394 connectorAmperageLimitationPowerLimit
=
395 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
396 ? ACElectricUtils
.powerTotal(
397 this.getNumberOfPhases(),
398 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
399 this.stationInfo
.voltageOut
!,
401 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
403 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
404 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
405 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
408 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
409 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
410 const connectorChargingProfilesPowerLimit
=
411 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
413 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
414 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
415 isNaN(connectorAmperageLimitationPowerLimit
!)
417 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
418 connectorAmperageLimitationPowerLimit
!,
419 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
420 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
424 public getTransactionIdTag (transactionId
: number): string | undefined {
426 for (const evseStatus
of this.evses
.values()) {
427 for (const connectorStatus
of evseStatus
.connectors
.values()) {
428 if (connectorStatus
.transactionId
=== transactionId
) {
429 return connectorStatus
.transactionIdTag
434 for (const connectorId
of this.connectors
.keys()) {
435 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
436 return this.getConnectorStatus(connectorId
)?.transactionIdTag
442 public getNumberOfRunningTransactions (): number {
443 let numberOfRunningTransactions
= 0
445 for (const [evseId
, evseStatus
] of this.evses
) {
449 for (const connectorStatus
of evseStatus
.connectors
.values()) {
450 if (connectorStatus
.transactionStarted
=== true) {
451 ++numberOfRunningTransactions
456 for (const connectorId
of this.connectors
.keys()) {
457 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
458 ++numberOfRunningTransactions
462 return numberOfRunningTransactions
465 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
466 if (transactionId
== null) {
468 } else if (this.hasEvses
) {
469 for (const evseStatus
of this.evses
.values()) {
470 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
471 if (connectorStatus
.transactionId
=== transactionId
) {
477 for (const connectorId
of this.connectors
.keys()) {
478 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
485 public getEnergyActiveImportRegisterByTransactionId (
486 transactionId
: number | undefined,
489 return this.getEnergyActiveImportRegister(
490 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
491 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
496 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
497 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
500 public getAuthorizeRemoteTxRequests (): boolean {
501 const authorizeRemoteTxRequests
= getConfigurationKey(
503 StandardParametersKey
.AuthorizeRemoteTxRequests
505 return authorizeRemoteTxRequests
!= null
506 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
510 public getLocalAuthListEnabled (): boolean {
511 const localAuthListEnabled
= getConfigurationKey(
513 StandardParametersKey
.LocalAuthListEnabled
515 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
518 public getHeartbeatInterval (): number {
519 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
520 if (HeartbeatInterval
!= null) {
521 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
523 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
524 if (HeartBeatInterval
!= null) {
525 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
527 this.stationInfo
?.autoRegister
=== false &&
529 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
530 Constants.DEFAULT_HEARTBEAT_INTERVAL
533 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
536 public setSupervisionUrl (url
: string): void {
538 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
539 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
541 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
543 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
544 this.stationInfo
!.supervisionUrls
= url
545 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
546 this.saveStationInfo()
550 public startHeartbeat (): void {
551 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
552 this.heartbeatSetInterval
= setInterval(() => {
553 this.ocppRequestService
554 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
555 .catch((error
: unknown
) => {
557 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
561 }, this.getHeartbeatInterval())
563 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
564 this.getHeartbeatInterval()
567 } else if (this.heartbeatSetInterval
!= null) {
569 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
570 this.getHeartbeatInterval()
575 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
580 public restartHeartbeat (): void {
584 this.startHeartbeat()
587 public restartWebSocketPing (): void {
588 // Stop WebSocket ping
589 this.stopWebSocketPing()
590 // Start WebSocket ping
591 this.startWebSocketPing()
594 public startMeterValues (connectorId
: number, interval
: number): void {
595 if (connectorId
=== 0) {
596 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
599 const connectorStatus
= this.getConnectorStatus(connectorId
)
600 if (connectorStatus
== null) {
602 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
607 if (connectorStatus
.transactionStarted
=== false) {
609 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
613 connectorStatus
.transactionStarted
=== true &&
614 connectorStatus
.transactionId
== null
617 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
622 connectorStatus
.transactionSetInterval
= setInterval(() => {
623 const meterValue
= buildMeterValue(
626 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
627 connectorStatus
.transactionId
!,
630 this.ocppRequestService
631 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
633 RequestCommand
.METER_VALUES
,
636 transactionId
: connectorStatus
.transactionId
,
637 meterValue
: [meterValue
]
640 .catch((error
: unknown
) => {
642 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
649 `${this.logPrefix()} Charging station ${
650 StandardParametersKey.MeterValueSampleInterval
651 } configuration set to ${interval}, not sending MeterValues`
656 public stopMeterValues (connectorId
: number): void {
657 const connectorStatus
= this.getConnectorStatus(connectorId
)
658 if (connectorStatus
?.transactionSetInterval
!= null) {
659 clearInterval(connectorStatus
.transactionSetInterval
)
663 private add (): void {
664 this.emit(ChargingStationEvents
.added
)
667 public async delete (deleteConfiguration
= true): Promise
<void> {
671 AutomaticTransactionGenerator
.deleteInstance(this)
672 PerformanceStatistics
.deleteInstance(this.stationInfo
?.hashId
)
673 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
674 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
675 this.requests
.clear()
676 this.connectors
.clear()
678 this.templateFileWatcher
?.unref()
679 deleteConfiguration
&& rmSync(this.configurationFile
, { force
: true })
680 this.chargingStationWorkerBroadcastChannel
.unref()
681 this.emit(ChargingStationEvents
.deleted
)
682 this.removeAllListeners()
685 public start (): void {
687 if (!this.starting
) {
689 if (this.stationInfo
?.enableStatistics
=== true) {
690 this.performanceStatistics
?.start()
692 this.openWSConnection()
693 // Monitor charging station template file
694 this.templateFileWatcher
= watchJsonFile(
696 FileType
.ChargingStationTemplate
,
699 (event
, filename
): void => {
700 if (isNotEmptyString(filename
) && event
=== 'change') {
703 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
705 } file have changed, reload`
707 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
708 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
709 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
713 const ATGStarted
= this.automaticTransactionGenerator
?.started
714 if (ATGStarted
=== true) {
715 this.stopAutomaticTransactionGenerator()
717 delete this.automaticTransactionGeneratorConfiguration
719 this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true &&
722 this.startAutomaticTransactionGenerator(undefined, true)
724 if (this.stationInfo
?.enableStatistics
=== true) {
725 this.performanceStatistics
?.restart()
727 this.performanceStatistics
?.stop()
729 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
732 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
740 this.emit(ChargingStationEvents
.started
)
741 this.starting
= false
743 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
746 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
751 reason
?: StopTransactionReason
,
752 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
755 if (!this.stopping
) {
757 await this.stopMessageSequence(reason
, stopTransactions
)
758 this.closeWSConnection()
759 if (this.stationInfo
?.enableStatistics
=== true) {
760 this.performanceStatistics
?.stop()
762 this.templateFileWatcher
?.close()
763 delete this.bootNotificationResponse
765 this.saveConfiguration()
766 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
767 this.emit(ChargingStationEvents
.stopped
)
768 this.stopping
= false
770 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
773 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
777 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
778 await this.stop(reason
)
779 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
780 await sleep(this.stationInfo
!.resetTime
!)
785 public saveOcppConfiguration (): void {
786 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
787 this.saveConfiguration()
791 public bufferMessage (message
: string): void {
792 this.messageBuffer
.add(message
)
793 this.setIntervalFlushMessageBuffer()
796 public openWSConnection (
798 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
801 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
802 ...this.stationInfo
?.wsOptions
,
805 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
806 if (!checkChargingStation(this, this.logPrefix())) {
809 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
810 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
812 if (params
.closeOpened
=== true) {
813 this.closeWSConnection()
815 if (params
.terminateOpened
=== true) {
816 this.terminateWSConnection()
819 if (this.isWebSocketConnectionOpened()) {
821 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.href} is already opened`
826 logger
.info(`${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.href}`)
828 this.wsConnection
= new WebSocket(
829 this.wsConnectionUrl
,
830 `ocpp${this.stationInfo?.ocppVersion}`,
834 // Handle WebSocket message
835 this.wsConnection
.on('message', data
=> {
836 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
838 // Handle WebSocket error
839 this.wsConnection
.on('error', this.onError
.bind(this))
840 // Handle WebSocket close
841 this.wsConnection
.on('close', this.onClose
.bind(this))
842 // Handle WebSocket open
843 this.wsConnection
.on('open', () => {
844 this.onOpen().catch((error
: unknown
) =>
845 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
848 // Handle WebSocket ping
849 this.wsConnection
.on('ping', this.onPing
.bind(this))
850 // Handle WebSocket pong
851 this.wsConnection
.on('pong', this.onPong
.bind(this))
854 public closeWSConnection (): void {
855 if (this.isWebSocketConnectionOpened()) {
856 this.wsConnection
?.close()
857 this.wsConnection
= null
861 public getAutomaticTransactionGeneratorConfiguration ():
862 | AutomaticTransactionGeneratorConfiguration
864 if (this.automaticTransactionGeneratorConfiguration
== null) {
865 let automaticTransactionGeneratorConfiguration
:
866 | AutomaticTransactionGeneratorConfiguration
868 const stationTemplate
= this.getTemplateFromFile()
869 const stationConfiguration
= this.getConfigurationFromFile()
871 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
872 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
873 stationConfiguration
?.automaticTransactionGenerator
!= null
875 automaticTransactionGeneratorConfiguration
=
876 stationConfiguration
.automaticTransactionGenerator
878 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
880 this.automaticTransactionGeneratorConfiguration
= {
881 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
882 ...automaticTransactionGeneratorConfiguration
885 return this.automaticTransactionGeneratorConfiguration
888 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
889 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
892 public startAutomaticTransactionGenerator (
893 connectorIds
?: number[],
894 stopAbsoluteDuration
?: boolean
896 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
897 if (isNotEmptyArray(connectorIds
)) {
898 for (const connectorId
of connectorIds
) {
899 this.automaticTransactionGenerator
?.startConnector(connectorId
, stopAbsoluteDuration
)
902 this.automaticTransactionGenerator
?.start(stopAbsoluteDuration
)
904 this.saveAutomaticTransactionGeneratorConfiguration()
905 this.emit(ChargingStationEvents
.updated
)
908 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
909 if (isNotEmptyArray(connectorIds
)) {
910 for (const connectorId
of connectorIds
) {
911 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
914 this.automaticTransactionGenerator
?.stop()
916 this.saveAutomaticTransactionGeneratorConfiguration()
917 this.emit(ChargingStationEvents
.updated
)
920 public async stopTransactionOnConnector (
922 reason
?: StopTransactionReason
923 ): Promise
<StopTransactionResponse
> {
924 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
926 this.stationInfo
?.beginEndMeterValues
=== true &&
927 this.stationInfo
.ocppStrictCompliance
=== true &&
928 this.stationInfo
.outOfOrderEndMeterValues
=== false
930 const transactionEndMeterValue
= buildTransactionEndMeterValue(
933 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
935 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
937 RequestCommand
.METER_VALUES
,
941 meterValue
: [transactionEndMeterValue
]
945 return await this.ocppRequestService
.requestHandler
<
946 StopTransactionRequest
,
947 StopTransactionResponse
948 >(this, RequestCommand
.STOP_TRANSACTION
, {
950 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
951 ...(reason
!= null && { reason
})
955 public getReserveConnectorZeroSupported (): boolean {
956 return convertToBoolean(
957 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
958 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
962 public async addReservation (reservation
: Reservation
): Promise
<void> {
963 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
964 if (reservationFound
!= null) {
965 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
967 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
968 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
969 await sendAndSetConnectorStatus(
971 reservation
.connectorId
,
972 ConnectorStatusEnum
.Reserved
,
974 { send
: reservation
.connectorId
!== 0 }
978 public async removeReservation (
979 reservation
: Reservation
,
980 reason
: ReservationTerminationReason
982 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
983 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
985 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
986 case ReservationTerminationReason
.TRANSACTION_STARTED
:
987 delete connector
.reservation
989 case ReservationTerminationReason
.RESERVATION_CANCELED
:
990 case ReservationTerminationReason
.REPLACE_EXISTING
:
991 case ReservationTerminationReason
.EXPIRED
:
992 await sendAndSetConnectorStatus(
994 reservation
.connectorId
,
995 ConnectorStatusEnum
.Available
,
997 { send
: reservation
.connectorId
!== 0 }
999 delete connector
.reservation
1002 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1003 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
1007 public getReservationBy (
1008 filterKey
: ReservationKey
,
1009 value
: number | string
1010 ): Reservation
| undefined {
1011 if (this.hasEvses
) {
1012 for (const evseStatus
of this.evses
.values()) {
1013 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1014 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1015 return connectorStatus
.reservation
1020 for (const connectorStatus
of this.connectors
.values()) {
1021 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1022 return connectorStatus
.reservation
1028 public isConnectorReservable (
1029 reservationId
: number,
1031 connectorId
?: number
1033 const reservation
= this.getReservationBy('reservationId', reservationId
)
1034 const reservationExists
= reservation
!= null && !hasReservationExpired(reservation
)
1035 if (arguments.length
=== 1) {
1036 return !reservationExists
1037 } else if (arguments.length
> 1) {
1038 const userReservation
= idTag
!= null ? this.getReservationBy('idTag', idTag
) : undefined
1039 const userReservationExists
=
1040 userReservation
!= null && !hasReservationExpired(userReservation
)
1041 const notConnectorZero
= connectorId
== null ? true : connectorId
> 0
1042 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
1044 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1050 private setIntervalFlushMessageBuffer (): void {
1051 if (this.flushMessageBufferSetInterval
== null) {
1052 this.flushMessageBufferSetInterval
= setInterval(() => {
1053 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
1054 this.flushMessageBuffer()
1056 if (this.messageBuffer
.size
=== 0) {
1057 this.clearIntervalFlushMessageBuffer()
1059 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1063 private clearIntervalFlushMessageBuffer (): void {
1064 if (this.flushMessageBufferSetInterval
!= null) {
1065 clearInterval(this.flushMessageBufferSetInterval
)
1066 delete this.flushMessageBufferSetInterval
1070 private getNumberOfReservableConnectors (): number {
1071 let numberOfReservableConnectors
= 0
1072 if (this.hasEvses
) {
1073 for (const evseStatus
of this.evses
.values()) {
1074 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1077 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1079 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1082 private getNumberOfReservationsOnConnectorZero (): number {
1084 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1085 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1092 private flushMessageBuffer (): void {
1093 if (this.messageBuffer
.size
> 0) {
1094 for (const message
of this.messageBuffer
.values()) {
1095 let beginId
: string | undefined
1096 let commandName
: RequestCommand
| undefined
1097 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1098 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1100 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1101 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1103 this.wsConnection
?.send(message
, (error
?: Error) => {
1104 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1105 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1106 if (error
== null) {
1108 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1110 )} OCPP message sent '${JSON.stringify(message)}'`
1112 this.messageBuffer
.delete(message
)
1115 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1117 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1126 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1127 let template
: ChargingStationTemplate
| undefined
1129 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1130 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1132 const measureId
= `${FileType.ChargingStationTemplate} read`
1133 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1134 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1135 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1136 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1137 .update(JSON
.stringify(template
))
1139 this.sharedLRUCache
.setChargingStationTemplate(template
)
1140 this.templateFileHash
= template
.templateHash
1143 handleFileException(
1145 FileType
.ChargingStationTemplate
,
1146 error
as NodeJS
.ErrnoException
,
1153 private getStationInfoFromTemplate (): ChargingStationInfo
{
1154 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1155 const stationTemplate
= this.getTemplateFromFile()!
1156 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1157 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
)
1158 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1159 if (stationTemplate
.Connectors
!= null) {
1160 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1162 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1163 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1164 stationInfo
.templateIndex
= this.index
1165 stationInfo
.templateName
= buildTemplateName(this.templateFile
)
1166 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1167 createSerialNumber(stationTemplate
, stationInfo
)
1168 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1169 if (isNotEmptyArray(stationTemplate
.power
)) {
1170 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1171 stationInfo
.maximumPower
=
1172 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1173 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1174 : stationTemplate
.power
[powerArrayRandomIndex
]
1176 stationInfo
.maximumPower
=
1177 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1178 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1179 stationTemplate
.power
! * 1000
1180 : stationTemplate
.power
1182 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1184 isNotEmptyString(stationInfo
.firmwareVersionPattern
) &&
1185 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1186 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1189 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1191 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1194 if (stationTemplate
.resetTime
!= null) {
1195 stationInfo
.resetTime
= secondsToMilliseconds(stationTemplate
.resetTime
)
1200 private getStationInfoFromFile (
1201 stationInfoPersistentConfiguration
: boolean | undefined = Constants
.DEFAULT_STATION_INFO
1202 .stationInfoPersistentConfiguration
1203 ): ChargingStationInfo
| undefined {
1204 let stationInfo
: ChargingStationInfo
| undefined
1205 if (stationInfoPersistentConfiguration
=== true) {
1206 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1207 if (stationInfo
!= null) {
1208 delete stationInfo
.infoHash
1209 delete (stationInfo
as ChargingStationTemplate
).numberOfConnectors
1210 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1211 if (stationInfo
.templateIndex
== null) {
1212 stationInfo
.templateIndex
= this.index
1214 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1215 if (stationInfo
.templateName
== null) {
1216 stationInfo
.templateName
= buildTemplateName(this.templateFile
)
1223 private getStationInfo (options
?: ChargingStationOptions
): ChargingStationInfo
{
1224 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1225 options
?.persistentConfiguration
!= null &&
1226 (stationInfoFromTemplate
.stationInfoPersistentConfiguration
= options
.persistentConfiguration
)
1227 const stationInfoFromFile
= this.getStationInfoFromFile(
1228 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1230 let stationInfo
: ChargingStationInfo
1232 // 1. charging station info from template
1233 // 2. charging station info from configuration file
1235 stationInfoFromFile
!= null &&
1236 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1238 stationInfo
= stationInfoFromFile
1240 stationInfo
= stationInfoFromTemplate
1241 stationInfoFromFile
!= null &&
1242 propagateSerialNumber(this.getTemplateFromFile(), stationInfoFromFile
, stationInfo
)
1244 return setChargingStationOptions(
1245 mergeDeepRight(Constants
.DEFAULT_STATION_INFO
, stationInfo
),
1250 private saveStationInfo (): void {
1251 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1252 this.saveConfiguration()
1256 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1257 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1258 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1259 throw new BaseError(errorMsg
)
1262 private initialize (options
?: ChargingStationOptions
): void {
1263 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1264 const stationTemplate
= this.getTemplateFromFile()!
1265 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1266 this.configurationFile
= join(
1267 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1268 `${getHashId(this.index, stationTemplate)}.json`
1270 const stationConfiguration
= this.getConfigurationFromFile()
1272 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1273 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1275 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1276 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1278 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1280 this.stationInfo
= this.getStationInfo(options
)
1282 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1283 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
) &&
1284 isNotEmptyString(this.stationInfo
.firmwareVersion
)
1286 const patternGroup
=
1287 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1288 this.stationInfo
.firmwareVersion
.split('.').length
1289 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1290 .exec(this.stationInfo
.firmwareVersion
)
1291 ?.slice(1, patternGroup
+ 1)
1292 if (match
!= null) {
1293 const patchLevelIndex
= match
.length
- 1
1294 match
[patchLevelIndex
] = (
1295 convertToInt(match
[patchLevelIndex
]) +
1296 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1297 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1299 this.stationInfo
.firmwareVersion
= match
.join('.')
1302 this.saveStationInfo()
1303 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1304 if (this.stationInfo
.enableStatistics
=== true) {
1305 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1306 this.stationInfo
.hashId
,
1307 this.stationInfo
.chargingStationId
,
1308 this.configuredSupervisionUrl
1311 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1312 if (bootNotificationRequest
== null) {
1313 const errorMsg
= 'Error while creating boot notification request'
1314 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1315 throw new BaseError(errorMsg
)
1317 this.bootNotificationRequest
= bootNotificationRequest
1318 this.powerDivider
= this.getPowerDivider()
1319 // OCPP configuration
1320 this.ocppConfiguration
= this.getOcppConfiguration(options
?.persistentConfiguration
)
1321 this.initializeOcppConfiguration()
1322 this.initializeOcppServices()
1323 if (this.stationInfo
.autoRegister
=== true) {
1324 this.bootNotificationResponse
= {
1325 currentTime
: new Date(),
1326 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1327 status: RegistrationStatusEnumType
.ACCEPTED
1332 private initializeOcppServices (): void {
1333 const ocppVersion
= this.stationInfo
?.ocppVersion
1334 switch (ocppVersion
) {
1335 case OCPPVersion
.VERSION_16
:
1336 this.ocppIncomingRequestService
=
1337 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1338 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1339 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1342 case OCPPVersion
.VERSION_20
:
1343 case OCPPVersion
.VERSION_201
:
1344 this.ocppIncomingRequestService
=
1345 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1346 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1347 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1351 this.handleUnsupportedVersion(ocppVersion
)
1356 private initializeOcppConfiguration (): void {
1357 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1358 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1360 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1361 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1364 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1365 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1366 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1368 addConfigurationKey(
1370 this.stationInfo
.supervisionUrlOcppKey
,
1371 this.configuredSupervisionUrl
.href
,
1375 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1376 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1377 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1379 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1382 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1383 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1385 addConfigurationKey(
1387 this.stationInfo
.amperageLimitationOcppKey
,
1389 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1390 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1393 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1394 addConfigurationKey(
1396 StandardParametersKey
.SupportedFeatureProfiles
,
1397 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1400 addConfigurationKey(
1402 StandardParametersKey
.NumberOfConnectors
,
1403 this.getNumberOfConnectors().toString(),
1407 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1408 addConfigurationKey(
1410 StandardParametersKey
.MeterValuesSampledData
,
1411 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1414 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1415 const connectorsPhaseRotation
: string[] = []
1416 if (this.hasEvses
) {
1417 for (const evseStatus
of this.evses
.values()) {
1418 for (const connectorId
of evseStatus
.connectors
.keys()) {
1419 connectorsPhaseRotation
.push(
1420 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1421 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1426 for (const connectorId
of this.connectors
.keys()) {
1427 connectorsPhaseRotation
.push(
1428 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1429 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1433 addConfigurationKey(
1435 StandardParametersKey
.ConnectorPhaseRotation
,
1436 connectorsPhaseRotation
.toString()
1439 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1440 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1443 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1444 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1446 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1448 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1449 addConfigurationKey(
1451 StandardParametersKey
.ConnectionTimeOut
,
1452 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1455 this.saveOcppConfiguration()
1458 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1459 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1460 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1461 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1463 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1464 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1465 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1466 delete evseStatus
.connectorsStatus
1467 this.evses
.set(evseId
, {
1468 ...(evseStatus
as EvseStatus
),
1469 connectors
: new Map
<number, ConnectorStatus
>(
1470 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1471 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1478 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1479 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1480 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1481 throw new BaseError(errorMsg
)
1483 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1484 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1485 throw new BaseError(errorMsg
)
1489 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1490 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1491 this.initializeConnectorsFromTemplate(stationTemplate
)
1492 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1493 this.initializeEvsesFromTemplate(stationTemplate
)
1494 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1495 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1496 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1497 throw new BaseError(errorMsg
)
1499 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1500 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1501 throw new BaseError(errorMsg
)
1505 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1506 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1507 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1508 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1509 throw new BaseError(errorMsg
)
1511 if (stationTemplate
.Connectors
?.[0] == null) {
1513 `${this.logPrefix()} Charging station information from template ${
1515 } with no connector id 0 configuration`
1518 if (stationTemplate
.Connectors
!= null) {
1519 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1520 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1521 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1523 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1526 const connectorsConfigChanged
=
1527 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1528 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1529 connectorsConfigChanged
&& this.connectors
.clear()
1530 this.connectorsConfigurationHash
= connectorsConfigHash
1531 if (templateMaxConnectors
> 0) {
1532 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1534 connectorId
=== 0 &&
1535 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1536 (stationTemplate
.Connectors
[connectorId
] == null ||
1537 !this.getUseConnectorId0(stationTemplate
))
1541 const templateConnectorId
=
1542 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1543 ? randomInt(1, templateMaxAvailableConnectors
)
1545 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1546 checkStationInfoConnectorStatus(
1547 templateConnectorId
,
1552 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1554 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1555 this.saveConnectorsStatus()
1558 `${this.logPrefix()} Charging station information from template ${
1560 } with no connectors configuration defined, cannot create connectors`
1566 `${this.logPrefix()} Charging station information from template ${
1568 } with no connectors configuration defined, using already defined connectors`
1573 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1574 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1575 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1576 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1577 throw new BaseError(errorMsg
)
1579 if (stationTemplate
.Evses
?.[0] == null) {
1581 `${this.logPrefix()} Charging station information from template ${
1583 } with no evse id 0 configuration`
1586 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1588 `${this.logPrefix()} Charging station information from template ${
1590 } with evse id 0 with no connector id 0 configuration`
1593 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1595 `${this.logPrefix()} Charging station information from template ${
1597 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1600 if (stationTemplate
.Evses
!= null) {
1601 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1602 .update(JSON
.stringify(stationTemplate
.Evses
))
1604 const evsesConfigChanged
=
1605 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1606 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1607 evsesConfigChanged
&& this.evses
.clear()
1608 this.evsesConfigurationHash
= evsesConfigHash
1609 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1610 if (templateMaxEvses
> 0) {
1611 for (const evseKey
in stationTemplate
.Evses
) {
1612 const evseId
= convertToInt(evseKey
)
1613 this.evses
.set(evseId
, {
1614 connectors
: buildConnectorsMap(
1615 stationTemplate
.Evses
[evseKey
].Connectors
,
1619 availability
: AvailabilityType
.Operative
1621 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1622 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1624 this.saveEvsesStatus()
1627 `${this.logPrefix()} Charging station information from template ${
1629 } with no evses configuration defined, cannot create evses`
1635 `${this.logPrefix()} Charging station information from template ${
1637 } with no evses configuration defined, using already defined evses`
1642 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1643 let configuration
: ChargingStationConfiguration
| undefined
1644 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1646 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1647 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1648 this.configurationFileHash
1651 const measureId
= `${FileType.ChargingStationConfiguration} read`
1652 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1653 configuration
= JSON
.parse(
1654 readFileSync(this.configurationFile
, 'utf8')
1655 ) as ChargingStationConfiguration
1656 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1657 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1658 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1659 this.configurationFileHash
= configuration
.configurationHash
!
1662 handleFileException(
1663 this.configurationFile
,
1664 FileType
.ChargingStationConfiguration
,
1665 error
as NodeJS
.ErrnoException
,
1670 return configuration
1673 private saveAutomaticTransactionGeneratorConfiguration (): void {
1674 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1675 this.saveConfiguration()
1679 private saveConnectorsStatus (): void {
1680 this.saveConfiguration()
1683 private saveEvsesStatus (): void {
1684 this.saveConfiguration()
1687 private saveConfiguration (): void {
1688 if (isNotEmptyString(this.configurationFile
)) {
1690 if (!existsSync(dirname(this.configurationFile
))) {
1691 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1693 const configurationFromFile
= this.getConfigurationFromFile()
1694 let configurationData
: ChargingStationConfiguration
=
1695 configurationFromFile
!= null
1696 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1698 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1699 configurationData
.stationInfo
= this.stationInfo
1701 delete configurationData
.stationInfo
1704 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1705 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1707 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1709 delete configurationData
.configurationKey
1711 configurationData
= mergeDeepRight(
1713 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1715 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
!== true) {
1716 delete configurationData
.automaticTransactionGenerator
1718 if (this.connectors
.size
> 0) {
1719 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1721 delete configurationData
.connectorsStatus
1723 if (this.evses
.size
> 0) {
1724 configurationData
.evsesStatus
= buildEvsesStatus(this)
1726 delete configurationData
.evsesStatus
1728 delete configurationData
.configurationHash
1729 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1732 stationInfo
: configurationData
.stationInfo
,
1733 configurationKey
: configurationData
.configurationKey
,
1734 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1735 ...(this.connectors
.size
> 0 && {
1736 connectorsStatus
: configurationData
.connectorsStatus
1738 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1739 } satisfies ChargingStationConfiguration
)
1742 if (this.configurationFileHash
!== configurationHash
) {
1743 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1744 configurationData
.configurationHash
= configurationHash
1745 const measureId
= `${FileType.ChargingStationConfiguration} write`
1746 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1748 this.configurationFile
,
1749 JSON
.stringify(configurationData
, undefined, 2),
1752 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1753 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1754 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1755 this.configurationFileHash
= configurationHash
1756 }).catch((error
: unknown
) => {
1757 handleFileException(
1758 this.configurationFile
,
1759 FileType
.ChargingStationConfiguration
,
1760 error
as NodeJS
.ErrnoException
,
1766 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1767 this.configurationFile
1772 handleFileException(
1773 this.configurationFile
,
1774 FileType
.ChargingStationConfiguration
,
1775 error
as NodeJS
.ErrnoException
,
1781 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1786 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1787 return this.getTemplateFromFile()?.Configuration
1790 private getOcppConfigurationFromFile (
1791 ocppPersistentConfiguration
?: boolean
1792 ): ChargingStationOcppConfiguration
| undefined {
1793 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1794 if (ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1795 return { configurationKey
}
1800 private getOcppConfiguration (
1801 ocppPersistentConfiguration
: boolean | undefined = this.stationInfo
?.ocppPersistentConfiguration
1802 ): ChargingStationOcppConfiguration
| undefined {
1803 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1804 this.getOcppConfigurationFromFile(ocppPersistentConfiguration
)
1805 if (ocppConfiguration
== null) {
1806 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1808 return ocppConfiguration
1811 private async onOpen (): Promise
<void> {
1812 if (this.isWebSocketConnectionOpened()) {
1813 this.emit(ChargingStationEvents
.updated
)
1815 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded`
1817 let registrationRetryCount
= 0
1818 if (!this.isRegistered()) {
1819 // Send BootNotification
1821 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1822 BootNotificationRequest
,
1823 BootNotificationResponse
1824 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1825 skipBufferingOnError
: true
1827 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1828 if (this.bootNotificationResponse
?.currentTime
!= null) {
1829 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1830 this.bootNotificationResponse
.currentTime
= convertToDate(
1831 this.bootNotificationResponse
.currentTime
1834 if (!this.isRegistered()) {
1835 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1837 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1838 this.bootNotificationResponse
?.interval
!= null
1839 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1840 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1844 !this.isRegistered() &&
1845 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1846 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1847 this.stationInfo
?.registrationMaxRetries
=== -1)
1850 if (this.isRegistered()) {
1851 this.emit(ChargingStationEvents
.registered
)
1852 if (this.inAcceptedState()) {
1853 this.emit(ChargingStationEvents
.accepted
)
1856 if (this.inRejectedState()) {
1857 this.emit(ChargingStationEvents
.rejected
)
1860 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1861 this.stationInfo?.registrationMaxRetries
1865 this.wsConnectionRetryCount
= 0
1866 this.emit(ChargingStationEvents
.updated
)
1869 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} failed`
1874 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1875 this.emit(ChargingStationEvents
.disconnected
)
1876 this.emit(ChargingStationEvents
.updated
)
1879 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1880 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1882 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1884 )}' and reason '${reason.toString()}'`
1886 this.wsConnectionRetryCount
= 0
1891 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1893 )}' and reason '${reason.toString()}'`
1898 this.emit(ChargingStationEvents
.updated
)
1900 .catch((error
: unknown
) =>
1901 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
1907 private getCachedRequest (
1908 messageType
: MessageType
| undefined,
1910 ): CachedRequest
| undefined {
1911 const cachedRequest
= this.requests
.get(messageId
)
1912 if (Array.isArray(cachedRequest
)) {
1913 return cachedRequest
1915 throw new OCPPError(
1916 ErrorType
.PROTOCOL_ERROR
,
1917 `Cached request for message id ${messageId} ${getMessageTypeString(
1919 )} is not an array`,
1925 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1926 const [messageType
, messageId
, commandName
, commandPayload
] = request
1927 if (this.stationInfo
?.enableStatistics
=== true) {
1928 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1931 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1935 // Process the message
1936 await this.ocppIncomingRequestService
.incomingRequestHandler(
1942 this.emit(ChargingStationEvents
.updated
)
1945 private handleResponseMessage (response
: Response
): void {
1946 const [messageType
, messageId
, commandPayload
] = response
1947 if (!this.requests
.has(messageId
)) {
1949 throw new OCPPError(
1950 ErrorType
.INTERNAL_ERROR
,
1951 `Response for unknown message id ${messageId}`,
1957 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1958 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1963 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1967 responseCallback(commandPayload
, requestPayload
)
1970 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1971 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1972 if (!this.requests
.has(messageId
)) {
1974 throw new OCPPError(
1975 ErrorType
.INTERNAL_ERROR
,
1976 `Error response for unknown message id ${messageId}`,
1978 { errorType
, errorMessage
, errorDetails
}
1981 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1982 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1984 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1988 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1991 private async onMessage (data
: RawData
): Promise
<void> {
1992 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1993 let messageType
: MessageType
| undefined
1994 let errorMsg
: string
1996 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1997 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
1998 if (Array.isArray(request
)) {
1999 [messageType
] = request
2000 // Check the type of message
2001 switch (messageType
) {
2003 case MessageType
.CALL_MESSAGE
:
2004 await this.handleIncomingMessage(request
as IncomingRequest
)
2007 case MessageType
.CALL_RESULT_MESSAGE
:
2008 this.handleResponseMessage(request
as Response
)
2011 case MessageType
.CALL_ERROR_MESSAGE
:
2012 this.handleErrorMessage(request
as ErrorResponse
)
2016 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2017 errorMsg
= `Wrong message type ${messageType}`
2018 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2019 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2022 throw new OCPPError(
2023 ErrorType
.PROTOCOL_ERROR
,
2024 'Incoming message is not an array',
2032 if (!Array.isArray(request
)) {
2033 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2036 let commandName
: IncomingRequestCommand
| undefined
2037 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2038 let errorCallback
: ErrorCallback
2039 const [, messageId
] = request
2040 switch (messageType
) {
2041 case MessageType
.CALL_MESSAGE
:
2042 [, , commandName
] = request
as IncomingRequest
2044 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2046 case MessageType
.CALL_RESULT_MESSAGE
:
2047 case MessageType
.CALL_ERROR_MESSAGE
:
2048 if (this.requests
.has(messageId
)) {
2049 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2050 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2051 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2052 errorCallback(error
as OCPPError
, false)
2054 // Remove the request from the cache in case of error at response handling
2055 this.requests
.delete(messageId
)
2059 if (!(error
instanceof OCPPError
)) {
2061 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2062 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2063 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2064 }' message '${data.toString()}' handling is not an OCPPError:`,
2069 `${this.logPrefix()} Incoming OCPP command '${
2070 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2071 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2072 }' message '${data.toString()}'${
2073 this.requests.has(messageId)
2074 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2076 } processing error:`,
2082 private onPing (): void {
2083 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2086 private onPong (): void {
2087 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2090 private onError (error
: WSError
): void {
2091 this.closeWSConnection()
2092 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2095 private getEnergyActiveImportRegister (
2096 connectorStatus
: ConnectorStatus
| undefined,
2099 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2102 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2103 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2105 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2110 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2111 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2113 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2117 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2118 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2119 return stationTemplate
?.useConnectorId0
?? Constants
.DEFAULT_STATION_INFO
.useConnectorId0
!
2122 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2123 if (this.hasEvses
) {
2124 for (const [evseId
, evseStatus
] of this.evses
) {
2128 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2129 if (connectorStatus
.transactionStarted
=== true) {
2130 await this.stopTransactionOnConnector(connectorId
, reason
)
2135 for (const connectorId
of this.connectors
.keys()) {
2136 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2137 await this.stopTransactionOnConnector(connectorId
, reason
)
2144 private getConnectionTimeout (): number {
2145 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2146 return convertToInt(
2147 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2148 Constants
.DEFAULT_CONNECTION_TIMEOUT
2151 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2154 private getPowerDivider (): number {
2155 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2156 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2157 powerDivider
= this.getNumberOfRunningTransactions()
2162 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2163 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2164 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2165 switch (this.getCurrentOutType(stationInfo
)) {
2166 case CurrentType
.AC
:
2167 return ACElectricUtils
.amperagePerPhaseFromPower(
2168 this.getNumberOfPhases(stationInfo
),
2169 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2170 this.getVoltageOut(stationInfo
)
2172 case CurrentType
.DC
:
2173 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2177 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2179 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2180 (stationInfo
?? this.stationInfo
!).currentOutType
??
2181 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2182 Constants
.DEFAULT_STATION_INFO
.currentOutType
!
2186 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2188 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2189 (stationInfo
?? this.stationInfo
!).voltageOut
??
2190 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2194 private getAmperageLimitation (): number | undefined {
2196 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2197 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2200 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2201 getAmperageLimitationUnitDivider(this.stationInfo
)
2206 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2207 if (this.stationInfo
?.autoRegister
=== true) {
2208 await this.ocppRequestService
.requestHandler
<
2209 BootNotificationRequest
,
2210 BootNotificationResponse
2211 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2212 skipBufferingOnError
: true
2215 // Start WebSocket ping
2216 this.startWebSocketPing()
2218 this.startHeartbeat()
2219 // Initialize connectors status
2220 if (this.hasEvses
) {
2221 for (const [evseId
, evseStatus
] of this.evses
) {
2223 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2224 await sendAndSetConnectorStatus(
2227 getBootConnectorStatus(this, connectorId
, connectorStatus
),
2234 for (const connectorId
of this.connectors
.keys()) {
2235 if (connectorId
> 0) {
2236 await sendAndSetConnectorStatus(
2239 getBootConnectorStatus(
2242 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2243 this.getConnectorStatus(connectorId
)!
2249 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2250 await this.ocppRequestService
.requestHandler
<
2251 FirmwareStatusNotificationRequest
,
2252 FirmwareStatusNotificationResponse
2253 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2254 status: FirmwareStatus
.Installed
2256 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2260 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2261 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2263 this.flushMessageBuffer()
2266 private internalStopMessageSequence (): void {
2267 // Stop WebSocket ping
2268 this.stopWebSocketPing()
2270 this.stopHeartbeat()
2272 if (this.automaticTransactionGenerator
?.started
=== true) {
2273 this.stopAutomaticTransactionGenerator()
2277 private async stopMessageSequence (
2278 reason
?: StopTransactionReason
,
2279 stopTransactions
?: boolean
2281 this.internalStopMessageSequence()
2282 // Stop ongoing transactions
2283 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2284 if (this.hasEvses
) {
2285 for (const [evseId
, evseStatus
] of this.evses
) {
2287 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2288 await sendAndSetConnectorStatus(
2291 ConnectorStatusEnum
.Unavailable
,
2294 delete connectorStatus
.status
2299 for (const connectorId
of this.connectors
.keys()) {
2300 if (connectorId
> 0) {
2301 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2302 delete this.getConnectorStatus(connectorId
)?.status
2308 private startWebSocketPing (): void {
2309 const webSocketPingInterval
=
2310 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2312 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2315 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2316 this.wsPingSetInterval
= setInterval(() => {
2317 if (this.isWebSocketConnectionOpened()) {
2318 this.wsConnection
?.ping()
2320 }, secondsToMilliseconds(webSocketPingInterval
))
2322 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2323 webSocketPingInterval
2326 } else if (this.wsPingSetInterval
!= null) {
2328 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2329 webSocketPingInterval
2334 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2339 private stopWebSocketPing (): void {
2340 if (this.wsPingSetInterval
!= null) {
2341 clearInterval(this.wsPingSetInterval
)
2342 delete this.wsPingSetInterval
2346 private getConfiguredSupervisionUrl (): URL
{
2347 let configuredSupervisionUrl
: string
2348 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2349 if (isNotEmptyArray(supervisionUrls
)) {
2350 let configuredSupervisionUrlIndex
: number
2351 switch (Configuration
.getSupervisionUrlDistribution()) {
2352 case SupervisionUrlDistribution
.RANDOM
:
2353 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2355 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2356 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2358 !Object.values(SupervisionUrlDistribution
).includes(
2359 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2360 Configuration
.getSupervisionUrlDistribution()!
2363 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2364 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' in configuration from values '${SupervisionUrlDistribution.toString()}', defaulting to '${
2365 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2368 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2371 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2373 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2374 configuredSupervisionUrl
= supervisionUrls
!
2376 if (isNotEmptyString(configuredSupervisionUrl
)) {
2377 return new URL(configuredSupervisionUrl
)
2379 const errorMsg
= 'No supervision url(s) configured'
2380 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2381 throw new BaseError(errorMsg
)
2384 private stopHeartbeat (): void {
2385 if (this.heartbeatSetInterval
!= null) {
2386 clearInterval(this.heartbeatSetInterval
)
2387 delete this.heartbeatSetInterval
2391 private terminateWSConnection (): void {
2392 if (this.isWebSocketConnectionOpened()) {
2393 this.wsConnection
?.terminate()
2394 this.wsConnection
= null
2398 private async reconnect (): Promise
<void> {
2400 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2401 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2402 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2404 this.wsConnectionRetried
= true
2405 ++this.wsConnectionRetryCount
2406 const reconnectDelay
=
2407 this.stationInfo
?.reconnectExponentialDelay
=== true
2408 ? exponentialDelay(this.wsConnectionRetryCount
)
2409 : secondsToMilliseconds(this.getConnectionTimeout())
2410 const reconnectDelayWithdraw
= 1000
2411 const reconnectTimeout
=
2412 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2414 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2417 )}ms, timeout ${reconnectTimeout}ms`
2419 await sleep(reconnectDelay
)
2421 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2423 this.openWSConnection(
2425 handshakeTimeout
: reconnectTimeout
2427 { closeOpened
: true }
2429 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2431 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2432 this.wsConnectionRetryCount
2433 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`