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
, rmSync
, writeFileSync
} from
'node:fs'
6 import { dirname
, join
, parse
} from
'node:path'
7 import { URL
} from
'node:url'
8 import { parentPort
} from
'node:worker_threads'
10 import { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns'
11 import { mergeDeepRight
} from
'rambda'
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 setChargingStationOptions
,
46 stationTemplateToStationInfo
,
47 warnTemplateKeysDeprecation
49 import { IdTagsCache
} from
'./IdTagsCache.js'
51 OCPP16IncomingRequestService
,
53 OCPP16ResponseService
,
54 OCPP20IncomingRequestService
,
56 OCPP20ResponseService
,
57 type OCPPIncomingRequestService
,
58 type OCPPRequestService
,
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 ChargingStationOptions
,
78 type ChargingStationTemplate
,
86 type EvseStatusConfiguration
,
89 type FirmwareStatusNotificationRequest
,
90 type FirmwareStatusNotificationResponse
,
91 type HeartbeatRequest
,
92 type HeartbeatResponse
,
94 type IncomingRequestCommand
,
97 type MeterValuesRequest
,
98 type MeterValuesResponse
,
100 type OutgoingRequest
,
102 RegistrationStatusEnumType
,
106 ReservationTerminationReason
,
108 StandardParametersKey
,
110 type StopTransactionReason
,
111 type StopTransactionRequest
,
112 type StopTransactionResponse
,
113 SupervisionUrlDistribution
,
114 SupportedFeatureProfiles
,
117 WebSocketCloseEventStatusCode
,
119 } from
'../types/index.js'
128 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
129 buildConnectorsStatus
,
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
165 public ocppConfiguration
?: ChargingStationOcppConfiguration
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
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 wsConnectionRetried
: boolean
186 private wsConnectionRetryCount
: number
187 private templateFileWatcher
?: FSWatcher
188 private templateFileHash
!: string
189 private readonly sharedLRUCache
: SharedLRUCache
190 private wsPingSetInterval
?: NodeJS
.Timeout
191 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
192 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
194 constructor (index
: number, templateFile
: string, options
?: ChargingStationOptions
) {
197 this.starting
= false
198 this.stopping
= false
199 this.wsConnection
= null
200 this.wsConnectionRetried
= false
201 this.wsConnectionRetryCount
= 0
203 this.templateFile
= templateFile
204 this.connectors
= new Map
<number, ConnectorStatus
>()
205 this.evses
= new Map
<number, EvseStatus
>()
206 this.requests
= new Map
<string, CachedRequest
>()
207 this.messageBuffer
= new Set
<string>()
208 this.sharedLRUCache
= SharedLRUCache
.getInstance()
209 this.idTagsCache
= IdTagsCache
.getInstance()
210 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this)
212 this.on(ChargingStationEvents
.added
, () => {
213 parentPort
?.postMessage(buildAddedMessage(this))
215 this.on(ChargingStationEvents
.deleted
, () => {
216 parentPort
?.postMessage(buildDeletedMessage(this))
218 this.on(ChargingStationEvents
.started
, () => {
219 parentPort
?.postMessage(buildStartedMessage(this))
221 this.on(ChargingStationEvents
.stopped
, () => {
222 parentPort
?.postMessage(buildStoppedMessage(this))
224 this.on(ChargingStationEvents
.updated
, () => {
225 parentPort
?.postMessage(buildUpdatedMessage(this))
227 this.on(ChargingStationEvents
.accepted
, () => {
228 this.startMessageSequence(
229 this.wsConnectionRetried
231 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
233 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
235 this.wsConnectionRetried
= false
237 this.on(ChargingStationEvents
.rejected
, () => {
238 this.wsConnectionRetried
= false
240 this.on(ChargingStationEvents
.disconnected
, () => {
242 this.internalStopMessageSequence()
245 `${this.logPrefix()} Error while stopping the internal message sequence:`,
251 this.initialize(options
)
255 if (this.stationInfo
?.autoStart
=== true) {
260 public get
hasEvses (): boolean {
261 return this.connectors
.size
=== 0 && this.evses
.size
> 0
264 public get
wsConnectionUrl (): URL
{
267 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
268 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
269 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
270 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
271 : this.configuredSupervisionUrl.href
272 }/${this.stationInfo?.chargingStationId}`
276 public logPrefix
= (): string => {
278 this instanceof ChargingStation
&&
279 this.stationInfo
!= null &&
280 isNotEmptyString(this.stationInfo
.chargingStationId
)
282 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
284 let stationTemplate
: ChargingStationTemplate
| undefined
286 stationTemplate
= JSON
.parse(
287 readFileSync(this.templateFile
, 'utf8')
288 ) as ChargingStationTemplate
290 stationTemplate
= undefined
292 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
295 public hasIdTags (): boolean {
296 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
297 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
300 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
301 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
302 const localStationInfo
= stationInfo
?? this.stationInfo
!
303 switch (this.getCurrentOutType(stationInfo
)) {
305 return localStationInfo
.numberOfPhases
?? 3
311 public isWebSocketConnectionOpened (): boolean {
312 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
315 public inUnknownState (): boolean {
316 return this.bootNotificationResponse
?.status == null
319 public inPendingState (): boolean {
320 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
323 public inAcceptedState (): boolean {
324 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
327 public inRejectedState (): boolean {
328 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
331 public isRegistered (): boolean {
332 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
335 public isChargingStationAvailable (): boolean {
336 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
339 public hasConnector (connectorId
: number): boolean {
341 for (const evseStatus
of this.evses
.values()) {
342 if (evseStatus
.connectors
.has(connectorId
)) {
348 return this.connectors
.has(connectorId
)
351 public isConnectorAvailable (connectorId
: number): boolean {
354 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
358 public getNumberOfConnectors (): number {
360 let numberOfConnectors
= 0
361 for (const [evseId
, evseStatus
] of this.evses
) {
363 numberOfConnectors
+= evseStatus
.connectors
.size
366 return numberOfConnectors
368 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
371 public getNumberOfEvses (): number {
372 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
375 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
377 for (const evseStatus
of this.evses
.values()) {
378 if (evseStatus
.connectors
.has(connectorId
)) {
379 return evseStatus
.connectors
.get(connectorId
)
384 return this.connectors
.get(connectorId
)
387 public getConnectorMaximumAvailablePower (connectorId
: number): number {
388 let connectorAmperageLimitationPowerLimit
: number | undefined
389 const amperageLimitation
= this.getAmperageLimitation()
391 amperageLimitation
!= null &&
392 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
393 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
395 connectorAmperageLimitationPowerLimit
=
396 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
397 ? ACElectricUtils
.powerTotal(
398 this.getNumberOfPhases(),
399 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
400 this.stationInfo
.voltageOut
!,
402 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
404 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
405 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
406 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
409 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
410 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
411 const connectorChargingProfilesPowerLimit
=
412 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
414 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
415 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
416 isNaN(connectorAmperageLimitationPowerLimit
!)
418 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
419 connectorAmperageLimitationPowerLimit
!,
420 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
421 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
425 public getTransactionIdTag (transactionId
: number): string | undefined {
427 for (const evseStatus
of this.evses
.values()) {
428 for (const connectorStatus
of evseStatus
.connectors
.values()) {
429 if (connectorStatus
.transactionId
=== transactionId
) {
430 return connectorStatus
.transactionIdTag
435 for (const connectorId
of this.connectors
.keys()) {
436 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
437 return this.getConnectorStatus(connectorId
)?.transactionIdTag
443 public getNumberOfRunningTransactions (): number {
444 let numberOfRunningTransactions
= 0
446 for (const [evseId
, evseStatus
] of this.evses
) {
450 for (const connectorStatus
of evseStatus
.connectors
.values()) {
451 if (connectorStatus
.transactionStarted
=== true) {
452 ++numberOfRunningTransactions
457 for (const connectorId
of this.connectors
.keys()) {
458 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
459 ++numberOfRunningTransactions
463 return numberOfRunningTransactions
466 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
467 if (transactionId
== null) {
469 } else if (this.hasEvses
) {
470 for (const evseStatus
of this.evses
.values()) {
471 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
472 if (connectorStatus
.transactionId
=== transactionId
) {
478 for (const connectorId
of this.connectors
.keys()) {
479 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
486 public getEnergyActiveImportRegisterByTransactionId (
487 transactionId
: number | undefined,
490 return this.getEnergyActiveImportRegister(
491 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
492 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
497 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
498 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
501 public getAuthorizeRemoteTxRequests (): boolean {
502 const authorizeRemoteTxRequests
= getConfigurationKey(
504 StandardParametersKey
.AuthorizeRemoteTxRequests
506 return authorizeRemoteTxRequests
!= null
507 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
511 public getLocalAuthListEnabled (): boolean {
512 const localAuthListEnabled
= getConfigurationKey(
514 StandardParametersKey
.LocalAuthListEnabled
516 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
519 public getHeartbeatInterval (): number {
520 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
521 if (HeartbeatInterval
!= null) {
522 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
524 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
525 if (HeartBeatInterval
!= null) {
526 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
528 this.stationInfo
?.autoRegister
=== false &&
530 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
531 Constants.DEFAULT_HEARTBEAT_INTERVAL
534 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
537 public setSupervisionUrl (url
: string): void {
539 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
540 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
542 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
544 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
545 this.stationInfo
!.supervisionUrls
= url
546 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
547 this.saveStationInfo()
551 public startHeartbeat (): void {
552 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
553 this.heartbeatSetInterval
= setInterval(() => {
554 this.ocppRequestService
555 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
558 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
562 }, this.getHeartbeatInterval())
564 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
565 this.getHeartbeatInterval()
568 } else if (this.heartbeatSetInterval
!= null) {
570 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
571 this.getHeartbeatInterval()
576 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
581 public restartHeartbeat (): void {
585 this.startHeartbeat()
588 public restartWebSocketPing (): void {
589 // Stop WebSocket ping
590 this.stopWebSocketPing()
591 // Start WebSocket ping
592 this.startWebSocketPing()
595 public startMeterValues (connectorId
: number, interval
: number): void {
596 if (connectorId
=== 0) {
597 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
600 const connectorStatus
= this.getConnectorStatus(connectorId
)
601 if (connectorStatus
== null) {
603 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
608 if (connectorStatus
.transactionStarted
=== false) {
610 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
614 connectorStatus
.transactionStarted
=== true &&
615 connectorStatus
.transactionId
== null
618 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
623 connectorStatus
.transactionSetInterval
= setInterval(() => {
624 const meterValue
= buildMeterValue(
627 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
628 connectorStatus
.transactionId
!,
631 this.ocppRequestService
632 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
634 RequestCommand
.METER_VALUES
,
637 transactionId
: connectorStatus
.transactionId
,
638 meterValue
: [meterValue
]
643 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
650 `${this.logPrefix()} Charging station ${
651 StandardParametersKey.MeterValueSampleInterval
652 } configuration set to ${interval}, not sending MeterValues`
657 public stopMeterValues (connectorId
: number): void {
658 const connectorStatus
= this.getConnectorStatus(connectorId
)
659 if (connectorStatus
?.transactionSetInterval
!= null) {
660 clearInterval(connectorStatus
.transactionSetInterval
)
664 private add (): void {
665 this.emit(ChargingStationEvents
.added
)
668 public async delete (deleteConfiguration
= true): Promise
<void> {
672 AutomaticTransactionGenerator
.deleteInstance(this)
673 PerformanceStatistics
.deleteInstance(this.stationInfo
?.hashId
)
674 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
675 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
676 this.requests
.clear()
677 this.connectors
.clear()
679 this.templateFileWatcher
?.unref()
680 deleteConfiguration
&& rmSync(this.configurationFile
, { force
: true })
681 this.chargingStationWorkerBroadcastChannel
.unref()
682 this.emit(ChargingStationEvents
.deleted
)
683 this.removeAllListeners()
686 public start (): void {
688 if (!this.starting
) {
690 if (this.stationInfo
?.enableStatistics
=== true) {
691 this.performanceStatistics
?.start()
693 this.openWSConnection()
694 // Monitor charging station template file
695 this.templateFileWatcher
= watchJsonFile(
697 FileType
.ChargingStationTemplate
,
700 (event
, filename
): void => {
701 if (isNotEmptyString(filename
) && event
=== 'change') {
704 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
706 } file have changed, reload`
708 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
709 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
710 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
714 const ATGStarted
= this.automaticTransactionGenerator
?.started
715 if (ATGStarted
=== true) {
716 this.stopAutomaticTransactionGenerator()
718 delete this.automaticTransactionGeneratorConfiguration
720 this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true &&
723 this.startAutomaticTransactionGenerator(undefined, true)
725 if (this.stationInfo
?.enableStatistics
=== true) {
726 this.performanceStatistics
?.restart()
728 this.performanceStatistics
?.stop()
730 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
733 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
741 this.emit(ChargingStationEvents
.started
)
742 this.starting
= false
744 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
747 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
752 reason
?: StopTransactionReason
,
753 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
756 if (!this.stopping
) {
758 await this.stopMessageSequence(reason
, stopTransactions
)
759 this.closeWSConnection()
760 if (this.stationInfo
?.enableStatistics
=== true) {
761 this.performanceStatistics
?.stop()
763 this.templateFileWatcher
?.close()
764 delete this.bootNotificationResponse
766 this.saveConfiguration()
767 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
768 this.emit(ChargingStationEvents
.stopped
)
769 this.stopping
= false
771 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
774 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
778 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
779 await this.stop(reason
)
780 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
781 await sleep(this.stationInfo
!.resetTime
!)
786 public saveOcppConfiguration (): void {
787 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
788 this.saveConfiguration()
792 public bufferMessage (message
: string): void {
793 this.messageBuffer
.add(message
)
794 this.setIntervalFlushMessageBuffer()
797 public openWSConnection (
799 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
802 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
803 ...this.stationInfo
?.wsOptions
,
806 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
807 if (!checkChargingStation(this, this.logPrefix())) {
810 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
811 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
813 if (params
.closeOpened
=== true) {
814 this.closeWSConnection()
816 if (params
.terminateOpened
=== true) {
817 this.terminateWSConnection()
820 if (this.isWebSocketConnectionOpened()) {
822 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.href} is already opened`
827 logger
.info(`${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.href}`)
829 this.wsConnection
= new WebSocket(
830 this.wsConnectionUrl
,
831 `ocpp${this.stationInfo?.ocppVersion}`,
835 // Handle WebSocket message
836 this.wsConnection
.on('message', data
=> {
837 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
839 // Handle WebSocket error
840 this.wsConnection
.on('error', this.onError
.bind(this))
841 // Handle WebSocket close
842 this.wsConnection
.on('close', this.onClose
.bind(this))
843 // Handle WebSocket open
844 this.wsConnection
.on('open', () => {
845 this.onOpen().catch(error
=>
846 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
849 // Handle WebSocket ping
850 this.wsConnection
.on('ping', this.onPing
.bind(this))
851 // Handle WebSocket pong
852 this.wsConnection
.on('pong', this.onPong
.bind(this))
855 public closeWSConnection (): void {
856 if (this.isWebSocketConnectionOpened()) {
857 this.wsConnection
?.close()
858 this.wsConnection
= null
862 public getAutomaticTransactionGeneratorConfiguration ():
863 | AutomaticTransactionGeneratorConfiguration
865 if (this.automaticTransactionGeneratorConfiguration
== null) {
866 let automaticTransactionGeneratorConfiguration
:
867 | AutomaticTransactionGeneratorConfiguration
869 const stationTemplate
= this.getTemplateFromFile()
870 const stationConfiguration
= this.getConfigurationFromFile()
872 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
873 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
874 stationConfiguration
?.automaticTransactionGenerator
!= null
876 automaticTransactionGeneratorConfiguration
=
877 stationConfiguration
.automaticTransactionGenerator
879 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
881 this.automaticTransactionGeneratorConfiguration
= {
882 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
883 ...automaticTransactionGeneratorConfiguration
886 return this.automaticTransactionGeneratorConfiguration
889 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
890 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
893 public startAutomaticTransactionGenerator (
894 connectorIds
?: number[],
895 stopAbsoluteDuration
?: boolean
897 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
898 if (isNotEmptyArray(connectorIds
)) {
899 for (const connectorId
of connectorIds
) {
900 this.automaticTransactionGenerator
?.startConnector(connectorId
, stopAbsoluteDuration
)
903 this.automaticTransactionGenerator
?.start(stopAbsoluteDuration
)
905 this.saveAutomaticTransactionGeneratorConfiguration()
906 this.emit(ChargingStationEvents
.updated
)
909 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
910 if (isNotEmptyArray(connectorIds
)) {
911 for (const connectorId
of connectorIds
) {
912 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
915 this.automaticTransactionGenerator
?.stop()
917 this.saveAutomaticTransactionGeneratorConfiguration()
918 this.emit(ChargingStationEvents
.updated
)
921 public async stopTransactionOnConnector (
923 reason
?: StopTransactionReason
924 ): Promise
<StopTransactionResponse
> {
925 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
927 this.stationInfo
?.beginEndMeterValues
=== true &&
928 this.stationInfo
.ocppStrictCompliance
=== true &&
929 this.stationInfo
.outOfOrderEndMeterValues
=== false
931 const transactionEndMeterValue
= buildTransactionEndMeterValue(
934 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
936 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
938 RequestCommand
.METER_VALUES
,
942 meterValue
: [transactionEndMeterValue
]
946 return await this.ocppRequestService
.requestHandler
<
947 StopTransactionRequest
,
948 StopTransactionResponse
949 >(this, RequestCommand
.STOP_TRANSACTION
, {
951 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
952 ...(reason
!= null && { reason
})
956 public getReserveConnectorZeroSupported (): boolean {
957 return convertToBoolean(
958 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
959 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
963 public async addReservation (reservation
: Reservation
): Promise
<void> {
964 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
965 if (reservationFound
!= null) {
966 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
968 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
969 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
970 await sendAndSetConnectorStatus(
972 reservation
.connectorId
,
973 ConnectorStatusEnum
.Reserved
,
975 { send
: reservation
.connectorId
!== 0 }
979 public async removeReservation (
980 reservation
: Reservation
,
981 reason
: ReservationTerminationReason
983 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
984 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
986 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
987 case ReservationTerminationReason
.TRANSACTION_STARTED
:
988 delete connector
.reservation
990 case ReservationTerminationReason
.RESERVATION_CANCELED
:
991 case ReservationTerminationReason
.REPLACE_EXISTING
:
992 case ReservationTerminationReason
.EXPIRED
:
993 await sendAndSetConnectorStatus(
995 reservation
.connectorId
,
996 ConnectorStatusEnum
.Available
,
998 { send
: reservation
.connectorId
!== 0 }
1000 delete connector
.reservation
1003 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1004 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
1008 public getReservationBy (
1009 filterKey
: ReservationKey
,
1010 value
: number | string
1011 ): Reservation
| undefined {
1012 if (this.hasEvses
) {
1013 for (const evseStatus
of this.evses
.values()) {
1014 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1015 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1016 return connectorStatus
.reservation
1021 for (const connectorStatus
of this.connectors
.values()) {
1022 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1023 return connectorStatus
.reservation
1029 public isConnectorReservable (
1030 reservationId
: number,
1032 connectorId
?: number
1034 const reservation
= this.getReservationBy('reservationId', reservationId
)
1035 const reservationExists
= reservation
!= null && !hasReservationExpired(reservation
)
1036 if (arguments.length
=== 1) {
1037 return !reservationExists
1038 } else if (arguments.length
> 1) {
1039 const userReservation
= idTag
!= null ? this.getReservationBy('idTag', idTag
) : undefined
1040 const userReservationExists
=
1041 userReservation
!= null && !hasReservationExpired(userReservation
)
1042 const notConnectorZero
= connectorId
== null ? true : connectorId
> 0
1043 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
1045 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1051 private setIntervalFlushMessageBuffer (): void {
1052 if (this.flushMessageBufferSetInterval
== null) {
1053 this.flushMessageBufferSetInterval
= setInterval(() => {
1054 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
1055 this.flushMessageBuffer()
1057 if (this.messageBuffer
.size
=== 0) {
1058 this.clearIntervalFlushMessageBuffer()
1060 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1064 private clearIntervalFlushMessageBuffer (): void {
1065 if (this.flushMessageBufferSetInterval
!= null) {
1066 clearInterval(this.flushMessageBufferSetInterval
)
1067 delete this.flushMessageBufferSetInterval
1071 private getNumberOfReservableConnectors (): number {
1072 let numberOfReservableConnectors
= 0
1073 if (this.hasEvses
) {
1074 for (const evseStatus
of this.evses
.values()) {
1075 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1078 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1080 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1083 private getNumberOfReservationsOnConnectorZero (): number {
1085 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1086 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1093 private flushMessageBuffer (): void {
1094 if (this.messageBuffer
.size
> 0) {
1095 for (const message
of this.messageBuffer
.values()) {
1096 let beginId
: string | undefined
1097 let commandName
: RequestCommand
| undefined
1098 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1099 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1101 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1102 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1104 this.wsConnection
?.send(message
, (error
?: Error) => {
1105 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1106 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1107 if (error
== null) {
1109 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1111 )} OCPP message sent '${JSON.stringify(message)}'`
1113 this.messageBuffer
.delete(message
)
1116 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1118 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1127 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1128 let template
: ChargingStationTemplate
| undefined
1130 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1131 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1133 const measureId
= `${FileType.ChargingStationTemplate} read`
1134 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1135 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1136 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1137 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1138 .update(JSON
.stringify(template
))
1140 this.sharedLRUCache
.setChargingStationTemplate(template
)
1141 this.templateFileHash
= template
.templateHash
1144 handleFileException(
1146 FileType
.ChargingStationTemplate
,
1147 error
as NodeJS
.ErrnoException
,
1154 private getStationInfoFromTemplate (): ChargingStationInfo
{
1155 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1156 const stationTemplate
= this.getTemplateFromFile()!
1157 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1158 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1159 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1160 if (stationTemplate
.Connectors
!= null) {
1161 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1163 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1164 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
1165 stationInfo
.templateIndex
= this.index
1166 stationInfo
.templateName
= parse(this.templateFile
).name
1167 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
)
1168 createSerialNumber(stationTemplate
, stationInfo
)
1169 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
)
1170 if (isNotEmptyArray(stationTemplate
.power
)) {
1171 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
)
1172 stationInfo
.maximumPower
=
1173 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1174 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1175 : stationTemplate
.power
[powerArrayRandomIndex
]
1177 stationInfo
.maximumPower
=
1178 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
1179 ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1180 stationTemplate
.power
! * 1000
1181 : stationTemplate
.power
1183 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
)
1185 isNotEmptyString(stationInfo
.firmwareVersionPattern
) &&
1186 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1187 !new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
)
1190 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1192 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1195 stationInfo
.firmwareUpgrade
= mergeDeepRight(
1202 stationTemplate
.firmwareUpgrade
?? {}
1204 if (stationTemplate
.resetTime
!= null) {
1205 stationInfo
.resetTime
= secondsToMilliseconds(stationTemplate
.resetTime
)
1210 private getStationInfoFromFile (
1211 stationInfoPersistentConfiguration
: boolean | undefined = Constants
.DEFAULT_STATION_INFO
1212 .stationInfoPersistentConfiguration
1213 ): ChargingStationInfo
| undefined {
1214 let stationInfo
: ChargingStationInfo
| undefined
1215 if (stationInfoPersistentConfiguration
=== true) {
1216 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1217 if (stationInfo
!= null) {
1218 delete stationInfo
.infoHash
1219 delete (stationInfo
as ChargingStationTemplate
).numberOfConnectors
1220 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1221 if (stationInfo
.templateIndex
== null) {
1222 stationInfo
.templateIndex
= this.index
1224 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1225 if (stationInfo
.templateName
== null) {
1226 stationInfo
.templateName
= parse(this.templateFile
).name
1233 private getStationInfo (options
?: ChargingStationOptions
): ChargingStationInfo
{
1234 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1235 options
?.persistentConfiguration
!= null &&
1236 (stationInfoFromTemplate
.stationInfoPersistentConfiguration
= options
.persistentConfiguration
)
1237 const stationInfoFromFile
= this.getStationInfoFromFile(
1238 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1241 // 1. charging station info from template
1242 // 2. charging station info from configuration file
1244 stationInfoFromFile
!= null &&
1245 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1247 return setChargingStationOptions(
1248 { ...Constants
.DEFAULT_STATION_INFO
, ...stationInfoFromFile
},
1252 stationInfoFromFile
!= null &&
1253 propagateSerialNumber(
1254 this.getTemplateFromFile(),
1255 stationInfoFromFile
,
1256 stationInfoFromTemplate
1258 return setChargingStationOptions(
1259 { ...Constants
.DEFAULT_STATION_INFO
, ...stationInfoFromTemplate
},
1264 private saveStationInfo (): void {
1265 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1266 this.saveConfiguration()
1270 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1271 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1272 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1273 throw new BaseError(errorMsg
)
1276 private initialize (options
?: ChargingStationOptions
): void {
1277 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1278 const stationTemplate
= this.getTemplateFromFile()!
1279 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1280 this.configurationFile
= join(
1281 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1282 `${getHashId(this.index, stationTemplate)}.json`
1284 const stationConfiguration
= this.getConfigurationFromFile()
1286 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1287 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1289 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1290 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1292 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1294 this.stationInfo
= this.getStationInfo(options
)
1296 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1297 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
) &&
1298 isNotEmptyString(this.stationInfo
.firmwareVersion
)
1300 const patternGroup
=
1301 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1302 this.stationInfo
.firmwareVersion
.split('.').length
1303 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1304 .exec(this.stationInfo
.firmwareVersion
)
1305 ?.slice(1, patternGroup
+ 1)
1306 if (match
!= null) {
1307 const patchLevelIndex
= match
.length
- 1
1308 match
[patchLevelIndex
] = (
1309 convertToInt(match
[patchLevelIndex
]) +
1310 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1311 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1313 this.stationInfo
.firmwareVersion
= match
.join('.')
1316 this.saveStationInfo()
1317 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1318 if (this.stationInfo
.enableStatistics
=== true) {
1319 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1320 this.stationInfo
.hashId
,
1321 this.stationInfo
.chargingStationId
,
1322 this.configuredSupervisionUrl
1325 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1326 if (bootNotificationRequest
== null) {
1327 const errorMsg
= 'Error while creating boot notification request'
1328 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1329 throw new BaseError(errorMsg
)
1331 this.bootNotificationRequest
= bootNotificationRequest
1332 this.powerDivider
= this.getPowerDivider()
1333 // OCPP configuration
1334 this.ocppConfiguration
= this.getOcppConfiguration(options
?.persistentConfiguration
)
1335 this.initializeOcppConfiguration()
1336 this.initializeOcppServices()
1337 if (this.stationInfo
.autoRegister
=== true) {
1338 this.bootNotificationResponse
= {
1339 currentTime
: new Date(),
1340 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1341 status: RegistrationStatusEnumType
.ACCEPTED
1346 private initializeOcppServices (): void {
1347 const ocppVersion
= this.stationInfo
?.ocppVersion
1348 switch (ocppVersion
) {
1349 case OCPPVersion
.VERSION_16
:
1350 this.ocppIncomingRequestService
=
1351 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1352 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1353 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1356 case OCPPVersion
.VERSION_20
:
1357 case OCPPVersion
.VERSION_201
:
1358 this.ocppIncomingRequestService
=
1359 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1360 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1361 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1365 this.handleUnsupportedVersion(ocppVersion
)
1370 private initializeOcppConfiguration (): void {
1371 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1372 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1374 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1375 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1378 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1379 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1380 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1382 addConfigurationKey(
1384 this.stationInfo
.supervisionUrlOcppKey
,
1385 this.configuredSupervisionUrl
.href
,
1389 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1390 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1391 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1393 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1396 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1397 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1399 addConfigurationKey(
1401 this.stationInfo
.amperageLimitationOcppKey
,
1403 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1404 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1407 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1408 addConfigurationKey(
1410 StandardParametersKey
.SupportedFeatureProfiles
,
1411 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1414 addConfigurationKey(
1416 StandardParametersKey
.NumberOfConnectors
,
1417 this.getNumberOfConnectors().toString(),
1421 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1422 addConfigurationKey(
1424 StandardParametersKey
.MeterValuesSampledData
,
1425 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1428 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1429 const connectorsPhaseRotation
: string[] = []
1430 if (this.hasEvses
) {
1431 for (const evseStatus
of this.evses
.values()) {
1432 for (const connectorId
of evseStatus
.connectors
.keys()) {
1433 connectorsPhaseRotation
.push(
1434 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1435 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1440 for (const connectorId
of this.connectors
.keys()) {
1441 connectorsPhaseRotation
.push(
1442 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1443 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1447 addConfigurationKey(
1449 StandardParametersKey
.ConnectorPhaseRotation
,
1450 connectorsPhaseRotation
.toString()
1453 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1454 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1457 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1458 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1460 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1462 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1463 addConfigurationKey(
1465 StandardParametersKey
.ConnectionTimeOut
,
1466 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1469 this.saveOcppConfiguration()
1472 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1473 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1474 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1475 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1477 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1478 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1479 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1480 delete evseStatus
.connectorsStatus
1481 this.evses
.set(evseId
, {
1482 ...(evseStatus
as EvseStatus
),
1483 connectors
: new Map
<number, ConnectorStatus
>(
1484 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1485 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1492 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1493 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1494 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1495 throw new BaseError(errorMsg
)
1497 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1498 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1499 throw new BaseError(errorMsg
)
1503 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1504 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1505 this.initializeConnectorsFromTemplate(stationTemplate
)
1506 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1507 this.initializeEvsesFromTemplate(stationTemplate
)
1508 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1509 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1510 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1511 throw new BaseError(errorMsg
)
1513 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1514 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1515 throw new BaseError(errorMsg
)
1519 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1520 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1521 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1522 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1523 throw new BaseError(errorMsg
)
1525 if (stationTemplate
.Connectors
?.[0] == null) {
1527 `${this.logPrefix()} Charging station information from template ${
1529 } with no connector id 0 configuration`
1532 if (stationTemplate
.Connectors
!= null) {
1533 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1534 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1535 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1537 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1540 const connectorsConfigChanged
=
1541 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1542 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1543 connectorsConfigChanged
&& this.connectors
.clear()
1544 this.connectorsConfigurationHash
= connectorsConfigHash
1545 if (templateMaxConnectors
> 0) {
1546 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1548 connectorId
=== 0 &&
1549 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1550 (stationTemplate
.Connectors
[connectorId
] == null ||
1551 !this.getUseConnectorId0(stationTemplate
))
1555 const templateConnectorId
=
1556 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1557 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1559 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1560 checkStationInfoConnectorStatus(
1561 templateConnectorId
,
1566 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1568 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1569 this.saveConnectorsStatus()
1572 `${this.logPrefix()} Charging station information from template ${
1574 } with no connectors configuration defined, cannot create connectors`
1580 `${this.logPrefix()} Charging station information from template ${
1582 } with no connectors configuration defined, using already defined connectors`
1587 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1588 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1589 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1590 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1591 throw new BaseError(errorMsg
)
1593 if (stationTemplate
.Evses
?.[0] == null) {
1595 `${this.logPrefix()} Charging station information from template ${
1597 } with no evse id 0 configuration`
1600 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1602 `${this.logPrefix()} Charging station information from template ${
1604 } with evse id 0 with no connector id 0 configuration`
1607 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1609 `${this.logPrefix()} Charging station information from template ${
1611 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1614 if (stationTemplate
.Evses
!= null) {
1615 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1616 .update(JSON
.stringify(stationTemplate
.Evses
))
1618 const evsesConfigChanged
=
1619 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1620 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1621 evsesConfigChanged
&& this.evses
.clear()
1622 this.evsesConfigurationHash
= evsesConfigHash
1623 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1624 if (templateMaxEvses
> 0) {
1625 for (const evseKey
in stationTemplate
.Evses
) {
1626 const evseId
= convertToInt(evseKey
)
1627 this.evses
.set(evseId
, {
1628 connectors
: buildConnectorsMap(
1629 stationTemplate
.Evses
[evseKey
].Connectors
,
1633 availability
: AvailabilityType
.Operative
1635 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1636 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1638 this.saveEvsesStatus()
1641 `${this.logPrefix()} Charging station information from template ${
1643 } with no evses configuration defined, cannot create evses`
1649 `${this.logPrefix()} Charging station information from template ${
1651 } with no evses configuration defined, using already defined evses`
1656 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1657 let configuration
: ChargingStationConfiguration
| undefined
1658 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1660 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1661 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1662 this.configurationFileHash
1665 const measureId
= `${FileType.ChargingStationConfiguration} read`
1666 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1667 configuration
= JSON
.parse(
1668 readFileSync(this.configurationFile
, 'utf8')
1669 ) as ChargingStationConfiguration
1670 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1671 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1672 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1673 this.configurationFileHash
= configuration
.configurationHash
!
1676 handleFileException(
1677 this.configurationFile
,
1678 FileType
.ChargingStationConfiguration
,
1679 error
as NodeJS
.ErrnoException
,
1684 return configuration
1687 private saveAutomaticTransactionGeneratorConfiguration (): void {
1688 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1689 this.saveConfiguration()
1693 private saveConnectorsStatus (): void {
1694 this.saveConfiguration()
1697 private saveEvsesStatus (): void {
1698 this.saveConfiguration()
1701 private saveConfiguration (): void {
1702 if (isNotEmptyString(this.configurationFile
)) {
1704 if (!existsSync(dirname(this.configurationFile
))) {
1705 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1707 const configurationFromFile
= this.getConfigurationFromFile()
1708 let configurationData
: ChargingStationConfiguration
=
1709 configurationFromFile
!= null
1710 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1712 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1713 configurationData
.stationInfo
= this.stationInfo
1715 delete configurationData
.stationInfo
1718 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1719 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1721 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1723 delete configurationData
.configurationKey
1725 configurationData
= mergeDeepRight(
1727 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1729 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
!== true) {
1730 delete configurationData
.automaticTransactionGenerator
1732 if (this.connectors
.size
> 0) {
1733 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1735 delete configurationData
.connectorsStatus
1737 if (this.evses
.size
> 0) {
1738 configurationData
.evsesStatus
= buildEvsesStatus(this)
1740 delete configurationData
.evsesStatus
1742 delete configurationData
.configurationHash
1743 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1746 stationInfo
: configurationData
.stationInfo
,
1747 configurationKey
: configurationData
.configurationKey
,
1748 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1749 ...(this.connectors
.size
> 0 && {
1750 connectorsStatus
: configurationData
.connectorsStatus
1752 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1753 } satisfies ChargingStationConfiguration
)
1756 if (this.configurationFileHash
!== configurationHash
) {
1757 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1758 configurationData
.configurationHash
= configurationHash
1759 const measureId
= `${FileType.ChargingStationConfiguration} write`
1760 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1762 this.configurationFile
,
1763 JSON
.stringify(configurationData
, undefined, 2),
1766 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1767 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1768 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1769 this.configurationFileHash
= configurationHash
1771 handleFileException(
1772 this.configurationFile
,
1773 FileType
.ChargingStationConfiguration
,
1774 error
as NodeJS
.ErrnoException
,
1780 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1781 this.configurationFile
1786 handleFileException(
1787 this.configurationFile
,
1788 FileType
.ChargingStationConfiguration
,
1789 error
as NodeJS
.ErrnoException
,
1795 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1800 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1801 return this.getTemplateFromFile()?.Configuration
1804 private getOcppConfigurationFromFile (
1805 ocppPersistentConfiguration
?: boolean
1806 ): ChargingStationOcppConfiguration
| undefined {
1807 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1808 if (ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1809 return { configurationKey
}
1814 private getOcppConfiguration (
1815 ocppPersistentConfiguration
: boolean | undefined = this.stationInfo
?.ocppPersistentConfiguration
1816 ): ChargingStationOcppConfiguration
| undefined {
1817 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1818 this.getOcppConfigurationFromFile(ocppPersistentConfiguration
)
1819 if (ocppConfiguration
== null) {
1820 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1822 return ocppConfiguration
1825 private async onOpen (): Promise
<void> {
1826 if (this.isWebSocketConnectionOpened()) {
1827 this.emit(ChargingStationEvents
.updated
)
1829 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded`
1831 let registrationRetryCount
= 0
1832 if (!this.isRegistered()) {
1833 // Send BootNotification
1835 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1836 BootNotificationRequest
,
1837 BootNotificationResponse
1838 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1839 skipBufferingOnError
: true
1841 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1842 if (this.bootNotificationResponse
?.currentTime
!= null) {
1843 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1844 this.bootNotificationResponse
.currentTime
= convertToDate(
1845 this.bootNotificationResponse
.currentTime
1848 if (!this.isRegistered()) {
1849 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1851 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1852 this.bootNotificationResponse
?.interval
!= null
1853 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1854 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1858 !this.isRegistered() &&
1859 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1860 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1861 this.stationInfo
?.registrationMaxRetries
=== -1)
1864 if (this.isRegistered()) {
1865 this.emit(ChargingStationEvents
.registered
)
1866 if (this.inAcceptedState()) {
1867 this.emit(ChargingStationEvents
.accepted
)
1870 if (this.inRejectedState()) {
1871 this.emit(ChargingStationEvents
.rejected
)
1874 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1875 this.stationInfo?.registrationMaxRetries
1879 this.wsConnectionRetryCount
= 0
1880 this.emit(ChargingStationEvents
.updated
)
1883 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} failed`
1888 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1889 this.emit(ChargingStationEvents
.disconnected
)
1890 this.emit(ChargingStationEvents
.updated
)
1893 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1894 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1896 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1898 )}' and reason '${reason.toString()}'`
1900 this.wsConnectionRetryCount
= 0
1905 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1907 )}' and reason '${reason.toString()}'`
1912 this.emit(ChargingStationEvents
.updated
)
1914 .catch(error
=> logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
))
1919 private getCachedRequest (
1920 messageType
: MessageType
| undefined,
1922 ): CachedRequest
| undefined {
1923 const cachedRequest
= this.requests
.get(messageId
)
1924 if (Array.isArray(cachedRequest
)) {
1925 return cachedRequest
1927 throw new OCPPError(
1928 ErrorType
.PROTOCOL_ERROR
,
1929 `Cached request for message id ${messageId} ${getMessageTypeString(
1931 )} is not an array`,
1937 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1938 const [messageType
, messageId
, commandName
, commandPayload
] = request
1939 if (this.stationInfo
?.enableStatistics
=== true) {
1940 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1943 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1947 // Process the message
1948 await this.ocppIncomingRequestService
.incomingRequestHandler(
1954 this.emit(ChargingStationEvents
.updated
)
1957 private handleResponseMessage (response
: Response
): void {
1958 const [messageType
, messageId
, commandPayload
] = response
1959 if (!this.requests
.has(messageId
)) {
1961 throw new OCPPError(
1962 ErrorType
.INTERNAL_ERROR
,
1963 `Response for unknown message id ${messageId}`,
1969 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1970 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1975 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1979 responseCallback(commandPayload
, requestPayload
)
1982 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1983 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1984 if (!this.requests
.has(messageId
)) {
1986 throw new OCPPError(
1987 ErrorType
.INTERNAL_ERROR
,
1988 `Error response for unknown message id ${messageId}`,
1990 { errorType
, errorMessage
, errorDetails
}
1993 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1994 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1996 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
2000 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
2003 private async onMessage (data
: RawData
): Promise
<void> {
2004 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
2005 let messageType
: MessageType
| undefined
2006 let errorMsg
: string
2008 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2009 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
2010 if (Array.isArray(request
)) {
2011 [messageType
] = request
2012 // Check the type of message
2013 switch (messageType
) {
2015 case MessageType
.CALL_MESSAGE
:
2016 await this.handleIncomingMessage(request
as IncomingRequest
)
2019 case MessageType
.CALL_RESULT_MESSAGE
:
2020 this.handleResponseMessage(request
as Response
)
2023 case MessageType
.CALL_ERROR_MESSAGE
:
2024 this.handleErrorMessage(request
as ErrorResponse
)
2028 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2029 errorMsg
= `Wrong message type ${messageType}`
2030 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2031 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2034 throw new OCPPError(
2035 ErrorType
.PROTOCOL_ERROR
,
2036 'Incoming message is not an array',
2044 if (!Array.isArray(request
)) {
2045 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2048 let commandName
: IncomingRequestCommand
| undefined
2049 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2050 let errorCallback
: ErrorCallback
2051 const [, messageId
] = request
2052 switch (messageType
) {
2053 case MessageType
.CALL_MESSAGE
:
2054 [, , commandName
] = request
as IncomingRequest
2056 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2058 case MessageType
.CALL_RESULT_MESSAGE
:
2059 case MessageType
.CALL_ERROR_MESSAGE
:
2060 if (this.requests
.has(messageId
)) {
2061 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2062 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2063 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2064 errorCallback(error
as OCPPError
, false)
2066 // Remove the request from the cache in case of error at response handling
2067 this.requests
.delete(messageId
)
2071 if (!(error
instanceof OCPPError
)) {
2073 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2074 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2075 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2076 }' message '${data.toString()}' handling is not an OCPPError:`,
2081 `${this.logPrefix()} Incoming OCPP command '${
2082 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2083 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2084 }' message '${data.toString()}'${
2085 this.requests.has(messageId)
2086 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2088 } processing error:`,
2094 private onPing (): void {
2095 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2098 private onPong (): void {
2099 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2102 private onError (error
: WSError
): void {
2103 this.closeWSConnection()
2104 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2107 private getEnergyActiveImportRegister (
2108 connectorStatus
: ConnectorStatus
| undefined,
2111 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2114 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2115 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2117 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2122 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2123 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2125 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2129 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2130 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2131 return stationTemplate
?.useConnectorId0
?? Constants
.DEFAULT_STATION_INFO
.useConnectorId0
!
2134 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2135 if (this.hasEvses
) {
2136 for (const [evseId
, evseStatus
] of this.evses
) {
2140 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2141 if (connectorStatus
.transactionStarted
=== true) {
2142 await this.stopTransactionOnConnector(connectorId
, reason
)
2147 for (const connectorId
of this.connectors
.keys()) {
2148 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2149 await this.stopTransactionOnConnector(connectorId
, reason
)
2156 private getConnectionTimeout (): number {
2157 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2158 return convertToInt(
2159 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2160 Constants
.DEFAULT_CONNECTION_TIMEOUT
2163 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2166 private getPowerDivider (): number {
2167 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2168 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2169 powerDivider
= this.getNumberOfRunningTransactions()
2174 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2175 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2176 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2177 switch (this.getCurrentOutType(stationInfo
)) {
2178 case CurrentType
.AC
:
2179 return ACElectricUtils
.amperagePerPhaseFromPower(
2180 this.getNumberOfPhases(stationInfo
),
2181 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2182 this.getVoltageOut(stationInfo
)
2184 case CurrentType
.DC
:
2185 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2189 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2191 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2192 (stationInfo
?? this.stationInfo
!).currentOutType
??
2193 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2194 Constants
.DEFAULT_STATION_INFO
.currentOutType
!
2198 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2200 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2201 (stationInfo
?? this.stationInfo
!).voltageOut
??
2202 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2206 private getAmperageLimitation (): number | undefined {
2208 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2209 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2212 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2213 getAmperageLimitationUnitDivider(this.stationInfo
)
2218 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2219 if (this.stationInfo
?.autoRegister
=== true) {
2220 await this.ocppRequestService
.requestHandler
<
2221 BootNotificationRequest
,
2222 BootNotificationResponse
2223 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2224 skipBufferingOnError
: true
2227 // Start WebSocket ping
2228 this.startWebSocketPing()
2230 this.startHeartbeat()
2231 // Initialize connectors status
2232 if (this.hasEvses
) {
2233 for (const [evseId
, evseStatus
] of this.evses
) {
2235 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2236 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2237 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2242 for (const connectorId
of this.connectors
.keys()) {
2243 if (connectorId
> 0) {
2244 const connectorBootStatus
= getBootConnectorStatus(
2247 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2248 this.getConnectorStatus(connectorId
)!
2250 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2254 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2255 await this.ocppRequestService
.requestHandler
<
2256 FirmwareStatusNotificationRequest
,
2257 FirmwareStatusNotificationResponse
2258 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2259 status: FirmwareStatus
.Installed
2261 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2265 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2266 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2268 this.flushMessageBuffer()
2271 private internalStopMessageSequence (): void {
2272 // Stop WebSocket ping
2273 this.stopWebSocketPing()
2275 this.stopHeartbeat()
2277 if (this.automaticTransactionGenerator
?.started
=== true) {
2278 this.stopAutomaticTransactionGenerator()
2282 private async stopMessageSequence (
2283 reason
?: StopTransactionReason
,
2284 stopTransactions
?: boolean
2286 this.internalStopMessageSequence()
2287 // Stop ongoing transactions
2288 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2289 if (this.hasEvses
) {
2290 for (const [evseId
, evseStatus
] of this.evses
) {
2292 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2293 await sendAndSetConnectorStatus(
2296 ConnectorStatusEnum
.Unavailable
,
2299 delete connectorStatus
.status
2304 for (const connectorId
of this.connectors
.keys()) {
2305 if (connectorId
> 0) {
2306 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2307 delete this.getConnectorStatus(connectorId
)?.status
2313 private startWebSocketPing (): void {
2314 const webSocketPingInterval
=
2315 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2317 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2320 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2321 this.wsPingSetInterval
= setInterval(() => {
2322 if (this.isWebSocketConnectionOpened()) {
2323 this.wsConnection
?.ping()
2325 }, secondsToMilliseconds(webSocketPingInterval
))
2327 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2328 webSocketPingInterval
2331 } else if (this.wsPingSetInterval
!= null) {
2333 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2334 webSocketPingInterval
2339 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2344 private stopWebSocketPing (): void {
2345 if (this.wsPingSetInterval
!= null) {
2346 clearInterval(this.wsPingSetInterval
)
2347 delete this.wsPingSetInterval
2351 private getConfiguredSupervisionUrl (): URL
{
2352 let configuredSupervisionUrl
: string
2353 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2354 if (isNotEmptyArray(supervisionUrls
)) {
2355 let configuredSupervisionUrlIndex
: number
2356 switch (Configuration
.getSupervisionUrlDistribution()) {
2357 case SupervisionUrlDistribution
.RANDOM
:
2358 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2360 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2361 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2363 !Object.values(SupervisionUrlDistribution
).includes(
2364 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2365 Configuration
.getSupervisionUrlDistribution()!
2368 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2369 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2370 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2373 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2376 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2378 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2379 configuredSupervisionUrl
= supervisionUrls
!
2381 if (isNotEmptyString(configuredSupervisionUrl
)) {
2382 return new URL(configuredSupervisionUrl
)
2384 const errorMsg
= 'No supervision url(s) configured'
2385 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2386 throw new BaseError(errorMsg
)
2389 private stopHeartbeat (): void {
2390 if (this.heartbeatSetInterval
!= null) {
2391 clearInterval(this.heartbeatSetInterval
)
2392 delete this.heartbeatSetInterval
2396 private terminateWSConnection (): void {
2397 if (this.isWebSocketConnectionOpened()) {
2398 this.wsConnection
?.terminate()
2399 this.wsConnection
= null
2403 private async reconnect (): Promise
<void> {
2405 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2406 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2407 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2409 this.wsConnectionRetried
= true
2410 ++this.wsConnectionRetryCount
2411 const reconnectDelay
=
2412 this.stationInfo
?.reconnectExponentialDelay
=== true
2413 ? exponentialDelay(this.wsConnectionRetryCount
)
2414 : secondsToMilliseconds(this.getConnectionTimeout())
2415 const reconnectDelayWithdraw
= 1000
2416 const reconnectTimeout
=
2417 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2419 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2422 )}ms, timeout ${reconnectTimeout}ms`
2424 await sleep(reconnectDelay
)
2426 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2428 this.openWSConnection(
2430 handshakeTimeout
: reconnectTimeout
2432 { closeOpened
: true }
2434 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2436 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2437 this.wsConnectionRetryCount
2438 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`