Commit | Line | Data |
---|---|---|
8244f5f0 JB |
1 | import { ProtocolCommand, ProtocolRequestHandler } from '../../../../types/UIProtocol'; |
2 | ||
3 | import AbstractUIService from '../AbstractUIService'; | |
4 | import BaseError from '../../../../exception/BaseError'; | |
5 | import WebSocketServer from '../../../WebSocketServer'; | |
6 | import logger from '../../../../utils/Logger'; | |
7 | ||
8 | export default class UIService extends AbstractUIService { | |
9 | private readonly messageHandlers: Map<ProtocolCommand, ProtocolRequestHandler>; | |
10 | ||
11 | constructor(webSocketServer: WebSocketServer) { | |
12 | super(webSocketServer); | |
13 | this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([ | |
14 | [ProtocolCommand.START_TRANSACTION, this.handleStartTransaction.bind(this)], | |
15 | [ProtocolCommand.STOP_TRANSACTION, this.handleStopTransaction.bind(this)], | |
16 | ]); | |
17 | } | |
18 | ||
19 | async handleMessage(command: ProtocolCommand, payload: Record<string, unknown>): Promise<void> { | |
20 | let messageResponse: Record<string, unknown>; | |
87002425 | 21 | if (this.messageHandlers.has(command) && command !== ProtocolCommand.UNKNOWN) { |
8244f5f0 JB |
22 | try { |
23 | // Call the method to build the response | |
24 | messageResponse = await this.messageHandlers.get(command)(payload); | |
25 | } catch (error) { | |
26 | // Log | |
1a3d7d3d | 27 | logger.error(this.webSocketServer.logPrefix() + ' Handle message error: %j', error); |
8244f5f0 JB |
28 | throw error; |
29 | } | |
30 | } else { | |
31 | // Throw exception | |
32 | throw new BaseError(`${command} is not implemented to handle message payload ${JSON.stringify(payload, null, 2)}`); | |
33 | } | |
34 | // Send the built response | |
35 | this.webSocketServer.broadcastToClients(messageResponse); | |
36 | } | |
37 | ||
38 | private handleStartTransaction(payload: Record<string, unknown>) { } | |
39 | private handleStopTransaction(payload: Record<string, unknown>) { } | |
40 | } |