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
} 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 buildStatusNotificationRequest
,
60 buildTransactionEndMeterValue
,
62 sendAndSetConnectorStatus
63 } from
'./ocpp/index.js'
64 import { SharedLRUCache
} from
'./SharedLRUCache.js'
65 import { BaseError
, OCPPError
} from
'../exception/index.js'
66 import { PerformanceStatistics
} from
'../performance/index.js'
68 type AutomaticTransactionGeneratorConfiguration
,
70 type BootNotificationRequest
,
71 type BootNotificationResponse
,
73 type ChargingStationConfiguration
,
74 ChargingStationEvents
,
75 type ChargingStationInfo
,
76 type ChargingStationOcppConfiguration
,
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 StatusNotificationRequest
,
111 type StatusNotificationResponse
,
112 type StopTransactionReason
,
113 type StopTransactionRequest
,
114 type StopTransactionResponse
,
115 SupervisionUrlDistribution
,
116 SupportedFeatureProfiles
,
119 WebSocketCloseEventStatusCode
,
121 } from
'../types/index.js'
129 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
130 buildConnectorsStatus
,
140 formatDurationMilliSeconds
,
141 formatDurationSeconds
,
143 getWebSocketCloseEventStatusString
,
155 } from
'../utils/index.js'
157 export class ChargingStation
extends EventEmitter
{
158 public readonly index
: number
159 public readonly templateFile
: string
160 public stationInfo
?: ChargingStationInfo
161 public started
: boolean
162 public starting
: boolean
163 public idTagsCache
: IdTagsCache
164 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined
165 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined
166 public wsConnection
: WebSocket
| null
167 public readonly connectors
: Map
<number, ConnectorStatus
>
168 public readonly evses
: Map
<number, EvseStatus
>
169 public readonly requests
: Map
<string, CachedRequest
>
170 public performanceStatistics
!: PerformanceStatistics
| undefined
171 public heartbeatSetInterval
?: NodeJS
.Timeout
172 public ocppRequestService
!: OCPPRequestService
173 public bootNotificationRequest
?: BootNotificationRequest
174 public bootNotificationResponse
?: BootNotificationResponse
175 public powerDivider
?: number
176 private stopping
: boolean
177 private configurationFile
!: string
178 private configurationFileHash
!: string
179 private connectorsConfigurationHash
!: string
180 private evsesConfigurationHash
!: string
181 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
182 private ocppIncomingRequestService
!: OCPPIncomingRequestService
183 private readonly messageBuffer
: Set
<string>
184 private configuredSupervisionUrl
!: URL
185 private autoReconnectRetryCount
: number
186 private templateFileWatcher
!: FSWatcher
| undefined
187 private templateFileHash
!: string
188 private readonly sharedLRUCache
: SharedLRUCache
189 private webSocketPingSetInterval
?: NodeJS
.Timeout
190 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
191 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
193 constructor (index
: number, templateFile
: string) {
196 this.starting
= false
197 this.stopping
= false
198 this.wsConnection
= null
199 this.autoReconnectRetryCount
= 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
.started
, () => {
211 parentPort
?.postMessage(buildStartedMessage(this))
213 this.on(ChargingStationEvents
.stopped
, () => {
214 parentPort
?.postMessage(buildStoppedMessage(this))
216 this.on(ChargingStationEvents
.updated
, () => {
217 parentPort
?.postMessage(buildUpdatedMessage(this))
223 public get
hasEvses (): boolean {
224 return this.connectors
.size
=== 0 && this.evses
.size
> 0
227 private get
wsConnectionUrl (): URL
{
230 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
231 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
232 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
233 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
234 : this.configuredSupervisionUrl.href
235 }/${this.stationInfo?.chargingStationId}`
239 public logPrefix
= (): string => {
241 this instanceof ChargingStation
&&
242 this.stationInfo
!= null &&
243 isNotEmptyString(this.stationInfo
.chargingStationId
)
245 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
247 let stationTemplate
: ChargingStationTemplate
| undefined
249 stationTemplate
= JSON
.parse(
250 readFileSync(this.templateFile
, 'utf8')
251 ) as ChargingStationTemplate
253 stationTemplate
= undefined
255 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
258 public hasIdTags (): boolean {
259 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
260 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
263 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
264 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
265 const localStationInfo
= stationInfo
?? this.stationInfo
!
266 switch (this.getCurrentOutType(stationInfo
)) {
268 return localStationInfo
.numberOfPhases
?? 3
274 public isWebSocketConnectionOpened (): boolean {
275 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
278 public inUnknownState (): boolean {
279 return this.bootNotificationResponse
?.status == null
282 public inPendingState (): boolean {
283 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
286 public inAcceptedState (): boolean {
287 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
290 public inRejectedState (): boolean {
291 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
294 public isRegistered (): boolean {
295 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
298 public isChargingStationAvailable (): boolean {
299 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
302 public hasConnector (connectorId
: number): boolean {
304 for (const evseStatus
of this.evses
.values()) {
305 if (evseStatus
.connectors
.has(connectorId
)) {
311 return this.connectors
.has(connectorId
)
314 public isConnectorAvailable (connectorId
: number): boolean {
317 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
321 public getNumberOfConnectors (): number {
323 let numberOfConnectors
= 0
324 for (const [evseId
, evseStatus
] of this.evses
) {
326 numberOfConnectors
+= evseStatus
.connectors
.size
329 return numberOfConnectors
331 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
334 public getNumberOfEvses (): number {
335 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
338 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
340 for (const evseStatus
of this.evses
.values()) {
341 if (evseStatus
.connectors
.has(connectorId
)) {
342 return evseStatus
.connectors
.get(connectorId
)
347 return this.connectors
.get(connectorId
)
350 public getConnectorMaximumAvailablePower (connectorId
: number): number {
351 let connectorAmperageLimitationPowerLimit
: number | undefined
352 const amperageLimitation
= this.getAmperageLimitation()
354 amperageLimitation
!= null &&
355 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
356 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
358 connectorAmperageLimitationPowerLimit
=
359 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
360 ? ACElectricUtils
.powerTotal(
361 this.getNumberOfPhases(),
362 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
363 this.stationInfo
.voltageOut
!,
365 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
367 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
369 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
372 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
373 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
374 const connectorChargingProfilesPowerLimit
=
375 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
377 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
378 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
379 isNaN(connectorAmperageLimitationPowerLimit
!)
381 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
382 connectorAmperageLimitationPowerLimit
!,
383 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
384 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
388 public getTransactionIdTag (transactionId
: number): string | undefined {
390 for (const evseStatus
of this.evses
.values()) {
391 for (const connectorStatus
of evseStatus
.connectors
.values()) {
392 if (connectorStatus
.transactionId
=== transactionId
) {
393 return connectorStatus
.transactionIdTag
398 for (const connectorId
of this.connectors
.keys()) {
399 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
400 return this.getConnectorStatus(connectorId
)?.transactionIdTag
406 public getNumberOfRunningTransactions (): number {
407 let numberOfRunningTransactions
= 0
409 for (const [evseId
, evseStatus
] of this.evses
) {
413 for (const connectorStatus
of evseStatus
.connectors
.values()) {
414 if (connectorStatus
.transactionStarted
=== true) {
415 ++numberOfRunningTransactions
420 for (const connectorId
of this.connectors
.keys()) {
421 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
422 ++numberOfRunningTransactions
426 return numberOfRunningTransactions
429 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
430 if (transactionId
== null) {
432 } else if (this.hasEvses
) {
433 for (const evseStatus
of this.evses
.values()) {
434 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
435 if (connectorStatus
.transactionId
=== transactionId
) {
441 for (const connectorId
of this.connectors
.keys()) {
442 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
449 public getEnergyActiveImportRegisterByTransactionId (
450 transactionId
: number | undefined,
453 return this.getEnergyActiveImportRegister(
454 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
455 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
460 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
461 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
464 public getAuthorizeRemoteTxRequests (): boolean {
465 const authorizeRemoteTxRequests
= getConfigurationKey(
467 StandardParametersKey
.AuthorizeRemoteTxRequests
469 return authorizeRemoteTxRequests
!= null
470 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
474 public getLocalAuthListEnabled (): boolean {
475 const localAuthListEnabled
= getConfigurationKey(
477 StandardParametersKey
.LocalAuthListEnabled
479 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
482 public getHeartbeatInterval (): number {
483 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
484 if (HeartbeatInterval
!= null) {
485 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
487 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
488 if (HeartBeatInterval
!= null) {
489 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
491 this.stationInfo
?.autoRegister
=== false &&
493 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
494 Constants.DEFAULT_HEARTBEAT_INTERVAL
497 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
500 public setSupervisionUrl (url
: string): void {
502 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
503 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
505 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
507 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
508 this.stationInfo
!.supervisionUrls
= url
509 this.saveStationInfo()
510 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
514 public startHeartbeat (): void {
515 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
516 this.heartbeatSetInterval
= setInterval(() => {
517 this.ocppRequestService
518 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
521 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
525 }, this.getHeartbeatInterval())
527 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
528 this.getHeartbeatInterval()
531 } else if (this.heartbeatSetInterval
!= null) {
533 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
534 this.getHeartbeatInterval()
539 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
544 public restartHeartbeat (): void {
548 this.startHeartbeat()
551 public restartWebSocketPing (): void {
552 // Stop WebSocket ping
553 this.stopWebSocketPing()
554 // Start WebSocket ping
555 this.startWebSocketPing()
558 public startMeterValues (connectorId
: number, interval
: number): void {
559 if (connectorId
=== 0) {
560 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
563 const connectorStatus
= this.getConnectorStatus(connectorId
)
564 if (connectorStatus
== null) {
566 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
571 if (connectorStatus
.transactionStarted
=== false) {
573 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
577 connectorStatus
.transactionStarted
=== true &&
578 connectorStatus
.transactionId
== null
581 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
586 connectorStatus
.transactionSetInterval
= setInterval(() => {
587 const meterValue
= buildMeterValue(
590 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
591 connectorStatus
.transactionId
!,
594 this.ocppRequestService
595 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
597 RequestCommand
.METER_VALUES
,
600 transactionId
: connectorStatus
.transactionId
,
601 meterValue
: [meterValue
]
606 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
613 `${this.logPrefix()} Charging station ${
614 StandardParametersKey.MeterValueSampleInterval
615 } configuration set to ${interval}, not sending MeterValues`
620 public stopMeterValues (connectorId
: number): void {
621 const connectorStatus
= this.getConnectorStatus(connectorId
)
622 if (connectorStatus
?.transactionSetInterval
!= null) {
623 clearInterval(connectorStatus
.transactionSetInterval
)
627 public start (): void {
629 if (!this.starting
) {
631 if (this.stationInfo
?.enableStatistics
=== true) {
632 this.performanceStatistics
?.start()
634 this.openWSConnection()
635 // Monitor charging station template file
636 this.templateFileWatcher
= watchJsonFile(
638 FileType
.ChargingStationTemplate
,
641 (event
, filename
): void => {
642 if (isNotEmptyString(filename
) && event
=== 'change') {
645 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
647 } file have changed, reload`
649 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
652 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
653 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
655 this.stopAutomaticTransactionGenerator()
656 delete this.automaticTransactionGeneratorConfiguration
657 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
658 this.startAutomaticTransactionGenerator()
660 if (this.stationInfo
?.enableStatistics
=== true) {
661 this.performanceStatistics
?.restart()
663 this.performanceStatistics
?.stop()
665 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
668 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
676 this.emit(ChargingStationEvents
.started
)
677 this.starting
= false
679 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
682 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
686 public async stop (reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
688 if (!this.stopping
) {
690 await this.stopMessageSequence(reason
, stopTransactions
)
691 this.closeWSConnection()
692 if (this.stationInfo
?.enableStatistics
=== true) {
693 this.performanceStatistics
?.stop()
695 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
696 this.templateFileWatcher
?.close()
697 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
698 delete this.bootNotificationResponse
700 this.saveConfiguration()
701 this.emit(ChargingStationEvents
.stopped
)
702 this.stopping
= false
704 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
707 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
711 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
712 await this.stop(reason
)
713 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
714 await sleep(this.stationInfo
!.resetTime
!)
719 public saveOcppConfiguration (): void {
720 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
721 this.saveConfiguration()
725 public bufferMessage (message
: string): void {
726 this.messageBuffer
.add(message
)
727 this.setIntervalFlushMessageBuffer()
730 public openWSConnection (
732 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
735 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
736 ...this.stationInfo
?.wsOptions
,
739 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
740 if (!checkChargingStation(this, this.logPrefix())) {
743 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
744 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
746 if (params
.closeOpened
=== true) {
747 this.closeWSConnection()
749 if (params
.terminateOpened
=== true) {
750 this.terminateWSConnection()
753 if (this.isWebSocketConnectionOpened()) {
755 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
761 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
764 this.wsConnection
= new WebSocket(
765 this.wsConnectionUrl
,
766 `ocpp${this.stationInfo?.ocppVersion}`,
770 // Handle WebSocket message
771 this.wsConnection
.on(
773 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
775 // Handle WebSocket error
776 this.wsConnection
.on(
778 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
780 // Handle WebSocket close
781 this.wsConnection
.on(
783 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
785 // Handle WebSocket open
786 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void)
787 // Handle WebSocket ping
788 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void)
789 // Handle WebSocket pong
790 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void)
793 public closeWSConnection (): void {
794 if (this.isWebSocketConnectionOpened()) {
795 this.wsConnection
?.close()
796 this.wsConnection
= null
800 public getAutomaticTransactionGeneratorConfiguration ():
801 | AutomaticTransactionGeneratorConfiguration
803 if (this.automaticTransactionGeneratorConfiguration
== null) {
804 let automaticTransactionGeneratorConfiguration
:
805 | AutomaticTransactionGeneratorConfiguration
807 const stationTemplate
= this.getTemplateFromFile()
808 const stationConfiguration
= this.getConfigurationFromFile()
810 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
811 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
812 stationConfiguration
?.automaticTransactionGenerator
!= null
814 automaticTransactionGeneratorConfiguration
=
815 stationConfiguration
.automaticTransactionGenerator
817 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
819 this.automaticTransactionGeneratorConfiguration
= {
820 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
821 ...automaticTransactionGeneratorConfiguration
824 return this.automaticTransactionGeneratorConfiguration
827 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
828 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
831 public startAutomaticTransactionGenerator (connectorIds
?: number[]): void {
832 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
833 if (isNotEmptyArray(connectorIds
)) {
834 for (const connectorId
of connectorIds
) {
835 this.automaticTransactionGenerator
?.startConnector(connectorId
)
838 this.automaticTransactionGenerator
?.start()
840 this.saveAutomaticTransactionGeneratorConfiguration()
841 this.emit(ChargingStationEvents
.updated
)
844 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
845 if (isNotEmptyArray(connectorIds
)) {
846 for (const connectorId
of connectorIds
) {
847 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
850 this.automaticTransactionGenerator
?.stop()
852 this.saveAutomaticTransactionGeneratorConfiguration()
853 this.emit(ChargingStationEvents
.updated
)
856 public async stopTransactionOnConnector (
858 reason
?: StopTransactionReason
859 ): Promise
<StopTransactionResponse
> {
860 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
862 this.stationInfo
?.beginEndMeterValues
=== true &&
863 this.stationInfo
.ocppStrictCompliance
=== true &&
864 this.stationInfo
.outOfOrderEndMeterValues
=== false
866 const transactionEndMeterValue
= buildTransactionEndMeterValue(
869 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
871 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
873 RequestCommand
.METER_VALUES
,
877 meterValue
: [transactionEndMeterValue
]
881 return await this.ocppRequestService
.requestHandler
<
882 StopTransactionRequest
,
883 StopTransactionResponse
884 >(this, RequestCommand
.STOP_TRANSACTION
, {
886 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
887 ...(reason
!= null && { reason
})
891 public getReserveConnectorZeroSupported (): boolean {
892 return convertToBoolean(
893 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
894 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
898 public async addReservation (reservation
: Reservation
): Promise
<void> {
899 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
900 if (reservationFound
!= null) {
901 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
903 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
904 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
905 await sendAndSetConnectorStatus(
907 reservation
.connectorId
,
908 ConnectorStatusEnum
.Reserved
,
910 { send
: reservation
.connectorId
!== 0 }
914 public async removeReservation (
915 reservation
: Reservation
,
916 reason
: ReservationTerminationReason
918 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
919 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
921 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
922 case ReservationTerminationReason
.TRANSACTION_STARTED
:
923 delete connector
.reservation
925 case ReservationTerminationReason
.RESERVATION_CANCELED
:
926 case ReservationTerminationReason
.REPLACE_EXISTING
:
927 case ReservationTerminationReason
.EXPIRED
:
928 await sendAndSetConnectorStatus(
930 reservation
.connectorId
,
931 ConnectorStatusEnum
.Available
,
933 { send
: reservation
.connectorId
!== 0 }
935 delete connector
.reservation
938 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
939 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
943 public getReservationBy (
944 filterKey
: ReservationKey
,
945 value
: number | string
946 ): Reservation
| undefined {
948 for (const evseStatus
of this.evses
.values()) {
949 for (const connectorStatus
of evseStatus
.connectors
.values()) {
950 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
951 return connectorStatus
.reservation
956 for (const connectorStatus
of this.connectors
.values()) {
957 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
958 return connectorStatus
.reservation
964 public isConnectorReservable (
965 reservationId
: number,
969 const reservation
= this.getReservationBy('reservationId', reservationId
)
970 const reservationExists
= reservation
!== undefined && !hasReservationExpired(reservation
)
971 if (arguments.length
=== 1) {
972 return !reservationExists
973 } else if (arguments.length
> 1) {
974 const userReservation
=
975 idTag
!== undefined ? this.getReservationBy('idTag', idTag
) : undefined
976 const userReservationExists
=
977 userReservation
!== undefined && !hasReservationExpired(userReservation
)
978 const notConnectorZero
= connectorId
=== undefined ? true : connectorId
> 0
979 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
981 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
987 private setIntervalFlushMessageBuffer (): void {
988 if (this.flushMessageBufferSetInterval
== null) {
989 this.flushMessageBufferSetInterval
= setInterval(() => {
990 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
991 this.flushMessageBuffer()
993 if (this.messageBuffer
.size
=== 0) {
994 this.clearIntervalFlushMessageBuffer()
996 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1000 private clearIntervalFlushMessageBuffer (): void {
1001 if (this.flushMessageBufferSetInterval
!= null) {
1002 clearInterval(this.flushMessageBufferSetInterval
)
1003 delete this.flushMessageBufferSetInterval
1007 private getNumberOfReservableConnectors (): number {
1008 let numberOfReservableConnectors
= 0
1009 if (this.hasEvses
) {
1010 for (const evseStatus
of this.evses
.values()) {
1011 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1014 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1016 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1019 private getNumberOfReservationsOnConnectorZero (): number {
1021 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1022 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1029 private flushMessageBuffer (): void {
1030 if (this.messageBuffer
.size
> 0) {
1031 for (const message
of this.messageBuffer
.values()) {
1032 let beginId
: string | undefined
1033 let commandName
: RequestCommand
| undefined
1034 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1035 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1037 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1038 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1040 this.wsConnection
?.send(message
, (error
?: Error) => {
1041 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1042 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1043 if (error
== null) {
1045 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1047 )} OCPP message sent '${JSON.stringify(message)}'`
1049 this.messageBuffer
.delete(message
)
1052 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1054 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1063 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1064 let template
: ChargingStationTemplate
| undefined
1066 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1067 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1069 const measureId
= `${FileType.ChargingStationTemplate} read`
1070 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1071 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1072 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1073 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1074 .update(JSON
.stringify(template
))
1076 this.sharedLRUCache
.setChargingStationTemplate(template
)
1077 this.templateFileHash
= template
.templateHash
1080 handleFileException(
1082 FileType
.ChargingStationTemplate
,
1083 error
as NodeJS
.ErrnoException
,
1090 private getStationInfoFromTemplate (): ChargingStationInfo
{
1091 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1092 const stationTemplate
= this.getTemplateFromFile()!
1093 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1094 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1095 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1096 if (stationTemplate
.Connectors
!= null) {
1097 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1099 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1100 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1101 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1102 stationInfo
.ocppVersion
= stationTemplate
.ocppVersion
?? OCPPVersion
.VERSION_16
1103 createSerialNumber(stationTemplate
, stationInfo
)
1104 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1105 if (isNotEmptyArray(stationTemplate
.power
)) {
1106 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1107 stationInfo
.maximumPower
=
1108 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1109 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1110 : stationTemplate
.power
[powerArrayRandomIndex
]
1112 stationInfo
.maximumPower
=
1113 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1114 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1115 stationTemplate
.power
! * 1000
1116 : stationTemplate
.power
1118 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1119 stationInfo
.firmwareVersionPattern
=
1120 stationTemplate
.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
1122 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1123 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1126 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1128 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1131 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1138 stationTemplate
.firmwareUpgrade
?? {}
1140 stationInfo
.resetTime
=
1141 stationTemplate
.resetTime
!= null
1142 ? secondsToMilliseconds(stationTemplate
.resetTime
)
1143 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
1147 private getStationInfoFromFile (
1148 stationInfoPersistentConfiguration
= true
1149 ): ChargingStationInfo
| undefined {
1150 let stationInfo
: ChargingStationInfo
| undefined
1151 if (stationInfoPersistentConfiguration
) {
1152 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1153 if (stationInfo
!= null) {
1154 delete stationInfo
.infoHash
1160 private getStationInfo (): ChargingStationInfo
{
1161 const defaultStationInfo
= Constants
.DEFAULT_STATION_INFO
1162 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1163 const stationInfoFromFile
= this.getStationInfoFromFile(
1164 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1167 // 1. charging station info from template
1168 // 2. charging station info from configuration file
1170 stationInfoFromFile
!= null &&
1171 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1173 return { ...defaultStationInfo
, ...stationInfoFromFile
}
1175 stationInfoFromFile
!= null &&
1176 propagateSerialNumber(
1177 this.getTemplateFromFile(),
1178 stationInfoFromFile
,
1179 stationInfoFromTemplate
1181 return { ...defaultStationInfo
, ...stationInfoFromTemplate
}
1184 private saveStationInfo (): void {
1185 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1186 this.saveConfiguration()
1190 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1191 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1192 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1193 throw new BaseError(errorMsg
)
1196 private initialize (): void {
1197 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1198 const stationTemplate
= this.getTemplateFromFile()!
1199 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1200 this.configurationFile
= join(
1201 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1202 `${getHashId(this.index, stationTemplate)}.json`
1204 const stationConfiguration
= this.getConfigurationFromFile()
1206 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1207 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1209 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1210 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1212 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1214 this.stationInfo
= this.getStationInfo()
1216 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1217 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1218 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1220 const patternGroup
=
1221 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1222 this.stationInfo
.firmwareVersion
.split('.').length
1223 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1224 .exec(this.stationInfo
.firmwareVersion
)
1225 ?.slice(1, patternGroup
+ 1)
1226 if (match
!= null) {
1227 const patchLevelIndex
= match
.length
- 1
1228 match
[patchLevelIndex
] = (
1229 convertToInt(match
[patchLevelIndex
]) +
1230 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1231 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1233 this.stationInfo
.firmwareVersion
= match
.join('.')
1236 this.saveStationInfo()
1237 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1238 if (this.stationInfo
.enableStatistics
=== true) {
1239 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1240 this.stationInfo
.hashId
,
1241 this.stationInfo
.chargingStationId
,
1242 this.configuredSupervisionUrl
1245 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1246 if (bootNotificationRequest
== null) {
1247 const errorMsg
= 'Error while creating boot notification request'
1248 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1249 throw new BaseError(errorMsg
)
1251 this.bootNotificationRequest
= bootNotificationRequest
1252 this.powerDivider
= this.getPowerDivider()
1253 // OCPP configuration
1254 this.ocppConfiguration
= this.getOcppConfiguration()
1255 this.initializeOcppConfiguration()
1256 this.initializeOcppServices()
1257 this.once(ChargingStationEvents
.accepted
, () => {
1258 this.startMessageSequence().catch(error
=> {
1259 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
1262 if (this.stationInfo
.autoRegister
=== true) {
1263 this.bootNotificationResponse
= {
1264 currentTime
: new Date(),
1265 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1266 status: RegistrationStatusEnumType
.ACCEPTED
1271 private initializeOcppServices (): void {
1272 const ocppVersion
= this.stationInfo
?.ocppVersion
1273 switch (ocppVersion
) {
1274 case OCPPVersion
.VERSION_16
:
1275 this.ocppIncomingRequestService
=
1276 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1277 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1278 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1281 case OCPPVersion
.VERSION_20
:
1282 case OCPPVersion
.VERSION_201
:
1283 this.ocppIncomingRequestService
=
1284 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1285 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1286 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1290 this.handleUnsupportedVersion(ocppVersion
)
1295 private initializeOcppConfiguration (): void {
1296 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1297 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1299 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1300 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1303 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1304 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1305 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1307 addConfigurationKey(
1309 this.stationInfo
.supervisionUrlOcppKey
,
1310 this.configuredSupervisionUrl
.href
,
1314 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1315 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1316 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1318 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1321 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1322 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1324 addConfigurationKey(
1326 this.stationInfo
.amperageLimitationOcppKey
,
1328 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1329 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1332 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1333 addConfigurationKey(
1335 StandardParametersKey
.SupportedFeatureProfiles
,
1336 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1339 addConfigurationKey(
1341 StandardParametersKey
.NumberOfConnectors
,
1342 this.getNumberOfConnectors().toString(),
1346 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1347 addConfigurationKey(
1349 StandardParametersKey
.MeterValuesSampledData
,
1350 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1353 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1354 const connectorsPhaseRotation
: string[] = []
1355 if (this.hasEvses
) {
1356 for (const evseStatus
of this.evses
.values()) {
1357 for (const connectorId
of evseStatus
.connectors
.keys()) {
1358 connectorsPhaseRotation
.push(
1359 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1360 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1365 for (const connectorId
of this.connectors
.keys()) {
1366 connectorsPhaseRotation
.push(
1367 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1368 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1372 addConfigurationKey(
1374 StandardParametersKey
.ConnectorPhaseRotation
,
1375 connectorsPhaseRotation
.toString()
1378 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1379 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1382 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1383 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1385 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1387 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1388 addConfigurationKey(
1390 StandardParametersKey
.ConnectionTimeOut
,
1391 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1394 this.saveOcppConfiguration()
1397 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1398 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1399 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1400 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1402 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1403 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1404 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1405 delete evseStatus
.connectorsStatus
1406 this.evses
.set(evseId
, {
1407 ...(evseStatus
as EvseStatus
),
1408 connectors
: new Map
<number, ConnectorStatus
>(
1409 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1410 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1417 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1418 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1419 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1420 throw new BaseError(errorMsg
)
1422 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1423 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1424 throw new BaseError(errorMsg
)
1428 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1429 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1430 this.initializeConnectorsFromTemplate(stationTemplate
)
1431 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1432 this.initializeEvsesFromTemplate(stationTemplate
)
1433 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1434 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1435 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1436 throw new BaseError(errorMsg
)
1438 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1439 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1440 throw new BaseError(errorMsg
)
1444 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1445 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1446 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1447 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1448 throw new BaseError(errorMsg
)
1450 if (stationTemplate
.Connectors
?.[0] == null) {
1452 `${this.logPrefix()} Charging station information from template ${
1454 } with no connector id 0 configuration`
1457 if (stationTemplate
.Connectors
!= null) {
1458 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1459 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1460 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1462 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1465 const connectorsConfigChanged
=
1466 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1467 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1468 connectorsConfigChanged
&& this.connectors
.clear()
1469 this.connectorsConfigurationHash
= connectorsConfigHash
1470 if (templateMaxConnectors
> 0) {
1471 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1473 connectorId
=== 0 &&
1474 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1475 (stationTemplate
.Connectors
[connectorId
] == null ||
1476 !this.getUseConnectorId0(stationTemplate
))
1480 const templateConnectorId
=
1481 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1482 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1484 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1485 checkStationInfoConnectorStatus(
1486 templateConnectorId
,
1491 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1493 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1494 this.saveConnectorsStatus()
1497 `${this.logPrefix()} Charging station information from template ${
1499 } with no connectors configuration defined, cannot create connectors`
1505 `${this.logPrefix()} Charging station information from template ${
1507 } with no connectors configuration defined, using already defined connectors`
1512 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1513 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1514 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1515 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1516 throw new BaseError(errorMsg
)
1518 if (stationTemplate
.Evses
?.[0] == null) {
1520 `${this.logPrefix()} Charging station information from template ${
1522 } with no evse id 0 configuration`
1525 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1527 `${this.logPrefix()} Charging station information from template ${
1529 } with evse id 0 with no connector id 0 configuration`
1532 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1534 `${this.logPrefix()} Charging station information from template ${
1536 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1539 if (stationTemplate
.Evses
!= null) {
1540 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1541 .update(JSON
.stringify(stationTemplate
.Evses
))
1543 const evsesConfigChanged
=
1544 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1545 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1546 evsesConfigChanged
&& this.evses
.clear()
1547 this.evsesConfigurationHash
= evsesConfigHash
1548 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1549 if (templateMaxEvses
> 0) {
1550 for (const evseKey
in stationTemplate
.Evses
) {
1551 const evseId
= convertToInt(evseKey
)
1552 this.evses
.set(evseId
, {
1553 connectors
: buildConnectorsMap(
1554 stationTemplate
.Evses
[evseKey
].Connectors
,
1558 availability
: AvailabilityType
.Operative
1560 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1561 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1563 this.saveEvsesStatus()
1566 `${this.logPrefix()} Charging station information from template ${
1568 } with no evses configuration defined, cannot create evses`
1574 `${this.logPrefix()} Charging station information from template ${
1576 } with no evses configuration defined, using already defined evses`
1581 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1582 let configuration
: ChargingStationConfiguration
| undefined
1583 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1585 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1586 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1587 this.configurationFileHash
1590 const measureId
= `${FileType.ChargingStationConfiguration} read`
1591 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1592 configuration
= JSON
.parse(
1593 readFileSync(this.configurationFile
, 'utf8')
1594 ) as ChargingStationConfiguration
1595 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1596 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1597 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1598 this.configurationFileHash
= configuration
.configurationHash
!
1601 handleFileException(
1602 this.configurationFile
,
1603 FileType
.ChargingStationConfiguration
,
1604 error
as NodeJS
.ErrnoException
,
1609 return configuration
1612 private saveAutomaticTransactionGeneratorConfiguration (): void {
1613 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1614 this.saveConfiguration()
1618 private saveConnectorsStatus (): void {
1619 this.saveConfiguration()
1622 private saveEvsesStatus (): void {
1623 this.saveConfiguration()
1626 private saveConfiguration (): void {
1627 if (isNotEmptyString(this.configurationFile
)) {
1629 if (!existsSync(dirname(this.configurationFile
))) {
1630 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1632 const configurationFromFile
= this.getConfigurationFromFile()
1633 let configurationData
: ChargingStationConfiguration
=
1634 configurationFromFile
!= null
1635 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1637 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1638 configurationData
.stationInfo
= this.stationInfo
1640 delete configurationData
.stationInfo
1643 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1644 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1646 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1648 delete configurationData
.configurationKey
1650 configurationData
= merge
<ChargingStationConfiguration
>(
1652 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1655 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== false ||
1656 this.getAutomaticTransactionGeneratorConfiguration() == null
1658 delete configurationData
.automaticTransactionGenerator
1660 if (this.connectors
.size
> 0) {
1661 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1663 delete configurationData
.connectorsStatus
1665 if (this.evses
.size
> 0) {
1666 configurationData
.evsesStatus
= buildEvsesStatus(this)
1668 delete configurationData
.evsesStatus
1670 delete configurationData
.configurationHash
1671 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1674 stationInfo
: configurationData
.stationInfo
,
1675 configurationKey
: configurationData
.configurationKey
,
1676 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1677 ...(this.connectors
.size
> 0 && {
1678 connectorsStatus
: configurationData
.connectorsStatus
1680 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1681 } satisfies ChargingStationConfiguration
)
1684 if (this.configurationFileHash
!== configurationHash
) {
1685 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1686 configurationData
.configurationHash
= configurationHash
1687 const measureId
= `${FileType.ChargingStationConfiguration} write`
1688 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1690 this.configurationFile
,
1691 JSON
.stringify(configurationData
, undefined, 2),
1694 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1695 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1696 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1697 this.configurationFileHash
= configurationHash
1699 handleFileException(
1700 this.configurationFile
,
1701 FileType
.ChargingStationConfiguration
,
1702 error
as NodeJS
.ErrnoException
,
1708 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1709 this.configurationFile
1714 handleFileException(
1715 this.configurationFile
,
1716 FileType
.ChargingStationConfiguration
,
1717 error
as NodeJS
.ErrnoException
,
1723 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1728 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1729 return this.getTemplateFromFile()?.Configuration
1732 private getOcppConfigurationFromFile (): ChargingStationOcppConfiguration
| undefined {
1733 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1734 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1735 return { configurationKey
}
1740 private getOcppConfiguration (): ChargingStationOcppConfiguration
| undefined {
1741 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1742 this.getOcppConfigurationFromFile()
1743 if (ocppConfiguration
== null) {
1744 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1746 return ocppConfiguration
1749 private async onOpen (): Promise
<void> {
1750 if (this.isWebSocketConnectionOpened()) {
1752 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1754 let registrationRetryCount
= 0
1755 if (!this.isRegistered()) {
1756 // Send BootNotification
1758 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1759 BootNotificationRequest
,
1760 BootNotificationResponse
1761 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1762 skipBufferingOnError
: true
1764 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1765 if (this.bootNotificationResponse
?.currentTime
!= null) {
1766 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1767 this.bootNotificationResponse
.currentTime
= convertToDate(
1768 this.bootNotificationResponse
.currentTime
1771 if (!this.isRegistered()) {
1772 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1774 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1775 this.bootNotificationResponse
?.interval
!= null
1776 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1777 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1781 !this.isRegistered() &&
1782 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1783 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1784 this.stationInfo
?.registrationMaxRetries
=== -1)
1787 if (this.isRegistered()) {
1788 this.emit(ChargingStationEvents
.registered
)
1789 if (this.inAcceptedState()) {
1790 this.emit(ChargingStationEvents
.accepted
)
1794 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1795 this.stationInfo?.registrationMaxRetries
1799 this.autoReconnectRetryCount
= 0
1800 this.emit(ChargingStationEvents
.updated
)
1803 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1808 private async onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): Promise
<void> {
1811 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1812 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1814 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1816 )}' and reason '${reason.toString()}'`
1818 this.autoReconnectRetryCount
= 0
1823 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1825 )}' and reason '${reason.toString()}'`
1827 this.started
&& (await this.reconnect())
1830 this.emit(ChargingStationEvents
.updated
)
1833 private getCachedRequest (messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1834 const cachedRequest
= this.requests
.get(messageId
)
1835 if (Array.isArray(cachedRequest
)) {
1836 return cachedRequest
1838 throw new OCPPError(
1839 ErrorType
.PROTOCOL_ERROR
,
1840 `Cached request for message id ${messageId} ${getMessageTypeString(
1842 )} is not an array`,
1848 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1849 const [messageType
, messageId
, commandName
, commandPayload
] = request
1850 if (this.stationInfo
?.enableStatistics
=== true) {
1851 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1854 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1858 // Process the message
1859 await this.ocppIncomingRequestService
.incomingRequestHandler(
1865 this.emit(ChargingStationEvents
.updated
)
1868 private handleResponseMessage (response
: Response
): void {
1869 const [messageType
, messageId
, commandPayload
] = response
1870 if (!this.requests
.has(messageId
)) {
1872 throw new OCPPError(
1873 ErrorType
.INTERNAL_ERROR
,
1874 `Response for unknown message id ${messageId}`,
1880 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1881 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1886 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1890 responseCallback(commandPayload
, requestPayload
)
1893 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1894 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1895 if (!this.requests
.has(messageId
)) {
1897 throw new OCPPError(
1898 ErrorType
.INTERNAL_ERROR
,
1899 `Error response for unknown message id ${messageId}`,
1901 { errorType
, errorMessage
, errorDetails
}
1904 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1905 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1907 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1911 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1914 private async onMessage (data
: RawData
): Promise
<void> {
1915 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1916 let messageType
: MessageType
| undefined
1917 let errorMsg
: string
1919 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1920 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
1921 if (Array.isArray(request
)) {
1922 [messageType
] = request
1923 // Check the type of message
1924 switch (messageType
) {
1926 case MessageType
.CALL_MESSAGE
:
1927 await this.handleIncomingMessage(request
as IncomingRequest
)
1930 case MessageType
.CALL_RESULT_MESSAGE
:
1931 this.handleResponseMessage(request
as Response
)
1934 case MessageType
.CALL_ERROR_MESSAGE
:
1935 this.handleErrorMessage(request
as ErrorResponse
)
1939 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1940 errorMsg
= `Wrong message type ${messageType}`
1941 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1942 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
1945 throw new OCPPError(
1946 ErrorType
.PROTOCOL_ERROR
,
1947 'Incoming message is not an array',
1955 let commandName
: IncomingRequestCommand
| undefined
1956 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
1957 let errorCallback
: ErrorCallback
1958 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1959 const [, messageId
] = request
!
1960 switch (messageType
) {
1961 case MessageType
.CALL_MESSAGE
:
1962 [, , commandName
] = request
as IncomingRequest
1964 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
1966 case MessageType
.CALL_RESULT_MESSAGE
:
1967 case MessageType
.CALL_ERROR_MESSAGE
:
1968 if (this.requests
.has(messageId
)) {
1969 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1970 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1971 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1972 errorCallback(error
as OCPPError
, false)
1974 // Remove the request from the cache in case of error at response handling
1975 this.requests
.delete(messageId
)
1979 if (!(error
instanceof OCPPError
)) {
1981 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1982 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1983 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1984 }' message '${data.toString()}' handling is not an OCPPError:`,
1989 `${this.logPrefix()} Incoming OCPP command '${
1990 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1991 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1992 }' message '${data.toString()}'${
1993 messageType !== MessageType.CALL_MESSAGE
1994 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1996 } processing error:`,
2002 private onPing (): void {
2003 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2006 private onPong (): void {
2007 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2010 private onError (error
: WSError
): void {
2011 this.closeWSConnection()
2012 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2015 private getEnergyActiveImportRegister (
2016 connectorStatus
: ConnectorStatus
| undefined,
2019 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2022 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2023 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2025 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2030 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2031 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2033 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2037 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2038 return stationTemplate
?.useConnectorId0
?? true
2041 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2042 if (this.hasEvses
) {
2043 for (const [evseId
, evseStatus
] of this.evses
) {
2047 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2048 if (connectorStatus
.transactionStarted
=== true) {
2049 await this.stopTransactionOnConnector(connectorId
, reason
)
2054 for (const connectorId
of this.connectors
.keys()) {
2055 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2056 await this.stopTransactionOnConnector(connectorId
, reason
)
2063 private getConnectionTimeout (): number {
2064 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2065 return convertToInt(
2066 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2067 Constants
.DEFAULT_CONNECTION_TIMEOUT
2070 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2073 private getPowerDivider (): number {
2074 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2075 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2076 powerDivider
= this.getNumberOfRunningTransactions()
2081 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2082 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2083 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2084 switch (this.getCurrentOutType(stationInfo
)) {
2085 case CurrentType
.AC
:
2086 return ACElectricUtils
.amperagePerPhaseFromPower(
2087 this.getNumberOfPhases(stationInfo
),
2088 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2089 this.getVoltageOut(stationInfo
)
2091 case CurrentType
.DC
:
2092 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2096 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2097 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2098 return (stationInfo
?? this.stationInfo
!).currentOutType
?? CurrentType
.AC
2101 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2103 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2104 (stationInfo
?? this.stationInfo
!).voltageOut
??
2105 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2109 private getAmperageLimitation (): number | undefined {
2111 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2112 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2115 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2116 getAmperageLimitationUnitDivider(this.stationInfo
)
2121 private async startMessageSequence (): Promise
<void> {
2122 if (this.stationInfo
?.autoRegister
=== true) {
2123 await this.ocppRequestService
.requestHandler
<
2124 BootNotificationRequest
,
2125 BootNotificationResponse
2126 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2127 skipBufferingOnError
: true
2130 // Start WebSocket ping
2131 this.startWebSocketPing()
2133 this.startHeartbeat()
2134 // Initialize connectors status
2135 if (this.hasEvses
) {
2136 for (const [evseId
, evseStatus
] of this.evses
) {
2138 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2139 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2140 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2145 for (const connectorId
of this.connectors
.keys()) {
2146 if (connectorId
> 0) {
2147 const connectorBootStatus
= getBootConnectorStatus(
2150 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2151 this.getConnectorStatus(connectorId
)!
2153 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2157 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2158 await this.ocppRequestService
.requestHandler
<
2159 FirmwareStatusNotificationRequest
,
2160 FirmwareStatusNotificationResponse
2161 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2162 status: FirmwareStatus
.Installed
2164 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2168 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2169 this.startAutomaticTransactionGenerator()
2171 this.flushMessageBuffer()
2174 private async stopMessageSequence (
2175 reason
?: StopTransactionReason
,
2176 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
2178 // Stop WebSocket ping
2179 this.stopWebSocketPing()
2181 this.stopHeartbeat()
2183 if (this.automaticTransactionGenerator
?.started
=== true) {
2184 this.stopAutomaticTransactionGenerator()
2186 // Stop ongoing transactions
2187 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2188 if (this.hasEvses
) {
2189 for (const [evseId
, evseStatus
] of this.evses
) {
2191 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2192 await this.ocppRequestService
.requestHandler
<
2193 StatusNotificationRequest
,
2194 StatusNotificationResponse
2197 RequestCommand
.STATUS_NOTIFICATION
,
2198 buildStatusNotificationRequest(
2201 ConnectorStatusEnum
.Unavailable
,
2205 delete connectorStatus
.status
2210 for (const connectorId
of this.connectors
.keys()) {
2211 if (connectorId
> 0) {
2212 await this.ocppRequestService
.requestHandler
<
2213 StatusNotificationRequest
,
2214 StatusNotificationResponse
2217 RequestCommand
.STATUS_NOTIFICATION
,
2218 buildStatusNotificationRequest(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2220 delete this.getConnectorStatus(connectorId
)?.status
2226 private startWebSocketPing (): void {
2227 const webSocketPingInterval
=
2228 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2230 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2233 if (webSocketPingInterval
> 0 && this.webSocketPingSetInterval
== null) {
2234 this.webSocketPingSetInterval
= setInterval(() => {
2235 if (this.isWebSocketConnectionOpened()) {
2236 this.wsConnection
?.ping()
2238 }, secondsToMilliseconds(webSocketPingInterval
))
2240 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2241 webSocketPingInterval
2244 } else if (this.webSocketPingSetInterval
!= null) {
2246 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2247 webSocketPingInterval
2252 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2257 private stopWebSocketPing (): void {
2258 if (this.webSocketPingSetInterval
!= null) {
2259 clearInterval(this.webSocketPingSetInterval
)
2260 delete this.webSocketPingSetInterval
2264 private getConfiguredSupervisionUrl (): URL
{
2265 let configuredSupervisionUrl
: string
2266 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2267 if (isNotEmptyArray(supervisionUrls
)) {
2268 let configuredSupervisionUrlIndex
: number
2269 switch (Configuration
.getSupervisionUrlDistribution()) {
2270 case SupervisionUrlDistribution
.RANDOM
:
2271 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2273 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2274 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2276 !Object.values(SupervisionUrlDistribution
).includes(
2277 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2278 Configuration
.getSupervisionUrlDistribution()!
2281 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2282 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2283 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2286 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2289 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2291 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2292 configuredSupervisionUrl
= supervisionUrls
!
2294 if (isNotEmptyString(configuredSupervisionUrl
)) {
2295 return new URL(configuredSupervisionUrl
)
2297 const errorMsg
= 'No supervision url(s) configured'
2298 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2299 throw new BaseError(errorMsg
)
2302 private stopHeartbeat (): void {
2303 if (this.heartbeatSetInterval
!= null) {
2304 clearInterval(this.heartbeatSetInterval
)
2305 delete this.heartbeatSetInterval
2309 private terminateWSConnection (): void {
2310 if (this.isWebSocketConnectionOpened()) {
2311 this.wsConnection
?.terminate()
2312 this.wsConnection
= null
2316 private async reconnect (): Promise
<void> {
2317 // Stop WebSocket ping
2318 this.stopWebSocketPing()
2320 this.stopHeartbeat()
2321 // Stop the ATG if needed
2322 if (this.getAutomaticTransactionGeneratorConfiguration()?.stopOnConnectionFailure
=== true) {
2323 this.stopAutomaticTransactionGenerator()
2326 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2327 this.autoReconnectRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2328 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2330 ++this.autoReconnectRetryCount
2331 const reconnectDelay
=
2332 this.stationInfo
?.reconnectExponentialDelay
=== true
2333 ? exponentialDelay(this.autoReconnectRetryCount
)
2334 : secondsToMilliseconds(this.getConnectionTimeout())
2335 const reconnectDelayWithdraw
= 1000
2336 const reconnectTimeout
=
2337 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2339 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2342 )}ms, timeout ${reconnectTimeout}ms`
2344 await sleep(reconnectDelay
)
2346 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2348 this.openWSConnection(
2350 handshakeTimeout
: reconnectTimeout
2352 { closeOpened
: true }
2354 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2356 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2357 this.autoReconnectRetryCount
2358 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`