From 776d723cdbc23bc01d23a247b3e5eb3e241d5d91 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 17 Aug 2025 20:41:32 +0200 Subject: [PATCH] refactor: cleanup up internal messaging code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ChargingStationWorkerBroadcastChannel.ts | 28 ++++++---- .../UIServiceWorkerBroadcastChannel.ts | 54 +++++++++---------- .../WorkerBroadcastChannel.ts | 5 +- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 649c057f..eb591836 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -173,6 +173,9 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne chargingStation, StandardParametersKey.MeterValueSampleInterval ) + const connectorId = requestPayload?.connectorId + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const transactionId = this.chargingStation.getConnectorStatus(connectorId!)?.transactionId return await this.chargingStation.ocppRequestService.requestHandler< MeterValuesRequest, MeterValuesResponse @@ -184,10 +187,9 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne buildMeterValue( this.chargingStation, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - requestPayload!.connectorId!, + connectorId!, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)! - .transactionId!, + transactionId!, configuredMeterValueSampleInterval != null ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value)) : Constants.DEFAULT_METER_VALUES_INTERVAL @@ -208,7 +210,13 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne [ BroadcastChannelProcedureName.SET_SUPERVISION_URL, (requestPayload?: BroadcastChannelRequestPayload) => { - this.chargingStation.setSupervisionUrl(requestPayload?.url as string) + const url = requestPayload?.url + if (typeof url !== 'string' || isEmpty(url)) { + throw new BaseError( + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'url' field is required` + ) + } + this.chargingStation.setSupervisionUrl(url) }, ], [ @@ -266,7 +274,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne async (requestPayload?: BroadcastChannelRequestPayload) => await this.chargingStation.ocppRequestService.requestHandler< StopTransactionRequest, - StartTransactionResponse + StopTransactionResponse >( this.chargingStation, RequestCommand.STOP_TRANSACTION, @@ -416,6 +424,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne return } let responsePayload: BroadcastChannelResponsePayload | undefined + // eslint-disable-next-line promise/catch-or-return this.commandHandler(command, requestPayload) .then(commandResponse => { if (commandResponse == null || isEmpty(commandResponse)) { @@ -432,10 +441,6 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne } return undefined }) - .finally(() => { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.sendResponse([uuid, responsePayload!]) - }) .catch((error: unknown) => { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, @@ -450,6 +455,11 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne requestPayload, status: ResponseStatus.FAILURE, } satisfies BroadcastChannelResponsePayload + return undefined + }) + .finally(() => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.sendResponse([uuid, responsePayload!]) }) } } diff --git a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts index 12bc6824..b1bd85c6 100644 --- a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts @@ -31,51 +31,44 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { } private buildResponsePayload (uuid: string): ResponsePayload { + const responsesArray = this.responses.get(uuid)?.responses ?? [] const responsesStatus = - this.responses - .get(uuid) - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - ?.responses.every(response => response?.status === ResponseStatus.SUCCESS) === true + responsesArray.length > 0 && + responsesArray.every(response => response.status === ResponseStatus.SUCCESS) ? ResponseStatus.SUCCESS : ResponseStatus.FAILURE return { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain - hashIdsSucceeded: this.responses - .get(uuid) - ?.responses.map(response => { + hashIdsSucceeded: responsesArray + .map(response => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (response?.hashId != null && response?.status === ResponseStatus.SUCCESS) { return response.hashId } return undefined }) - .filter(hashId => hashId != null)!, + .filter((hashId): hashId is string => hashId != null), status: responsesStatus, ...(responsesStatus === ResponseStatus.FAILURE && { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain - hashIdsFailed: this.responses - .get(uuid) - ?.responses.map(response => { + hashIdsFailed: responsesArray + .map(response => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (response?.hashId != null && response?.status === ResponseStatus.FAILURE) { return response.hashId } return undefined }) - .filter(hashId => hashId != null)!, + .filter((hashId): hashId is string => hashId != null), }), ...(responsesStatus === ResponseStatus.FAILURE && { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain - responsesFailed: this.responses - .get(uuid) - ?.responses.map(response => { + responsesFailed: responsesArray + .map(response => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (response?.status === ResponseStatus.FAILURE) { return response } return undefined }) - .filter(response => response != null)!, + .filter((response): response is BroadcastChannelResponsePayload => response != null), }), } } @@ -102,17 +95,22 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid), responsesReceived: 1, }) - } else if ( + } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.responses.get(uuid)!.responsesReceived <= this.responses.get(uuid)!.responsesExpected - ) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.responses.get(uuid)!.responsesReceived - this.responses.get(uuid)?.responses.push(responsePayload) + const responses = this.responses.get(uuid)! + if (responses.responsesReceived < responses.responsesExpected) { + ++responses.responsesReceived + responses.responses.push(responsePayload) + } else { + logger.debug( + `${this.uiService.logPrefix(moduleName, 'responseHandler')} Received response after all expected responses:`, + { responsePayload, uuid } + ) + } } - if ( - this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected - ) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const responses = this.responses.get(uuid)! + if (responses.responsesReceived >= responses.responsesExpected) { this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)) this.responses.delete(uuid) this.uiService.deleteBroadcastChannelRequest(uuid) diff --git a/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts index cb371457..0846ed05 100644 --- a/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts @@ -33,7 +33,8 @@ export abstract class WorkerBroadcastChannel extends BroadcastChannel { } protected validateMessageEvent (messageEvent: MessageEvent): false | MessageEvent { - if (!Array.isArray(messageEvent.data)) { + const data = messageEvent.data + if (!Array.isArray(data)) { logger.error( `${this.logPrefix( moduleName, @@ -42,7 +43,7 @@ export abstract class WorkerBroadcastChannel extends BroadcastChannel { ) return false } - if (!validateUUID(messageEvent.data[0])) { + if (!validateUUID(data[0])) { logger.error( `${this.logPrefix( moduleName, -- 2.43.0