X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIServiceWorkerBroadcastChannel.ts;h=cdbe108df333e9f6b6388f91e5327be60a8a3821;hb=78202038ffd2aca15aa97f45bc66ba42f40f2ec4;hp=c64a7ccc2212d021189b373278ad8d5a804961bc;hpb=b3b45e6c81c2a01d793c3dc9ab48157e51c82ca3;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index c64a7ccc..cdbe108d 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -1,37 +1,107 @@ -import type { ResponsePayload } from '../types/UIProtocol'; -import type { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; -import logger from '../utils/Logger'; import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; import WorkerBroadcastChannel from './WorkerBroadcastChannel'; +import { type ResponsePayload, ResponseStatus } from '../types/UIProtocol'; +import type { + BroadcastChannelResponse, + BroadcastChannelResponsePayload, + MessageEvent, +} from '../types/WorkerBroadcastChannel'; +import logger from '../utils/Logger'; const moduleName = 'UIServiceWorkerBroadcastChannel'; +type Responses = { + responsesExpected: number; + responsesReceived: number; + responses: BroadcastChannelResponsePayload[]; +}; + export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { - private uiService: AbstractUIService; + private readonly uiService: AbstractUIService; + private readonly responses: Map; constructor(uiService: AbstractUIService) { super(); this.uiService = uiService; this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void; this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; + this.responses = new Map(); } private responseHandler(messageEvent: MessageEvent): void { - if (this.isRequest(messageEvent.data)) { + const validatedMessageEvent = this.validateMessageEvent(messageEvent); + if (validatedMessageEvent === false) { + return; + } + if (this.isRequest(validatedMessageEvent.data) === true) { return; } - this.validateMessageEvent(messageEvent); - const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; - // TODO: handle multiple responses for the same uuid - delete responsePayload.hashId; + const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse; + if (this.responses.has(uuid) === false) { + this.responses.set(uuid, { + responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid), + responsesReceived: 1, + responses: [responsePayload], + }); + } else if ( + this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected + ) { + this.responses.get(uuid).responsesReceived++; + this.responses.get(uuid).responses.push(responsePayload); + } + if ( + this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected + ) { + this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)); + this.responses.delete(uuid); + this.uiService.deleteBroadcastChannelRequest(uuid); + } + } - this.uiService.sendResponse(uuid, responsePayload as ResponsePayload); + private buildResponsePayload(uuid: string): ResponsePayload { + const responsesStatus = + this.responses + .get(uuid) + ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS) === true + ? ResponseStatus.SUCCESS + : ResponseStatus.FAILURE; + return { + status: responsesStatus, + hashIdsSucceeded: this.responses + .get(uuid) + ?.responses.map(({ status, hashId }) => { + if (status === ResponseStatus.SUCCESS) { + return hashId; + } + }) + .filter((hashId) => hashId !== undefined), + ...(responsesStatus === ResponseStatus.FAILURE && { + hashIdsFailed: this.responses + .get(uuid) + ?.responses.map(({ status, hashId }) => { + if (status === ResponseStatus.FAILURE) { + return hashId; + } + }) + .filter((hashId) => hashId !== undefined), + }), + ...(responsesStatus === ResponseStatus.FAILURE && { + responsesFailed: this.responses + .get(uuid) + ?.responses.map((response) => { + if (response.status === ResponseStatus.FAILURE) { + return response; + } + }) + .filter((response) => response !== undefined), + }), + }; } private messageErrorHandler(messageEvent: MessageEvent): void { logger.error( `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, - { messageEvent, messageEventData: messageEvent.data } + messageEvent ); } }