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))
219 this.on(ChargingStationEvents
.accepted
, () => {
220 this.startMessageSequence().catch(error
=> {
221 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
228 public get
hasEvses (): boolean {
229 return this.connectors
.size
=== 0 && this.evses
.size
> 0
232 private get
wsConnectionUrl (): URL
{
235 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
236 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
237 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
238 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
239 : this.configuredSupervisionUrl.href
240 }/${this.stationInfo?.chargingStationId}`
244 public logPrefix
= (): string => {
246 this instanceof ChargingStation
&&
247 this.stationInfo
!= null &&
248 isNotEmptyString(this.stationInfo
.chargingStationId
)
250 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
252 let stationTemplate
: ChargingStationTemplate
| undefined
254 stationTemplate
= JSON
.parse(
255 readFileSync(this.templateFile
, 'utf8')
256 ) as ChargingStationTemplate
258 stationTemplate
= undefined
260 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
263 public hasIdTags (): boolean {
264 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
265 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
268 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
269 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
270 const localStationInfo
= stationInfo
?? this.stationInfo
!
271 switch (this.getCurrentOutType(stationInfo
)) {
273 return localStationInfo
.numberOfPhases
?? 3
279 public isWebSocketConnectionOpened (): boolean {
280 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
283 public inUnknownState (): boolean {
284 return this.bootNotificationResponse
?.status == null
287 public inPendingState (): boolean {
288 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
291 public inAcceptedState (): boolean {
292 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
295 public inRejectedState (): boolean {
296 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
299 public isRegistered (): boolean {
300 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
303 public isChargingStationAvailable (): boolean {
304 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
307 public hasConnector (connectorId
: number): boolean {
309 for (const evseStatus
of this.evses
.values()) {
310 if (evseStatus
.connectors
.has(connectorId
)) {
316 return this.connectors
.has(connectorId
)
319 public isConnectorAvailable (connectorId
: number): boolean {
322 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
326 public getNumberOfConnectors (): number {
328 let numberOfConnectors
= 0
329 for (const [evseId
, evseStatus
] of this.evses
) {
331 numberOfConnectors
+= evseStatus
.connectors
.size
334 return numberOfConnectors
336 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
339 public getNumberOfEvses (): number {
340 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
343 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
345 for (const evseStatus
of this.evses
.values()) {
346 if (evseStatus
.connectors
.has(connectorId
)) {
347 return evseStatus
.connectors
.get(connectorId
)
352 return this.connectors
.get(connectorId
)
355 public getConnectorMaximumAvailablePower (connectorId
: number): number {
356 let connectorAmperageLimitationPowerLimit
: number | undefined
357 const amperageLimitation
= this.getAmperageLimitation()
359 amperageLimitation
!= null &&
360 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
361 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
363 connectorAmperageLimitationPowerLimit
=
364 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
365 ? ACElectricUtils
.powerTotal(
366 this.getNumberOfPhases(),
367 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368 this.stationInfo
.voltageOut
!,
370 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
372 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
373 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
374 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
377 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
378 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
379 const connectorChargingProfilesPowerLimit
=
380 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
382 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
383 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
384 isNaN(connectorAmperageLimitationPowerLimit
!)
386 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
387 connectorAmperageLimitationPowerLimit
!,
388 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
389 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
393 public getTransactionIdTag (transactionId
: number): string | undefined {
395 for (const evseStatus
of this.evses
.values()) {
396 for (const connectorStatus
of evseStatus
.connectors
.values()) {
397 if (connectorStatus
.transactionId
=== transactionId
) {
398 return connectorStatus
.transactionIdTag
403 for (const connectorId
of this.connectors
.keys()) {
404 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
405 return this.getConnectorStatus(connectorId
)?.transactionIdTag
411 public getNumberOfRunningTransactions (): number {
412 let numberOfRunningTransactions
= 0
414 for (const [evseId
, evseStatus
] of this.evses
) {
418 for (const connectorStatus
of evseStatus
.connectors
.values()) {
419 if (connectorStatus
.transactionStarted
=== true) {
420 ++numberOfRunningTransactions
425 for (const connectorId
of this.connectors
.keys()) {
426 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
427 ++numberOfRunningTransactions
431 return numberOfRunningTransactions
434 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
435 if (transactionId
== null) {
437 } else if (this.hasEvses
) {
438 for (const evseStatus
of this.evses
.values()) {
439 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
440 if (connectorStatus
.transactionId
=== transactionId
) {
446 for (const connectorId
of this.connectors
.keys()) {
447 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
454 public getEnergyActiveImportRegisterByTransactionId (
455 transactionId
: number | undefined,
458 return this.getEnergyActiveImportRegister(
459 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
460 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
465 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
466 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
469 public getAuthorizeRemoteTxRequests (): boolean {
470 const authorizeRemoteTxRequests
= getConfigurationKey(
472 StandardParametersKey
.AuthorizeRemoteTxRequests
474 return authorizeRemoteTxRequests
!= null
475 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
479 public getLocalAuthListEnabled (): boolean {
480 const localAuthListEnabled
= getConfigurationKey(
482 StandardParametersKey
.LocalAuthListEnabled
484 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
487 public getHeartbeatInterval (): number {
488 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
489 if (HeartbeatInterval
!= null) {
490 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
492 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
493 if (HeartBeatInterval
!= null) {
494 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
496 this.stationInfo
?.autoRegister
=== false &&
498 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
499 Constants.DEFAULT_HEARTBEAT_INTERVAL
502 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
505 public setSupervisionUrl (url
: string): void {
507 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
508 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
510 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
512 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
513 this.stationInfo
!.supervisionUrls
= url
514 this.saveStationInfo()
515 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
519 public startHeartbeat (): void {
520 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
521 this.heartbeatSetInterval
= setInterval(() => {
522 this.ocppRequestService
523 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
526 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
530 }, this.getHeartbeatInterval())
532 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
533 this.getHeartbeatInterval()
536 } else if (this.heartbeatSetInterval
!= null) {
538 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
539 this.getHeartbeatInterval()
544 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
549 public restartHeartbeat (): void {
553 this.startHeartbeat()
556 public restartWebSocketPing (): void {
557 // Stop WebSocket ping
558 this.stopWebSocketPing()
559 // Start WebSocket ping
560 this.startWebSocketPing()
563 public startMeterValues (connectorId
: number, interval
: number): void {
564 if (connectorId
=== 0) {
565 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
568 const connectorStatus
= this.getConnectorStatus(connectorId
)
569 if (connectorStatus
== null) {
571 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
576 if (connectorStatus
.transactionStarted
=== false) {
578 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
582 connectorStatus
.transactionStarted
=== true &&
583 connectorStatus
.transactionId
== null
586 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
591 connectorStatus
.transactionSetInterval
= setInterval(() => {
592 const meterValue
= buildMeterValue(
595 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
596 connectorStatus
.transactionId
!,
599 this.ocppRequestService
600 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
602 RequestCommand
.METER_VALUES
,
605 transactionId
: connectorStatus
.transactionId
,
606 meterValue
: [meterValue
]
611 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
618 `${this.logPrefix()} Charging station ${
619 StandardParametersKey.MeterValueSampleInterval
620 } configuration set to ${interval}, not sending MeterValues`
625 public stopMeterValues (connectorId
: number): void {
626 const connectorStatus
= this.getConnectorStatus(connectorId
)
627 if (connectorStatus
?.transactionSetInterval
!= null) {
628 clearInterval(connectorStatus
.transactionSetInterval
)
632 public start (): void {
634 if (!this.starting
) {
636 if (this.stationInfo
?.enableStatistics
=== true) {
637 this.performanceStatistics
?.start()
639 this.openWSConnection()
640 // Monitor charging station template file
641 this.templateFileWatcher
= watchJsonFile(
643 FileType
.ChargingStationTemplate
,
646 (event
, filename
): void => {
647 if (isNotEmptyString(filename
) && event
=== 'change') {
650 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
652 } file have changed, reload`
654 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
657 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
658 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
660 this.stopAutomaticTransactionGenerator()
661 delete this.automaticTransactionGeneratorConfiguration
662 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
663 this.startAutomaticTransactionGenerator()
665 if (this.stationInfo
?.enableStatistics
=== true) {
666 this.performanceStatistics
?.restart()
668 this.performanceStatistics
?.stop()
670 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
673 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
681 this.emit(ChargingStationEvents
.started
)
682 this.starting
= false
684 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
687 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
691 public async stop (reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
693 if (!this.stopping
) {
695 await this.stopMessageSequence(reason
, stopTransactions
)
696 this.closeWSConnection()
697 if (this.stationInfo
?.enableStatistics
=== true) {
698 this.performanceStatistics
?.stop()
700 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
701 this.templateFileWatcher
?.close()
702 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
703 delete this.bootNotificationResponse
705 this.saveConfiguration()
706 this.emit(ChargingStationEvents
.stopped
)
707 this.stopping
= false
709 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
712 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
716 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
717 await this.stop(reason
)
718 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
719 await sleep(this.stationInfo
!.resetTime
!)
724 public saveOcppConfiguration (): void {
725 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
726 this.saveConfiguration()
730 public bufferMessage (message
: string): void {
731 this.messageBuffer
.add(message
)
732 this.setIntervalFlushMessageBuffer()
735 public openWSConnection (
737 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
740 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
741 ...this.stationInfo
?.wsOptions
,
744 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
745 if (!checkChargingStation(this, this.logPrefix())) {
748 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
749 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
751 if (params
.closeOpened
=== true) {
752 this.closeWSConnection()
754 if (params
.terminateOpened
=== true) {
755 this.terminateWSConnection()
758 if (this.isWebSocketConnectionOpened()) {
760 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
766 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
769 this.wsConnection
= new WebSocket(
770 this.wsConnectionUrl
,
771 `ocpp${this.stationInfo?.ocppVersion}`,
775 // Handle WebSocket message
776 this.wsConnection
.on('message', data
=> {
777 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
779 // Handle WebSocket error
780 this.wsConnection
.on('error', this.onError
.bind(this))
781 // Handle WebSocket close
782 this.wsConnection
.on('close', this.onClose
.bind(this))
783 // Handle WebSocket open
784 this.wsConnection
.on('open', () => {
785 this.onOpen().catch(error
=>
786 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
789 // Handle WebSocket ping
790 this.wsConnection
.on('ping', this.onPing
.bind(this))
791 // Handle WebSocket pong
792 this.wsConnection
.on('pong', this.onPong
.bind(this))
795 public closeWSConnection (): void {
796 if (this.isWebSocketConnectionOpened()) {
797 this.wsConnection
?.close()
798 this.wsConnection
= null
802 public getAutomaticTransactionGeneratorConfiguration ():
803 | AutomaticTransactionGeneratorConfiguration
805 if (this.automaticTransactionGeneratorConfiguration
== null) {
806 let automaticTransactionGeneratorConfiguration
:
807 | AutomaticTransactionGeneratorConfiguration
809 const stationTemplate
= this.getTemplateFromFile()
810 const stationConfiguration
= this.getConfigurationFromFile()
812 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
813 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
814 stationConfiguration
?.automaticTransactionGenerator
!= null
816 automaticTransactionGeneratorConfiguration
=
817 stationConfiguration
.automaticTransactionGenerator
819 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
821 this.automaticTransactionGeneratorConfiguration
= {
822 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
823 ...automaticTransactionGeneratorConfiguration
826 return this.automaticTransactionGeneratorConfiguration
829 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
830 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
833 public startAutomaticTransactionGenerator (connectorIds
?: number[]): void {
834 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
835 if (isNotEmptyArray(connectorIds
)) {
836 for (const connectorId
of connectorIds
) {
837 this.automaticTransactionGenerator
?.startConnector(connectorId
)
840 this.automaticTransactionGenerator
?.start()
842 this.saveAutomaticTransactionGeneratorConfiguration()
843 this.emit(ChargingStationEvents
.updated
)
846 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
847 if (isNotEmptyArray(connectorIds
)) {
848 for (const connectorId
of connectorIds
) {
849 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
852 this.automaticTransactionGenerator
?.stop()
854 this.saveAutomaticTransactionGeneratorConfiguration()
855 this.emit(ChargingStationEvents
.updated
)
858 public async stopTransactionOnConnector (
860 reason
?: StopTransactionReason
861 ): Promise
<StopTransactionResponse
> {
862 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
864 this.stationInfo
?.beginEndMeterValues
=== true &&
865 this.stationInfo
.ocppStrictCompliance
=== true &&
866 this.stationInfo
.outOfOrderEndMeterValues
=== false
868 const transactionEndMeterValue
= buildTransactionEndMeterValue(
871 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
873 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
875 RequestCommand
.METER_VALUES
,
879 meterValue
: [transactionEndMeterValue
]
883 return await this.ocppRequestService
.requestHandler
<
884 StopTransactionRequest
,
885 StopTransactionResponse
886 >(this, RequestCommand
.STOP_TRANSACTION
, {
888 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
889 ...(reason
!= null && { reason
})
893 public getReserveConnectorZeroSupported (): boolean {
894 return convertToBoolean(
895 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
896 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
900 public async addReservation (reservation
: Reservation
): Promise
<void> {
901 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
902 if (reservationFound
!= null) {
903 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
905 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
906 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
907 await sendAndSetConnectorStatus(
909 reservation
.connectorId
,
910 ConnectorStatusEnum
.Reserved
,
912 { send
: reservation
.connectorId
!== 0 }
916 public async removeReservation (
917 reservation
: Reservation
,
918 reason
: ReservationTerminationReason
920 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
921 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
923 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
924 case ReservationTerminationReason
.TRANSACTION_STARTED
:
925 delete connector
.reservation
927 case ReservationTerminationReason
.RESERVATION_CANCELED
:
928 case ReservationTerminationReason
.REPLACE_EXISTING
:
929 case ReservationTerminationReason
.EXPIRED
:
930 await sendAndSetConnectorStatus(
932 reservation
.connectorId
,
933 ConnectorStatusEnum
.Available
,
935 { send
: reservation
.connectorId
!== 0 }
937 delete connector
.reservation
940 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
941 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
945 public getReservationBy (
946 filterKey
: ReservationKey
,
947 value
: number | string
948 ): Reservation
| undefined {
950 for (const evseStatus
of this.evses
.values()) {
951 for (const connectorStatus
of evseStatus
.connectors
.values()) {
952 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
953 return connectorStatus
.reservation
958 for (const connectorStatus
of this.connectors
.values()) {
959 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
960 return connectorStatus
.reservation
966 public isConnectorReservable (
967 reservationId
: number,
971 const reservation
= this.getReservationBy('reservationId', reservationId
)
972 const reservationExists
= reservation
!== undefined && !hasReservationExpired(reservation
)
973 if (arguments.length
=== 1) {
974 return !reservationExists
975 } else if (arguments.length
> 1) {
976 const userReservation
=
977 idTag
!== undefined ? this.getReservationBy('idTag', idTag
) : undefined
978 const userReservationExists
=
979 userReservation
!== undefined && !hasReservationExpired(userReservation
)
980 const notConnectorZero
= connectorId
=== undefined ? true : connectorId
> 0
981 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
983 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
989 private setIntervalFlushMessageBuffer (): void {
990 if (this.flushMessageBufferSetInterval
== null) {
991 this.flushMessageBufferSetInterval
= setInterval(() => {
992 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
993 this.flushMessageBuffer()
995 if (this.messageBuffer
.size
=== 0) {
996 this.clearIntervalFlushMessageBuffer()
998 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1002 private clearIntervalFlushMessageBuffer (): void {
1003 if (this.flushMessageBufferSetInterval
!= null) {
1004 clearInterval(this.flushMessageBufferSetInterval
)
1005 delete this.flushMessageBufferSetInterval
1009 private getNumberOfReservableConnectors (): number {
1010 let numberOfReservableConnectors
= 0
1011 if (this.hasEvses
) {
1012 for (const evseStatus
of this.evses
.values()) {
1013 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1016 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1018 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1021 private getNumberOfReservationsOnConnectorZero (): number {
1023 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1024 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1031 private flushMessageBuffer (): void {
1032 if (this.messageBuffer
.size
> 0) {
1033 for (const message
of this.messageBuffer
.values()) {
1034 let beginId
: string | undefined
1035 let commandName
: RequestCommand
| undefined
1036 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1037 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1039 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1040 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1042 this.wsConnection
?.send(message
, (error
?: Error) => {
1043 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1044 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1045 if (error
== null) {
1047 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1049 )} OCPP message sent '${JSON.stringify(message)}'`
1051 this.messageBuffer
.delete(message
)
1054 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1056 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1065 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1066 let template
: ChargingStationTemplate
| undefined
1068 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1069 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1071 const measureId
= `${FileType.ChargingStationTemplate} read`
1072 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1073 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1074 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1075 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1076 .update(JSON
.stringify(template
))
1078 this.sharedLRUCache
.setChargingStationTemplate(template
)
1079 this.templateFileHash
= template
.templateHash
1082 handleFileException(
1084 FileType
.ChargingStationTemplate
,
1085 error
as NodeJS
.ErrnoException
,
1092 private getStationInfoFromTemplate (): ChargingStationInfo
{
1093 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1094 const stationTemplate
= this.getTemplateFromFile()!
1095 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1096 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1097 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1098 if (stationTemplate
.Connectors
!= null) {
1099 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1101 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1102 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1103 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1104 stationInfo
.ocppVersion
= stationTemplate
.ocppVersion
?? OCPPVersion
.VERSION_16
1105 createSerialNumber(stationTemplate
, stationInfo
)
1106 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1107 if (isNotEmptyArray(stationTemplate
.power
)) {
1108 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1109 stationInfo
.maximumPower
=
1110 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1111 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1112 : stationTemplate
.power
[powerArrayRandomIndex
]
1114 stationInfo
.maximumPower
=
1115 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1116 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1117 stationTemplate
.power
! * 1000
1118 : stationTemplate
.power
1120 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1121 stationInfo
.firmwareVersionPattern
=
1122 stationTemplate
.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
1124 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1125 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1128 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1130 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1133 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1140 stationTemplate
.firmwareUpgrade
?? {}
1142 stationInfo
.resetTime
=
1143 stationTemplate
.resetTime
!= null
1144 ? secondsToMilliseconds(stationTemplate
.resetTime
)
1145 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
1149 private getStationInfoFromFile (
1150 stationInfoPersistentConfiguration
= true
1151 ): ChargingStationInfo
| undefined {
1152 let stationInfo
: ChargingStationInfo
| undefined
1153 if (stationInfoPersistentConfiguration
) {
1154 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1155 if (stationInfo
!= null) {
1156 delete stationInfo
.infoHash
1162 private getStationInfo (): ChargingStationInfo
{
1163 const defaultStationInfo
= Constants
.DEFAULT_STATION_INFO
1164 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1165 const stationInfoFromFile
= this.getStationInfoFromFile(
1166 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1169 // 1. charging station info from template
1170 // 2. charging station info from configuration file
1172 stationInfoFromFile
!= null &&
1173 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1175 return { ...defaultStationInfo
, ...stationInfoFromFile
}
1177 stationInfoFromFile
!= null &&
1178 propagateSerialNumber(
1179 this.getTemplateFromFile(),
1180 stationInfoFromFile
,
1181 stationInfoFromTemplate
1183 return { ...defaultStationInfo
, ...stationInfoFromTemplate
}
1186 private saveStationInfo (): void {
1187 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1188 this.saveConfiguration()
1192 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1193 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1194 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1195 throw new BaseError(errorMsg
)
1198 private initialize (): void {
1199 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1200 const stationTemplate
= this.getTemplateFromFile()!
1201 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1202 this.configurationFile
= join(
1203 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1204 `${getHashId(this.index, stationTemplate)}.json`
1206 const stationConfiguration
= this.getConfigurationFromFile()
1208 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1209 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1211 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1212 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1214 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1216 this.stationInfo
= this.getStationInfo()
1218 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1219 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1220 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1222 const patternGroup
=
1223 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1224 this.stationInfo
.firmwareVersion
.split('.').length
1225 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1226 .exec(this.stationInfo
.firmwareVersion
)
1227 ?.slice(1, patternGroup
+ 1)
1228 if (match
!= null) {
1229 const patchLevelIndex
= match
.length
- 1
1230 match
[patchLevelIndex
] = (
1231 convertToInt(match
[patchLevelIndex
]) +
1232 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1233 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1235 this.stationInfo
.firmwareVersion
= match
.join('.')
1238 this.saveStationInfo()
1239 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1240 if (this.stationInfo
.enableStatistics
=== true) {
1241 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1242 this.stationInfo
.hashId
,
1243 this.stationInfo
.chargingStationId
,
1244 this.configuredSupervisionUrl
1247 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1248 if (bootNotificationRequest
== null) {
1249 const errorMsg
= 'Error while creating boot notification request'
1250 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1251 throw new BaseError(errorMsg
)
1253 this.bootNotificationRequest
= bootNotificationRequest
1254 this.powerDivider
= this.getPowerDivider()
1255 // OCPP configuration
1256 this.ocppConfiguration
= this.getOcppConfiguration()
1257 this.initializeOcppConfiguration()
1258 this.initializeOcppServices()
1259 if (this.stationInfo
.autoRegister
=== true) {
1260 this.bootNotificationResponse
= {
1261 currentTime
: new Date(),
1262 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1263 status: RegistrationStatusEnumType
.ACCEPTED
1268 private initializeOcppServices (): void {
1269 const ocppVersion
= this.stationInfo
?.ocppVersion
1270 switch (ocppVersion
) {
1271 case OCPPVersion
.VERSION_16
:
1272 this.ocppIncomingRequestService
=
1273 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1274 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1275 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1278 case OCPPVersion
.VERSION_20
:
1279 case OCPPVersion
.VERSION_201
:
1280 this.ocppIncomingRequestService
=
1281 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1282 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1283 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1287 this.handleUnsupportedVersion(ocppVersion
)
1292 private initializeOcppConfiguration (): void {
1293 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1294 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1296 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1297 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1300 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1301 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1302 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1304 addConfigurationKey(
1306 this.stationInfo
.supervisionUrlOcppKey
,
1307 this.configuredSupervisionUrl
.href
,
1311 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1312 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1313 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1315 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1318 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1319 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1321 addConfigurationKey(
1323 this.stationInfo
.amperageLimitationOcppKey
,
1325 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1326 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1329 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1330 addConfigurationKey(
1332 StandardParametersKey
.SupportedFeatureProfiles
,
1333 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1336 addConfigurationKey(
1338 StandardParametersKey
.NumberOfConnectors
,
1339 this.getNumberOfConnectors().toString(),
1343 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1344 addConfigurationKey(
1346 StandardParametersKey
.MeterValuesSampledData
,
1347 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1350 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1351 const connectorsPhaseRotation
: string[] = []
1352 if (this.hasEvses
) {
1353 for (const evseStatus
of this.evses
.values()) {
1354 for (const connectorId
of evseStatus
.connectors
.keys()) {
1355 connectorsPhaseRotation
.push(
1356 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1357 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1362 for (const connectorId
of this.connectors
.keys()) {
1363 connectorsPhaseRotation
.push(
1364 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1365 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1369 addConfigurationKey(
1371 StandardParametersKey
.ConnectorPhaseRotation
,
1372 connectorsPhaseRotation
.toString()
1375 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1376 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1379 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1380 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1382 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1384 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1385 addConfigurationKey(
1387 StandardParametersKey
.ConnectionTimeOut
,
1388 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1391 this.saveOcppConfiguration()
1394 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1395 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1396 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1397 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1399 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1400 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1401 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1402 delete evseStatus
.connectorsStatus
1403 this.evses
.set(evseId
, {
1404 ...(evseStatus
as EvseStatus
),
1405 connectors
: new Map
<number, ConnectorStatus
>(
1406 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1407 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1414 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1415 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1416 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1417 throw new BaseError(errorMsg
)
1419 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1420 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1421 throw new BaseError(errorMsg
)
1425 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1426 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1427 this.initializeConnectorsFromTemplate(stationTemplate
)
1428 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1429 this.initializeEvsesFromTemplate(stationTemplate
)
1430 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1431 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1432 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1433 throw new BaseError(errorMsg
)
1435 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1436 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1437 throw new BaseError(errorMsg
)
1441 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1442 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1443 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1444 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1445 throw new BaseError(errorMsg
)
1447 if (stationTemplate
.Connectors
?.[0] == null) {
1449 `${this.logPrefix()} Charging station information from template ${
1451 } with no connector id 0 configuration`
1454 if (stationTemplate
.Connectors
!= null) {
1455 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1456 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1457 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1459 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1462 const connectorsConfigChanged
=
1463 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1464 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1465 connectorsConfigChanged
&& this.connectors
.clear()
1466 this.connectorsConfigurationHash
= connectorsConfigHash
1467 if (templateMaxConnectors
> 0) {
1468 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1470 connectorId
=== 0 &&
1471 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1472 (stationTemplate
.Connectors
[connectorId
] == null ||
1473 !this.getUseConnectorId0(stationTemplate
))
1477 const templateConnectorId
=
1478 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1479 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1481 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1482 checkStationInfoConnectorStatus(
1483 templateConnectorId
,
1488 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1490 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1491 this.saveConnectorsStatus()
1494 `${this.logPrefix()} Charging station information from template ${
1496 } with no connectors configuration defined, cannot create connectors`
1502 `${this.logPrefix()} Charging station information from template ${
1504 } with no connectors configuration defined, using already defined connectors`
1509 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1510 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1511 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1512 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1513 throw new BaseError(errorMsg
)
1515 if (stationTemplate
.Evses
?.[0] == null) {
1517 `${this.logPrefix()} Charging station information from template ${
1519 } with no evse id 0 configuration`
1522 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1524 `${this.logPrefix()} Charging station information from template ${
1526 } with evse id 0 with no connector id 0 configuration`
1529 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1531 `${this.logPrefix()} Charging station information from template ${
1533 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1536 if (stationTemplate
.Evses
!= null) {
1537 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1538 .update(JSON
.stringify(stationTemplate
.Evses
))
1540 const evsesConfigChanged
=
1541 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1542 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1543 evsesConfigChanged
&& this.evses
.clear()
1544 this.evsesConfigurationHash
= evsesConfigHash
1545 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1546 if (templateMaxEvses
> 0) {
1547 for (const evseKey
in stationTemplate
.Evses
) {
1548 const evseId
= convertToInt(evseKey
)
1549 this.evses
.set(evseId
, {
1550 connectors
: buildConnectorsMap(
1551 stationTemplate
.Evses
[evseKey
].Connectors
,
1555 availability
: AvailabilityType
.Operative
1557 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1558 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1560 this.saveEvsesStatus()
1563 `${this.logPrefix()} Charging station information from template ${
1565 } with no evses configuration defined, cannot create evses`
1571 `${this.logPrefix()} Charging station information from template ${
1573 } with no evses configuration defined, using already defined evses`
1578 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1579 let configuration
: ChargingStationConfiguration
| undefined
1580 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1582 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1583 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1584 this.configurationFileHash
1587 const measureId
= `${FileType.ChargingStationConfiguration} read`
1588 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1589 configuration
= JSON
.parse(
1590 readFileSync(this.configurationFile
, 'utf8')
1591 ) as ChargingStationConfiguration
1592 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1593 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1594 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1595 this.configurationFileHash
= configuration
.configurationHash
!
1598 handleFileException(
1599 this.configurationFile
,
1600 FileType
.ChargingStationConfiguration
,
1601 error
as NodeJS
.ErrnoException
,
1606 return configuration
1609 private saveAutomaticTransactionGeneratorConfiguration (): void {
1610 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1611 this.saveConfiguration()
1615 private saveConnectorsStatus (): void {
1616 this.saveConfiguration()
1619 private saveEvsesStatus (): void {
1620 this.saveConfiguration()
1623 private saveConfiguration (): void {
1624 if (isNotEmptyString(this.configurationFile
)) {
1626 if (!existsSync(dirname(this.configurationFile
))) {
1627 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1629 const configurationFromFile
= this.getConfigurationFromFile()
1630 let configurationData
: ChargingStationConfiguration
=
1631 configurationFromFile
!= null
1632 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1634 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1635 configurationData
.stationInfo
= this.stationInfo
1637 delete configurationData
.stationInfo
1640 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1641 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1643 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1645 delete configurationData
.configurationKey
1647 configurationData
= merge
<ChargingStationConfiguration
>(
1649 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1652 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== false ||
1653 this.getAutomaticTransactionGeneratorConfiguration() == null
1655 delete configurationData
.automaticTransactionGenerator
1657 if (this.connectors
.size
> 0) {
1658 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1660 delete configurationData
.connectorsStatus
1662 if (this.evses
.size
> 0) {
1663 configurationData
.evsesStatus
= buildEvsesStatus(this)
1665 delete configurationData
.evsesStatus
1667 delete configurationData
.configurationHash
1668 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1671 stationInfo
: configurationData
.stationInfo
,
1672 configurationKey
: configurationData
.configurationKey
,
1673 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1674 ...(this.connectors
.size
> 0 && {
1675 connectorsStatus
: configurationData
.connectorsStatus
1677 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1678 } satisfies ChargingStationConfiguration
)
1681 if (this.configurationFileHash
!== configurationHash
) {
1682 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1683 configurationData
.configurationHash
= configurationHash
1684 const measureId
= `${FileType.ChargingStationConfiguration} write`
1685 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1687 this.configurationFile
,
1688 JSON
.stringify(configurationData
, undefined, 2),
1691 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1692 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1693 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1694 this.configurationFileHash
= configurationHash
1696 handleFileException(
1697 this.configurationFile
,
1698 FileType
.ChargingStationConfiguration
,
1699 error
as NodeJS
.ErrnoException
,
1705 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1706 this.configurationFile
1711 handleFileException(
1712 this.configurationFile
,
1713 FileType
.ChargingStationConfiguration
,
1714 error
as NodeJS
.ErrnoException
,
1720 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1725 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1726 return this.getTemplateFromFile()?.Configuration
1729 private getOcppConfigurationFromFile (): ChargingStationOcppConfiguration
| undefined {
1730 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1731 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1732 return { configurationKey
}
1737 private getOcppConfiguration (): ChargingStationOcppConfiguration
| undefined {
1738 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1739 this.getOcppConfigurationFromFile()
1740 if (ocppConfiguration
== null) {
1741 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1743 return ocppConfiguration
1746 private async onOpen (): Promise
<void> {
1747 if (this.isWebSocketConnectionOpened()) {
1749 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1751 let registrationRetryCount
= 0
1752 if (!this.isRegistered()) {
1753 // Send BootNotification
1755 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1756 BootNotificationRequest
,
1757 BootNotificationResponse
1758 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1759 skipBufferingOnError
: true
1761 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1762 if (this.bootNotificationResponse
?.currentTime
!= null) {
1763 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1764 this.bootNotificationResponse
.currentTime
= convertToDate(
1765 this.bootNotificationResponse
.currentTime
1768 if (!this.isRegistered()) {
1769 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1771 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1772 this.bootNotificationResponse
?.interval
!= null
1773 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1774 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1778 !this.isRegistered() &&
1779 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1780 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1781 this.stationInfo
?.registrationMaxRetries
=== -1)
1784 if (this.isRegistered()) {
1785 this.emit(ChargingStationEvents
.registered
)
1786 if (this.inAcceptedState()) {
1787 this.emit(ChargingStationEvents
.accepted
)
1791 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1792 this.stationInfo?.registrationMaxRetries
1796 this.autoReconnectRetryCount
= 0
1797 this.emit(ChargingStationEvents
.updated
)
1800 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1805 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1808 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1809 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1811 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1813 )}' and reason '${reason.toString()}'`
1815 this.autoReconnectRetryCount
= 0
1820 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1822 )}' and reason '${reason.toString()}'`
1825 this.reconnect().catch(error
=>
1826 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
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 if (!Array.isArray(request
)) {
1956 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
1959 let commandName
: IncomingRequestCommand
| undefined
1960 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
1961 let errorCallback
: ErrorCallback
1962 const [, messageId
] = request
1963 switch (messageType
) {
1964 case MessageType
.CALL_MESSAGE
:
1965 [, , commandName
] = request
as IncomingRequest
1967 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
1969 case MessageType
.CALL_RESULT_MESSAGE
:
1970 case MessageType
.CALL_ERROR_MESSAGE
:
1971 if (this.requests
.has(messageId
)) {
1972 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1973 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1974 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1975 errorCallback(error
as OCPPError
, false)
1977 // Remove the request from the cache in case of error at response handling
1978 this.requests
.delete(messageId
)
1982 if (!(error
instanceof OCPPError
)) {
1984 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1985 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1986 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1987 }' message '${data.toString()}' handling is not an OCPPError:`,
1992 `${this.logPrefix()} Incoming OCPP command '${
1993 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1994 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1995 }' message '${data.toString()}'${
1996 messageType !== MessageType.CALL_MESSAGE
1997 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1999 } processing error:`,
2005 private onPing (): void {
2006 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2009 private onPong (): void {
2010 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2013 private onError (error
: WSError
): void {
2014 this.closeWSConnection()
2015 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2018 private getEnergyActiveImportRegister (
2019 connectorStatus
: ConnectorStatus
| undefined,
2022 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2025 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2026 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2028 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2033 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2034 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2036 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2040 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2041 return stationTemplate
?.useConnectorId0
?? true
2044 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2045 if (this.hasEvses
) {
2046 for (const [evseId
, evseStatus
] of this.evses
) {
2050 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2051 if (connectorStatus
.transactionStarted
=== true) {
2052 await this.stopTransactionOnConnector(connectorId
, reason
)
2057 for (const connectorId
of this.connectors
.keys()) {
2058 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2059 await this.stopTransactionOnConnector(connectorId
, reason
)
2066 private getConnectionTimeout (): number {
2067 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2068 return convertToInt(
2069 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2070 Constants
.DEFAULT_CONNECTION_TIMEOUT
2073 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2076 private getPowerDivider (): number {
2077 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2078 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2079 powerDivider
= this.getNumberOfRunningTransactions()
2084 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2085 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2086 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2087 switch (this.getCurrentOutType(stationInfo
)) {
2088 case CurrentType
.AC
:
2089 return ACElectricUtils
.amperagePerPhaseFromPower(
2090 this.getNumberOfPhases(stationInfo
),
2091 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2092 this.getVoltageOut(stationInfo
)
2094 case CurrentType
.DC
:
2095 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2099 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2100 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2101 return (stationInfo
?? this.stationInfo
!).currentOutType
?? CurrentType
.AC
2104 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2106 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2107 (stationInfo
?? this.stationInfo
!).voltageOut
??
2108 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2112 private getAmperageLimitation (): number | undefined {
2114 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2115 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2118 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2119 getAmperageLimitationUnitDivider(this.stationInfo
)
2124 private async startMessageSequence (): Promise
<void> {
2125 if (this.stationInfo
?.autoRegister
=== true) {
2126 await this.ocppRequestService
.requestHandler
<
2127 BootNotificationRequest
,
2128 BootNotificationResponse
2129 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2130 skipBufferingOnError
: true
2133 // Start WebSocket ping
2134 this.startWebSocketPing()
2136 this.startHeartbeat()
2137 // Initialize connectors status
2138 if (this.hasEvses
) {
2139 for (const [evseId
, evseStatus
] of this.evses
) {
2141 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2142 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2143 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2148 for (const connectorId
of this.connectors
.keys()) {
2149 if (connectorId
> 0) {
2150 const connectorBootStatus
= getBootConnectorStatus(
2153 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2154 this.getConnectorStatus(connectorId
)!
2156 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2160 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2161 await this.ocppRequestService
.requestHandler
<
2162 FirmwareStatusNotificationRequest
,
2163 FirmwareStatusNotificationResponse
2164 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2165 status: FirmwareStatus
.Installed
2167 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2171 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2172 this.startAutomaticTransactionGenerator()
2174 this.flushMessageBuffer()
2177 private async stopMessageSequence (
2178 reason
?: StopTransactionReason
,
2179 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
2181 // Stop WebSocket ping
2182 this.stopWebSocketPing()
2184 this.stopHeartbeat()
2186 if (this.automaticTransactionGenerator
?.started
=== true) {
2187 this.stopAutomaticTransactionGenerator()
2189 // Stop ongoing transactions
2190 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2191 if (this.hasEvses
) {
2192 for (const [evseId
, evseStatus
] of this.evses
) {
2194 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2195 await this.ocppRequestService
.requestHandler
<
2196 StatusNotificationRequest
,
2197 StatusNotificationResponse
2200 RequestCommand
.STATUS_NOTIFICATION
,
2201 buildStatusNotificationRequest(
2204 ConnectorStatusEnum
.Unavailable
,
2208 delete connectorStatus
.status
2213 for (const connectorId
of this.connectors
.keys()) {
2214 if (connectorId
> 0) {
2215 await this.ocppRequestService
.requestHandler
<
2216 StatusNotificationRequest
,
2217 StatusNotificationResponse
2220 RequestCommand
.STATUS_NOTIFICATION
,
2221 buildStatusNotificationRequest(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2223 delete this.getConnectorStatus(connectorId
)?.status
2229 private startWebSocketPing (): void {
2230 const webSocketPingInterval
=
2231 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2233 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2236 if (webSocketPingInterval
> 0 && this.webSocketPingSetInterval
== null) {
2237 this.webSocketPingSetInterval
= setInterval(() => {
2238 if (this.isWebSocketConnectionOpened()) {
2239 this.wsConnection
?.ping()
2241 }, secondsToMilliseconds(webSocketPingInterval
))
2243 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2244 webSocketPingInterval
2247 } else if (this.webSocketPingSetInterval
!= null) {
2249 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2250 webSocketPingInterval
2255 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2260 private stopWebSocketPing (): void {
2261 if (this.webSocketPingSetInterval
!= null) {
2262 clearInterval(this.webSocketPingSetInterval
)
2263 delete this.webSocketPingSetInterval
2267 private getConfiguredSupervisionUrl (): URL
{
2268 let configuredSupervisionUrl
: string
2269 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2270 if (isNotEmptyArray(supervisionUrls
)) {
2271 let configuredSupervisionUrlIndex
: number
2272 switch (Configuration
.getSupervisionUrlDistribution()) {
2273 case SupervisionUrlDistribution
.RANDOM
:
2274 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2276 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2277 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2279 !Object.values(SupervisionUrlDistribution
).includes(
2280 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2281 Configuration
.getSupervisionUrlDistribution()!
2284 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2285 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2286 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2289 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2292 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2294 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2295 configuredSupervisionUrl
= supervisionUrls
!
2297 if (isNotEmptyString(configuredSupervisionUrl
)) {
2298 return new URL(configuredSupervisionUrl
)
2300 const errorMsg
= 'No supervision url(s) configured'
2301 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2302 throw new BaseError(errorMsg
)
2305 private stopHeartbeat (): void {
2306 if (this.heartbeatSetInterval
!= null) {
2307 clearInterval(this.heartbeatSetInterval
)
2308 delete this.heartbeatSetInterval
2312 private terminateWSConnection (): void {
2313 if (this.isWebSocketConnectionOpened()) {
2314 this.wsConnection
?.terminate()
2315 this.wsConnection
= null
2319 private async reconnect (): Promise
<void> {
2320 // Stop WebSocket ping
2321 this.stopWebSocketPing()
2323 this.stopHeartbeat()
2324 // Stop the ATG if needed
2325 if (this.getAutomaticTransactionGeneratorConfiguration()?.stopOnConnectionFailure
=== true) {
2326 this.stopAutomaticTransactionGenerator()
2329 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2330 this.autoReconnectRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2331 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2333 ++this.autoReconnectRetryCount
2334 const reconnectDelay
=
2335 this.stationInfo
?.reconnectExponentialDelay
=== true
2336 ? exponentialDelay(this.autoReconnectRetryCount
)
2337 : secondsToMilliseconds(this.getConnectionTimeout())
2338 const reconnectDelayWithdraw
= 1000
2339 const reconnectTimeout
=
2340 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2342 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2345 )}ms, timeout ${reconnectTimeout}ms`
2347 await sleep(reconnectDelay
)
2349 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2351 this.openWSConnection(
2353 handshakeTimeout
: reconnectTimeout
2355 { closeOpened
: true }
2357 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2359 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2360 this.autoReconnectRetryCount
2361 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`