X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=e8d6387fc5d3f6513f7809dbe9470a5dcbcce53a;hb=aa5f2ea2958ef34e8d34e20edbe8486b8911d82b;hp=c31c00f7b49343b8b3f92f0e15daeb34e0b8c5ce;hpb=314793aaf25bf1a99deb3f8209c09421235942ba;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index c31c00f7..e8d6387f 100644 --- 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 private configuredSupervisionUrl!: URL - private wsConnectionRetried: boolean 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.wsConnectionRetried = false 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.wsConnectionRetried + this.wsConnectionRetryCount > 0 ? true : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration ).catch((error: unknown) => { logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error) }) - this.wsConnectionRetried = false + this.wsConnectionRetryCount = 0 }) 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 { @@ -261,14 +264,17 @@ export class ChargingStation extends EventEmitter { } public get wsConnectionUrl (): URL { + const wsConnectionBaseUrlStr = `${ + this.stationInfo?.supervisionUrlOcppConfiguration === true && + isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && + isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value) + ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value + : this.configuredSupervisionUrl.href + }` return new URL( - `${ - this.stationInfo?.supervisionUrlOcppConfiguration === true && - isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && - isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value) - ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value - : this.configuredSupervisionUrl.href - }/${this.stationInfo?.chargingStationId}` + `${wsConnectionBaseUrlStr}${ + !wsConnectionBaseUrlStr.endsWith('/') ? '/' : '' + }${this.stationInfo?.chargingStationId}` ) } @@ -410,14 +416,17 @@ export class ChargingStation extends EventEmitter { const connectorChargingProfilesPowerLimit = getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId) return min( - isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, + isNaN(connectorMaximumPower) ? Number.POSITIVE_INFINITY : connectorMaximumPower, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion isNaN(connectorAmperageLimitationPowerLimit!) - ? Infinity + ? Number.POSITIVE_INFINITY : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion connectorAmperageLimitationPowerLimit!, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - isNaN(connectorChargingProfilesPowerLimit!) ? Infinity : connectorChargingProfilesPowerLimit! + isNaN(connectorChargingProfilesPowerLimit!) + ? Number.POSITIVE_INFINITY + : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + connectorChargingProfilesPowerLimit! ) } @@ -548,7 +557,8 @@ export class ChargingStation extends EventEmitter { } 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(this, RequestCommand.HEARTBEAT) @@ -558,21 +568,21 @@ export class ChargingStation extends EventEmitter { error ) }) - }, this.getHeartbeatInterval()) + }, heartbeatInterval) 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( - this.getHeartbeatInterval() + heartbeatInterval )}` ) } 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` ) } } @@ -1358,7 +1368,9 @@ export class ChargingStation extends EventEmitter { addConfigurationKey(this, StandardParametersKey.HeartbeatInterval, '0') } if (getConfigurationKey(this, StandardParametersKey.HeartBeatInterval) == null) { - addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { visible: false }) + addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { + visible: false + }) } if ( this.stationInfo?.supervisionUrlOcppConfiguration === true && @@ -1376,7 +1388,9 @@ export class ChargingStation extends EventEmitter { isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey) != null ) { - deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey, { save: false }) + deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey, { + save: false + }) } if ( isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && @@ -1735,7 +1749,9 @@ export class ChargingStation extends EventEmitter { ...(this.connectors.size > 0 && { connectorsStatus: configurationData.connectorsStatus }), - ...(this.evses.size > 0 && { evsesStatus: configurationData.evsesStatus }) + ...(this.evses.size > 0 && { + evsesStatus: configurationData.evsesStatus + }) } satisfies ChargingStationConfiguration) ) .digest('hex') @@ -1810,15 +1826,18 @@ export class ChargingStation extends EventEmitter { private async onOpen (): Promise { if (this.isWebSocketConnectionOpened()) { + this.emit(ChargingStationEvents.connected) this.emit(ChargingStationEvents.updated) logger.info( - `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded` + `${this.logPrefix()} Connection to OCPP server through ${ + this.wsConnectionUrl.href + } succeeded` ) let registrationRetryCount = 0 if (!this.isRegistered()) { // Send BootNotification do { - this.bootNotificationResponse = await this.ocppRequestService.requestHandler< + await this.ocppRequestService.requestHandler< BootNotificationRequest, BootNotificationResponse >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { @@ -1847,22 +1866,13 @@ export class ChargingStation extends EventEmitter { this.stationInfo?.registrationMaxRetries === -1) ) } - if (this.isRegistered()) { - this.emit(ChargingStationEvents.registered) - if (this.inAcceptedState()) { - this.emit(ChargingStationEvents.accepted) - } - } else { - if (this.inRejectedState()) { - this.emit(ChargingStationEvents.rejected) - } + if (!this.isRegistered()) { logger.error( `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${ this.stationInfo?.registrationMaxRetries })` ) } - this.wsConnectionRetryCount = 0 this.emit(ChargingStationEvents.updated) } else { logger.warn( @@ -2071,7 +2081,9 @@ export class ChargingStation extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-base-to-string }' message '${data.toString()}'${ this.requests.has(messageId) - ? ` matching cached request '${JSON.stringify(this.getCachedRequest(messageType, messageId))}'` + ? ` matching cached request '${JSON.stringify( + this.getCachedRequest(messageType, messageId) + )}'` : '' } processing error:`, error @@ -2213,9 +2225,13 @@ export class ChargingStation extends EventEmitter { }) } // Start WebSocket ping - this.startWebSocketPing() + if (this.wsPingSetInterval == null) { + this.startWebSocketPing() + } // Start heartbeat - this.startHeartbeat() + if (this.heartbeatSetInterval == null) { + this.startHeartbeat() + } // Initialize connectors status if (this.hasEvses) { for (const [evseId, evseStatus] of this.evses) { @@ -2305,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 { - 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()) { @@ -2401,7 +2418,6 @@ export class ChargingStation extends EventEmitter { this.wsConnectionRetryCount < this.stationInfo!.autoReconnectMaxRetries! || this.stationInfo?.autoReconnectMaxRetries === -1 ) { - this.wsConnectionRetried = true ++this.wsConnectionRetryCount const reconnectDelay = this.stationInfo?.reconnectExponentialDelay === true @@ -2428,9 +2444,7 @@ export class ChargingStation extends EventEmitter { ) } 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()})` ) } }