repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: cleanup connection retries logic
[e-mobility-charging-stations-simulator.git]
/
src
/
charging-station
/
ChargingStation.ts
diff --git
a/src/charging-station/ChargingStation.ts
b/src/charging-station/ChargingStation.ts
index 7cfb70f265bc3a9e0bad2f0c9accb8dba76ba038..e8d6387fc5d3f6513f7809dbe9470a5dcbcce53a 100644
(file)
--- a/
src/charging-station/ChargingStation.ts
+++ b/
src/charging-station/ChargingStation.ts
@@
-181,7
+181,6
@@
export class ChargingStation extends EventEmitter {
private ocppIncomingRequestService!: OCPPIncomingRequestService
private readonly messageBuffer: Set<string>
private configuredSupervisionUrl!: URL
private ocppIncomingRequestService!: OCPPIncomingRequestService
private readonly messageBuffer: Set<string>
private configuredSupervisionUrl!: URL
- private wsConnectionRetried: boolean
private wsConnectionRetryCount: number
private templateFileWatcher?: FSWatcher
private templateFileHash!: string
private wsConnectionRetryCount: number
private templateFileWatcher?: FSWatcher
private templateFileHash!: string
@@
-196,7
+195,6
@@
export class ChargingStation extends EventEmitter {
this.starting = false
this.stopping = false
this.wsConnection = null
this.starting = false
this.stopping = false
this.wsConnection = null
- this.wsConnectionRetried = false
this.wsConnectionRetryCount = 0
this.index = index
this.templateFile = templateFile
this.wsConnectionRetryCount = 0
this.index = index
this.templateFile = templateFile
@@
-225,16
+223,21
@@
export class ChargingStation extends EventEmitter {
})
this.on(ChargingStationEvents.accepted, () => {
this.startMessageSequence(
})
this.on(ChargingStationEvents.accepted, () => {
this.startMessageSequence(
- this.wsConnectionRetr
ied
+ this.wsConnectionRetr
yCount > 0
? true
: this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
).catch((error: unknown) => {
logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
})
? true
: this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
).catch((error: unknown) => {
logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
})
- this.wsConnectionRetr
ied = false
+ this.wsConnectionRetr
yCount = 0
})
this.on(ChargingStationEvents.rejected, () => {
})
this.on(ChargingStationEvents.rejected, () => {
- this.wsConnectionRetried = false
+ this.wsConnectionRetryCount = 0
+ })
+ this.on(ChargingStationEvents.connected, () => {
+ if (this.wsPingSetInterval == null) {
+ this.startWebSocketPing()
+ }
})
this.on(ChargingStationEvents.disconnected, () => {
try {
})
this.on(ChargingStationEvents.disconnected, () => {
try {
@@
-554,7
+557,8
@@
export class ChargingStation extends EventEmitter {
}
public startHeartbeat (): void {
}
public startHeartbeat (): void {
- if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval == null) {
+ const heartbeatInterval = this.getHeartbeatInterval()
+ if (heartbeatInterval > 0 && this.heartbeatSetInterval == null) {
this.heartbeatSetInterval = setInterval(() => {
this.ocppRequestService
.requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
this.heartbeatSetInterval = setInterval(() => {
this.ocppRequestService
.requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
@@
-564,21
+568,21
@@
export class ChargingStation extends EventEmitter {
error
)
})
error
)
})
- },
this.getHeartbeatInterval()
)
+ },
heartbeatInterval
)
logger.info(
`${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
logger.info(
`${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
- this.getHeartbeatInterval()
+ heartbeatInterval
)}`
)
} else if (this.heartbeatSetInterval != null) {
logger.info(
`${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
)}`
)
} else if (this.heartbeatSetInterval != null) {
logger.info(
`${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
- this.getHeartbeatInterval()
+ heartbeatInterval
)}`
)
} else {
logger.error(
)}`
)
} else {
logger.error(
- `${this.logPrefix()} Heartbeat interval set to ${
this.getHeartbeatInterval()
}, not starting the heartbeat`
+ `${this.logPrefix()} Heartbeat interval set to ${
heartbeatInterval
}, not starting the heartbeat`
)
}
}
)
}
}
@@
-1822,6
+1826,7
@@
export class ChargingStation extends EventEmitter {
private async onOpen (): Promise<void> {
if (this.isWebSocketConnectionOpened()) {
private async onOpen (): Promise<void> {
if (this.isWebSocketConnectionOpened()) {
+ this.emit(ChargingStationEvents.connected)
this.emit(ChargingStationEvents.updated)
logger.info(
`${this.logPrefix()} Connection to OCPP server through ${
this.emit(ChargingStationEvents.updated)
logger.info(
`${this.logPrefix()} Connection to OCPP server through ${
@@
-1832,8
+1837,7
@@
export class ChargingStation extends EventEmitter {
if (!this.isRegistered()) {
// Send BootNotification
do {
if (!this.isRegistered()) {
// Send BootNotification
do {
- // FIXME: duplicated assignment with the boot notification response handler
- this.bootNotificationResponse = await this.ocppRequestService.requestHandler<
+ await this.ocppRequestService.requestHandler<
BootNotificationRequest,
BootNotificationResponse
>(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
BootNotificationRequest,
BootNotificationResponse
>(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
@@
-1869,7
+1873,6
@@
export class ChargingStation extends EventEmitter {
})`
)
}
})`
)
}
- this.wsConnectionRetryCount = 0
this.emit(ChargingStationEvents.updated)
} else {
logger.warn(
this.emit(ChargingStationEvents.updated)
} else {
logger.warn(
@@
-2222,9
+2225,13
@@
export class ChargingStation extends EventEmitter {
})
}
// Start WebSocket ping
})
}
// Start WebSocket ping
- this.startWebSocketPing()
+ if (this.wsPingSetInterval == null) {
+ this.startWebSocketPing()
+ }
// Start heartbeat
// Start heartbeat
- this.startHeartbeat()
+ if (this.heartbeatSetInterval == null) {
+ this.startHeartbeat()
+ }
// Initialize connectors status
if (this.hasEvses) {
for (const [evseId, evseStatus] of this.evses) {
// Initialize connectors status
if (this.hasEvses) {
for (const [evseId, evseStatus] of this.evses) {
@@
-2314,13
+2321,14
@@
export class ChargingStation extends EventEmitter {
}
}
}
}
+ private getWebSocketPingInterval (): number {
+ return getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null
+ ? convertToInt(getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value)
+ : 0
+ }
+
private startWebSocketPing (): void {
private startWebSocketPing (): void {
- const webSocketPingInterval =
- getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null
- ? convertToInt(
- getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value
- )
- : 0
+ const webSocketPingInterval = this.getWebSocketPingInterval()
if (webSocketPingInterval > 0 && this.wsPingSetInterval == null) {
this.wsPingSetInterval = setInterval(() => {
if (this.isWebSocketConnectionOpened()) {
if (webSocketPingInterval > 0 && this.wsPingSetInterval == null) {
this.wsPingSetInterval = setInterval(() => {
if (this.isWebSocketConnectionOpened()) {
@@
-2410,7
+2418,6
@@
export class ChargingStation extends EventEmitter {
this.wsConnectionRetryCount < this.stationInfo!.autoReconnectMaxRetries! ||
this.stationInfo?.autoReconnectMaxRetries === -1
) {
this.wsConnectionRetryCount < this.stationInfo!.autoReconnectMaxRetries! ||
this.stationInfo?.autoReconnectMaxRetries === -1
) {
- this.wsConnectionRetried = true
++this.wsConnectionRetryCount
const reconnectDelay =
this.stationInfo?.reconnectExponentialDelay === true
++this.wsConnectionRetryCount
const reconnectDelay =
this.stationInfo?.reconnectExponentialDelay === true
@@
-2437,9
+2444,7
@@
export class ChargingStation extends EventEmitter {
)
} else if (this.stationInfo?.autoReconnectMaxRetries !== -1) {
logger.error(
)
} else if (this.stationInfo?.autoReconnectMaxRetries !== -1) {
logger.error(
- `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
- this.wsConnectionRetryCount
- }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`
+ `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${this.wsConnectionRetryCount.toString()}) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries?.toString()})`
)
}
}
)
}
}