X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIServiceWorkerBroadcastChannel.ts;h=a2b209220ae084ba84e1fa7accda1e782443929b;hb=a65319ba9924b96204d7bfd3cbdcf8b84c113768;hp=e921972d4c183d7dd5d86fc611de8d5a656ab8ab;hpb=6c8f5d901f6dbfd66e921decde63bf73548c156e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index e921972d..a2b20922 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -1,33 +1,95 @@ -import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel'; +import { ResponsePayload, ResponseStatus } from '../types/UIProtocol'; +import type { + BroadcastChannelResponse, + BroadcastChannelResponsePayload, + MessageEvent, +} from '../types/WorkerBroadcastChannel'; import logger from '../utils/Logger'; -import AbstractUIService from './ui-server/ui-services/AbstractUIService'; +import type AbstractUIService from './ui-server/ui-services/AbstractUIService'; import WorkerBroadcastChannel from './WorkerBroadcastChannel'; 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)) { return; } - const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; + const [uuid, responsePayload] = this.validateMessageEvent(messageEvent) + .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 + 1 < + this.responses.get(uuid)?.responsesExpected + ) { + this.responses.get(uuid).responsesReceived++; + this.responses.get(uuid).responses.push(responsePayload); + } else if ( + this.responses.get(uuid)?.responsesReceived + 1 === + this.responses.get(uuid)?.responsesExpected + ) { + this.responses.get(uuid).responses.push(responsePayload); + this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid)); + this.responses.delete(uuid); + this.uiService.deleteBroadcastChannelRequest(uuid); + } + } - this.uiService.sendResponse(uuid, responsePayload); + private buildResponsePayload(uuid: string): ResponsePayload { + const responsesStatus = this.responses + .get(uuid) + ?.responses.every((response) => response.status === ResponseStatus.SUCCESS) + ? 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), + }), + }; } private messageErrorHandler(messageEvent: MessageEvent): void { logger.error( `${this.uiService.logPrefix(moduleName, 'messageErrorHandler')} Error at handling message:`, - { messageEvent, messageEventData: messageEvent.data } + { messageEvent } ); } }