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 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 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
,
92 type HeartbeatRequest
,
93 type HeartbeatResponse
,
95 type IncomingRequestCommand
,
98 type MeterValuesRequest
,
99 type MeterValuesResponse
,
101 type OutgoingRequest
,
103 RegistrationStatusEnumType
,
107 ReservationTerminationReason
,
109 StandardParametersKey
,
111 type StopTransactionReason
,
112 type StopTransactionRequest
,
113 type StopTransactionResponse
,
114 SupervisionUrlDistribution
,
115 SupportedFeatureProfiles
,
118 WebSocketCloseEventStatusCode
,
120 } from
'../types/index.js'
129 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
130 buildConnectorsStatus
,
141 formatDurationMilliSeconds
,
142 formatDurationSeconds
,
144 getWebSocketCloseEventStatusString
,
156 } from
'../utils/index.js'
158 export class ChargingStation
extends EventEmitter
{
159 public readonly index
: number
160 public readonly templateFile
: string
161 public stationInfo
?: ChargingStationInfo
162 public started
: boolean
163 public starting
: boolean
164 public idTagsCache
: IdTagsCache
165 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined
166 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined
167 public wsConnection
: WebSocket
| null
168 public readonly connectors
: Map
<number, ConnectorStatus
>
169 public readonly evses
: Map
<number, EvseStatus
>
170 public readonly requests
: Map
<string, CachedRequest
>
171 public performanceStatistics
: PerformanceStatistics
| undefined
172 public heartbeatSetInterval
?: NodeJS
.Timeout
173 public ocppRequestService
!: OCPPRequestService
174 public bootNotificationRequest
?: BootNotificationRequest
175 public bootNotificationResponse
?: BootNotificationResponse
176 public powerDivider
?: number
177 private stopping
: boolean
178 private configurationFile
!: string
179 private configurationFileHash
!: string
180 private connectorsConfigurationHash
!: string
181 private evsesConfigurationHash
!: string
182 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
183 private ocppIncomingRequestService
!: OCPPIncomingRequestService
184 private readonly messageBuffer
: Set
<string>
185 private configuredSupervisionUrl
!: URL
186 private wsConnectionRetried
: boolean
187 private wsConnectionRetryCount
: number
188 private templateFileWatcher
: FSWatcher
| undefined
189 private templateFileHash
!: string
190 private readonly sharedLRUCache
: SharedLRUCache
191 private wsPingSetInterval
?: NodeJS
.Timeout
192 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
193 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
195 constructor (index
: number, templateFile
: string, options
?: ChargingStationOptions
) {
198 this.starting
= false
199 this.stopping
= false
200 this.wsConnection
= null
201 this.wsConnectionRetried
= false
202 this.wsConnectionRetryCount
= 0
204 this.templateFile
= templateFile
205 this.connectors
= new Map
<number, ConnectorStatus
>()
206 this.evses
= new Map
<number, EvseStatus
>()
207 this.requests
= new Map
<string, CachedRequest
>()
208 this.messageBuffer
= new Set
<string>()
209 this.sharedLRUCache
= SharedLRUCache
.getInstance()
210 this.idTagsCache
= IdTagsCache
.getInstance()
211 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this)
213 this.on(ChargingStationEvents
.added
, () => {
214 parentPort
?.postMessage(buildAddedMessage(this))
216 this.on(ChargingStationEvents
.deleted
, () => {
217 parentPort
?.postMessage(buildDeletedMessage(this))
219 this.on(ChargingStationEvents
.started
, () => {
220 parentPort
?.postMessage(buildStartedMessage(this))
222 this.on(ChargingStationEvents
.stopped
, () => {
223 parentPort
?.postMessage(buildStoppedMessage(this))
225 this.on(ChargingStationEvents
.updated
, () => {
226 parentPort
?.postMessage(buildUpdatedMessage(this))
228 this.on(ChargingStationEvents
.accepted
, () => {
229 this.startMessageSequence(
230 this.wsConnectionRetried
232 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
234 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
236 this.wsConnectionRetried
= false
238 this.on(ChargingStationEvents
.rejected
, () => {
239 this.wsConnectionRetried
= false
241 this.on(ChargingStationEvents
.disconnected
, () => {
243 this.internalStopMessageSequence()
246 `${this.logPrefix()} Error while stopping the internal message sequence:`,
252 this.initialize(options
)
256 if (this.stationInfo
?.autoStart
=== true) {
261 public get
hasEvses (): boolean {
262 return this.connectors
.size
=== 0 && this.evses
.size
> 0
265 public get
wsConnectionUrl (): URL
{
268 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
269 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
270 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
271 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
272 : this.configuredSupervisionUrl.href
273 }/${this.stationInfo?.chargingStationId}`
277 public logPrefix
= (): string => {
279 this instanceof ChargingStation
&&
280 this.stationInfo
!= null &&
281 isNotEmptyString(this.stationInfo
.chargingStationId
)
283 return logPrefix(` ${this.stationInfo.chargingStationId} |`)
285 let stationTemplate
: ChargingStationTemplate
| undefined
287 stationTemplate
= JSON
.parse(
288 readFileSync(this.templateFile
, 'utf8')
289 ) as ChargingStationTemplate
291 stationTemplate
= undefined
293 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`)
296 public hasIdTags (): boolean {
297 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
298 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
!)!))
301 public getNumberOfPhases (stationInfo
?: ChargingStationInfo
): number {
302 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
303 const localStationInfo
= stationInfo
?? this.stationInfo
!
304 switch (this.getCurrentOutType(stationInfo
)) {
306 return localStationInfo
.numberOfPhases
?? 3
312 public isWebSocketConnectionOpened (): boolean {
313 return this.wsConnection
?.readyState
=== WebSocket
.OPEN
316 public inUnknownState (): boolean {
317 return this.bootNotificationResponse
?.status == null
320 public inPendingState (): boolean {
321 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
324 public inAcceptedState (): boolean {
325 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
328 public inRejectedState (): boolean {
329 return this.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
332 public isRegistered (): boolean {
333 return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState())
336 public isChargingStationAvailable (): boolean {
337 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
340 public hasConnector (connectorId
: number): boolean {
342 for (const evseStatus
of this.evses
.values()) {
343 if (evseStatus
.connectors
.has(connectorId
)) {
349 return this.connectors
.has(connectorId
)
352 public isConnectorAvailable (connectorId
: number): boolean {
355 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
359 public getNumberOfConnectors (): number {
361 let numberOfConnectors
= 0
362 for (const [evseId
, evseStatus
] of this.evses
) {
364 numberOfConnectors
+= evseStatus
.connectors
.size
367 return numberOfConnectors
369 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
372 public getNumberOfEvses (): number {
373 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
376 public getConnectorStatus (connectorId
: number): ConnectorStatus
| undefined {
378 for (const evseStatus
of this.evses
.values()) {
379 if (evseStatus
.connectors
.has(connectorId
)) {
380 return evseStatus
.connectors
.get(connectorId
)
385 return this.connectors
.get(connectorId
)
388 public getConnectorMaximumAvailablePower (connectorId
: number): number {
389 let connectorAmperageLimitationPowerLimit
: number | undefined
390 const amperageLimitation
= this.getAmperageLimitation()
392 amperageLimitation
!= null &&
393 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
394 amperageLimitation
< this.stationInfo
!.maximumAmperage
!
396 connectorAmperageLimitationPowerLimit
=
397 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
398 ? ACElectricUtils
.powerTotal(
399 this.getNumberOfPhases(),
400 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
401 this.stationInfo
.voltageOut
!,
403 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
405 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
406 DCElectricUtils
.power(this.stationInfo
!.voltageOut
!, amperageLimitation
)) /
407 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
410 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
411 const connectorMaximumPower
= this.stationInfo
!.maximumPower
! / this.powerDivider
!
412 const connectorChargingProfilesPowerLimit
=
413 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
)
415 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
416 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
417 isNaN(connectorAmperageLimitationPowerLimit
!)
419 : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
420 connectorAmperageLimitationPowerLimit
!,
421 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
422 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!
426 public getTransactionIdTag (transactionId
: number): string | undefined {
428 for (const evseStatus
of this.evses
.values()) {
429 for (const connectorStatus
of evseStatus
.connectors
.values()) {
430 if (connectorStatus
.transactionId
=== transactionId
) {
431 return connectorStatus
.transactionIdTag
436 for (const connectorId
of this.connectors
.keys()) {
437 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
438 return this.getConnectorStatus(connectorId
)?.transactionIdTag
444 public getNumberOfRunningTransactions (): number {
445 let numberOfRunningTransactions
= 0
447 for (const [evseId
, evseStatus
] of this.evses
) {
451 for (const connectorStatus
of evseStatus
.connectors
.values()) {
452 if (connectorStatus
.transactionStarted
=== true) {
453 ++numberOfRunningTransactions
458 for (const connectorId
of this.connectors
.keys()) {
459 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
460 ++numberOfRunningTransactions
464 return numberOfRunningTransactions
467 public getConnectorIdByTransactionId (transactionId
: number | undefined): number | undefined {
468 if (transactionId
== null) {
470 } else if (this.hasEvses
) {
471 for (const evseStatus
of this.evses
.values()) {
472 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
473 if (connectorStatus
.transactionId
=== transactionId
) {
479 for (const connectorId
of this.connectors
.keys()) {
480 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
487 public getEnergyActiveImportRegisterByTransactionId (
488 transactionId
: number | undefined,
491 return this.getEnergyActiveImportRegister(
492 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
493 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!),
498 public getEnergyActiveImportRegisterByConnectorId (connectorId
: number, rounded
= false): number {
499 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
)
502 public getAuthorizeRemoteTxRequests (): boolean {
503 const authorizeRemoteTxRequests
= getConfigurationKey(
505 StandardParametersKey
.AuthorizeRemoteTxRequests
507 return authorizeRemoteTxRequests
!= null
508 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
512 public getLocalAuthListEnabled (): boolean {
513 const localAuthListEnabled
= getConfigurationKey(
515 StandardParametersKey
.LocalAuthListEnabled
517 return localAuthListEnabled
!= null ? convertToBoolean(localAuthListEnabled
.value
) : false
520 public getHeartbeatInterval (): number {
521 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)
522 if (HeartbeatInterval
!= null) {
523 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
))
525 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)
526 if (HeartBeatInterval
!= null) {
527 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
))
529 this.stationInfo
?.autoRegister
=== false &&
531 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
532 Constants.DEFAULT_HEARTBEAT_INTERVAL
535 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
538 public setSupervisionUrl (url
: string): void {
540 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
541 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
)
543 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
, url
)
545 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
546 this.stationInfo
!.supervisionUrls
= url
547 this.saveStationInfo()
548 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
552 public startHeartbeat (): void {
553 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
== null) {
554 this.heartbeatSetInterval
= setInterval(() => {
555 this.ocppRequestService
556 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
559 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
563 }, this.getHeartbeatInterval())
565 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
566 this.getHeartbeatInterval()
569 } else if (this.heartbeatSetInterval
!= null) {
571 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
572 this.getHeartbeatInterval()
577 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
582 public restartHeartbeat (): void {
586 this.startHeartbeat()
589 public restartWebSocketPing (): void {
590 // Stop WebSocket ping
591 this.stopWebSocketPing()
592 // Start WebSocket ping
593 this.startWebSocketPing()
596 public startMeterValues (connectorId
: number, interval
: number): void {
597 if (connectorId
=== 0) {
598 logger
.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`)
601 const connectorStatus
= this.getConnectorStatus(connectorId
)
602 if (connectorStatus
== null) {
604 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
609 if (connectorStatus
.transactionStarted
=== false) {
611 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
615 connectorStatus
.transactionStarted
=== true &&
616 connectorStatus
.transactionId
== null
619 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
624 connectorStatus
.transactionSetInterval
= setInterval(() => {
625 const meterValue
= buildMeterValue(
628 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
629 connectorStatus
.transactionId
!,
632 this.ocppRequestService
633 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
635 RequestCommand
.METER_VALUES
,
638 transactionId
: connectorStatus
.transactionId
,
639 meterValue
: [meterValue
]
644 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
651 `${this.logPrefix()} Charging station ${
652 StandardParametersKey.MeterValueSampleInterval
653 } configuration set to ${interval}, not sending MeterValues`
658 public stopMeterValues (connectorId
: number): void {
659 const connectorStatus
= this.getConnectorStatus(connectorId
)
660 if (connectorStatus
?.transactionSetInterval
!= null) {
661 clearInterval(connectorStatus
.transactionSetInterval
)
665 private add (): void {
666 this.emit(ChargingStationEvents
.added
)
669 public async delete (deleteConfiguration
= true): Promise
<void> {
673 AutomaticTransactionGenerator
.deleteInstance(this)
674 PerformanceStatistics
.deleteInstance(this.stationInfo
?.hashId
)
675 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
676 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
677 this.requests
.clear()
678 this.connectors
.clear()
680 this.templateFileWatcher
?.unref()
681 deleteConfiguration
&& rmSync(this.configurationFile
, { force
: true })
682 this.chargingStationWorkerBroadcastChannel
.unref()
683 this.emit(ChargingStationEvents
.deleted
)
684 this.removeAllListeners()
687 public start (): void {
689 if (!this.starting
) {
691 if (this.stationInfo
?.enableStatistics
=== true) {
692 this.performanceStatistics
?.start()
694 this.openWSConnection()
695 // Monitor charging station template file
696 this.templateFileWatcher
= watchJsonFile(
698 FileType
.ChargingStationTemplate
,
701 (event
, filename
): void => {
702 if (isNotEmptyString(filename
) && event
=== 'change') {
705 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
707 } file have changed, reload`
709 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
)
710 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
711 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
!)!)
715 const ATGStarted
= this.automaticTransactionGenerator
?.started
716 if (ATGStarted
=== true) {
717 this.stopAutomaticTransactionGenerator()
719 delete this.automaticTransactionGeneratorConfiguration
721 this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true &&
724 this.startAutomaticTransactionGenerator(undefined, true)
726 if (this.stationInfo
?.enableStatistics
=== true) {
727 this.performanceStatistics
?.restart()
729 this.performanceStatistics
?.stop()
731 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
734 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
742 this.emit(ChargingStationEvents
.started
)
743 this.starting
= false
745 logger
.warn(`${this.logPrefix()} Charging station is already starting...`)
748 logger
.warn(`${this.logPrefix()} Charging station is already started...`)
753 reason
?: StopTransactionReason
,
754 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
757 if (!this.stopping
) {
759 await this.stopMessageSequence(reason
, stopTransactions
)
760 this.closeWSConnection()
761 if (this.stationInfo
?.enableStatistics
=== true) {
762 this.performanceStatistics
?.stop()
764 this.templateFileWatcher
?.close()
765 delete this.bootNotificationResponse
767 this.saveConfiguration()
768 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
769 this.emit(ChargingStationEvents
.stopped
)
770 this.stopping
= false
772 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`)
775 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`)
779 public async reset (reason
?: StopTransactionReason
): Promise
<void> {
780 await this.stop(reason
)
781 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
782 await sleep(this.stationInfo
!.resetTime
!)
787 public saveOcppConfiguration (): void {
788 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
789 this.saveConfiguration()
793 public bufferMessage (message
: string): void {
794 this.messageBuffer
.add(message
)
795 this.setIntervalFlushMessageBuffer()
798 public openWSConnection (
800 params
?: { closeOpened
?: boolean, terminateOpened
?: boolean }
803 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
804 ...this.stationInfo
?.wsOptions
,
807 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
}
808 if (!checkChargingStation(this, this.logPrefix())) {
811 if (this.stationInfo
?.supervisionUser
!= null && this.stationInfo
.supervisionPassword
!= null) {
812 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`
814 if (params
.closeOpened
=== true) {
815 this.closeWSConnection()
817 if (params
.terminateOpened
=== true) {
818 this.terminateWSConnection()
821 if (this.isWebSocketConnectionOpened()) {
823 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.href} is already opened`
828 logger
.info(`${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.href}`)
830 this.wsConnection
= new WebSocket(
831 this.wsConnectionUrl
,
832 `ocpp${this.stationInfo?.ocppVersion}`,
836 // Handle WebSocket message
837 this.wsConnection
.on('message', data
=> {
838 this.onMessage(data
).catch(Constants
.EMPTY_FUNCTION
)
840 // Handle WebSocket error
841 this.wsConnection
.on('error', this.onError
.bind(this))
842 // Handle WebSocket close
843 this.wsConnection
.on('close', this.onClose
.bind(this))
844 // Handle WebSocket open
845 this.wsConnection
.on('open', () => {
846 this.onOpen().catch(error
=>
847 logger
.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error
)
850 // Handle WebSocket ping
851 this.wsConnection
.on('ping', this.onPing
.bind(this))
852 // Handle WebSocket pong
853 this.wsConnection
.on('pong', this.onPong
.bind(this))
856 public closeWSConnection (): void {
857 if (this.isWebSocketConnectionOpened()) {
858 this.wsConnection
?.close()
859 this.wsConnection
= null
863 public getAutomaticTransactionGeneratorConfiguration ():
864 | AutomaticTransactionGeneratorConfiguration
866 if (this.automaticTransactionGeneratorConfiguration
== null) {
867 let automaticTransactionGeneratorConfiguration
:
868 | AutomaticTransactionGeneratorConfiguration
870 const stationTemplate
= this.getTemplateFromFile()
871 const stationConfiguration
= this.getConfigurationFromFile()
873 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
874 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
875 stationConfiguration
?.automaticTransactionGenerator
!= null
877 automaticTransactionGeneratorConfiguration
=
878 stationConfiguration
.automaticTransactionGenerator
880 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
882 this.automaticTransactionGeneratorConfiguration
= {
883 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
884 ...automaticTransactionGeneratorConfiguration
887 return this.automaticTransactionGeneratorConfiguration
890 public getAutomaticTransactionGeneratorStatuses (): Status
[] | undefined {
891 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
894 public startAutomaticTransactionGenerator (
895 connectorIds
?: number[],
896 stopAbsoluteDuration
?: boolean
898 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this)
899 if (isNotEmptyArray(connectorIds
)) {
900 for (const connectorId
of connectorIds
) {
901 this.automaticTransactionGenerator
?.startConnector(connectorId
, stopAbsoluteDuration
)
904 this.automaticTransactionGenerator
?.start(stopAbsoluteDuration
)
906 this.saveAutomaticTransactionGeneratorConfiguration()
907 this.emit(ChargingStationEvents
.updated
)
910 public stopAutomaticTransactionGenerator (connectorIds
?: number[]): void {
911 if (isNotEmptyArray(connectorIds
)) {
912 for (const connectorId
of connectorIds
) {
913 this.automaticTransactionGenerator
?.stopConnector(connectorId
)
916 this.automaticTransactionGenerator
?.stop()
918 this.saveAutomaticTransactionGeneratorConfiguration()
919 this.emit(ChargingStationEvents
.updated
)
922 public async stopTransactionOnConnector (
924 reason
?: StopTransactionReason
925 ): Promise
<StopTransactionResponse
> {
926 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
928 this.stationInfo
?.beginEndMeterValues
=== true &&
929 this.stationInfo
.ocppStrictCompliance
=== true &&
930 this.stationInfo
.outOfOrderEndMeterValues
=== false
932 const transactionEndMeterValue
= buildTransactionEndMeterValue(
935 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
937 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
939 RequestCommand
.METER_VALUES
,
943 meterValue
: [transactionEndMeterValue
]
947 return await this.ocppRequestService
.requestHandler
<
948 StopTransactionRequest
,
949 StopTransactionResponse
950 >(this, RequestCommand
.STOP_TRANSACTION
, {
952 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
953 ...(reason
!= null && { reason
})
957 public getReserveConnectorZeroSupported (): boolean {
958 return convertToBoolean(
959 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
960 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
964 public async addReservation (reservation
: Reservation
): Promise
<void> {
965 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
)
966 if (reservationFound
!= null) {
967 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
)
969 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
970 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
971 await sendAndSetConnectorStatus(
973 reservation
.connectorId
,
974 ConnectorStatusEnum
.Reserved
,
976 { send
: reservation
.connectorId
!== 0 }
980 public async removeReservation (
981 reservation
: Reservation
,
982 reason
: ReservationTerminationReason
984 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
985 const connector
= this.getConnectorStatus(reservation
.connectorId
)!
987 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
988 case ReservationTerminationReason
.TRANSACTION_STARTED
:
989 delete connector
.reservation
991 case ReservationTerminationReason
.RESERVATION_CANCELED
:
992 case ReservationTerminationReason
.REPLACE_EXISTING
:
993 case ReservationTerminationReason
.EXPIRED
:
994 await sendAndSetConnectorStatus(
996 reservation
.connectorId
,
997 ConnectorStatusEnum
.Available
,
999 { send
: reservation
.connectorId
!== 0 }
1001 delete connector
.reservation
1004 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1005 throw new BaseError(`Unknown reservation termination reason '${reason}'`)
1009 public getReservationBy (
1010 filterKey
: ReservationKey
,
1011 value
: number | string
1012 ): Reservation
| undefined {
1013 if (this.hasEvses
) {
1014 for (const evseStatus
of this.evses
.values()) {
1015 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1016 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1017 return connectorStatus
.reservation
1022 for (const connectorStatus
of this.connectors
.values()) {
1023 if (connectorStatus
.reservation
?.[filterKey
] === value
) {
1024 return connectorStatus
.reservation
1030 public isConnectorReservable (
1031 reservationId
: number,
1033 connectorId
?: number
1035 const reservation
= this.getReservationBy('reservationId', reservationId
)
1036 const reservationExists
= reservation
!= null && !hasReservationExpired(reservation
)
1037 if (arguments.length
=== 1) {
1038 return !reservationExists
1039 } else if (arguments.length
> 1) {
1040 const userReservation
= idTag
!= null ? this.getReservationBy('idTag', idTag
) : undefined
1041 const userReservationExists
=
1042 userReservation
!= null && !hasReservationExpired(userReservation
)
1043 const notConnectorZero
= connectorId
== null ? true : connectorId
> 0
1044 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0
1046 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1052 private setIntervalFlushMessageBuffer (): void {
1053 if (this.flushMessageBufferSetInterval
== null) {
1054 this.flushMessageBufferSetInterval
= setInterval(() => {
1055 if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) {
1056 this.flushMessageBuffer()
1058 if (this.messageBuffer
.size
=== 0) {
1059 this.clearIntervalFlushMessageBuffer()
1061 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
)
1065 private clearIntervalFlushMessageBuffer (): void {
1066 if (this.flushMessageBufferSetInterval
!= null) {
1067 clearInterval(this.flushMessageBufferSetInterval
)
1068 delete this.flushMessageBufferSetInterval
1072 private getNumberOfReservableConnectors (): number {
1073 let numberOfReservableConnectors
= 0
1074 if (this.hasEvses
) {
1075 for (const evseStatus
of this.evses
.values()) {
1076 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
)
1079 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
)
1081 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero()
1084 private getNumberOfReservationsOnConnectorZero (): number {
1086 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
!= null) ||
1087 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
!= null)
1094 private flushMessageBuffer (): void {
1095 if (this.messageBuffer
.size
> 0) {
1096 for (const message
of this.messageBuffer
.values()) {
1097 let beginId
: string | undefined
1098 let commandName
: RequestCommand
| undefined
1099 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
1100 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
1102 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
1103 beginId
= PerformanceStatistics
.beginMeasure(commandName
)
1105 this.wsConnection
?.send(message
, (error
?: Error) => {
1106 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1107 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!)
1108 if (error
== null) {
1110 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1112 )} OCPP message sent '${JSON.stringify(message)}'`
1114 this.messageBuffer
.delete(message
)
1117 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1119 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1128 private getTemplateFromFile (): ChargingStationTemplate
| undefined {
1129 let template
: ChargingStationTemplate
| undefined
1131 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1132 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
)
1134 const measureId
= `${FileType.ChargingStationTemplate} read`
1135 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1136 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
1137 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1138 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1139 .update(JSON
.stringify(template
))
1141 this.sharedLRUCache
.setChargingStationTemplate(template
)
1142 this.templateFileHash
= template
.templateHash
1145 handleFileException(
1147 FileType
.ChargingStationTemplate
,
1148 error
as NodeJS
.ErrnoException
,
1155 private getStationInfoFromTemplate (): ChargingStationInfo
{
1156 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1157 const stationTemplate
= this.getTemplateFromFile()!
1158 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1159 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this)
1160 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
)
1161 if (stationTemplate
.Connectors
!= null) {
1162 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1164 const stationInfo
= stationTemplateToStationInfo(stationTemplate
)
1165 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
)
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
= merge
<FirmwareUpgrade
>(
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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1220 if (stationInfo
.templateName
== null) {
1221 stationInfo
.templateName
= parse(this.templateFile
).name
1228 private getStationInfo (options
?: ChargingStationOptions
): ChargingStationInfo
{
1229 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1230 options
?.persistentConfiguration
!= null &&
1231 (stationInfoFromTemplate
.stationInfoPersistentConfiguration
= options
.persistentConfiguration
)
1232 const stationInfoFromFile
= this.getStationInfoFromFile(
1233 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1236 // 1. charging station info from template
1237 // 2. charging station info from configuration file
1239 stationInfoFromFile
!= null &&
1240 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1242 return setChargingStationOptions(
1243 { ...Constants
.DEFAULT_STATION_INFO
, ...stationInfoFromFile
},
1247 stationInfoFromFile
!= null &&
1248 propagateSerialNumber(
1249 this.getTemplateFromFile(),
1250 stationInfoFromFile
,
1251 stationInfoFromTemplate
1253 return setChargingStationOptions(
1254 { ...Constants
.DEFAULT_STATION_INFO
, ...stationInfoFromTemplate
},
1259 private saveStationInfo (): void {
1260 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1261 this.saveConfiguration()
1265 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1266 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1267 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1268 throw new BaseError(errorMsg
)
1271 private initialize (options
?: ChargingStationOptions
): void {
1272 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1273 const stationTemplate
= this.getTemplateFromFile()!
1274 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1275 this.configurationFile
= join(
1276 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1277 `${getHashId(this.index, stationTemplate)}.json`
1279 const stationConfiguration
= this.getConfigurationFromFile()
1281 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1282 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1284 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1285 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1287 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1289 this.stationInfo
= this.getStationInfo(options
)
1291 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1292 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
) &&
1293 isNotEmptyString(this.stationInfo
.firmwareVersion
)
1295 const patternGroup
=
1296 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1297 this.stationInfo
.firmwareVersion
.split('.').length
1298 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1299 .exec(this.stationInfo
.firmwareVersion
)
1300 ?.slice(1, patternGroup
+ 1)
1301 if (match
!= null) {
1302 const patchLevelIndex
= match
.length
- 1
1303 match
[patchLevelIndex
] = (
1304 convertToInt(match
[patchLevelIndex
]) +
1305 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1306 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1308 this.stationInfo
.firmwareVersion
= match
.join('.')
1311 this.saveStationInfo()
1312 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1313 if (this.stationInfo
.enableStatistics
=== true) {
1314 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1315 this.stationInfo
.hashId
,
1316 this.stationInfo
.chargingStationId
,
1317 this.configuredSupervisionUrl
1320 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1321 if (bootNotificationRequest
== null) {
1322 const errorMsg
= 'Error while creating boot notification request'
1323 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1324 throw new BaseError(errorMsg
)
1326 this.bootNotificationRequest
= bootNotificationRequest
1327 this.powerDivider
= this.getPowerDivider()
1328 // OCPP configuration
1329 this.ocppConfiguration
= this.getOcppConfiguration(options
?.persistentConfiguration
)
1330 this.initializeOcppConfiguration()
1331 this.initializeOcppServices()
1332 if (this.stationInfo
.autoRegister
=== true) {
1333 this.bootNotificationResponse
= {
1334 currentTime
: new Date(),
1335 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1336 status: RegistrationStatusEnumType
.ACCEPTED
1341 private initializeOcppServices (): void {
1342 const ocppVersion
= this.stationInfo
?.ocppVersion
1343 switch (ocppVersion
) {
1344 case OCPPVersion
.VERSION_16
:
1345 this.ocppIncomingRequestService
=
1346 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1347 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1348 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1351 case OCPPVersion
.VERSION_20
:
1352 case OCPPVersion
.VERSION_201
:
1353 this.ocppIncomingRequestService
=
1354 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1355 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1356 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1360 this.handleUnsupportedVersion(ocppVersion
)
1365 private initializeOcppConfiguration (): void {
1366 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1367 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1369 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1370 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1373 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1374 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1375 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1377 addConfigurationKey(
1379 this.stationInfo
.supervisionUrlOcppKey
,
1380 this.configuredSupervisionUrl
.href
,
1384 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1385 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1386 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1388 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1391 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1392 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1394 addConfigurationKey(
1396 this.stationInfo
.amperageLimitationOcppKey
,
1398 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1399 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1402 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1403 addConfigurationKey(
1405 StandardParametersKey
.SupportedFeatureProfiles
,
1406 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1409 addConfigurationKey(
1411 StandardParametersKey
.NumberOfConnectors
,
1412 this.getNumberOfConnectors().toString(),
1416 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1417 addConfigurationKey(
1419 StandardParametersKey
.MeterValuesSampledData
,
1420 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1423 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1424 const connectorsPhaseRotation
: string[] = []
1425 if (this.hasEvses
) {
1426 for (const evseStatus
of this.evses
.values()) {
1427 for (const connectorId
of evseStatus
.connectors
.keys()) {
1428 connectorsPhaseRotation
.push(
1429 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1430 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1435 for (const connectorId
of this.connectors
.keys()) {
1436 connectorsPhaseRotation
.push(
1437 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1438 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1442 addConfigurationKey(
1444 StandardParametersKey
.ConnectorPhaseRotation
,
1445 connectorsPhaseRotation
.toString()
1448 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1449 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1452 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1453 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1455 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1457 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1458 addConfigurationKey(
1460 StandardParametersKey
.ConnectionTimeOut
,
1461 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1464 this.saveOcppConfiguration()
1467 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1468 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1469 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1470 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1472 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1473 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1474 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1475 delete evseStatus
.connectorsStatus
1476 this.evses
.set(evseId
, {
1477 ...(evseStatus
as EvseStatus
),
1478 connectors
: new Map
<number, ConnectorStatus
>(
1479 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1480 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1487 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1488 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1489 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1490 throw new BaseError(errorMsg
)
1492 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1493 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1494 throw new BaseError(errorMsg
)
1498 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1499 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1500 this.initializeConnectorsFromTemplate(stationTemplate
)
1501 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1502 this.initializeEvsesFromTemplate(stationTemplate
)
1503 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1504 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1505 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1506 throw new BaseError(errorMsg
)
1508 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1509 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1510 throw new BaseError(errorMsg
)
1514 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1515 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1516 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1517 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1518 throw new BaseError(errorMsg
)
1520 if (stationTemplate
.Connectors
?.[0] == null) {
1522 `${this.logPrefix()} Charging station information from template ${
1524 } with no connector id 0 configuration`
1527 if (stationTemplate
.Connectors
!= null) {
1528 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1529 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1530 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1532 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1535 const connectorsConfigChanged
=
1536 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1537 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1538 connectorsConfigChanged
&& this.connectors
.clear()
1539 this.connectorsConfigurationHash
= connectorsConfigHash
1540 if (templateMaxConnectors
> 0) {
1541 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1543 connectorId
=== 0 &&
1544 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1545 (stationTemplate
.Connectors
[connectorId
] == null ||
1546 !this.getUseConnectorId0(stationTemplate
))
1550 const templateConnectorId
=
1551 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1552 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1554 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1555 checkStationInfoConnectorStatus(
1556 templateConnectorId
,
1561 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1563 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1564 this.saveConnectorsStatus()
1567 `${this.logPrefix()} Charging station information from template ${
1569 } with no connectors configuration defined, cannot create connectors`
1575 `${this.logPrefix()} Charging station information from template ${
1577 } with no connectors configuration defined, using already defined connectors`
1582 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1583 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1584 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1585 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1586 throw new BaseError(errorMsg
)
1588 if (stationTemplate
.Evses
?.[0] == null) {
1590 `${this.logPrefix()} Charging station information from template ${
1592 } with no evse id 0 configuration`
1595 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1597 `${this.logPrefix()} Charging station information from template ${
1599 } with evse id 0 with no connector id 0 configuration`
1602 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1604 `${this.logPrefix()} Charging station information from template ${
1606 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1609 if (stationTemplate
.Evses
!= null) {
1610 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1611 .update(JSON
.stringify(stationTemplate
.Evses
))
1613 const evsesConfigChanged
=
1614 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1615 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1616 evsesConfigChanged
&& this.evses
.clear()
1617 this.evsesConfigurationHash
= evsesConfigHash
1618 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1619 if (templateMaxEvses
> 0) {
1620 for (const evseKey
in stationTemplate
.Evses
) {
1621 const evseId
= convertToInt(evseKey
)
1622 this.evses
.set(evseId
, {
1623 connectors
: buildConnectorsMap(
1624 stationTemplate
.Evses
[evseKey
].Connectors
,
1628 availability
: AvailabilityType
.Operative
1630 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1631 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1633 this.saveEvsesStatus()
1636 `${this.logPrefix()} Charging station information from template ${
1638 } with no evses configuration defined, cannot create evses`
1644 `${this.logPrefix()} Charging station information from template ${
1646 } with no evses configuration defined, using already defined evses`
1651 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1652 let configuration
: ChargingStationConfiguration
| undefined
1653 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1655 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1656 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1657 this.configurationFileHash
1660 const measureId
= `${FileType.ChargingStationConfiguration} read`
1661 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1662 configuration
= JSON
.parse(
1663 readFileSync(this.configurationFile
, 'utf8')
1664 ) as ChargingStationConfiguration
1665 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1666 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1667 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1668 this.configurationFileHash
= configuration
.configurationHash
!
1671 handleFileException(
1672 this.configurationFile
,
1673 FileType
.ChargingStationConfiguration
,
1674 error
as NodeJS
.ErrnoException
,
1679 return configuration
1682 private saveAutomaticTransactionGeneratorConfiguration (): void {
1683 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1684 this.saveConfiguration()
1688 private saveConnectorsStatus (): void {
1689 this.saveConfiguration()
1692 private saveEvsesStatus (): void {
1693 this.saveConfiguration()
1696 private saveConfiguration (): void {
1697 if (isNotEmptyString(this.configurationFile
)) {
1699 if (!existsSync(dirname(this.configurationFile
))) {
1700 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1702 const configurationFromFile
= this.getConfigurationFromFile()
1703 let configurationData
: ChargingStationConfiguration
=
1704 configurationFromFile
!= null
1705 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1707 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1708 configurationData
.stationInfo
= this.stationInfo
1710 delete configurationData
.stationInfo
1713 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1714 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1716 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1718 delete configurationData
.configurationKey
1720 configurationData
= merge
<ChargingStationConfiguration
>(
1722 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1724 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
!== true) {
1725 delete configurationData
.automaticTransactionGenerator
1727 if (this.connectors
.size
> 0) {
1728 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1730 delete configurationData
.connectorsStatus
1732 if (this.evses
.size
> 0) {
1733 configurationData
.evsesStatus
= buildEvsesStatus(this)
1735 delete configurationData
.evsesStatus
1737 delete configurationData
.configurationHash
1738 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1741 stationInfo
: configurationData
.stationInfo
,
1742 configurationKey
: configurationData
.configurationKey
,
1743 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1744 ...(this.connectors
.size
> 0 && {
1745 connectorsStatus
: configurationData
.connectorsStatus
1747 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1748 } satisfies ChargingStationConfiguration
)
1751 if (this.configurationFileHash
!== configurationHash
) {
1752 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1753 configurationData
.configurationHash
= configurationHash
1754 const measureId
= `${FileType.ChargingStationConfiguration} write`
1755 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1757 this.configurationFile
,
1758 JSON
.stringify(configurationData
, undefined, 2),
1761 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1762 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1763 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1764 this.configurationFileHash
= configurationHash
1766 handleFileException(
1767 this.configurationFile
,
1768 FileType
.ChargingStationConfiguration
,
1769 error
as NodeJS
.ErrnoException
,
1775 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1776 this.configurationFile
1781 handleFileException(
1782 this.configurationFile
,
1783 FileType
.ChargingStationConfiguration
,
1784 error
as NodeJS
.ErrnoException
,
1790 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1795 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1796 return this.getTemplateFromFile()?.Configuration
1799 private getOcppConfigurationFromFile (
1800 ocppPersistentConfiguration
?: boolean
1801 ): ChargingStationOcppConfiguration
| undefined {
1802 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1803 if (ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1804 return { configurationKey
}
1809 private getOcppConfiguration (
1810 ocppPersistentConfiguration
: boolean | undefined = this.stationInfo
?.ocppPersistentConfiguration
1811 ): ChargingStationOcppConfiguration
| undefined {
1812 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1813 this.getOcppConfigurationFromFile(ocppPersistentConfiguration
)
1814 if (ocppConfiguration
== null) {
1815 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1817 return ocppConfiguration
1820 private async onOpen (): Promise
<void> {
1821 if (this.isWebSocketConnectionOpened()) {
1823 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded`
1825 let registrationRetryCount
= 0
1826 if (!this.isRegistered()) {
1827 // Send BootNotification
1829 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1830 BootNotificationRequest
,
1831 BootNotificationResponse
1832 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1833 skipBufferingOnError
: true
1835 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1836 if (this.bootNotificationResponse
?.currentTime
!= null) {
1837 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1838 this.bootNotificationResponse
.currentTime
= convertToDate(
1839 this.bootNotificationResponse
.currentTime
1842 if (!this.isRegistered()) {
1843 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1845 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1846 this.bootNotificationResponse
?.interval
!= null
1847 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1848 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1852 !this.isRegistered() &&
1853 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1854 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1855 this.stationInfo
?.registrationMaxRetries
=== -1)
1858 if (this.isRegistered()) {
1859 this.emit(ChargingStationEvents
.registered
)
1860 if (this.inAcceptedState()) {
1861 this.emit(ChargingStationEvents
.accepted
)
1864 if (this.inRejectedState()) {
1865 this.emit(ChargingStationEvents
.rejected
)
1868 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1869 this.stationInfo?.registrationMaxRetries
1873 this.wsConnectionRetryCount
= 0
1874 this.emit(ChargingStationEvents
.updated
)
1877 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} failed`
1882 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1883 this.emit(ChargingStationEvents
.disconnected
)
1886 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1887 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1889 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1891 )}' and reason '${reason.toString()}'`
1893 this.wsConnectionRetryCount
= 0
1898 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1900 )}' and reason '${reason.toString()}'`
1903 this.reconnect().catch(error
=>
1904 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
1908 this.emit(ChargingStationEvents
.updated
)
1911 private getCachedRequest (
1912 messageType
: MessageType
| undefined,
1914 ): CachedRequest
| undefined {
1915 const cachedRequest
= this.requests
.get(messageId
)
1916 if (Array.isArray(cachedRequest
)) {
1917 return cachedRequest
1919 throw new OCPPError(
1920 ErrorType
.PROTOCOL_ERROR
,
1921 `Cached request for message id ${messageId} ${getMessageTypeString(
1923 )} is not an array`,
1929 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1930 const [messageType
, messageId
, commandName
, commandPayload
] = request
1931 if (this.stationInfo
?.enableStatistics
=== true) {
1932 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1935 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1939 // Process the message
1940 await this.ocppIncomingRequestService
.incomingRequestHandler(
1946 this.emit(ChargingStationEvents
.updated
)
1949 private handleResponseMessage (response
: Response
): void {
1950 const [messageType
, messageId
, commandPayload
] = response
1951 if (!this.requests
.has(messageId
)) {
1953 throw new OCPPError(
1954 ErrorType
.INTERNAL_ERROR
,
1955 `Response for unknown message id ${messageId}`,
1961 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1962 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1967 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1971 responseCallback(commandPayload
, requestPayload
)
1974 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1975 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1976 if (!this.requests
.has(messageId
)) {
1978 throw new OCPPError(
1979 ErrorType
.INTERNAL_ERROR
,
1980 `Error response for unknown message id ${messageId}`,
1982 { errorType
, errorMessage
, errorDetails
}
1985 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1986 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1988 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1992 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1995 private async onMessage (data
: RawData
): Promise
<void> {
1996 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1997 let messageType
: MessageType
| undefined
1998 let errorMsg
: string
2000 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2001 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
2002 if (Array.isArray(request
)) {
2003 [messageType
] = request
2004 // Check the type of message
2005 switch (messageType
) {
2007 case MessageType
.CALL_MESSAGE
:
2008 await this.handleIncomingMessage(request
as IncomingRequest
)
2011 case MessageType
.CALL_RESULT_MESSAGE
:
2012 this.handleResponseMessage(request
as Response
)
2015 case MessageType
.CALL_ERROR_MESSAGE
:
2016 this.handleErrorMessage(request
as ErrorResponse
)
2020 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2021 errorMsg
= `Wrong message type ${messageType}`
2022 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2023 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2026 throw new OCPPError(
2027 ErrorType
.PROTOCOL_ERROR
,
2028 'Incoming message is not an array',
2036 if (!Array.isArray(request
)) {
2037 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2040 let commandName
: IncomingRequestCommand
| undefined
2041 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2042 let errorCallback
: ErrorCallback
2043 const [, messageId
] = request
2044 switch (messageType
) {
2045 case MessageType
.CALL_MESSAGE
:
2046 [, , commandName
] = request
as IncomingRequest
2048 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2050 case MessageType
.CALL_RESULT_MESSAGE
:
2051 case MessageType
.CALL_ERROR_MESSAGE
:
2052 if (this.requests
.has(messageId
)) {
2053 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2054 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2055 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2056 errorCallback(error
as OCPPError
, false)
2058 // Remove the request from the cache in case of error at response handling
2059 this.requests
.delete(messageId
)
2063 if (!(error
instanceof OCPPError
)) {
2065 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2066 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2067 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2068 }' message '${data.toString()}' handling is not an OCPPError:`,
2073 `${this.logPrefix()} 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()}'${
2077 this.requests.has(messageId)
2078 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2080 } processing error:`,
2086 private onPing (): void {
2087 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2090 private onPong (): void {
2091 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2094 private onError (error
: WSError
): void {
2095 this.closeWSConnection()
2096 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2099 private getEnergyActiveImportRegister (
2100 connectorStatus
: ConnectorStatus
| undefined,
2103 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2106 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2107 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2109 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2114 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2115 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2117 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2121 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2122 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2123 return stationTemplate
?.useConnectorId0
?? Constants
.DEFAULT_STATION_INFO
.useConnectorId0
!
2126 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2127 if (this.hasEvses
) {
2128 for (const [evseId
, evseStatus
] of this.evses
) {
2132 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2133 if (connectorStatus
.transactionStarted
=== true) {
2134 await this.stopTransactionOnConnector(connectorId
, reason
)
2139 for (const connectorId
of this.connectors
.keys()) {
2140 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2141 await this.stopTransactionOnConnector(connectorId
, reason
)
2148 private getConnectionTimeout (): number {
2149 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2150 return convertToInt(
2151 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2152 Constants
.DEFAULT_CONNECTION_TIMEOUT
2155 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2158 private getPowerDivider (): number {
2159 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2160 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2161 powerDivider
= this.getNumberOfRunningTransactions()
2166 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2167 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2168 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2169 switch (this.getCurrentOutType(stationInfo
)) {
2170 case CurrentType
.AC
:
2171 return ACElectricUtils
.amperagePerPhaseFromPower(
2172 this.getNumberOfPhases(stationInfo
),
2173 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2174 this.getVoltageOut(stationInfo
)
2176 case CurrentType
.DC
:
2177 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2181 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2183 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2184 (stationInfo
?? this.stationInfo
!).currentOutType
??
2185 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2186 Constants
.DEFAULT_STATION_INFO
.currentOutType
!
2190 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2192 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2193 (stationInfo
?? this.stationInfo
!).voltageOut
??
2194 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2198 private getAmperageLimitation (): number | undefined {
2200 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2201 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2204 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2205 getAmperageLimitationUnitDivider(this.stationInfo
)
2210 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2211 if (this.stationInfo
?.autoRegister
=== true) {
2212 await this.ocppRequestService
.requestHandler
<
2213 BootNotificationRequest
,
2214 BootNotificationResponse
2215 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2216 skipBufferingOnError
: true
2219 // Start WebSocket ping
2220 this.startWebSocketPing()
2222 this.startHeartbeat()
2223 // Initialize connectors status
2224 if (this.hasEvses
) {
2225 for (const [evseId
, evseStatus
] of this.evses
) {
2227 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2228 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
)
2229 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
)
2234 for (const connectorId
of this.connectors
.keys()) {
2235 if (connectorId
> 0) {
2236 const connectorBootStatus
= getBootConnectorStatus(
2239 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2240 this.getConnectorStatus(connectorId
)!
2242 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
)
2246 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2247 await this.ocppRequestService
.requestHandler
<
2248 FirmwareStatusNotificationRequest
,
2249 FirmwareStatusNotificationResponse
2250 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2251 status: FirmwareStatus
.Installed
2253 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2257 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2258 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2260 this.flushMessageBuffer()
2263 private internalStopMessageSequence (): void {
2264 // Stop WebSocket ping
2265 this.stopWebSocketPing()
2267 this.stopHeartbeat()
2269 if (this.automaticTransactionGenerator
?.started
=== true) {
2270 this.stopAutomaticTransactionGenerator()
2274 private async stopMessageSequence (
2275 reason
?: StopTransactionReason
,
2276 stopTransactions
?: boolean
2278 this.internalStopMessageSequence()
2279 // Stop ongoing transactions
2280 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2281 if (this.hasEvses
) {
2282 for (const [evseId
, evseStatus
] of this.evses
) {
2284 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2285 await sendAndSetConnectorStatus(
2288 ConnectorStatusEnum
.Unavailable
,
2291 delete connectorStatus
.status
2296 for (const connectorId
of this.connectors
.keys()) {
2297 if (connectorId
> 0) {
2298 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2299 delete this.getConnectorStatus(connectorId
)?.status
2305 private startWebSocketPing (): void {
2306 const webSocketPingInterval
=
2307 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2309 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2312 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2313 this.wsPingSetInterval
= setInterval(() => {
2314 if (this.isWebSocketConnectionOpened()) {
2315 this.wsConnection
?.ping()
2317 }, secondsToMilliseconds(webSocketPingInterval
))
2319 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2320 webSocketPingInterval
2323 } else if (this.wsPingSetInterval
!= null) {
2325 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2326 webSocketPingInterval
2331 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2336 private stopWebSocketPing (): void {
2337 if (this.wsPingSetInterval
!= null) {
2338 clearInterval(this.wsPingSetInterval
)
2339 delete this.wsPingSetInterval
2343 private getConfiguredSupervisionUrl (): URL
{
2344 let configuredSupervisionUrl
: string
2345 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2346 if (isNotEmptyArray(supervisionUrls
)) {
2347 let configuredSupervisionUrlIndex
: number
2348 switch (Configuration
.getSupervisionUrlDistribution()) {
2349 case SupervisionUrlDistribution
.RANDOM
:
2350 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2352 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2353 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2355 !Object.values(SupervisionUrlDistribution
).includes(
2356 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2357 Configuration
.getSupervisionUrlDistribution()!
2360 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2361 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2362 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2365 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2368 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2370 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2371 configuredSupervisionUrl
= supervisionUrls
!
2373 if (isNotEmptyString(configuredSupervisionUrl
)) {
2374 return new URL(configuredSupervisionUrl
)
2376 const errorMsg
= 'No supervision url(s) configured'
2377 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2378 throw new BaseError(errorMsg
)
2381 private stopHeartbeat (): void {
2382 if (this.heartbeatSetInterval
!= null) {
2383 clearInterval(this.heartbeatSetInterval
)
2384 delete this.heartbeatSetInterval
2388 private terminateWSConnection (): void {
2389 if (this.isWebSocketConnectionOpened()) {
2390 this.wsConnection
?.terminate()
2391 this.wsConnection
= null
2395 private async reconnect (): Promise
<void> {
2397 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2398 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2399 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2401 this.wsConnectionRetried
= true
2402 ++this.wsConnectionRetryCount
2403 const reconnectDelay
=
2404 this.stationInfo
?.reconnectExponentialDelay
=== true
2405 ? exponentialDelay(this.wsConnectionRetryCount
)
2406 : secondsToMilliseconds(this.getConnectionTimeout())
2407 const reconnectDelayWithdraw
= 1000
2408 const reconnectTimeout
=
2409 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2411 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2414 )}ms, timeout ${reconnectTimeout}ms`
2416 await sleep(reconnectDelay
)
2418 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2420 this.openWSConnection(
2422 handshakeTimeout
: reconnectTimeout
2424 { closeOpened
: true }
2426 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2428 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2429 this.wsConnectionRetryCount
2430 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`