1 // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { createHash
, randomInt
} from
'node:crypto'
4 import { EventEmitter
} from
'node:events'
5 import { existsSync
, type FSWatcher
, mkdirSync
, readFileSync
, rmSync
, writeFileSync
} from
'node:fs'
6 import { dirname
, join
} from
'node:path'
7 import { URL
} from
'node:url'
8 import { parentPort
} from
'node:worker_threads'
10 import { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns'
11 import { mergeDeepRight
, once
} from
'rambda'
12 import { type RawData
, WebSocket
} from
'ws'
14 import { BaseError
, OCPPError
} from
'../exception/index.js'
15 import { PerformanceStatistics
} from
'../performance/index.js'
17 type AutomaticTransactionGeneratorConfiguration
,
19 type BootNotificationRequest
,
20 type BootNotificationResponse
,
22 type ChargingStationConfiguration
,
23 ChargingStationEvents
,
24 type ChargingStationInfo
,
25 type ChargingStationOcppConfiguration
,
26 type ChargingStationOptions
,
27 type ChargingStationTemplate
,
35 type EvseStatusConfiguration
,
38 type FirmwareStatusNotificationRequest
,
39 type FirmwareStatusNotificationResponse
,
40 type HeartbeatRequest
,
41 type HeartbeatResponse
,
43 type IncomingRequestCommand
,
46 type MeterValuesRequest
,
47 type MeterValuesResponse
,
51 RegistrationStatusEnumType
,
55 ReservationTerminationReason
,
57 StandardParametersKey
,
59 type StopTransactionReason
,
60 type StopTransactionRequest
,
61 type StopTransactionResponse
,
62 SupervisionUrlDistribution
,
63 SupportedFeatureProfiles
,
65 WebSocketCloseEventStatusCode
,
68 } from
'../types/index.js'
74 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
75 buildConnectorsStatus
,
89 formatDurationMilliSeconds
,
90 formatDurationSeconds
,
91 getWebSocketCloseEventStatusString
,
102 } from
'../utils/index.js'
103 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator.js'
104 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel.js'
107 deleteConfigurationKey
,
109 setConfigurationKeyValue
110 } from
'./ConfigurationKeyUtils.js'
114 checkChargingStation
,
116 checkConnectorsConfiguration
,
117 checkStationInfoConnectorStatus
,
119 createBootNotificationRequest
,
121 getAmperageLimitationUnitDivider
,
122 getBootConnectorStatus
,
123 getChargingStationConnectorChargingProfilesPowerLimit
,
124 getChargingStationId
,
125 getDefaultVoltageOut
,
129 getNumberOfReservableConnectors
,
130 getPhaseRotationValue
,
132 hasReservationExpired
,
133 initializeConnectorsMapStatus
,
134 propagateSerialNumber
,
135 setChargingStationOptions
,
136 stationTemplateToStationInfo
,
137 warnTemplateKeysDeprecation
138 } from
'./Helpers.js'
139 import { IdTagsCache
} from
'./IdTagsCache.js'
142 buildTransactionEndMeterValue
,
143 getMessageTypeString
,
144 OCPP16IncomingRequestService
,
145 OCPP16RequestService
,
146 OCPP16ResponseService
,
147 OCPP20IncomingRequestService
,
148 OCPP20RequestService
,
149 OCPP20ResponseService
,
150 type OCPPIncomingRequestService
,
151 type OCPPRequestService
,
152 sendAndSetConnectorStatus
153 } from
'./ocpp/index.js'
154 import { SharedLRUCache
} from
'./SharedLRUCache.js'
156 export class ChargingStation
extends EventEmitter
{
157 public readonly index
: number
158 public readonly templateFile
: string
159 public stationInfo
?: ChargingStationInfo
160 public started
: boolean
161 public starting
: boolean
162 public idTagsCache
: IdTagsCache
163 public automaticTransactionGenerator
?: AutomaticTransactionGenerator
164 public ocppConfiguration
?: ChargingStationOcppConfiguration
165 public wsConnection
: WebSocket
| null
166 public readonly connectors
: Map
<number, ConnectorStatus
>
167 public readonly evses
: Map
<number, EvseStatus
>
168 public readonly requests
: Map
<string, CachedRequest
>
169 public performanceStatistics
?: PerformanceStatistics
170 public heartbeatSetInterval
?: NodeJS
.Timeout
171 public ocppRequestService
!: OCPPRequestService
172 public bootNotificationRequest
?: BootNotificationRequest
173 public bootNotificationResponse
?: BootNotificationResponse
174 public powerDivider
?: number
175 private stopping
: boolean
176 private configurationFile
!: string
177 private configurationFileHash
!: string
178 private connectorsConfigurationHash
!: string
179 private evsesConfigurationHash
!: string
180 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
181 private ocppIncomingRequestService
!: OCPPIncomingRequestService
182 private readonly messageBuffer
: Set
<string>
183 private configuredSupervisionUrl
!: URL
184 private wsConnectionRetried
: boolean
185 private wsConnectionRetryCount
: number
186 private templateFileWatcher
?: FSWatcher
187 private templateFileHash
!: string
188 private readonly sharedLRUCache
: SharedLRUCache
189 private wsPingSetInterval
?: NodeJS
.Timeout
190 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
191 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
193 constructor (index
: number, templateFile
: string, options
?: ChargingStationOptions
) {
196 this.starting
= false
197 this.stopping
= false
198 this.wsConnection
= null
199 this.wsConnectionRetried
= false
200 this.wsConnectionRetryCount
= 0
202 this.templateFile
= templateFile
203 this.connectors
= new Map
<number, ConnectorStatus
>()
204 this.evses
= new Map
<number, EvseStatus
>()
205 this.requests
= new Map
<string, CachedRequest
>()
206 this.messageBuffer
= new Set
<string>()
207 this.sharedLRUCache
= SharedLRUCache
.getInstance()
208 this.idTagsCache
= IdTagsCache
.getInstance()
209 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this)
211 this.on(ChargingStationEvents
.added
, () => {
212 parentPort
?.postMessage(buildAddedMessage(this))
214 this.on(ChargingStationEvents
.deleted
, () => {
215 parentPort
?.postMessage(buildDeletedMessage(this))
217 this.on(ChargingStationEvents
.started
, () => {
218 parentPort
?.postMessage(buildStartedMessage(this))
220 this.on(ChargingStationEvents
.stopped
, () => {
221 parentPort
?.postMessage(buildStoppedMessage(this))
223 this.on(ChargingStationEvents
.updated
, () => {
224 parentPort
?.postMessage(buildUpdatedMessage(this))
226 this.on(ChargingStationEvents
.accepted
, () => {
227 this.startMessageSequence(
228 this.wsConnectionRetried
230 : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
231 ).catch((error
: unknown
) => {
232 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
)
234 this.wsConnectionRetried
= false
236 this.on(ChargingStationEvents
.rejected
, () => {
237 this.wsConnectionRetried
= false
239 this.on(ChargingStationEvents
.disconnected
, () => {
241 this.internalStopMessageSequence()
244 `${this.logPrefix()} Error while stopping the internal message sequence:`,
250 this.initialize(options
)
254 if (this.stationInfo
?.autoStart
=== true) {
259 public get
hasEvses (): boolean {
260 return this.connectors
.size
=== 0 && this.evses
.size
> 0
263 public get
wsConnectionUrl (): URL
{
264 const wsConnectionBaseUrlStr
= `${
265 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
266 isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) &&
267 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value)
268 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value
269 : this.configuredSupervisionUrl.href
272 `${wsConnectionBaseUrlStr}${!wsConnectionBaseUrlStr.endsWith('/') ? '/' : ''}${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
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
)
556 .catch((error
: unknown
) => {
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
]
641 .catch((error
: unknown
) => {
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
: unknown
) =>
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 Partial
<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
)
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
= buildTemplateName(this.templateFile
)
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 if (stationTemplate
.resetTime
!= null) {
1196 stationInfo
.resetTime
= secondsToMilliseconds(stationTemplate
.resetTime
)
1201 private getStationInfoFromFile (
1202 stationInfoPersistentConfiguration
: boolean | undefined = Constants
.DEFAULT_STATION_INFO
1203 .stationInfoPersistentConfiguration
1204 ): ChargingStationInfo
| undefined {
1205 let stationInfo
: ChargingStationInfo
| undefined
1206 if (stationInfoPersistentConfiguration
=== true) {
1207 stationInfo
= this.getConfigurationFromFile()?.stationInfo
1208 if (stationInfo
!= null) {
1209 delete stationInfo
.infoHash
1210 delete (stationInfo
as ChargingStationTemplate
).numberOfConnectors
1211 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1212 if (stationInfo
.templateIndex
== null) {
1213 stationInfo
.templateIndex
= this.index
1215 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1216 if (stationInfo
.templateName
== null) {
1217 stationInfo
.templateName
= buildTemplateName(this.templateFile
)
1224 private getStationInfo (options
?: ChargingStationOptions
): ChargingStationInfo
{
1225 const stationInfoFromTemplate
= this.getStationInfoFromTemplate()
1226 options
?.persistentConfiguration
!= null &&
1227 (stationInfoFromTemplate
.stationInfoPersistentConfiguration
= options
.persistentConfiguration
)
1228 const stationInfoFromFile
= this.getStationInfoFromFile(
1229 stationInfoFromTemplate
.stationInfoPersistentConfiguration
1231 let stationInfo
: ChargingStationInfo
1233 // 1. charging station info from template
1234 // 2. charging station info from configuration file
1236 stationInfoFromFile
!= null &&
1237 stationInfoFromFile
.templateHash
=== stationInfoFromTemplate
.templateHash
1239 stationInfo
= stationInfoFromFile
1241 stationInfo
= stationInfoFromTemplate
1242 stationInfoFromFile
!= null &&
1243 propagateSerialNumber(this.getTemplateFromFile(), stationInfoFromFile
, stationInfo
)
1245 return setChargingStationOptions(
1246 mergeDeepRight(Constants
.DEFAULT_STATION_INFO
, stationInfo
),
1251 private saveStationInfo (): void {
1252 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1253 this.saveConfiguration()
1257 private handleUnsupportedVersion (version
: OCPPVersion
| undefined): void {
1258 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`
1259 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1260 throw new BaseError(errorMsg
)
1263 private initialize (options
?: ChargingStationOptions
): void {
1264 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1265 const stationTemplate
= this.getTemplateFromFile()!
1266 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
)
1267 this.configurationFile
= join(
1268 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1269 `${getHashId(this.index, stationTemplate)}.json`
1271 const stationConfiguration
= this.getConfigurationFromFile()
1273 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
.templateHash
&&
1274 (stationConfiguration
?.connectorsStatus
!= null || stationConfiguration
?.evsesStatus
!= null)
1276 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
)
1277 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
)
1279 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
)
1281 this.stationInfo
= this.getStationInfo(options
)
1283 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1284 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
) &&
1285 isNotEmptyString(this.stationInfo
.firmwareVersion
)
1287 const patternGroup
=
1288 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1289 this.stationInfo
.firmwareVersion
.split('.').length
1290 const match
= new RegExp(this.stationInfo
.firmwareVersionPattern
)
1291 .exec(this.stationInfo
.firmwareVersion
)
1292 ?.slice(1, patternGroup
+ 1)
1293 if (match
!= null) {
1294 const patchLevelIndex
= match
.length
- 1
1295 match
[patchLevelIndex
] = (
1296 convertToInt(match
[patchLevelIndex
]) +
1297 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1298 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1300 this.stationInfo
.firmwareVersion
= match
.join('.')
1303 this.saveStationInfo()
1304 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl()
1305 if (this.stationInfo
.enableStatistics
=== true) {
1306 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1307 this.stationInfo
.hashId
,
1308 this.stationInfo
.chargingStationId
,
1309 this.configuredSupervisionUrl
1312 const bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
)
1313 if (bootNotificationRequest
== null) {
1314 const errorMsg
= 'Error while creating boot notification request'
1315 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1316 throw new BaseError(errorMsg
)
1318 this.bootNotificationRequest
= bootNotificationRequest
1319 this.powerDivider
= this.getPowerDivider()
1320 // OCPP configuration
1321 this.ocppConfiguration
= this.getOcppConfiguration(options
?.persistentConfiguration
)
1322 this.initializeOcppConfiguration()
1323 this.initializeOcppServices()
1324 if (this.stationInfo
.autoRegister
=== true) {
1325 this.bootNotificationResponse
= {
1326 currentTime
: new Date(),
1327 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1328 status: RegistrationStatusEnumType
.ACCEPTED
1333 private initializeOcppServices (): void {
1334 const ocppVersion
= this.stationInfo
?.ocppVersion
1335 switch (ocppVersion
) {
1336 case OCPPVersion
.VERSION_16
:
1337 this.ocppIncomingRequestService
=
1338 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>()
1339 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1340 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1343 case OCPPVersion
.VERSION_20
:
1344 case OCPPVersion
.VERSION_201
:
1345 this.ocppIncomingRequestService
=
1346 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>()
1347 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1348 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1352 this.handleUnsupportedVersion(ocppVersion
)
1357 private initializeOcppConfiguration (): void {
1358 if (getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
) == null) {
1359 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0')
1361 if (getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
) == null) {
1362 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false })
1365 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1366 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1367 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) == null
1369 addConfigurationKey(
1371 this.stationInfo
.supervisionUrlOcppKey
,
1372 this.configuredSupervisionUrl
.href
,
1376 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1377 isNotEmptyString(this.stationInfo
.supervisionUrlOcppKey
) &&
1378 getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
) != null
1380 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
, { save
: false })
1383 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1384 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) == null
1386 addConfigurationKey(
1388 this.stationInfo
.amperageLimitationOcppKey
,
1390 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1391 (this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)).toString()
1394 if (getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
) == null) {
1395 addConfigurationKey(
1397 StandardParametersKey
.SupportedFeatureProfiles
,
1398 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1401 addConfigurationKey(
1403 StandardParametersKey
.NumberOfConnectors
,
1404 this.getNumberOfConnectors().toString(),
1408 if (getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
) == null) {
1409 addConfigurationKey(
1411 StandardParametersKey
.MeterValuesSampledData
,
1412 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1415 if (getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
) == null) {
1416 const connectorsPhaseRotation
: string[] = []
1417 if (this.hasEvses
) {
1418 for (const evseStatus
of this.evses
.values()) {
1419 for (const connectorId
of evseStatus
.connectors
.keys()) {
1420 connectorsPhaseRotation
.push(
1421 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1422 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1427 for (const connectorId
of this.connectors
.keys()) {
1428 connectorsPhaseRotation
.push(
1429 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1430 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!
1434 addConfigurationKey(
1436 StandardParametersKey
.ConnectorPhaseRotation
,
1437 connectorsPhaseRotation
.toString()
1440 if (getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
) == null) {
1441 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true')
1444 getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) == null &&
1445 hasFeatureProfile(this, SupportedFeatureProfiles
.LocalAuthListManagement
) === true
1447 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false')
1449 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) == null) {
1450 addConfigurationKey(
1452 StandardParametersKey
.ConnectionTimeOut
,
1453 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1456 this.saveOcppConfiguration()
1459 private initializeConnectorsOrEvsesFromFile (configuration
: ChargingStationConfiguration
): void {
1460 if (configuration
.connectorsStatus
!= null && configuration
.evsesStatus
== null) {
1461 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1462 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1464 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
== null) {
1465 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1466 const evseStatus
= clone
<EvseStatusConfiguration
>(evseStatusConfiguration
)
1467 delete evseStatus
.connectorsStatus
1468 this.evses
.set(evseId
, {
1469 ...(evseStatus
as EvseStatus
),
1470 connectors
: new Map
<number, ConnectorStatus
>(
1471 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1472 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1479 } else if (configuration
.evsesStatus
!= null && configuration
.connectorsStatus
!= null) {
1480 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`
1481 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1482 throw new BaseError(errorMsg
)
1484 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`
1485 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1486 throw new BaseError(errorMsg
)
1490 private initializeConnectorsOrEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1491 if (stationTemplate
.Connectors
!= null && stationTemplate
.Evses
== null) {
1492 this.initializeConnectorsFromTemplate(stationTemplate
)
1493 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
== null) {
1494 this.initializeEvsesFromTemplate(stationTemplate
)
1495 } else if (stationTemplate
.Evses
!= null && stationTemplate
.Connectors
!= null) {
1496 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`
1497 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1498 throw new BaseError(errorMsg
)
1500 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`
1501 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1502 throw new BaseError(errorMsg
)
1506 private initializeConnectorsFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1507 if (stationTemplate
.Connectors
== null && this.connectors
.size
=== 0) {
1508 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`
1509 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1510 throw new BaseError(errorMsg
)
1512 if (stationTemplate
.Connectors
?.[0] == null) {
1514 `${this.logPrefix()} Charging station information from template ${
1516 } with no connector id 0 configuration`
1519 if (stationTemplate
.Connectors
!= null) {
1520 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1521 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
)
1522 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1524 `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}`
1527 const connectorsConfigChanged
=
1528 this.connectors
.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
1529 if (this.connectors
.size
=== 0 || connectorsConfigChanged
) {
1530 connectorsConfigChanged
&& this.connectors
.clear()
1531 this.connectorsConfigurationHash
= connectorsConfigHash
1532 if (templateMaxConnectors
> 0) {
1533 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1535 connectorId
=== 0 &&
1536 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1537 (stationTemplate
.Connectors
[connectorId
] == null ||
1538 !this.getUseConnectorId0(stationTemplate
))
1542 const templateConnectorId
=
1543 connectorId
> 0 && stationTemplate
.randomConnectors
=== true
1544 ? randomInt(1, templateMaxAvailableConnectors
)
1546 const connectorStatus
= stationTemplate
.Connectors
[templateConnectorId
]
1547 checkStationInfoConnectorStatus(
1548 templateConnectorId
,
1553 this.connectors
.set(connectorId
, clone
<ConnectorStatus
>(connectorStatus
))
1555 initializeConnectorsMapStatus(this.connectors
, this.logPrefix())
1556 this.saveConnectorsStatus()
1559 `${this.logPrefix()} Charging station information from template ${
1561 } with no connectors configuration defined, cannot create connectors`
1567 `${this.logPrefix()} Charging station information from template ${
1569 } with no connectors configuration defined, using already defined connectors`
1574 private initializeEvsesFromTemplate (stationTemplate
: ChargingStationTemplate
): void {
1575 if (stationTemplate
.Evses
== null && this.evses
.size
=== 0) {
1576 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`
1577 logger
.error(`${this.logPrefix()} ${errorMsg}`)
1578 throw new BaseError(errorMsg
)
1580 if (stationTemplate
.Evses
?.[0] == null) {
1582 `${this.logPrefix()} Charging station information from template ${
1584 } with no evse id 0 configuration`
1587 if (stationTemplate
.Evses
?.[0]?.Connectors
[0] == null) {
1589 `${this.logPrefix()} Charging station information from template ${
1591 } with evse id 0 with no connector id 0 configuration`
1594 if (Object.keys(stationTemplate
.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1596 `${this.logPrefix()} Charging station information from template ${
1598 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`
1601 if (stationTemplate
.Evses
!= null) {
1602 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1603 .update(JSON
.stringify(stationTemplate
.Evses
))
1605 const evsesConfigChanged
=
1606 this.evses
.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
1607 if (this.evses
.size
=== 0 || evsesConfigChanged
) {
1608 evsesConfigChanged
&& this.evses
.clear()
1609 this.evsesConfigurationHash
= evsesConfigHash
1610 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
.Evses
)
1611 if (templateMaxEvses
> 0) {
1612 for (const evseKey
in stationTemplate
.Evses
) {
1613 const evseId
= convertToInt(evseKey
)
1614 this.evses
.set(evseId
, {
1615 connectors
: buildConnectorsMap(
1616 stationTemplate
.Evses
[evseKey
].Connectors
,
1620 availability
: AvailabilityType
.Operative
1622 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1623 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix())
1625 this.saveEvsesStatus()
1628 `${this.logPrefix()} Charging station information from template ${
1630 } with no evses configuration defined, cannot create evses`
1636 `${this.logPrefix()} Charging station information from template ${
1638 } with no evses configuration defined, using already defined evses`
1643 private getConfigurationFromFile (): ChargingStationConfiguration
| undefined {
1644 let configuration
: ChargingStationConfiguration
| undefined
1645 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1647 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1648 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1649 this.configurationFileHash
1652 const measureId
= `${FileType.ChargingStationConfiguration} read`
1653 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1654 configuration
= JSON
.parse(
1655 readFileSync(this.configurationFile
, 'utf8')
1656 ) as ChargingStationConfiguration
1657 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1658 this.sharedLRUCache
.setChargingStationConfiguration(configuration
)
1659 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1660 this.configurationFileHash
= configuration
.configurationHash
!
1663 handleFileException(
1664 this.configurationFile
,
1665 FileType
.ChargingStationConfiguration
,
1666 error
as NodeJS
.ErrnoException
,
1671 return configuration
1674 private saveAutomaticTransactionGeneratorConfiguration (): void {
1675 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1676 this.saveConfiguration()
1680 private saveConnectorsStatus (): void {
1681 this.saveConfiguration()
1684 private saveEvsesStatus (): void {
1685 this.saveConfiguration()
1688 private saveConfiguration (): void {
1689 if (isNotEmptyString(this.configurationFile
)) {
1691 if (!existsSync(dirname(this.configurationFile
))) {
1692 mkdirSync(dirname(this.configurationFile
), { recursive
: true })
1694 const configurationFromFile
= this.getConfigurationFromFile()
1695 let configurationData
: ChargingStationConfiguration
=
1696 configurationFromFile
!= null
1697 ? clone
<ChargingStationConfiguration
>(configurationFromFile
)
1699 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1700 configurationData
.stationInfo
= this.stationInfo
1702 delete configurationData
.stationInfo
1705 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1706 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1708 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
1710 delete configurationData
.configurationKey
1712 configurationData
= mergeDeepRight(
1714 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1716 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
!== true) {
1717 delete configurationData
.automaticTransactionGenerator
1719 if (this.connectors
.size
> 0) {
1720 configurationData
.connectorsStatus
= buildConnectorsStatus(this)
1722 delete configurationData
.connectorsStatus
1724 if (this.evses
.size
> 0) {
1725 configurationData
.evsesStatus
= buildEvsesStatus(this)
1727 delete configurationData
.evsesStatus
1729 delete configurationData
.configurationHash
1730 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1733 stationInfo
: configurationData
.stationInfo
,
1734 configurationKey
: configurationData
.configurationKey
,
1735 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1736 ...(this.connectors
.size
> 0 && {
1737 connectorsStatus
: configurationData
.connectorsStatus
1739 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
})
1740 } satisfies ChargingStationConfiguration
)
1743 if (this.configurationFileHash
!== configurationHash
) {
1744 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1745 configurationData
.configurationHash
= configurationHash
1746 const measureId
= `${FileType.ChargingStationConfiguration} write`
1747 const beginId
= PerformanceStatistics
.beginMeasure(measureId
)
1749 this.configurationFile
,
1750 JSON
.stringify(configurationData
, undefined, 2),
1753 PerformanceStatistics
.endMeasure(measureId
, beginId
)
1754 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
)
1755 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
)
1756 this.configurationFileHash
= configurationHash
1757 }).catch((error
: unknown
) => {
1758 handleFileException(
1759 this.configurationFile
,
1760 FileType
.ChargingStationConfiguration
,
1761 error
as NodeJS
.ErrnoException
,
1767 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1768 this.configurationFile
1773 handleFileException(
1774 this.configurationFile
,
1775 FileType
.ChargingStationConfiguration
,
1776 error
as NodeJS
.ErrnoException
,
1782 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1787 private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration
| undefined {
1788 return this.getTemplateFromFile()?.Configuration
1791 private getOcppConfigurationFromFile (
1792 ocppPersistentConfiguration
?: boolean
1793 ): ChargingStationOcppConfiguration
| undefined {
1794 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
1795 if (ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1796 return { configurationKey
}
1801 private getOcppConfiguration (
1802 ocppPersistentConfiguration
: boolean | undefined = this.stationInfo
?.ocppPersistentConfiguration
1803 ): ChargingStationOcppConfiguration
| undefined {
1804 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1805 this.getOcppConfigurationFromFile(ocppPersistentConfiguration
)
1806 if (ocppConfiguration
== null) {
1807 ocppConfiguration
= this.getOcppConfigurationFromTemplate()
1809 return ocppConfiguration
1812 private async onOpen (): Promise
<void> {
1813 if (this.isWebSocketConnectionOpened()) {
1814 this.emit(ChargingStationEvents
.updated
)
1816 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded`
1818 let registrationRetryCount
= 0
1819 if (!this.isRegistered()) {
1820 // Send BootNotification
1822 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1823 BootNotificationRequest
,
1824 BootNotificationResponse
1825 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1826 skipBufferingOnError
: true
1828 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1829 if (this.bootNotificationResponse
?.currentTime
!= null) {
1830 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1831 this.bootNotificationResponse
.currentTime
= convertToDate(
1832 this.bootNotificationResponse
.currentTime
1835 if (!this.isRegistered()) {
1836 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
1838 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1839 this.bootNotificationResponse
?.interval
!= null
1840 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1841 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1845 !this.isRegistered() &&
1846 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1847 (registrationRetryCount
<= this.stationInfo
!.registrationMaxRetries
! ||
1848 this.stationInfo
?.registrationMaxRetries
=== -1)
1851 if (this.isRegistered()) {
1852 this.emit(ChargingStationEvents
.registered
)
1853 if (this.inAcceptedState()) {
1854 this.emit(ChargingStationEvents
.accepted
)
1857 if (this.inRejectedState()) {
1858 this.emit(ChargingStationEvents
.rejected
)
1861 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
1862 this.stationInfo?.registrationMaxRetries
1866 this.wsConnectionRetryCount
= 0
1867 this.emit(ChargingStationEvents
.updated
)
1870 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} failed`
1875 private onClose (code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): void {
1876 this.emit(ChargingStationEvents
.disconnected
)
1877 this.emit(ChargingStationEvents
.updated
)
1880 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1881 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1883 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1885 )}' and reason '${reason.toString()}'`
1887 this.wsConnectionRetryCount
= 0
1892 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1894 )}' and reason '${reason.toString()}'`
1899 this.emit(ChargingStationEvents
.updated
)
1901 .catch((error
: unknown
) =>
1902 logger
.error(`${this.logPrefix()} Error while reconnecting:`, error
)
1908 private getCachedRequest (
1909 messageType
: MessageType
| undefined,
1911 ): CachedRequest
| undefined {
1912 const cachedRequest
= this.requests
.get(messageId
)
1913 if (Array.isArray(cachedRequest
)) {
1914 return cachedRequest
1916 throw new OCPPError(
1917 ErrorType
.PROTOCOL_ERROR
,
1918 `Cached request for message id ${messageId} ${getMessageTypeString(
1920 )} is not an array`,
1926 private async handleIncomingMessage (request
: IncomingRequest
): Promise
<void> {
1927 const [messageType
, messageId
, commandName
, commandPayload
] = request
1928 if (this.stationInfo
?.enableStatistics
=== true) {
1929 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
)
1932 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1936 // Process the message
1937 await this.ocppIncomingRequestService
.incomingRequestHandler(
1943 this.emit(ChargingStationEvents
.updated
)
1946 private handleResponseMessage (response
: Response
): void {
1947 const [messageType
, messageId
, commandPayload
] = response
1948 if (!this.requests
.has(messageId
)) {
1950 throw new OCPPError(
1951 ErrorType
.INTERNAL_ERROR
,
1952 `Response for unknown message id ${messageId}`,
1958 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1959 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1964 `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
1968 responseCallback(commandPayload
, requestPayload
)
1971 private handleErrorMessage (errorResponse
: ErrorResponse
): void {
1972 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
1973 if (!this.requests
.has(messageId
)) {
1975 throw new OCPPError(
1976 ErrorType
.INTERNAL_ERROR
,
1977 `Error response for unknown message id ${messageId}`,
1979 { errorType
, errorMessage
, errorDetails
}
1982 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1983 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
1985 `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify(
1989 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
))
1992 private async onMessage (data
: RawData
): Promise
<void> {
1993 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined
1994 let messageType
: MessageType
| undefined
1995 let errorMsg
: string
1997 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1998 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
1999 if (Array.isArray(request
)) {
2000 [messageType
] = request
2001 // Check the type of message
2002 switch (messageType
) {
2004 case MessageType
.CALL_MESSAGE
:
2005 await this.handleIncomingMessage(request
as IncomingRequest
)
2008 case MessageType
.CALL_RESULT_MESSAGE
:
2009 this.handleResponseMessage(request
as Response
)
2012 case MessageType
.CALL_ERROR_MESSAGE
:
2013 this.handleErrorMessage(request
as ErrorResponse
)
2017 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2018 errorMsg
= `Wrong message type ${messageType}`
2019 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2020 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
)
2023 throw new OCPPError(
2024 ErrorType
.PROTOCOL_ERROR
,
2025 'Incoming message is not an array',
2033 if (!Array.isArray(request
)) {
2034 logger
.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error
)
2037 let commandName
: IncomingRequestCommand
| undefined
2038 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined
2039 let errorCallback
: ErrorCallback
2040 const [, messageId
] = request
2041 switch (messageType
) {
2042 case MessageType
.CALL_MESSAGE
:
2043 [, , commandName
] = request
as IncomingRequest
2045 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
)
2047 case MessageType
.CALL_RESULT_MESSAGE
:
2048 case MessageType
.CALL_ERROR_MESSAGE
:
2049 if (this.requests
.has(messageId
)) {
2050 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2051 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!
2052 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2053 errorCallback(error
as OCPPError
, false)
2055 // Remove the request from the cache in case of error at response handling
2056 this.requests
.delete(messageId
)
2060 if (!(error
instanceof OCPPError
)) {
2062 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2063 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2064 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2065 }' message '${data.toString()}' handling is not an OCPPError:`,
2070 `${this.logPrefix()} Incoming OCPP command '${
2071 commandName ?? requestCommandName ?? Constants.UNKNOWN_OCPP_COMMAND
2072 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2073 }' message '${data.toString()}'${
2074 this.requests.has(messageId)
2075 ? ` matching cached request
'${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
2077 } processing error:`,
2083 private onPing (): void {
2084 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`)
2087 private onPong (): void {
2088 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`)
2091 private onError (error
: WSError
): void {
2092 this.closeWSConnection()
2093 logger
.error(`${this.logPrefix()} WebSocket error:`, error
)
2096 private getEnergyActiveImportRegister (
2097 connectorStatus
: ConnectorStatus
| undefined,
2100 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2103 ? connectorStatus
?.transactionEnergyActiveImportRegisterValue
!= null
2104 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
)
2106 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2111 ? connectorStatus
?.energyActiveImportRegisterValue
!= null
2112 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
)
2114 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2118 private getUseConnectorId0 (stationTemplate
?: ChargingStationTemplate
): boolean {
2119 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2120 return stationTemplate
?.useConnectorId0
?? Constants
.DEFAULT_STATION_INFO
.useConnectorId0
!
2123 private async stopRunningTransactions (reason
?: StopTransactionReason
): Promise
<void> {
2124 if (this.hasEvses
) {
2125 for (const [evseId
, evseStatus
] of this.evses
) {
2129 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2130 if (connectorStatus
.transactionStarted
=== true) {
2131 await this.stopTransactionOnConnector(connectorId
, reason
)
2136 for (const connectorId
of this.connectors
.keys()) {
2137 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2138 await this.stopTransactionOnConnector(connectorId
, reason
)
2145 private getConnectionTimeout (): number {
2146 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) != null) {
2147 return convertToInt(
2148 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)?.value
??
2149 Constants
.DEFAULT_CONNECTION_TIMEOUT
2152 return Constants
.DEFAULT_CONNECTION_TIMEOUT
2155 private getPowerDivider (): number {
2156 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()
2157 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2158 powerDivider
= this.getNumberOfRunningTransactions()
2163 private getMaximumAmperage (stationInfo
?: ChargingStationInfo
): number | undefined {
2164 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2165 const maximumPower
= (stationInfo
?? this.stationInfo
!).maximumPower
!
2166 switch (this.getCurrentOutType(stationInfo
)) {
2167 case CurrentType
.AC
:
2168 return ACElectricUtils
.amperagePerPhaseFromPower(
2169 this.getNumberOfPhases(stationInfo
),
2170 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2171 this.getVoltageOut(stationInfo
)
2173 case CurrentType
.DC
:
2174 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
))
2178 private getCurrentOutType (stationInfo
?: ChargingStationInfo
): CurrentType
{
2180 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2181 (stationInfo
?? this.stationInfo
!).currentOutType
??
2182 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2183 Constants
.DEFAULT_STATION_INFO
.currentOutType
!
2187 private getVoltageOut (stationInfo
?: ChargingStationInfo
): Voltage
{
2189 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2190 (stationInfo
?? this.stationInfo
!).voltageOut
??
2191 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2195 private getAmperageLimitation (): number | undefined {
2197 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2198 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
) != null
2201 convertToInt(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
)?.value
) /
2202 getAmperageLimitationUnitDivider(this.stationInfo
)
2207 private async startMessageSequence (ATGStopAbsoluteDuration
?: boolean): Promise
<void> {
2208 if (this.stationInfo
?.autoRegister
=== true) {
2209 await this.ocppRequestService
.requestHandler
<
2210 BootNotificationRequest
,
2211 BootNotificationResponse
2212 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2213 skipBufferingOnError
: true
2216 // Start WebSocket ping
2217 this.startWebSocketPing()
2219 this.startHeartbeat()
2220 // Initialize connectors status
2221 if (this.hasEvses
) {
2222 for (const [evseId
, evseStatus
] of this.evses
) {
2224 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2225 await sendAndSetConnectorStatus(
2228 getBootConnectorStatus(this, connectorId
, connectorStatus
),
2235 for (const connectorId
of this.connectors
.keys()) {
2236 if (connectorId
> 0) {
2237 await sendAndSetConnectorStatus(
2240 getBootConnectorStatus(
2243 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2244 this.getConnectorStatus(connectorId
)!
2250 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2251 await this.ocppRequestService
.requestHandler
<
2252 FirmwareStatusNotificationRequest
,
2253 FirmwareStatusNotificationResponse
2254 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2255 status: FirmwareStatus
.Installed
2257 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
2261 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2262 this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration
)
2264 this.flushMessageBuffer()
2267 private internalStopMessageSequence (): void {
2268 // Stop WebSocket ping
2269 this.stopWebSocketPing()
2271 this.stopHeartbeat()
2273 if (this.automaticTransactionGenerator
?.started
=== true) {
2274 this.stopAutomaticTransactionGenerator()
2278 private async stopMessageSequence (
2279 reason
?: StopTransactionReason
,
2280 stopTransactions
?: boolean
2282 this.internalStopMessageSequence()
2283 // Stop ongoing transactions
2284 stopTransactions
=== true && (await this.stopRunningTransactions(reason
))
2285 if (this.hasEvses
) {
2286 for (const [evseId
, evseStatus
] of this.evses
) {
2288 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2289 await sendAndSetConnectorStatus(
2292 ConnectorStatusEnum
.Unavailable
,
2295 delete connectorStatus
.status
2300 for (const connectorId
of this.connectors
.keys()) {
2301 if (connectorId
> 0) {
2302 await sendAndSetConnectorStatus(this, connectorId
, ConnectorStatusEnum
.Unavailable
)
2303 delete this.getConnectorStatus(connectorId
)?.status
2309 private startWebSocketPing (): void {
2310 const webSocketPingInterval
=
2311 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) != null
2313 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
2316 if (webSocketPingInterval
> 0 && this.wsPingSetInterval
== null) {
2317 this.wsPingSetInterval
= setInterval(() => {
2318 if (this.isWebSocketConnectionOpened()) {
2319 this.wsConnection
?.ping()
2321 }, secondsToMilliseconds(webSocketPingInterval
))
2323 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2324 webSocketPingInterval
2327 } else if (this.wsPingSetInterval
!= null) {
2329 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2330 webSocketPingInterval
2335 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2340 private stopWebSocketPing (): void {
2341 if (this.wsPingSetInterval
!= null) {
2342 clearInterval(this.wsPingSetInterval
)
2343 delete this.wsPingSetInterval
2347 private getConfiguredSupervisionUrl (): URL
{
2348 let configuredSupervisionUrl
: string
2349 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls()
2350 if (isNotEmptyArray(supervisionUrls
)) {
2351 let configuredSupervisionUrlIndex
: number
2352 switch (Configuration
.getSupervisionUrlDistribution()) {
2353 case SupervisionUrlDistribution
.RANDOM
:
2354 configuredSupervisionUrlIndex
= Math.floor(secureRandom() * supervisionUrls
.length
)
2356 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2357 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2359 !Object.values(SupervisionUrlDistribution
).includes(
2360 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2361 Configuration
.getSupervisionUrlDistribution()!
2364 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2365 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' in configuration from values '${SupervisionUrlDistribution.toString()}', defaulting to '${
2366 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2369 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
2372 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
]
2374 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2375 configuredSupervisionUrl
= supervisionUrls
!
2377 if (isNotEmptyString(configuredSupervisionUrl
)) {
2378 return new URL(configuredSupervisionUrl
)
2380 const errorMsg
= 'No supervision url(s) configured'
2381 logger
.error(`${this.logPrefix()} ${errorMsg}`)
2382 throw new BaseError(errorMsg
)
2385 private stopHeartbeat (): void {
2386 if (this.heartbeatSetInterval
!= null) {
2387 clearInterval(this.heartbeatSetInterval
)
2388 delete this.heartbeatSetInterval
2392 private terminateWSConnection (): void {
2393 if (this.isWebSocketConnectionOpened()) {
2394 this.wsConnection
?.terminate()
2395 this.wsConnection
= null
2399 private async reconnect (): Promise
<void> {
2401 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2402 this.wsConnectionRetryCount
< this.stationInfo
!.autoReconnectMaxRetries
! ||
2403 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2405 this.wsConnectionRetried
= true
2406 ++this.wsConnectionRetryCount
2407 const reconnectDelay
=
2408 this.stationInfo
?.reconnectExponentialDelay
=== true
2409 ? exponentialDelay(this.wsConnectionRetryCount
)
2410 : secondsToMilliseconds(this.getConnectionTimeout())
2411 const reconnectDelayWithdraw
= 1000
2412 const reconnectTimeout
=
2413 reconnectDelay
- reconnectDelayWithdraw
> 0 ? reconnectDelay
- reconnectDelayWithdraw
: 0
2415 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2418 )}ms, timeout ${reconnectTimeout}ms`
2420 await sleep(reconnectDelay
)
2422 `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
2424 this.openWSConnection(
2426 handshakeTimeout
: reconnectTimeout
2428 { closeOpened
: true }
2430 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2432 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2433 this.wsConnectionRetryCount
2434 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`