| 1 | import type { RawData } from 'ws'; |
| 2 | |
| 3 | import BaseError from '../../../exception/BaseError'; |
| 4 | import { Bootstrap } from '../../../internal'; |
| 5 | import type { JsonType } from '../../../types/JsonType'; |
| 6 | import { |
| 7 | ProcedureName, |
| 8 | ProtocolRequest, |
| 9 | ProtocolRequestHandler, |
| 10 | ProtocolVersion, |
| 11 | RequestPayload, |
| 12 | ResponsePayload, |
| 13 | ResponseStatus, |
| 14 | } from '../../../types/UIProtocol'; |
| 15 | import logger from '../../../utils/Logger'; |
| 16 | import UIServiceWorkerBroadcastChannel from '../../UIServiceWorkerBroadcastChannel'; |
| 17 | import type { AbstractUIServer } from '../AbstractUIServer'; |
| 18 | |
| 19 | const moduleName = 'AbstractUIService'; |
| 20 | |
| 21 | export default abstract class AbstractUIService { |
| 22 | protected readonly version: ProtocolVersion; |
| 23 | protected readonly uiServer: AbstractUIServer; |
| 24 | protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>; |
| 25 | protected uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel; |
| 26 | |
| 27 | constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { |
| 28 | this.version = version; |
| 29 | this.uiServer = uiServer; |
| 30 | this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([ |
| 31 | [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)], |
| 32 | [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)], |
| 33 | [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)], |
| 34 | ]); |
| 35 | this.uiServiceWorkerBroadcastChannel = new UIServiceWorkerBroadcastChannel(this); |
| 36 | } |
| 37 | |
| 38 | public async requestHandler(request: RawData | JsonType): Promise<void> { |
| 39 | let messageId: string; |
| 40 | let command: ProcedureName; |
| 41 | let requestPayload: RequestPayload | undefined; |
| 42 | let responsePayload: ResponsePayload; |
| 43 | try { |
| 44 | [messageId, command, requestPayload] = this.requestValidation(request); |
| 45 | |
| 46 | if (this.requestHandlers.has(command) === false) { |
| 47 | throw new BaseError( |
| 48 | `${command} is not implemented to handle message payload ${JSON.stringify( |
| 49 | requestPayload, |
| 50 | null, |
| 51 | 2 |
| 52 | )}` |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // Call the request handler to build the response payload |
| 57 | responsePayload = await this.requestHandlers.get(command)(messageId, requestPayload); |
| 58 | } catch (error) { |
| 59 | // Log |
| 60 | logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error); |
| 61 | responsePayload = { |
| 62 | status: ResponseStatus.FAILURE, |
| 63 | command, |
| 64 | requestPayload, |
| 65 | responsePayload, |
| 66 | errorMessage: (error as Error).message, |
| 67 | errorStack: (error as Error).stack, |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | if (responsePayload !== undefined) { |
| 72 | // Send the response |
| 73 | this.sendResponse(messageId ?? 'error', responsePayload); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public sendRequest( |
| 78 | messageId: string, |
| 79 | procedureName: ProcedureName, |
| 80 | requestPayload: RequestPayload |
| 81 | ): void { |
| 82 | this.uiServer.sendRequest( |
| 83 | this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | public sendResponse(messageId: string, responsePayload: ResponsePayload): void { |
| 88 | this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload)); |
| 89 | } |
| 90 | |
| 91 | public logPrefix(modName: string, methodName: string): string { |
| 92 | return this.uiServer.logPrefix(modName, methodName); |
| 93 | } |
| 94 | |
| 95 | // Validate the raw data received from the UI server |
| 96 | private requestValidation(rawData: RawData | JsonType): ProtocolRequest { |
| 97 | // logger.debug( |
| 98 | // `${this.logPrefix( |
| 99 | // moduleName, |
| 100 | // 'requestValidation' |
| 101 | // )} Data received in string format: ${rawData.toString()}` |
| 102 | // ); |
| 103 | |
| 104 | const data = JSON.parse(rawData.toString()) as JsonType[]; |
| 105 | |
| 106 | if (Array.isArray(data) === false) { |
| 107 | throw new BaseError('UI protocol request is not an array'); |
| 108 | } |
| 109 | |
| 110 | if (data.length !== 3) { |
| 111 | throw new BaseError('UI protocol request is malformed'); |
| 112 | } |
| 113 | |
| 114 | return data as ProtocolRequest; |
| 115 | } |
| 116 | |
| 117 | private handleListChargingStations(): ResponsePayload { |
| 118 | // TODO: remove cast to unknown |
| 119 | return { |
| 120 | status: ResponseStatus.SUCCESS, |
| 121 | ...[...this.uiServer.chargingStations.values()], |
| 122 | } as unknown as ResponsePayload; |
| 123 | } |
| 124 | |
| 125 | private async handleStartSimulator(): Promise<ResponsePayload> { |
| 126 | await Bootstrap.getInstance().start(); |
| 127 | return { status: ResponseStatus.SUCCESS }; |
| 128 | } |
| 129 | |
| 130 | private async handleStopSimulator(): Promise<ResponsePayload> { |
| 131 | await Bootstrap.getInstance().stop(); |
| 132 | return { status: ResponseStatus.SUCCESS }; |
| 133 | } |
| 134 | } |