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 ChargingStationTemplate
,
84 type EvseStatusConfiguration
,
87 type FirmwareStatusNotificationRequest
,
88 type FirmwareStatusNotificationResponse
,
90 type HeartbeatRequest
,
91 type HeartbeatResponse
,
93 type IncomingRequestCommand
,
96 type MeterValuesRequest
,
97 type MeterValuesResponse
,
101 RegistrationStatusEnumType
,
105 ReservationTerminationReason
,
107 StandardParametersKey
,
109 type StopTransactionReason
,
110 type StopTransactionRequest
,
111 type StopTransactionResponse
,
112 SupervisionUrlDistribution
,
113 SupportedFeatureProfiles
,
116 WebSocketCloseEventStatusCode
,
118 } from
'../types/index.js'
127 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
128 buildConnectorsStatus
,
138 formatDurationMilliSeconds
,
139 formatDurationSeconds
,
141 getWebSocketCloseEventStatusString
,
153 } from
'../utils/index.js'
155 export class ChargingStation
extends EventEmitter
{
156 public readonly index
: number
157 public readonly templateFile
: string
158 public stationInfo
?: ChargingStationInfo
159 public started
: boolean
160 public starting
: boolean
161 public idTagsCache
: IdTagsCache
162 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined
163 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined
164 public wsConnection
: WebSocket
| null
165 public readonly connectors
: Map
<number, ConnectorStatus
>
166 public readonly evses
: Map
<number, EvseStatus
>
167 public readonly requests
: Map
<string, CachedRequest
>
168 public performanceStatistics
!: PerformanceStatistics
| undefined
169 public heartbeatSetInterval
?: NodeJS
.Timeout
170 public ocppRequestService
!: OCPPRequestService
171 public bootNotificationRequest
?: BootNotificationRequest
172 public bootNotificationResponse
?: BootNotificationResponse
173 public powerDivider
?: number
174 private stopping
: boolean
175 private configurationFile
!: string
176 private configurationFileHash
!: string
177 private connectorsConfigurationHash
!: string
178 private evsesConfigurationHash
!: string
179 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
180 private ocppIncomingRequestService
!: OCPPIncomingRequestService
181 private readonly messageBuffer
: Set
<string>
182 private configuredSupervisionUrl
!: URL
183 private wsConnectionRetried
: boolean
184 private wsConnectionRetryCount
: number
185 private templateFileWatcher
!: FSWatcher
| undefined
186 private templateFileHash
!: string
187 private readonly sharedLRUCache
: SharedLRUCache
188 private wsPingSetInterval
?: NodeJS
.Timeout
189 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
190 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
192 constructor (index
: number, templateFile
: string) {
195 this.starting
= false
196 this.stopping
= false
197 this.wsConnection
= null
198 this.wsConnectionRetried
= false
199 this.wsConnectionRetryCount
= 0
201 this.templateFile
= templateFile
202 this.connectors
= new Map
<number, ConnectorStatus
>()
203 this.evses
= new Map
<number, EvseStatus
>()
204 this.requests
= new Map
<string, CachedRequest
>()
205 this.messageBuffer
= new Set
<string>()
206 this.sharedLRUCache
= SharedLRUCache
.getInstance()
207 this.idTagsCache
= IdTagsCache
.getInstance()
208 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this)
210 this.on(ChargingStationEvents
.added
, () => {
211 parentPort
?.postMessage(buildAddedMessage(this))
213 this.on(ChargingStationEvents
.started
, () => {
214 parentPort
?.postMessage(buildStartedMessage(this))
216 this.on(ChargingStationEvents
.stopped
, () => {
217 parentPort
?.postMessage(buildStoppedMessage(this))
219 this.on(ChargingStationEvents
.updated
, () => {
220 parentPort
?.postMessage(buildUpdatedMessage(this))
222 this.on(ChargingStationEvents
.accepted
, () => {
223 this.startMessageSequence(
224 this.wsConnectionRetried
226 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
228 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
230 this.wsConnectionRetried
= false
232 this.on(ChargingStationEvents
.rejected
, () => {
233 this.wsConnectionRetried
= false
235 this.on(ChargingStationEvents
.disconnected
, () => {
237 this.internalStopMessageSequence()
240 `${this.logPrefix()} Error while stopping the internal message sequence:`,
248 this.stationInfo
?.autoStart
=== true && this.start()
251 public get
hasEvses (): boolean {
252 return this.connectors
.size
=== 0 && this.evses
.size
> 0
255 private get
wsConnectionUrl (): URL
{
258 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
259 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
260 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
261 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
262 : this.configuredSupervisionUrl.href
263 }/${this.stationInfo?.chargingStationId}`
267 public logPrefix
= (): string => {
269 this instanceof ChargingStation
&&
270 this.stationInfo
!= null &&
271 isNotEmptyString(this.stationInfo
.chargingStationId
)
273 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
275 let stationTemplate
: ChargingStationTemplate
| undefined
277 stationTemplate
= JSON
.parse(
278 readFileSync(this.templateFile
, 'utf8')
279 ) as ChargingStationTemplate
281 stationTemplate
= undefined
283 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
286 public hasIdTags (): boolean {
287 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
288 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
291 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
292 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
293 const localStationInfo
= stationInfo
?? this.stationInfo
!
294 switch (this.getCurrentOutType(stationInfo
)) {
296 return localStationInfo
.numberOfPhases
?? 3
302 public isWebSocketConnectionOpened (): boolean {
303 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
306 public inUnknownState (): boolean {
307 return this.bootNotificationResponse
?.status == null
310 public inPendingState (): boolean {
311 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
314 public inAcceptedState (): boolean {
315 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
318 public inRejectedState (): boolean {
319 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
322 public isRegistered (): boolean {
323 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
326 public isChargingStationAvailable (): boolean {
327 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
330 public hasConnector (connectorId
: number): boolean {
332 for (const evseStatus
of this.evses
.values()) {
333 if (evseStatus
.connectors
.has(connectorId
)) {
339 return this.connectors
.has(connectorId
)
342 public isConnectorAvailable (connectorId
: number): boolean {
345 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
349 public getNumberOfConnectors (): number {
351 let numberOfConnectors
= 0
352 for (const [evseId
, evseStatus
] of this.evses
) {
354 numberOfConnectors
+= evseStatus
.connectors
.size
357 return numberOfConnectors
359 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
362 public getNumberOfEvses (): number {
363 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
366 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
368 for (const evseStatus
of this.evses
.values()) {
369 if (evseStatus
.connectors
.has(connectorId
)) {
370 return evseStatus
.connectors
.get(connectorId
)
375 return this.connectors
.get(connectorId
)
378 public getConnectorMaximumAvailablePower (connectorId
: number): number {
379 let connectorAmperageLimitationPowerLimit
: number | undefined
380 const amperageLimitation
= this.getAmperageLimitation()
382 amperageLimitation
!= null &&
383 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
384 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
386 connectorAmperageLimitationPowerLimit
=
387 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
388 ? ACElectricUtils
.powerTotal(
389 this.getNumberOfPhases(),
390 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
391 this.stationInfo
.voltageOut
!,
393 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
395 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
396 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
397 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
400 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
401 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
402 const connectorChargingProfilesPowerLimit
=
403 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
405 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
406 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
407 isNaN(connectorAmperageLimitationPowerLimit
!)
409 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
410 connectorAmperageLimitationPowerLimit
!,
411 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
412 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
416 public getTransactionIdTag (transactionId
: number): string | undefined {
418 for (const evseStatus
of this.evses
.values()) {
419 for (const connectorStatus
of evseStatus
.connectors
.values()) {
420 if (connectorStatus
.transactionId
=== transactionId
) {
421 return connectorStatus
.transactionIdTag
426 for (const connectorId
of this.connectors
.keys()) {
427 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
428 return this.getConnectorStatus(connectorId
)?.transactionIdTag
434 public getNumberOfRunningTransactions (): number {
435 let numberOfRunningTransactions
= 0
437 for (const [evseId
, evseStatus
] of this.evses
) {
441 for (const connectorStatus
of evseStatus
.connectors
.values()) {
442 if (connectorStatus
.transactionStarted
=== true) {
443 ++numberOfRunningTransactions
448 for (const connectorId
of this.connectors
.keys()) {
449 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
450 ++numberOfRunningTransactions
454 return numberOfRunningTransactions
457 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
458 if (transactionId
== null) {
460 } else if (this.hasEvses
) {
461 for (const evseStatus
of this.evses
.values()) {
462 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
463 if (connectorStatus
.transactionId
=== transactionId
) {
469 for (const connectorId
of this.connectors
.keys()) {
470 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
477 public getEnergyActiveImportRegisterByTransactionId (
478 transactionId
: number | undefined,
481 return this.getEnergyActiveImportRegister(
482 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
483 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
488 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
489 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
492 public getAuthorizeRemoteTxRequests (): boolean {
493 const authorizeRemoteTxRequests
= getConfigurationKey(
495 StandardParametersKey
.AuthorizeRemoteTxRequests
497 return authorizeRemoteTxRequests
!= null
498 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
502 public getLocalAuthListEnabled (): boolean {
503 const localAuthListEnabled
= getConfigurationKey(
505 StandardParametersKey
.LocalAuthListEnabled
507 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
510 public getHeartbeatInterval (): number {
511 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
512 if (HeartbeatInterval
!= null) {
513 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
515 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
516 if (HeartBeatInterval
!= null) {
517 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
519 this.stationInfo
?.autoRegister
=== false &&
521 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
522 Constants.DEFAULT_HEARTBEAT_INTERVAL
525 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
528 public setSupervisionUrl (url
: string): void {
530 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
531 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
533 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
535 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
536 this.stationInfo
!.supervisionUrls
= url
537 this.saveStationInfo()
538 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
542 public startHeartbeat (): void {
543 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
544 this.heartbeatSetInterval
= setInterval(() => {
545 this.ocppRequestService
546 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
549 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
553 }, this.getHeartbeatInterval())
555 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
556 this.getHeartbeatInterval()
559 } else if (this.heartbeatSetInterval
!= null) {
561 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
562 this.getHeartbeatInterval()
567 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
572 public restartHeartbeat (): void {
576 this.startHeartbeat()
579 public restartWebSocketPing (): void {
580 // Stop WebSocket ping
581 this.stopWebSocketPing()
582 // Start WebSocket ping
583 this.startWebSocketPing()
586 public startMeterValues (connectorId
: number, interval
: number): void {
587 if (connectorId
=== 0) {
588 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
591 const connectorStatus
= this.getConnectorStatus(connectorId
)
592 if (connectorStatus
== null) {
594 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
599 if (connectorStatus
.transactionStarted
=== false) {
601 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
605 connectorStatus
.transactionStarted
=== true &&
606 connectorStatus
.transactionId
== null
609 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
614 connectorStatus
.transactionSetInterval
= setInterval(() => {
615 const meterValue
= buildMeterValue(
618 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
619 connectorStatus
.transactionId
!,
622 this.ocppRequestService
623 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
625 RequestCommand
.METER_VALUES
,
628 transactionId
: connectorStatus
.transactionId
,
629 meterValue
: [meterValue
]
634 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
641 `${this.logPrefix()} Charging station ${
642 StandardParametersKey.MeterValueSampleInterval
643 } configuration set to ${interval}, not sending MeterValues`
648 public stopMeterValues (connectorId
: number): void {
649 const connectorStatus
= this.getConnectorStatus(connectorId
)
650 if (connectorStatus
?.transactionSetInterval
!= null) {
651 clearInterval(connectorStatus
.transactionSetInterval
)
655 public add (): void {
656 this.emit(ChargingStationEvents
.added
)
659 public start (): void {
661 if (!this.starting
) {
663 if (this.stationInfo
?.enableStatistics
=== true) {
664 this.performanceStatistics
?.start()
666 this.openWSConnection()
667 // Monitor charging station template file
668 this.templateFileWatcher
= watchJsonFile(
670 FileType
.ChargingStationTemplate
,
673 (event
, filename
): void => {
674 if (isNotEmptyString(filename
) && event
=== 'change') {
677 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
679 } file have changed, reload`
681 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
684 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
685 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
687 const ATGStarted
= this.automaticTransactionGenerator
?.started
688 if (ATGStarted
=== true) {
689 this.stopAutomaticTransactionGenerator()
691 delete this.automaticTransactionGeneratorConfiguration
693 this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true &&
696 this.startAutomaticTransactionGenerator(undefined, true)
698 if (this.stationInfo
?.enableStatistics
=== true) {
699 this.performanceStatistics
?.restart()
701 this.performanceStatistics
?.stop()
703 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
706 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
714 this.emit(ChargingStationEvents
.started
)
715 this.starting
= false
717 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
720 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
725 reason
?: StopTransactionReason
,
726 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
729 if (!this.stopping
) {
731 await this.stopMessageSequence(reason
, stopTransactions
)
732 this.closeWSConnection()
733 if (this.stationInfo
?.enableStatistics
=== true) {
734 this.performanceStatistics
?.stop()
736 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
737 this.templateFileWatcher
?.close()
738 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
739 delete this.bootNotificationResponse
741 this.saveConfiguration()
742 this.emit(ChargingStationEvents
.stopped
)
743 this.stopping
= false
745 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
748 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
752 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
753 await this.stop(reason
)
754 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
755 await sleep(this.stationInfo
!.resetTime
!)
760 public saveOcppConfiguration (): void {
761 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
762 this.saveConfiguration()
766 public bufferMessage (message
: string): void {
767 this.messageBuffer
.add(message
)
768 this.setIntervalFlushMessageBuffer()
771 public openWSConnection (
773 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
776 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
777 ...this.stationInfo
?.wsOptions
,
780 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
781 if (!checkChargingStation(this, this.logPrefix())) {
784 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
785 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
787 if (params
.closeOpened
=== true) {
788 this.closeWSConnection()
790 if (params
.terminateOpened
=== true) {
791 this.terminateWSConnection()
794 if (this.isWebSocketConnectionOpened()) {
796 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
802 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
805 this.wsConnection
= new WebSocket(
806 this.wsConnectionUrl
,
807 `ocpp${this.stationInfo?.ocppVersion}`,
811 // Handle WebSocket message
812 this.wsConnection
.on('message', data
=> {
813 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
815 // Handle WebSocket error
816 this.wsConnection
.on('error', this.onError
.bind(this))
817 // Handle WebSocket close
818 this.wsConnection
.on('close', this.onClose
.bind(this))
819 // Handle WebSocket open
820 this.wsConnection
.on('open', () => {
821 this.onOpen().catch(error
=>
822 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
825 // Handle WebSocket ping
826 this.wsConnection
.on('ping', this.onPing
.bind(this))
827 // Handle WebSocket pong
828 this.wsConnection
.on('pong', this.onPong
.bind(this))
831 public closeWSConnection (): void {
832 if (this.isWebSocketConnectionOpened()) {
833 this.wsConnection
?.close()
834 this.wsConnection
= null
838 public getAutomaticTransactionGeneratorConfiguration ():
839 | AutomaticTransactionGeneratorConfiguration
841 if (this.automaticTransactionGeneratorConfiguration
== null) {
842 let automaticTransactionGeneratorConfiguration
:
843 | AutomaticTransactionGeneratorConfiguration
845 const stationTemplate
= this.getTemplateFromFile()
846 const stationConfiguration
= this.getConfigurationFromFile()
848 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
849 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
850 stationConfiguration
?.automaticTransactionGenerator
!= null
852 automaticTransactionGeneratorConfiguration
=
853 stationConfiguration
.automaticTransactionGenerator
855 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
857 this.automaticTransactionGeneratorConfiguration
= {
858 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
859 ...automaticTransactionGeneratorConfiguration
862 return this.automaticTransactionGeneratorConfiguration
865 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
866 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
869 public startAutomaticTransactionGenerator (
870 connectorIds
?: number[],
871 stopAbsoluteDuration
?: boolean
873 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
874 if (isNotEmptyArray(connectorIds
)) {
875 for (const connectorId
of connectorIds
) {
876 this.automaticTransactionGenerator
?.startConnector(connectorId
, stopAbsoluteDuration
)
879 this.automaticTransactionGenerator
?.start(stopAbsoluteDuration
)
881 this.saveAutomaticTransactionGeneratorConfiguration()
882 this.emit(ChargingStationEvents
.updated
)
885 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
886 if (isNotEmptyArray(connectorIds
)) {
887 for (const connectorId
of connectorIds
) {
888 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
891 this.automaticTransactionGenerator
?.stop()
893 this.saveAutomaticTransactionGeneratorConfiguration()
894 this.emit(ChargingStationEvents
.updated
)
897 public async stopTransactionOnConnector (
899 reason
?: StopTransactionReason
900 ): Promise
<StopTransactionResponse
> {
901 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
903 this.stationInfo
?.beginEndMeterValues
=== true &&
904 this.stationInfo
.ocppStrictCompliance
=== true &&
905 this.stationInfo
.outOfOrderEndMeterValues
=== false
907 const transactionEndMeterValue
= buildTransactionEndMeterValue(
910 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
912 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
914 RequestCommand
.METER_VALUES
,
918 meterValue
: [transactionEndMeterValue
]
922 return await this.ocppRequestService
.requestHandler
<
923 StopTransactionRequest
,
924 StopTransactionResponse
925 >(this, RequestCommand
.STOP_TRANSACTION
, {
927 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
928 ...(reason
!= null && { reason
})
932 public getReserveConnectorZeroSupported (): boolean {
933 return convertToBoolean(
934 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
935 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
939 public async addReservation (reservation
: Reservation
): Promise
<void> {
940 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
941 if (reservationFound
!= null) {
942 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
944 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
945 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
946 await sendAndSetConnectorStatus(
948 reservation
.connectorId
,
949 ConnectorStatusEnum
.Reserved
,
951 { send
: reservation
.connectorId
!== 0 }
955 public async removeReservation (
956 reservation
: Reservation
,
957 reason
: ReservationTerminationReason
959 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
960 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
962 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
963 case ReservationTerminationReason
.TRANSACTION_STARTED
:
964 delete connector
.reservation
966 case ReservationTerminationReason
.RESERVATION_CANCELED
:
967 case ReservationTerminationReason
.REPLACE_EXISTING
:
968 case ReservationTerminationReason
.EXPIRED
:
969 await sendAndSetConnectorStatus(
971 reservation
.connectorId
,
972 ConnectorStatusEnum
.Available
,
974 { send
: reservation
.connectorId
!== 0 }
976 delete connector
.reservation
979 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
980 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
984 public getReservationBy (
985 filterKey
: ReservationKey
,
986 value
: number | string
987 ): Reservation
| undefined {
989 for (const evseStatus
of this.evses
.values()) {
990 for (const connectorStatus
of evseStatus
.connectors
.values()) {
991 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
992 return connectorStatus
.reservation
997 for (const connectorStatus
of this.connectors
.values()) {
998 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
999 return connectorStatus
.reservation
1005 public isConnectorReservable (
1006 reservationId
: number,
1008 connectorId
?: number
1010 const reservation
= this.getReservationBy('reservationId', reservationId
)
1011 const reservationExists
= reservation
!== undefined && !hasReservationExpired(reservation
)
1012 if (arguments.length
=== 1) {
1013 return !reservationExists
1014 } else if (arguments.length
> 1) {
1015 const userReservation
=
1016 idTag
!== undefined ? this.getReservationBy('idTag', idTag
) : undefined
1017 const userReservationExists
=
1018 userReservation
!== undefined && !hasReservationExpired(userReservation
)
1019 const notConnectorZero
= connectorId
=== undefined ? true : connectorId
> 0
1020 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
1022 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1028 private setIntervalFlushMessageBuffer (): void {
1029 if (this.flushMessageBufferSetInterval
== null) {
1030 this.flushMessageBufferSetInterval
= setInterval(() => {
1031 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
1032 this.flushMessageBuffer()
1034 if (this.messageBuffer
.size
=== 0) {
1035 this.clearIntervalFlushMessageBuffer()
1037 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1041 private clearIntervalFlushMessageBuffer (): void {
1042 if (this.flushMessageBufferSetInterval
!= null) {
1043 clearInterval(this.flushMessageBufferSetInterval
)
1044 delete this.flushMessageBufferSetInterval
1048 private getNumberOfReservableConnectors (): number {
1049 let numberOfReservableConnectors
= 0
1050 if (this.hasEvses
) {
1051 for (const evseStatus
of this.evses
.values()) {
1052 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1055 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1057 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1060 private getNumberOfReservationsOnConnectorZero (): number {
1062 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1063 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1070 private flushMessageBuffer (): void {
1071 if (this.messageBuffer
.size
> 0) {
1072 for (const message
of this.messageBuffer
.values()) {
1073 let beginId
: string | undefined
1074 let commandName
: RequestCommand
| undefined
1075 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1076 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1078 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1079 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1081 this.wsConnection
?.send(message
, (error
?: Error) => {
1082 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1083 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1084 if (error
== null) {
1086 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1088 )} OCPP message sent '${JSON.stringify(message)}'`
1090 this.messageBuffer
.delete(message
)
1093 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1095 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1104 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1105 let template
: ChargingStationTemplate
| undefined
1107 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1108 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1110 const measureId
= `${FileType.ChargingStationTemplate} read`
1111 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1112 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1113 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1114 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1115 .update(JSON
.stringify(template
))
1117 this.sharedLRUCache
.setChargingStationTemplate(template
)
1118 this.templateFileHash
= template
.templateHash
1121 handleFileException(
1123 FileType
.ChargingStationTemplate
,
1124 error
as NodeJS
.ErrnoException
,
1131 private getStationInfoFromTemplate (): ChargingStationInfo
{
1132 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1133 const stationTemplate
= this.getTemplateFromFile()!
1134 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1135 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1136 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1137 if (stationTemplate
.Connectors
!= null) {
1138 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1140 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1141 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1142 stationInfo
.autoStart
= stationTemplate
.autoStart
?? true
1143 stationInfo
.templateName
= parse(this.templateFile
).name
1144 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1145 stationInfo
.ocppVersion
= stationTemplate
.ocppVersion
?? OCPPVersion
.VERSION_16
1146 createSerialNumber(stationTemplate
, stationInfo
)
1147 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1148 if (isNotEmptyArray(stationTemplate
.power
)) {
1149 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1150 stationInfo
.maximumPower
=
1151 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1152 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1153 : stationTemplate
.power
[powerArrayRandomIndex
]
1155 stationInfo
.maximumPower
=
1156 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1157 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1158 stationTemplate
.power
! * 1000
1159 : stationTemplate
.power
1161 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1162 stationInfo
.firmwareVersionPattern
=
1163 stationTemplate
.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
1165 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1166 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1169 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1171 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1174 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1181 stationTemplate
.firmwareUpgrade
?? {}
1183 stationInfo
.resetTime
=
1184 stationTemplate
.resetTime
!= null
1185 ? secondsToMilliseconds(stationTemplate
.resetTime
)
1186 : Constants
.DEFAULT_CHARGING_STATION_RESET_TIME
1190 private getStationInfoFromFile (
1191 stationInfoPersistentConfiguration
= true
1192 ): ChargingStationInfo
| undefined {
1193 let stationInfo
: ChargingStationInfo
| undefined
1194 if (stationInfoPersistentConfiguration
) {
1195 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1196 if (stationInfo
!= null) {
1197 delete stationInfo
.infoHash
1198 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1199 if (stationInfo
.templateName
== null) {
1200 stationInfo
.templateName
= parse(this.templateFile
).name
1202 if (stationInfo
.autoStart
== null) {
1203 stationInfo
.autoStart
= true
1210 private getStationInfo (): ChargingStationInfo
{
1211 const defaultStationInfo
= Constants
.DEFAULT_STATION_INFO
1212 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1213 const stationInfoFromFile
= this.getStationInfoFromFile(
1214 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1217 // 1. charging station info from template
1218 // 2. charging station info from configuration file
1220 stationInfoFromFile
!= null &&
1221 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1223 return { ...defaultStationInfo
, ...stationInfoFromFile
}
1225 stationInfoFromFile
!= null &&
1226 propagateSerialNumber(
1227 this.getTemplateFromFile(),
1228 stationInfoFromFile
,
1229 stationInfoFromTemplate
1231 return { ...defaultStationInfo
, ...stationInfoFromTemplate
}
1234 private saveStationInfo (): void {
1235 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1236 this.saveConfiguration()
1240 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1241 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1242 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1243 throw new BaseError(errorMsg
)
1246 private initialize (): void {
1247 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1248 const stationTemplate
= this.getTemplateFromFile()!
1249 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1250 this.configurationFile
= join(
1251 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1252 `${getHashId(this.index, stationTemplate)}.json`
1254 const stationConfiguration
= this.getConfigurationFromFile()
1256 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1257 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1259 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1260 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1262 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1264 this.stationInfo
= this.getStationInfo()
1266 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1267 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1268 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1270 const patternGroup
=
1271 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1272 this.stationInfo
.firmwareVersion
.split('.').length
1273 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1274 .exec(this.stationInfo
.firmwareVersion
)
1275 ?.slice(1, patternGroup
+ 1)
1276 if (match
!= null) {
1277 const patchLevelIndex
= match
.length
- 1
1278 match
[patchLevelIndex
] = (
1279 convertToInt(match
[patchLevelIndex
]) +
1280 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1281 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1283 this.stationInfo
.firmwareVersion
= match
.join('.')
1286 this.saveStationInfo()
1287 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1288 if (this.stationInfo
.enableStatistics
=== true) {
1289 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1290 this.stationInfo
.hashId
,
1291 this.stationInfo
.chargingStationId
,
1292 this.configuredSupervisionUrl
1295 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1296 if (bootNotificationRequest
== null) {
1297 const errorMsg
= 'Error while creating boot notification request'
1298 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1299 throw new BaseError(errorMsg
)
1301 this.bootNotificationRequest
= bootNotificationRequest
1302 this.powerDivider
= this.getPowerDivider()
1303 // OCPP configuration
1304 this.ocppConfiguration
= this.getOcppConfiguration()
1305 this.initializeOcppConfiguration()
1306 this.initializeOcppServices()
1307 if (this.stationInfo
.autoRegister
=== true) {
1308 this.bootNotificationResponse
= {
1309 currentTime
: new Date(),
1310 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1311 status: RegistrationStatusEnumType
.ACCEPTED
1316 private initializeOcppServices (): void {
1317 const ocppVersion
= this.stationInfo
?.ocppVersion
1318 switch (ocppVersion
) {
1319 case OCPPVersion
.VERSION_16
:
1320 this.ocppIncomingRequestService
=
1321 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1322 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1323 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1326 case OCPPVersion
.VERSION_20
:
1327 case OCPPVersion
.VERSION_201
:
1328 this.ocppIncomingRequestService
=
1329 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1330 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1331 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1335 this.handleUnsupportedVersion(ocppVersion
)
1340 private initializeOcppConfiguration (): void {
1341 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1342 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1344 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1345 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1348 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1349 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1350 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1352 addConfigurationKey(
1354 this.stationInfo
.supervisionUrlOcppKey
,
1355 this.configuredSupervisionUrl
.href
,
1359 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1360 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1361 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1363 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1366 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1367 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1369 addConfigurationKey(
1371 this.stationInfo
.amperageLimitationOcppKey
,
1373 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1374 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1377 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1378 addConfigurationKey(
1380 StandardParametersKey
.SupportedFeatureProfiles
,
1381 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1384 addConfigurationKey(
1386 StandardParametersKey
.NumberOfConnectors
,
1387 this.getNumberOfConnectors().toString(),
1391 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1392 addConfigurationKey(
1394 StandardParametersKey
.MeterValuesSampledData
,
1395 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1398 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1399 const connectorsPhaseRotation
: string[] = []
1400 if (this.hasEvses
) {
1401 for (const evseStatus
of this.evses
.values()) {
1402 for (const connectorId
of evseStatus
.connectors
.keys()) {
1403 connectorsPhaseRotation
.push(
1404 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1405 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1410 for (const connectorId
of this.connectors
.keys()) {
1411 connectorsPhaseRotation
.push(
1412 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1413 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1417 addConfigurationKey(
1419 StandardParametersKey
.ConnectorPhaseRotation
,
1420 connectorsPhaseRotation
.toString()
1423 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1424 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1427 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1428 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1430 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1432 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1433 addConfigurationKey(
1435 StandardParametersKey
.ConnectionTimeOut
,
1436 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1439 this.saveOcppConfiguration()
1442 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1443 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1444 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1445 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1447 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1448 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1449 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1450 delete evseStatus
.connectorsStatus
1451 this.evses
.set(evseId
, {
1452 ...(evseStatus
as EvseStatus
),
1453 connectors
: new Map
<number, ConnectorStatus
>(
1454 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1455 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1462 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1463 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1464 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1465 throw new BaseError(errorMsg
)
1467 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1468 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1469 throw new BaseError(errorMsg
)
1473 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1474 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1475 this.initializeConnectorsFromTemplate(stationTemplate
)
1476 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1477 this.initializeEvsesFromTemplate(stationTemplate
)
1478 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1479 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1480 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1481 throw new BaseError(errorMsg
)
1483 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1484 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1485 throw new BaseError(errorMsg
)
1489 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1490 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1491 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1492 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1493 throw new BaseError(errorMsg
)
1495 if (stationTemplate
.Connectors
?.[0] == null) {
1497 `${this.logPrefix()} Charging station information from template ${
1499 } with no connector id 0 configuration`
1502 if (stationTemplate
.Connectors
!= null) {
1503 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1504 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1505 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1507 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1510 const connectorsConfigChanged
=
1511 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1512 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1513 connectorsConfigChanged
&& this.connectors
.clear()
1514 this.connectorsConfigurationHash
= connectorsConfigHash
1515 if (templateMaxConnectors
> 0) {
1516 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1518 connectorId
=== 0 &&
1519 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1520 (stationTemplate
.Connectors
[connectorId
] == null ||
1521 !this.getUseConnectorId0(stationTemplate
))
1525 const templateConnectorId
=
1526 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1527 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1529 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1530 checkStationInfoConnectorStatus(
1531 templateConnectorId
,
1536 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1538 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1539 this.saveConnectorsStatus()
1542 `${this.logPrefix()} Charging station information from template ${
1544 } with no connectors configuration defined, cannot create connectors`
1550 `${this.logPrefix()} Charging station information from template ${
1552 } with no connectors configuration defined, using already defined connectors`
1557 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1558 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1559 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1560 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1561 throw new BaseError(errorMsg
)
1563 if (stationTemplate
.Evses
?.[0] == null) {
1565 `${this.logPrefix()} Charging station information from template ${
1567 } with no evse id 0 configuration`
1570 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1572 `${this.logPrefix()} Charging station information from template ${
1574 } with evse id 0 with no connector id 0 configuration`
1577 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1579 `${this.logPrefix()} Charging station information from template ${
1581 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1584 if (stationTemplate
.Evses
!= null) {
1585 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1586 .update(JSON
.stringify(stationTemplate
.Evses
))
1588 const evsesConfigChanged
=
1589 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1590 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1591 evsesConfigChanged
&& this.evses
.clear()
1592 this.evsesConfigurationHash
= evsesConfigHash
1593 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1594 if (templateMaxEvses
> 0) {
1595 for (const evseKey
in stationTemplate
.Evses
) {
1596 const evseId
= convertToInt(evseKey
)
1597 this.evses
.set(evseId
, {
1598 connectors
: buildConnectorsMap(
1599 stationTemplate
.Evses
[evseKey
].Connectors
,
1603 availability
: AvailabilityType
.Operative
1605 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1606 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1608 this.saveEvsesStatus()
1611 `${this.logPrefix()} Charging station information from template ${
1613 } with no evses configuration defined, cannot create evses`
1619 `${this.logPrefix()} Charging station information from template ${
1621 } with no evses configuration defined, using already defined evses`
1626 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1627 let configuration
: ChargingStationConfiguration
| undefined
1628 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1630 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1631 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1632 this.configurationFileHash
1635 const measureId
= `${FileType.ChargingStationConfiguration} read`
1636 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1637 configuration
= JSON
.parse(
1638 readFileSync(this.configurationFile
, 'utf8')
1639 ) as ChargingStationConfiguration
1640 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1641 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1642 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1643 this.configurationFileHash
= configuration
.configurationHash
!
1646 handleFileException(
1647 this.configurationFile
,
1648 FileType
.ChargingStationConfiguration
,
1649 error
as NodeJS
.ErrnoException
,
1654 return configuration
1657 private saveAutomaticTransactionGeneratorConfiguration (): void {
1658 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1659 this.saveConfiguration()
1663 private saveConnectorsStatus (): void {
1664 this.saveConfiguration()
1667 private saveEvsesStatus (): void {
1668 this.saveConfiguration()
1671 private saveConfiguration (): void {
1672 if (isNotEmptyString(this.configurationFile
)) {
1674 if (!existsSync(dirname(this.configurationFile
))) {
1675 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1677 const configurationFromFile
= this.getConfigurationFromFile()
1678 let configurationData
: ChargingStationConfiguration
=
1679 configurationFromFile
!= null
1680 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1682 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1683 configurationData
.stationInfo
= this.stationInfo
1685 delete configurationData
.stationInfo
1688 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1689 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1691 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1693 delete configurationData
.configurationKey
1695 configurationData
= merge
<ChargingStationConfiguration
>(
1697 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1700 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== false ||
1701 this.getAutomaticTransactionGeneratorConfiguration() == null
1703 delete configurationData
.automaticTransactionGenerator
1705 if (this.connectors
.size
> 0) {
1706 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1708 delete configurationData
.connectorsStatus
1710 if (this.evses
.size
> 0) {
1711 configurationData
.evsesStatus
= buildEvsesStatus(this)
1713 delete configurationData
.evsesStatus
1715 delete configurationData
.configurationHash
1716 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1719 stationInfo
: configurationData
.stationInfo
,
1720 configurationKey
: configurationData
.configurationKey
,
1721 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1722 ...(this.connectors
.size
> 0 && {
1723 connectorsStatus
: configurationData
.connectorsStatus
1725 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1726 } satisfies ChargingStationConfiguration
)
1729 if (this.configurationFileHash
!== configurationHash
) {
1730 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1731 configurationData
.configurationHash
= configurationHash
1732 const measureId
= `${FileType.ChargingStationConfiguration} write`
1733 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1735 this.configurationFile
,
1736 JSON
.stringify(configurationData
, undefined, 2),
1739 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1740 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1741 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1742 this.configurationFileHash
= configurationHash
1744 handleFileException(
1745 this.configurationFile
,
1746 FileType
.ChargingStationConfiguration
,
1747 error
as NodeJS
.ErrnoException
,
1753 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1754 this.configurationFile
1759 handleFileException(
1760 this.configurationFile
,
1761 FileType
.ChargingStationConfiguration
,
1762 error
as NodeJS
.ErrnoException
,
1768 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1773 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1774 return this.getTemplateFromFile()?.Configuration
1777 private getOcppConfigurationFromFile (): ChargingStationOcppConfiguration
| undefined {
1778 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1779 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1780 return { configurationKey
}
1785 private getOcppConfiguration (): ChargingStationOcppConfiguration
| undefined {
1786 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1787 this.getOcppConfigurationFromFile()
1788 if (ocppConfiguration
== null) {
1789 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1791 return ocppConfiguration
1794 private async onOpen (): Promise
<void> {
1795 if (this.isWebSocketConnectionOpened()) {
1797 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1799 let registrationRetryCount
= 0
1800 if (!this.isRegistered()) {
1801 // Send BootNotification
1803 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1804 BootNotificationRequest
,
1805 BootNotificationResponse
1806 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1807 skipBufferingOnError
: true
1809 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1810 if (this.bootNotificationResponse
?.currentTime
!= null) {
1811 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1812 this.bootNotificationResponse
.currentTime
= convertToDate(
1813 this.bootNotificationResponse
.currentTime
1816 if (!this.isRegistered()) {
1817 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1819 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1820 this.bootNotificationResponse
?.interval
!= null
1821 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1822 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1826 !this.isRegistered() &&
1827 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1828 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1829 this.stationInfo
?.registrationMaxRetries
=== -1)
1832 if (this.isRegistered()) {
1833 this.emit(ChargingStationEvents
.registered
)
1834 if (this.inAcceptedState()) {
1835 this.emit(ChargingStationEvents
.accepted
)
1838 if (this.inRejectedState()) {
1839 this.emit(ChargingStationEvents
.rejected
)
1842 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1843 this.stationInfo?.registrationMaxRetries
1847 this.wsConnectionRetryCount
= 0
1848 this.emit(ChargingStationEvents
.updated
)
1851 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1856 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1857 this.emit(ChargingStationEvents
.disconnected
)
1860 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1861 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1863 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1865 )}' and reason '${reason.toString()}'`
1867 this.wsConnectionRetryCount
= 0
1872 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1874 )}' and reason '${reason.toString()}'`
1877 this.reconnect().catch(error
=>
1878 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
1882 this.emit(ChargingStationEvents
.updated
)
1885 private getCachedRequest (
1886 messageType
: MessageType
| undefined,
1888 ): CachedRequest
| undefined {
1889 const cachedRequest
= this.requests
.get(messageId
)
1890 if (Array.isArray(cachedRequest
)) {
1891 return cachedRequest
1893 throw new OCPPError(
1894 ErrorType
.PROTOCOL_ERROR
,
1895 `Cached request for message id ${messageId} ${getMessageTypeString(
1897 )} is not an array`,
1903 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1904 const [messageType
, messageId
, commandName
, commandPayload
] = request
1905 if (this.stationInfo
?.enableStatistics
=== true) {
1906 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1909 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1913 // Process the message
1914 await this.ocppIncomingRequestService
.incomingRequestHandler(
1920 this.emit(ChargingStationEvents
.updated
)
1923 private handleResponseMessage (response
: Response
): void {
1924 const [messageType
, messageId
, commandPayload
] = response
1925 if (!this.requests
.has(messageId
)) {
1927 throw new OCPPError(
1928 ErrorType
.INTERNAL_ERROR
,
1929 `Response for unknown message id ${messageId}`,
1935 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1936 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1941 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1945 responseCallback(commandPayload
, requestPayload
)
1948 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1949 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1950 if (!this.requests
.has(messageId
)) {
1952 throw new OCPPError(
1953 ErrorType
.INTERNAL_ERROR
,
1954 `Error response for unknown message id ${messageId}`,
1956 { errorType
, errorMessage
, errorDetails
}
1959 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1960 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1962 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1966 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1969 private async onMessage (data
: RawData
): Promise
<void> {
1970 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1971 let messageType
: MessageType
| undefined
1972 let errorMsg
: string
1974 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1975 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
1976 if (Array.isArray(request
)) {
1977 [messageType
] = request
1978 // Check the type of message
1979 switch (messageType
) {
1981 case MessageType
.CALL_MESSAGE
:
1982 await this.handleIncomingMessage(request
as IncomingRequest
)
1985 case MessageType
.CALL_RESULT_MESSAGE
:
1986 this.handleResponseMessage(request
as Response
)
1989 case MessageType
.CALL_ERROR_MESSAGE
:
1990 this.handleErrorMessage(request
as ErrorResponse
)
1994 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1995 errorMsg
= `Wrong message type ${messageType}`
1996 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1997 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2000 throw new OCPPError(
2001 ErrorType
.PROTOCOL_ERROR
,
2002 'Incoming message is not an array',
2010 if (!Array.isArray(request
)) {
2011 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2014 let commandName
: IncomingRequestCommand
| undefined
2015 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2016 let errorCallback
: ErrorCallback
2017 const [, messageId
] = request
2018 switch (messageType
) {
2019 case MessageType
.CALL_MESSAGE
:
2020 [, , commandName
] = request
as IncomingRequest
2022 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2024 case MessageType
.CALL_RESULT_MESSAGE
:
2025 case MessageType
.CALL_ERROR_MESSAGE
:
2026 if (this.requests
.has(messageId
)) {
2027 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2028 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2029 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2030 errorCallback(error
as OCPPError
, false)
2032 // Remove the request from the cache in case of error at response handling
2033 this.requests
.delete(messageId
)
2037 if (!(error
instanceof OCPPError
)) {
2039 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2040 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2041 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2042 }' message '${data.toString()}' handling is not an OCPPError:`,
2047 `${this.logPrefix()} Incoming OCPP command '${
2048 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2049 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2050 }' message '${data.toString()}'${
2051 this.requests.has(messageId)
2052 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2054 } processing error:`,
2060 private onPing (): void {
2061 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2064 private onPong (): void {
2065 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2068 private onError (error
: WSError
): void {
2069 this.closeWSConnection()
2070 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2073 private getEnergyActiveImportRegister (
2074 connectorStatus
: ConnectorStatus
| undefined,
2077 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2080 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2081 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2083 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2088 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2089 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2091 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2095 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2096 return stationTemplate
?.useConnectorId0
?? true
2099 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2100 if (this.hasEvses
) {
2101 for (const [evseId
, evseStatus
] of this.evses
) {
2105 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2106 if (connectorStatus
.transactionStarted
=== true) {
2107 await this.stopTransactionOnConnector(connectorId
, reason
)
2112 for (const connectorId
of this.connectors
.keys()) {
2113 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2114 await this.stopTransactionOnConnector(connectorId
, reason
)
2121 private getConnectionTimeout (): number {
2122 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2123 return convertToInt(
2124 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2125 Constants
.DEFAULT_CONNECTION_TIMEOUT
2128 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2131 private getPowerDivider (): number {
2132 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2133 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2134 powerDivider
= this.getNumberOfRunningTransactions()
2139 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2140 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2141 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2142 switch (this.getCurrentOutType(stationInfo
)) {
2143 case CurrentType
.AC
:
2144 return ACElectricUtils
.amperagePerPhaseFromPower(
2145 this.getNumberOfPhases(stationInfo
),
2146 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2147 this.getVoltageOut(stationInfo
)
2149 case CurrentType
.DC
:
2150 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2154 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2155 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2156 return (stationInfo
?? this.stationInfo
!).currentOutType
?? CurrentType
.AC
2159 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2161 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2162 (stationInfo
?? this.stationInfo
!).voltageOut
??
2163 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2167 private getAmperageLimitation (): number | undefined {
2169 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2170 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2173 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2174 getAmperageLimitationUnitDivider(this.stationInfo
)
2179 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2180 if (this.stationInfo
?.autoRegister
=== true) {
2181 await this.ocppRequestService
.requestHandler
<
2182 BootNotificationRequest
,
2183 BootNotificationResponse
2184 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2185 skipBufferingOnError
: true
2188 // Start WebSocket ping
2189 this.startWebSocketPing()
2191 this.startHeartbeat()
2192 // Initialize connectors status
2193 if (this.hasEvses
) {
2194 for (const [evseId
, evseStatus
] of this.evses
) {
2196 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2197 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2198 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2203 for (const connectorId
of this.connectors
.keys()) {
2204 if (connectorId
> 0) {
2205 const connectorBootStatus
= getBootConnectorStatus(
2208 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2209 this.getConnectorStatus(connectorId
)!
2211 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2215 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2216 await this.ocppRequestService
.requestHandler
<
2217 FirmwareStatusNotificationRequest
,
2218 FirmwareStatusNotificationResponse
2219 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2220 status: FirmwareStatus
.Installed
2222 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2226 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2227 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2229 this.flushMessageBuffer()
2232 private internalStopMessageSequence (): void {
2233 // Stop WebSocket ping
2234 this.stopWebSocketPing()
2236 this.stopHeartbeat()
2238 if (this.automaticTransactionGenerator
?.started
=== true) {
2239 this.stopAutomaticTransactionGenerator()
2243 private async stopMessageSequence (
2244 reason
?: StopTransactionReason
,
2245 stopTransactions
?: boolean
2247 this.internalStopMessageSequence()
2248 // Stop ongoing transactions
2249 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2250 if (this.hasEvses
) {
2251 for (const [evseId
, evseStatus
] of this.evses
) {
2253 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2254 await sendAndSetConnectorStatus(
2257 ConnectorStatusEnum
.Unavailable
,
2260 delete connectorStatus
.status
2265 for (const connectorId
of this.connectors
.keys()) {
2266 if (connectorId
> 0) {
2267 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2268 delete this.getConnectorStatus(connectorId
)?.status
2274 private startWebSocketPing (): void {
2275 const webSocketPingInterval
=
2276 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2278 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2281 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2282 this.wsPingSetInterval
= setInterval(() => {
2283 if (this.isWebSocketConnectionOpened()) {
2284 this.wsConnection
?.ping()
2286 }, secondsToMilliseconds(webSocketPingInterval
))
2288 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2289 webSocketPingInterval
2292 } else if (this.wsPingSetInterval
!= null) {
2294 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2295 webSocketPingInterval
2300 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2305 private stopWebSocketPing (): void {
2306 if (this.wsPingSetInterval
!= null) {
2307 clearInterval(this.wsPingSetInterval
)
2308 delete this.wsPingSetInterval
2312 private getConfiguredSupervisionUrl (): URL
{
2313 let configuredSupervisionUrl
: string
2314 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2315 if (isNotEmptyArray(supervisionUrls
)) {
2316 let configuredSupervisionUrlIndex
: number
2317 switch (Configuration
.getSupervisionUrlDistribution()) {
2318 case SupervisionUrlDistribution
.RANDOM
:
2319 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2321 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2322 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2324 !Object.values(SupervisionUrlDistribution
).includes(
2325 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2326 Configuration
.getSupervisionUrlDistribution()!
2329 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2330 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2331 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2334 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2337 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2339 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2340 configuredSupervisionUrl
= supervisionUrls
!
2342 if (isNotEmptyString(configuredSupervisionUrl
)) {
2343 return new URL(configuredSupervisionUrl
)
2345 const errorMsg
= 'No supervision url(s) configured'
2346 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2347 throw new BaseError(errorMsg
)
2350 private stopHeartbeat (): void {
2351 if (this.heartbeatSetInterval
!= null) {
2352 clearInterval(this.heartbeatSetInterval
)
2353 delete this.heartbeatSetInterval
2357 private terminateWSConnection (): void {
2358 if (this.isWebSocketConnectionOpened()) {
2359 this.wsConnection
?.terminate()
2360 this.wsConnection
= null
2364 private async reconnect (): Promise
<void> {
2366 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2367 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2368 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2370 this.wsConnectionRetried
= true
2371 ++this.wsConnectionRetryCount
2372 const reconnectDelay
=
2373 this.stationInfo
?.reconnectExponentialDelay
=== true
2374 ? exponentialDelay(this.wsConnectionRetryCount
)
2375 : secondsToMilliseconds(this.getConnectionTimeout())
2376 const reconnectDelayWithdraw
= 1000
2377 const reconnectTimeout
=
2378 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2380 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2383 )}ms, timeout ${reconnectTimeout}ms`
2385 await sleep(reconnectDelay
)
2387 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2389 this.openWSConnection(
2391 handshakeTimeout
: reconnectTimeout
2393 { closeOpened
: true }
2395 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2397 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2398 this.wsConnectionRetryCount
2399 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`