X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2Fui-services%2FAbstractUIService.ts;h=7d4f69d0a784b1183883e66f04606f5584a2335e;hb=7acb3f7bb991a6d02a4521c0cbc5f163aa5e8a61;hp=9120f19976e8a682afd136afc1d469ee0af2fe30;hpb=0d2cec7666c01396c8125a752341d5b716d12906;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 9120f199..7d4f69d0 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -1,8 +1,5 @@ -import type { RawData } from 'ws'; - import BaseError from '../../../exception/BaseError'; import { Bootstrap } from '../../../internal'; -import type { JsonType } from '../../../types/JsonType'; import { ProcedureName, ProtocolRequest, @@ -24,10 +21,10 @@ import type { AbstractUIServer } from '../AbstractUIServer'; const moduleName = 'AbstractUIService'; export default abstract class AbstractUIService { - protected readonly version: ProtocolVersion; - protected readonly uiServer: AbstractUIServer; protected readonly requestHandlers: Map; - private uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel; + private readonly version: ProtocolVersion; + private readonly uiServer: AbstractUIServer; + private readonly uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel; private readonly broadcastChannelRequests: Map; constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { @@ -42,13 +39,13 @@ export default abstract class AbstractUIService { this.broadcastChannelRequests = new Map(); } - public async requestHandler(request: RawData | JsonType): Promise { + public async requestHandler(request: ProtocolRequest): Promise { let messageId: string; let command: ProcedureName; let requestPayload: RequestPayload | undefined; let responsePayload: ResponsePayload; try { - [messageId, command, requestPayload] = this.requestValidation(request); + [messageId, command, requestPayload] = request; if (this.requestHandlers.has(command) === false) { throw new BaseError( @@ -129,38 +126,15 @@ export default abstract class AbstractUIService { const expectedNumberOfResponses = !Utils.isEmptyArray(payload.hashIds) ? payload.hashIds.length : this.uiServer.chargingStations.size; - this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); - } - - // Validate the raw data received from the UI server - private requestValidation(rawData: RawData | JsonType): ProtocolRequest { - // logger.debug( - // `${this.logPrefix( - // moduleName, - // 'requestValidation' - // )} Data received in string format: ${rawData.toString()}` - // ); - - const data = JSON.parse(rawData.toString()) as JsonType[]; - - if (Array.isArray(data) === false) { - throw new BaseError('UI protocol request is not an array'); - } - - if (data.length !== 3) { - throw new BaseError('UI protocol request is malformed'); - } - - return data as ProtocolRequest; + this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); } private handleListChargingStations(): ResponsePayload { - // TODO: remove cast to unknown return { status: ResponseStatus.SUCCESS, - ...[...this.uiServer.chargingStations.values()], - } as unknown as ResponsePayload; + chargingStations: [...this.uiServer.chargingStations.values()], + } as ResponsePayload; } private async handleStartSimulator(): Promise {