1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { createHash
} from
'node:crypto'
4 import { EventEmitter
} from
'node:events'
5 import { type FSWatcher
, existsSync
, mkdirSync
, readFileSync
, writeFileSync
} from
'node:fs'
6 import { dirname
, join
, parse
} 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 merge from
'just-merge'
12 import { type RawData
, WebSocket
} from
'ws'
14 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator.js'
15 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel.js'
18 deleteConfigurationKey
,
20 setConfigurationKeyValue
21 } from
'./ConfigurationKeyUtils.js'
26 checkConnectorsConfiguration
,
27 checkStationInfoConnectorStatus
,
29 createBootNotificationRequest
,
31 getAmperageLimitationUnitDivider
,
32 getBootConnectorStatus
,
33 getChargingStationConnectorChargingProfilesPowerLimit
,
39 getNumberOfReservableConnectors
,
40 getPhaseRotationValue
,
42 hasReservationExpired
,
43 initializeConnectorsMapStatus
,
44 propagateSerialNumber
,
45 stationTemplateToStationInfo
,
46 warnTemplateKeysDeprecation
48 import { IdTagsCache
} from
'./IdTagsCache.js'
50 OCPP16IncomingRequestService
,
52 OCPP16ResponseService
,
53 OCPP20IncomingRequestService
,
55 OCPP20ResponseService
,
56 type OCPPIncomingRequestService
,
57 type OCPPRequestService
,
59 buildTransactionEndMeterValue
,
61 sendAndSetConnectorStatus
62 } from
'./ocpp/index.js'
63 import { SharedLRUCache
} from
'./SharedLRUCache.js'
64 import { BaseError
, OCPPError
} from
'../exception/index.js'
65 import { PerformanceStatistics
} from
'../performance/index.js'
67 type AutomaticTransactionGeneratorConfiguration
,
69 type BootNotificationRequest
,
70 type BootNotificationResponse
,
72 type ChargingStationConfiguration
,
73 ChargingStationEvents
,
74 type ChargingStationInfo
,
75 type ChargingStationOcppConfiguration
,
76 type ChargingStationOptions
,
77 type ChargingStationTemplate
,
85 type EvseStatusConfiguration
,
88 type FirmwareStatusNotificationRequest
,
89 type FirmwareStatusNotificationResponse
,
91 type HeartbeatRequest
,
92 type HeartbeatResponse
,
94 type IncomingRequestCommand
,
97 type MeterValuesRequest
,
98 type MeterValuesResponse
,
100 type OutgoingRequest
,
102 RegistrationStatusEnumType
,
106 ReservationTerminationReason
,
108 StandardParametersKey
,
110 type StopTransactionReason
,
111 type StopTransactionRequest
,
112 type StopTransactionResponse
,
113 SupervisionUrlDistribution
,
114 SupportedFeatureProfiles
,
117 WebSocketCloseEventStatusCode
,
119 } from
'../types/index.js'
128 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
129 buildConnectorsStatus
,
139 formatDurationMilliSeconds
,
140 formatDurationSeconds
,
142 getWebSocketCloseEventStatusString
,
154 } from
'../utils/index.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
| undefined
164 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined
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
| undefined
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
| undefined
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
.started
, () => {
215 parentPort
?.postMessage(buildStartedMessage(this))
217 this.on(ChargingStationEvents
.stopped
, () => {
218 parentPort
?.postMessage(buildStoppedMessage(this))
220 this.on(ChargingStationEvents
.updated
, () => {
221 parentPort
?.postMessage(buildUpdatedMessage(this))
223 this.on(ChargingStationEvents
.accepted
, () => {
224 this.startMessageSequence(
225 this.wsConnectionRetried
227 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
229 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
231 this.wsConnectionRetried
= false
233 this.on(ChargingStationEvents
.rejected
, () => {
234 this.wsConnectionRetried
= false
236 this.on(ChargingStationEvents
.disconnected
, () => {
238 this.internalStopMessageSequence()
241 `${this.logPrefix()} Error while stopping the internal message sequence:`,
252 options
?.autoStart
=== true ||
253 (options
?.autoStart
!== false && this.stationInfo
?.autoStart
=== true)
259 public get
hasEvses (): boolean {
260 return this.connectors
.size
=== 0 && this.evses
.size
> 0
263 private 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
289 stationTemplate
= undefined
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.saveStationInfo()
546 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
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
)
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
]
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 start (): void {
669 if (!this.starting
) {
671 if (this.stationInfo
?.enableStatistics
=== true) {
672 this.performanceStatistics
?.start()
674 this.openWSConnection()
675 // Monitor charging station template file
676 this.templateFileWatcher
= watchJsonFile(
678 FileType
.ChargingStationTemplate
,
681 (event
, filename
): void => {
682 if (isNotEmptyString(filename
) && event
=== 'change') {
685 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
687 } file have changed, reload`
689 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
692 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
693 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
695 const ATGStarted
= this.automaticTransactionGenerator
?.started
696 if (ATGStarted
=== true) {
697 this.stopAutomaticTransactionGenerator()
699 delete this.automaticTransactionGeneratorConfiguration
701 this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true &&
704 this.startAutomaticTransactionGenerator(undefined, true)
706 if (this.stationInfo
?.enableStatistics
=== true) {
707 this.performanceStatistics
?.restart()
709 this.performanceStatistics
?.stop()
711 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
714 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
722 this.emit(ChargingStationEvents
.started
)
723 this.starting
= false
725 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
728 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
733 reason
?: StopTransactionReason
,
734 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
737 if (!this.stopping
) {
739 await this.stopMessageSequence(reason
, stopTransactions
)
740 this.closeWSConnection()
741 if (this.stationInfo
?.enableStatistics
=== true) {
742 this.performanceStatistics
?.stop()
744 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
745 this.templateFileWatcher
?.close()
746 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
747 delete this.bootNotificationResponse
749 this.saveConfiguration()
750 this.emit(ChargingStationEvents
.stopped
)
751 this.stopping
= false
753 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
756 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
760 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
761 await this.stop(reason
)
762 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
763 await sleep(this.stationInfo
!.resetTime
!)
768 public saveOcppConfiguration (): void {
769 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
770 this.saveConfiguration()
774 public bufferMessage (message
: string): void {
775 this.messageBuffer
.add(message
)
776 this.setIntervalFlushMessageBuffer()
779 public openWSConnection (
781 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
784 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
785 ...this.stationInfo
?.wsOptions
,
788 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
789 if (!checkChargingStation(this, this.logPrefix())) {
792 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
793 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
795 if (params
.closeOpened
=== true) {
796 this.closeWSConnection()
798 if (params
.terminateOpened
=== true) {
799 this.terminateWSConnection()
802 if (this.isWebSocketConnectionOpened()) {
804 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
810 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
813 this.wsConnection
= new WebSocket(
814 this.wsConnectionUrl
,
815 `ocpp${this.stationInfo?.ocppVersion}`,
819 // Handle WebSocket message
820 this.wsConnection
.on('message', data
=> {
821 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
823 // Handle WebSocket error
824 this.wsConnection
.on('error', this.onError
.bind(this))
825 // Handle WebSocket close
826 this.wsConnection
.on('close', this.onClose
.bind(this))
827 // Handle WebSocket open
828 this.wsConnection
.on('open', () => {
829 this.onOpen().catch(error
=>
830 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
833 // Handle WebSocket ping
834 this.wsConnection
.on('ping', this.onPing
.bind(this))
835 // Handle WebSocket pong
836 this.wsConnection
.on('pong', this.onPong
.bind(this))
839 public closeWSConnection (): void {
840 if (this.isWebSocketConnectionOpened()) {
841 this.wsConnection
?.close()
842 this.wsConnection
= null
846 public getAutomaticTransactionGeneratorConfiguration ():
847 | AutomaticTransactionGeneratorConfiguration
849 if (this.automaticTransactionGeneratorConfiguration
== null) {
850 let automaticTransactionGeneratorConfiguration
:
851 | AutomaticTransactionGeneratorConfiguration
853 const stationTemplate
= this.getTemplateFromFile()
854 const stationConfiguration
= this.getConfigurationFromFile()
856 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
857 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
858 stationConfiguration
?.automaticTransactionGenerator
!= null
860 automaticTransactionGeneratorConfiguration
=
861 stationConfiguration
.automaticTransactionGenerator
863 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
865 this.automaticTransactionGeneratorConfiguration
= {
866 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
867 ...automaticTransactionGeneratorConfiguration
870 return this.automaticTransactionGeneratorConfiguration
873 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
874 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
877 public startAutomaticTransactionGenerator (
878 connectorIds
?: number[],
879 stopAbsoluteDuration
?: boolean
881 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
882 if (isNotEmptyArray(connectorIds
)) {
883 for (const connectorId
of connectorIds
) {
884 this.automaticTransactionGenerator
?.startConnector(connectorId
, stopAbsoluteDuration
)
887 this.automaticTransactionGenerator
?.start(stopAbsoluteDuration
)
889 this.saveAutomaticTransactionGeneratorConfiguration()
890 this.emit(ChargingStationEvents
.updated
)
893 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
894 if (isNotEmptyArray(connectorIds
)) {
895 for (const connectorId
of connectorIds
) {
896 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
899 this.automaticTransactionGenerator
?.stop()
901 this.saveAutomaticTransactionGeneratorConfiguration()
902 this.emit(ChargingStationEvents
.updated
)
905 public async stopTransactionOnConnector (
907 reason
?: StopTransactionReason
908 ): Promise
<StopTransactionResponse
> {
909 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
911 this.stationInfo
?.beginEndMeterValues
=== true &&
912 this.stationInfo
.ocppStrictCompliance
=== true &&
913 this.stationInfo
.outOfOrderEndMeterValues
=== false
915 const transactionEndMeterValue
= buildTransactionEndMeterValue(
918 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
920 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
922 RequestCommand
.METER_VALUES
,
926 meterValue
: [transactionEndMeterValue
]
930 return await this.ocppRequestService
.requestHandler
<
931 StopTransactionRequest
,
932 StopTransactionResponse
933 >(this, RequestCommand
.STOP_TRANSACTION
, {
935 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
936 ...(reason
!= null && { reason
})
940 public getReserveConnectorZeroSupported (): boolean {
941 return convertToBoolean(
942 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
943 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
947 public async addReservation (reservation
: Reservation
): Promise
<void> {
948 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
949 if (reservationFound
!= null) {
950 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
952 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
953 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
954 await sendAndSetConnectorStatus(
956 reservation
.connectorId
,
957 ConnectorStatusEnum
.Reserved
,
959 { send
: reservation
.connectorId
!== 0 }
963 public async removeReservation (
964 reservation
: Reservation
,
965 reason
: ReservationTerminationReason
967 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
968 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
970 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
971 case ReservationTerminationReason
.TRANSACTION_STARTED
:
972 delete connector
.reservation
974 case ReservationTerminationReason
.RESERVATION_CANCELED
:
975 case ReservationTerminationReason
.REPLACE_EXISTING
:
976 case ReservationTerminationReason
.EXPIRED
:
977 await sendAndSetConnectorStatus(
979 reservation
.connectorId
,
980 ConnectorStatusEnum
.Available
,
982 { send
: reservation
.connectorId
!== 0 }
984 delete connector
.reservation
987 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
988 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
992 public getReservationBy (
993 filterKey
: ReservationKey
,
994 value
: number | string
995 ): Reservation
| undefined {
997 for (const evseStatus
of this.evses
.values()) {
998 for (const connectorStatus
of evseStatus
.connectors
.values()) {
999 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1000 return connectorStatus
.reservation
1005 for (const connectorStatus
of this.connectors
.values()) {
1006 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1007 return connectorStatus
.reservation
1013 public isConnectorReservable (
1014 reservationId
: number,
1016 connectorId
?: number
1018 const reservation
= this.getReservationBy('reservationId', reservationId
)
1019 const reservationExists
= reservation
!== undefined && !hasReservationExpired(reservation
)
1020 if (arguments.length
=== 1) {
1021 return !reservationExists
1022 } else if (arguments.length
> 1) {
1023 const userReservation
=
1024 idTag
!== undefined ? this.getReservationBy('idTag', idTag
) : undefined
1025 const userReservationExists
=
1026 userReservation
!== undefined && !hasReservationExpired(userReservation
)
1027 const notConnectorZero
= connectorId
=== undefined ? true : connectorId
> 0
1028 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
1030 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1036 private setIntervalFlushMessageBuffer (): void {
1037 if (this.flushMessageBufferSetInterval
== null) {
1038 this.flushMessageBufferSetInterval
= setInterval(() => {
1039 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
1040 this.flushMessageBuffer()
1042 if (this.messageBuffer
.size
=== 0) {
1043 this.clearIntervalFlushMessageBuffer()
1045 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1049 private clearIntervalFlushMessageBuffer (): void {
1050 if (this.flushMessageBufferSetInterval
!= null) {
1051 clearInterval(this.flushMessageBufferSetInterval
)
1052 delete this.flushMessageBufferSetInterval
1056 private getNumberOfReservableConnectors (): number {
1057 let numberOfReservableConnectors
= 0
1058 if (this.hasEvses
) {
1059 for (const evseStatus
of this.evses
.values()) {
1060 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1063 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1065 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1068 private getNumberOfReservationsOnConnectorZero (): number {
1070 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1071 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1078 private flushMessageBuffer (): void {
1079 if (this.messageBuffer
.size
> 0) {
1080 for (const message
of this.messageBuffer
.values()) {
1081 let beginId
: string | undefined
1082 let commandName
: RequestCommand
| undefined
1083 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1084 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1086 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1087 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1089 this.wsConnection
?.send(message
, (error
?: Error) => {
1090 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1091 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1092 if (error
== null) {
1094 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1096 )} OCPP message sent '${JSON.stringify(message)}'`
1098 this.messageBuffer
.delete(message
)
1101 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1103 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1112 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1113 let template
: ChargingStationTemplate
| undefined
1115 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1116 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1118 const measureId
= `${FileType.ChargingStationTemplate} read`
1119 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1120 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1121 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1122 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1123 .update(JSON
.stringify(template
))
1125 this.sharedLRUCache
.setChargingStationTemplate(template
)
1126 this.templateFileHash
= template
.templateHash
1129 handleFileException(
1131 FileType
.ChargingStationTemplate
,
1132 error
as NodeJS
.ErrnoException
,
1139 private getStationInfoFromTemplate (): ChargingStationInfo
{
1140 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1141 const stationTemplate
= this.getTemplateFromFile()!
1142 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1143 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1144 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1145 if (stationTemplate
.Connectors
!= null) {
1146 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1148 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1149 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1150 stationInfo
.autoStart
= stationTemplate
.autoStart
?? true
1151 stationInfo
.templateName
= parse(this.templateFile
).name
1152 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1153 stationInfo
.ocppVersion
= stationTemplate
.ocppVersion
?? OCPPVersion
.VERSION_16
1154 createSerialNumber(stationTemplate
, stationInfo
)
1155 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1156 if (isNotEmptyArray(stationTemplate
.power
)) {
1157 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1158 stationInfo
.maximumPower
=
1159 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1160 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1161 : stationTemplate
.power
[powerArrayRandomIndex
]
1163 stationInfo
.maximumPower
=
1164 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1165 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1166 stationTemplate
.power
! * 1000
1167 : stationTemplate
.power
1169 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1170 stationInfo
.firmwareVersionPattern
=
1171 stationTemplate
.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
1173 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1174 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1177 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1179 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1182 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1189 stationTemplate
.firmwareUpgrade
?? {}
1191 stationInfo
.resetTime
=
1192 stationTemplate
.resetTime
!= null
1193 ? secondsToMilliseconds(stationTemplate
.resetTime
)
1194 : Constants
.DEFAULT_CHARGING_STATION_RESET_TIME
1198 private getStationInfoFromFile (
1199 stationInfoPersistentConfiguration
= true
1200 ): ChargingStationInfo
| undefined {
1201 let stationInfo
: ChargingStationInfo
| undefined
1202 if (stationInfoPersistentConfiguration
) {
1203 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1204 if (stationInfo
!= null) {
1205 delete stationInfo
.infoHash
1206 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1207 if (stationInfo
.templateName
== null) {
1208 stationInfo
.templateName
= parse(this.templateFile
).name
1210 if (stationInfo
.autoStart
== null) {
1211 stationInfo
.autoStart
= true
1218 private getStationInfo (): ChargingStationInfo
{
1219 const defaultStationInfo
= Constants
.DEFAULT_STATION_INFO
1220 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1221 const stationInfoFromFile
= this.getStationInfoFromFile(
1222 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1225 // 1. charging station info from template
1226 // 2. charging station info from configuration file
1228 stationInfoFromFile
!= null &&
1229 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1231 return { ...defaultStationInfo
, ...stationInfoFromFile
}
1233 stationInfoFromFile
!= null &&
1234 propagateSerialNumber(
1235 this.getTemplateFromFile(),
1236 stationInfoFromFile
,
1237 stationInfoFromTemplate
1239 return { ...defaultStationInfo
, ...stationInfoFromTemplate
}
1242 private saveStationInfo (): void {
1243 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1244 this.saveConfiguration()
1248 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1249 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1250 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1251 throw new BaseError(errorMsg
)
1254 private initialize (): void {
1255 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1256 const stationTemplate
= this.getTemplateFromFile()!
1257 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1258 this.configurationFile
= join(
1259 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1260 `${getHashId(this.index, stationTemplate)}.json`
1262 const stationConfiguration
= this.getConfigurationFromFile()
1264 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1265 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1267 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1268 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1270 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1272 this.stationInfo
= this.getStationInfo()
1274 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1275 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1276 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1278 const patternGroup
=
1279 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1280 this.stationInfo
.firmwareVersion
.split('.').length
1281 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1282 .exec(this.stationInfo
.firmwareVersion
)
1283 ?.slice(1, patternGroup
+ 1)
1284 if (match
!= null) {
1285 const patchLevelIndex
= match
.length
- 1
1286 match
[patchLevelIndex
] = (
1287 convertToInt(match
[patchLevelIndex
]) +
1288 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1289 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1291 this.stationInfo
.firmwareVersion
= match
.join('.')
1294 this.saveStationInfo()
1295 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1296 if (this.stationInfo
.enableStatistics
=== true) {
1297 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1298 this.stationInfo
.hashId
,
1299 this.stationInfo
.chargingStationId
,
1300 this.configuredSupervisionUrl
1303 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1304 if (bootNotificationRequest
== null) {
1305 const errorMsg
= 'Error while creating boot notification request'
1306 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1307 throw new BaseError(errorMsg
)
1309 this.bootNotificationRequest
= bootNotificationRequest
1310 this.powerDivider
= this.getPowerDivider()
1311 // OCPP configuration
1312 this.ocppConfiguration
= this.getOcppConfiguration()
1313 this.initializeOcppConfiguration()
1314 this.initializeOcppServices()
1315 if (this.stationInfo
.autoRegister
=== true) {
1316 this.bootNotificationResponse
= {
1317 currentTime
: new Date(),
1318 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1319 status: RegistrationStatusEnumType
.ACCEPTED
1324 private initializeOcppServices (): void {
1325 const ocppVersion
= this.stationInfo
?.ocppVersion
1326 switch (ocppVersion
) {
1327 case OCPPVersion
.VERSION_16
:
1328 this.ocppIncomingRequestService
=
1329 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1330 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1331 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1334 case OCPPVersion
.VERSION_20
:
1335 case OCPPVersion
.VERSION_201
:
1336 this.ocppIncomingRequestService
=
1337 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1338 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1339 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1343 this.handleUnsupportedVersion(ocppVersion
)
1348 private initializeOcppConfiguration (): void {
1349 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1350 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1352 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1353 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1356 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1357 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1358 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1360 addConfigurationKey(
1362 this.stationInfo
.supervisionUrlOcppKey
,
1363 this.configuredSupervisionUrl
.href
,
1367 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1368 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1369 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1371 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1374 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1375 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1377 addConfigurationKey(
1379 this.stationInfo
.amperageLimitationOcppKey
,
1381 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1382 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1385 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1386 addConfigurationKey(
1388 StandardParametersKey
.SupportedFeatureProfiles
,
1389 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1392 addConfigurationKey(
1394 StandardParametersKey
.NumberOfConnectors
,
1395 this.getNumberOfConnectors().toString(),
1399 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1400 addConfigurationKey(
1402 StandardParametersKey
.MeterValuesSampledData
,
1403 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1406 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1407 const connectorsPhaseRotation
: string[] = []
1408 if (this.hasEvses
) {
1409 for (const evseStatus
of this.evses
.values()) {
1410 for (const connectorId
of evseStatus
.connectors
.keys()) {
1411 connectorsPhaseRotation
.push(
1412 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1413 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1418 for (const connectorId
of this.connectors
.keys()) {
1419 connectorsPhaseRotation
.push(
1420 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1421 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1425 addConfigurationKey(
1427 StandardParametersKey
.ConnectorPhaseRotation
,
1428 connectorsPhaseRotation
.toString()
1431 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1432 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1435 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1436 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1438 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1440 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1441 addConfigurationKey(
1443 StandardParametersKey
.ConnectionTimeOut
,
1444 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1447 this.saveOcppConfiguration()
1450 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1451 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1452 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1453 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1455 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1456 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1457 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1458 delete evseStatus
.connectorsStatus
1459 this.evses
.set(evseId
, {
1460 ...(evseStatus
as EvseStatus
),
1461 connectors
: new Map
<number, ConnectorStatus
>(
1462 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1463 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1470 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1471 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1472 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1473 throw new BaseError(errorMsg
)
1475 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1476 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1477 throw new BaseError(errorMsg
)
1481 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1482 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1483 this.initializeConnectorsFromTemplate(stationTemplate
)
1484 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1485 this.initializeEvsesFromTemplate(stationTemplate
)
1486 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1487 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1488 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1489 throw new BaseError(errorMsg
)
1491 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1492 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1493 throw new BaseError(errorMsg
)
1497 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1498 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1499 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1500 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1501 throw new BaseError(errorMsg
)
1503 if (stationTemplate
.Connectors
?.[0] == null) {
1505 `${this.logPrefix()} Charging station information from template ${
1507 } with no connector id 0 configuration`
1510 if (stationTemplate
.Connectors
!= null) {
1511 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1512 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1513 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1515 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1518 const connectorsConfigChanged
=
1519 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1520 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1521 connectorsConfigChanged
&& this.connectors
.clear()
1522 this.connectorsConfigurationHash
= connectorsConfigHash
1523 if (templateMaxConnectors
> 0) {
1524 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1526 connectorId
=== 0 &&
1527 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1528 (stationTemplate
.Connectors
[connectorId
] == null ||
1529 !this.getUseConnectorId0(stationTemplate
))
1533 const templateConnectorId
=
1534 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1535 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1537 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1538 checkStationInfoConnectorStatus(
1539 templateConnectorId
,
1544 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1546 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1547 this.saveConnectorsStatus()
1550 `${this.logPrefix()} Charging station information from template ${
1552 } with no connectors configuration defined, cannot create connectors`
1558 `${this.logPrefix()} Charging station information from template ${
1560 } with no connectors configuration defined, using already defined connectors`
1565 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1566 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1567 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1568 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1569 throw new BaseError(errorMsg
)
1571 if (stationTemplate
.Evses
?.[0] == null) {
1573 `${this.logPrefix()} Charging station information from template ${
1575 } with no evse id 0 configuration`
1578 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1580 `${this.logPrefix()} Charging station information from template ${
1582 } with evse id 0 with no connector id 0 configuration`
1585 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1587 `${this.logPrefix()} Charging station information from template ${
1589 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1592 if (stationTemplate
.Evses
!= null) {
1593 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1594 .update(JSON
.stringify(stationTemplate
.Evses
))
1596 const evsesConfigChanged
=
1597 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1598 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1599 evsesConfigChanged
&& this.evses
.clear()
1600 this.evsesConfigurationHash
= evsesConfigHash
1601 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1602 if (templateMaxEvses
> 0) {
1603 for (const evseKey
in stationTemplate
.Evses
) {
1604 const evseId
= convertToInt(evseKey
)
1605 this.evses
.set(evseId
, {
1606 connectors
: buildConnectorsMap(
1607 stationTemplate
.Evses
[evseKey
].Connectors
,
1611 availability
: AvailabilityType
.Operative
1613 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1614 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1616 this.saveEvsesStatus()
1619 `${this.logPrefix()} Charging station information from template ${
1621 } with no evses configuration defined, cannot create evses`
1627 `${this.logPrefix()} Charging station information from template ${
1629 } with no evses configuration defined, using already defined evses`
1634 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1635 let configuration
: ChargingStationConfiguration
| undefined
1636 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1638 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1639 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1640 this.configurationFileHash
1643 const measureId
= `${FileType.ChargingStationConfiguration} read`
1644 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1645 configuration
= JSON
.parse(
1646 readFileSync(this.configurationFile
, 'utf8')
1647 ) as ChargingStationConfiguration
1648 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1649 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1650 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1651 this.configurationFileHash
= configuration
.configurationHash
!
1654 handleFileException(
1655 this.configurationFile
,
1656 FileType
.ChargingStationConfiguration
,
1657 error
as NodeJS
.ErrnoException
,
1662 return configuration
1665 private saveAutomaticTransactionGeneratorConfiguration (): void {
1666 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1667 this.saveConfiguration()
1671 private saveConnectorsStatus (): void {
1672 this.saveConfiguration()
1675 private saveEvsesStatus (): void {
1676 this.saveConfiguration()
1679 private saveConfiguration (): void {
1680 if (isNotEmptyString(this.configurationFile
)) {
1682 if (!existsSync(dirname(this.configurationFile
))) {
1683 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1685 const configurationFromFile
= this.getConfigurationFromFile()
1686 let configurationData
: ChargingStationConfiguration
=
1687 configurationFromFile
!= null
1688 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1690 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1691 configurationData
.stationInfo
= this.stationInfo
1693 delete configurationData
.stationInfo
1696 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1697 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1699 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1701 delete configurationData
.configurationKey
1703 configurationData
= merge
<ChargingStationConfiguration
>(
1705 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1708 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== false ||
1709 this.getAutomaticTransactionGeneratorConfiguration() == null
1711 delete configurationData
.automaticTransactionGenerator
1713 if (this.connectors
.size
> 0) {
1714 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1716 delete configurationData
.connectorsStatus
1718 if (this.evses
.size
> 0) {
1719 configurationData
.evsesStatus
= buildEvsesStatus(this)
1721 delete configurationData
.evsesStatus
1723 delete configurationData
.configurationHash
1724 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1727 stationInfo
: configurationData
.stationInfo
,
1728 configurationKey
: configurationData
.configurationKey
,
1729 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1730 ...(this.connectors
.size
> 0 && {
1731 connectorsStatus
: configurationData
.connectorsStatus
1733 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1734 } satisfies ChargingStationConfiguration
)
1737 if (this.configurationFileHash
!== configurationHash
) {
1738 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1739 configurationData
.configurationHash
= configurationHash
1740 const measureId
= `${FileType.ChargingStationConfiguration} write`
1741 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1743 this.configurationFile
,
1744 JSON
.stringify(configurationData
, undefined, 2),
1747 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1748 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1749 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1750 this.configurationFileHash
= configurationHash
1752 handleFileException(
1753 this.configurationFile
,
1754 FileType
.ChargingStationConfiguration
,
1755 error
as NodeJS
.ErrnoException
,
1761 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1762 this.configurationFile
1767 handleFileException(
1768 this.configurationFile
,
1769 FileType
.ChargingStationConfiguration
,
1770 error
as NodeJS
.ErrnoException
,
1776 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1781 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1782 return this.getTemplateFromFile()?.Configuration
1785 private getOcppConfigurationFromFile (): ChargingStationOcppConfiguration
| undefined {
1786 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1787 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1788 return { configurationKey
}
1793 private getOcppConfiguration (): ChargingStationOcppConfiguration
| undefined {
1794 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1795 this.getOcppConfigurationFromFile()
1796 if (ocppConfiguration
== null) {
1797 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1799 return ocppConfiguration
1802 private async onOpen (): Promise
<void> {
1803 if (this.isWebSocketConnectionOpened()) {
1805 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1807 let registrationRetryCount
= 0
1808 if (!this.isRegistered()) {
1809 // Send BootNotification
1811 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1812 BootNotificationRequest
,
1813 BootNotificationResponse
1814 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1815 skipBufferingOnError
: true
1817 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1818 if (this.bootNotificationResponse
?.currentTime
!= null) {
1819 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1820 this.bootNotificationResponse
.currentTime
= convertToDate(
1821 this.bootNotificationResponse
.currentTime
1824 if (!this.isRegistered()) {
1825 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1827 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1828 this.bootNotificationResponse
?.interval
!= null
1829 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1830 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1834 !this.isRegistered() &&
1835 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1836 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1837 this.stationInfo
?.registrationMaxRetries
=== -1)
1840 if (this.isRegistered()) {
1841 this.emit(ChargingStationEvents
.registered
)
1842 if (this.inAcceptedState()) {
1843 this.emit(ChargingStationEvents
.accepted
)
1846 if (this.inRejectedState()) {
1847 this.emit(ChargingStationEvents
.rejected
)
1850 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1851 this.stationInfo?.registrationMaxRetries
1855 this.wsConnectionRetryCount
= 0
1856 this.emit(ChargingStationEvents
.updated
)
1859 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1864 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1865 this.emit(ChargingStationEvents
.disconnected
)
1868 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1869 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1871 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1873 )}' and reason '${reason.toString()}'`
1875 this.wsConnectionRetryCount
= 0
1880 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1882 )}' and reason '${reason.toString()}'`
1885 this.reconnect().catch(error
=>
1886 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
1890 this.emit(ChargingStationEvents
.updated
)
1893 private getCachedRequest (
1894 messageType
: MessageType
| undefined,
1896 ): CachedRequest
| undefined {
1897 const cachedRequest
= this.requests
.get(messageId
)
1898 if (Array.isArray(cachedRequest
)) {
1899 return cachedRequest
1901 throw new OCPPError(
1902 ErrorType
.PROTOCOL_ERROR
,
1903 `Cached request for message id ${messageId} ${getMessageTypeString(
1905 )} is not an array`,
1911 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1912 const [messageType
, messageId
, commandName
, commandPayload
] = request
1913 if (this.stationInfo
?.enableStatistics
=== true) {
1914 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1917 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1921 // Process the message
1922 await this.ocppIncomingRequestService
.incomingRequestHandler(
1928 this.emit(ChargingStationEvents
.updated
)
1931 private handleResponseMessage (response
: Response
): void {
1932 const [messageType
, messageId
, commandPayload
] = response
1933 if (!this.requests
.has(messageId
)) {
1935 throw new OCPPError(
1936 ErrorType
.INTERNAL_ERROR
,
1937 `Response for unknown message id ${messageId}`,
1943 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1944 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1949 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1953 responseCallback(commandPayload
, requestPayload
)
1956 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1957 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1958 if (!this.requests
.has(messageId
)) {
1960 throw new OCPPError(
1961 ErrorType
.INTERNAL_ERROR
,
1962 `Error response for unknown message id ${messageId}`,
1964 { errorType
, errorMessage
, errorDetails
}
1967 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1968 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1970 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1974 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1977 private async onMessage (data
: RawData
): Promise
<void> {
1978 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1979 let messageType
: MessageType
| undefined
1980 let errorMsg
: string
1982 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1983 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
1984 if (Array.isArray(request
)) {
1985 [messageType
] = request
1986 // Check the type of message
1987 switch (messageType
) {
1989 case MessageType
.CALL_MESSAGE
:
1990 await this.handleIncomingMessage(request
as IncomingRequest
)
1993 case MessageType
.CALL_RESULT_MESSAGE
:
1994 this.handleResponseMessage(request
as Response
)
1997 case MessageType
.CALL_ERROR_MESSAGE
:
1998 this.handleErrorMessage(request
as ErrorResponse
)
2002 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2003 errorMsg
= `Wrong message type ${messageType}`
2004 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2005 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2008 throw new OCPPError(
2009 ErrorType
.PROTOCOL_ERROR
,
2010 'Incoming message is not an array',
2018 if (!Array.isArray(request
)) {
2019 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2022 let commandName
: IncomingRequestCommand
| undefined
2023 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2024 let errorCallback
: ErrorCallback
2025 const [, messageId
] = request
2026 switch (messageType
) {
2027 case MessageType
.CALL_MESSAGE
:
2028 [, , commandName
] = request
as IncomingRequest
2030 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2032 case MessageType
.CALL_RESULT_MESSAGE
:
2033 case MessageType
.CALL_ERROR_MESSAGE
:
2034 if (this.requests
.has(messageId
)) {
2035 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2036 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2037 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2038 errorCallback(error
as OCPPError
, false)
2040 // Remove the request from the cache in case of error at response handling
2041 this.requests
.delete(messageId
)
2045 if (!(error
instanceof OCPPError
)) {
2047 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2048 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2049 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2050 }' message '${data.toString()}' handling is not an OCPPError:`,
2055 `${this.logPrefix()} Incoming OCPP command '${
2056 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2057 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2058 }' message '${data.toString()}'${
2059 this.requests.has(messageId)
2060 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2062 } processing error:`,
2068 private onPing (): void {
2069 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2072 private onPong (): void {
2073 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2076 private onError (error
: WSError
): void {
2077 this.closeWSConnection()
2078 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2081 private getEnergyActiveImportRegister (
2082 connectorStatus
: ConnectorStatus
| undefined,
2085 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2088 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2089 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2091 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2096 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2097 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2099 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2103 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2104 return stationTemplate
?.useConnectorId0
?? true
2107 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2108 if (this.hasEvses
) {
2109 for (const [evseId
, evseStatus
] of this.evses
) {
2113 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2114 if (connectorStatus
.transactionStarted
=== true) {
2115 await this.stopTransactionOnConnector(connectorId
, reason
)
2120 for (const connectorId
of this.connectors
.keys()) {
2121 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2122 await this.stopTransactionOnConnector(connectorId
, reason
)
2129 private getConnectionTimeout (): number {
2130 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2131 return convertToInt(
2132 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2133 Constants
.DEFAULT_CONNECTION_TIMEOUT
2136 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2139 private getPowerDivider (): number {
2140 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2141 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2142 powerDivider
= this.getNumberOfRunningTransactions()
2147 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2148 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2149 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2150 switch (this.getCurrentOutType(stationInfo
)) {
2151 case CurrentType
.AC
:
2152 return ACElectricUtils
.amperagePerPhaseFromPower(
2153 this.getNumberOfPhases(stationInfo
),
2154 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2155 this.getVoltageOut(stationInfo
)
2157 case CurrentType
.DC
:
2158 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2162 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2163 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2164 return (stationInfo
?? this.stationInfo
!).currentOutType
?? CurrentType
.AC
2167 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2169 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2170 (stationInfo
?? this.stationInfo
!).voltageOut
??
2171 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2175 private getAmperageLimitation (): number | undefined {
2177 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2178 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2181 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2182 getAmperageLimitationUnitDivider(this.stationInfo
)
2187 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2188 if (this.stationInfo
?.autoRegister
=== true) {
2189 await this.ocppRequestService
.requestHandler
<
2190 BootNotificationRequest
,
2191 BootNotificationResponse
2192 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2193 skipBufferingOnError
: true
2196 // Start WebSocket ping
2197 this.startWebSocketPing()
2199 this.startHeartbeat()
2200 // Initialize connectors status
2201 if (this.hasEvses
) {
2202 for (const [evseId
, evseStatus
] of this.evses
) {
2204 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2205 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2206 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2211 for (const connectorId
of this.connectors
.keys()) {
2212 if (connectorId
> 0) {
2213 const connectorBootStatus
= getBootConnectorStatus(
2216 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2217 this.getConnectorStatus(connectorId
)!
2219 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2223 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2224 await this.ocppRequestService
.requestHandler
<
2225 FirmwareStatusNotificationRequest
,
2226 FirmwareStatusNotificationResponse
2227 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2228 status: FirmwareStatus
.Installed
2230 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2234 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2235 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2237 this.flushMessageBuffer()
2240 private internalStopMessageSequence (): void {
2241 // Stop WebSocket ping
2242 this.stopWebSocketPing()
2244 this.stopHeartbeat()
2246 if (this.automaticTransactionGenerator
?.started
=== true) {
2247 this.stopAutomaticTransactionGenerator()
2251 private async stopMessageSequence (
2252 reason
?: StopTransactionReason
,
2253 stopTransactions
?: boolean
2255 this.internalStopMessageSequence()
2256 // Stop ongoing transactions
2257 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2258 if (this.hasEvses
) {
2259 for (const [evseId
, evseStatus
] of this.evses
) {
2261 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2262 await sendAndSetConnectorStatus(
2265 ConnectorStatusEnum
.Unavailable
,
2268 delete connectorStatus
.status
2273 for (const connectorId
of this.connectors
.keys()) {
2274 if (connectorId
> 0) {
2275 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2276 delete this.getConnectorStatus(connectorId
)?.status
2282 private startWebSocketPing (): void {
2283 const webSocketPingInterval
=
2284 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2286 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2289 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2290 this.wsPingSetInterval
= setInterval(() => {
2291 if (this.isWebSocketConnectionOpened()) {
2292 this.wsConnection
?.ping()
2294 }, secondsToMilliseconds(webSocketPingInterval
))
2296 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2297 webSocketPingInterval
2300 } else if (this.wsPingSetInterval
!= null) {
2302 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2303 webSocketPingInterval
2308 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2313 private stopWebSocketPing (): void {
2314 if (this.wsPingSetInterval
!= null) {
2315 clearInterval(this.wsPingSetInterval
)
2316 delete this.wsPingSetInterval
2320 private getConfiguredSupervisionUrl (): URL
{
2321 let configuredSupervisionUrl
: string
2322 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2323 if (isNotEmptyArray(supervisionUrls
)) {
2324 let configuredSupervisionUrlIndex
: number
2325 switch (Configuration
.getSupervisionUrlDistribution()) {
2326 case SupervisionUrlDistribution
.RANDOM
:
2327 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2329 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2330 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2332 !Object.values(SupervisionUrlDistribution
).includes(
2333 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2334 Configuration
.getSupervisionUrlDistribution()!
2337 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2338 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2339 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2342 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2345 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2347 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2348 configuredSupervisionUrl
= supervisionUrls
!
2350 if (isNotEmptyString(configuredSupervisionUrl
)) {
2351 return new URL(configuredSupervisionUrl
)
2353 const errorMsg
= 'No supervision url(s) configured'
2354 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2355 throw new BaseError(errorMsg
)
2358 private stopHeartbeat (): void {
2359 if (this.heartbeatSetInterval
!= null) {
2360 clearInterval(this.heartbeatSetInterval
)
2361 delete this.heartbeatSetInterval
2365 private terminateWSConnection (): void {
2366 if (this.isWebSocketConnectionOpened()) {
2367 this.wsConnection
?.terminate()
2368 this.wsConnection
= null
2372 private async reconnect (): Promise
<void> {
2374 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2375 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2376 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2378 this.wsConnectionRetried
= true
2379 ++this.wsConnectionRetryCount
2380 const reconnectDelay
=
2381 this.stationInfo
?.reconnectExponentialDelay
=== true
2382 ? exponentialDelay(this.wsConnectionRetryCount
)
2383 : secondsToMilliseconds(this.getConnectionTimeout())
2384 const reconnectDelayWithdraw
= 1000
2385 const reconnectTimeout
=
2386 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2388 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2391 )}ms, timeout ${reconnectTimeout}ms`
2393 await sleep(reconnectDelay
)
2395 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2397 this.openWSConnection(
2399 handshakeTimeout
: reconnectTimeout
2401 { closeOpened
: true }
2403 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2405 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2406 this.wsConnectionRetryCount
2407 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`