Commit | Line | Data |
---|---|---|
8114d10e JB |
1 | import { RawData } from 'ws'; |
2 | ||
3 | import BaseError from '../../../exception/BaseError'; | |
4 | import { JsonType } from '../../../types/JsonType'; | |
675fa8e3 JB |
5 | import { |
6 | ProtocolCommand, | |
7 | ProtocolRequest, | |
8 | ProtocolRequestHandler, | |
33cea517 | 9 | ProtocolVersion, |
675fa8e3 | 10 | } from '../../../types/UIProtocol'; |
675fa8e3 | 11 | import logger from '../../../utils/Logger'; |
8114d10e JB |
12 | import Utils from '../../../utils/Utils'; |
13 | import { AbstractUIServer } from '../AbstractUIServer'; | |
4198ad5c JB |
14 | |
15 | export default abstract class AbstractUIService { | |
33cea517 | 16 | protected readonly version: ProtocolVersion; |
fe94fce0 | 17 | protected readonly uiServer: AbstractUIServer; |
4198ad5c JB |
18 | protected readonly messageHandlers: Map<ProtocolCommand, ProtocolRequestHandler>; |
19 | ||
33cea517 JB |
20 | constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { |
21 | this.version = version; | |
675fa8e3 | 22 | this.uiServer = uiServer; |
4198ad5c JB |
23 | this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([ |
24 | [ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)], | |
25 | ]); | |
26 | } | |
27 | ||
178ac666 | 28 | public async messageHandler(request: RawData): Promise<void> { |
98a5256a | 29 | let messageId: string; |
178ac666 JB |
30 | let command: ProtocolCommand; |
31 | let payload: JsonType; | |
32 | const protocolRequest = JSON.parse(request.toString()) as ProtocolRequest; | |
33 | if (Utils.isIterable(protocolRequest)) { | |
98a5256a | 34 | [messageId, command, payload] = protocolRequest; |
178ac666 JB |
35 | } else { |
36 | throw new BaseError('UI protocol request is not iterable'); | |
37 | } | |
6ebe395c JB |
38 | // TODO: should probably be moved to the ws verify clients callback |
39 | if (protocolRequest.length !== 3) { | |
40 | throw new BaseError('UI protocol request is malformed'); | |
41 | } | |
5cc4b63b | 42 | let messageResponse: JsonType; |
4198ad5c JB |
43 | if (this.messageHandlers.has(command)) { |
44 | try { | |
178ac666 | 45 | // Call the message handler to build the message response |
5cc4b63b | 46 | messageResponse = (await this.messageHandlers.get(command)(payload)) as JsonType; |
4198ad5c JB |
47 | } catch (error) { |
48 | // Log | |
675fa8e3 | 49 | logger.error(this.uiServer.logPrefix() + ' Handle message error: %j', error); |
4198ad5c JB |
50 | throw error; |
51 | } | |
52 | } else { | |
53 | // Throw exception | |
e7aeea18 JB |
54 | throw new BaseError( |
55 | `${command} is not implemented to handle message payload ${JSON.stringify( | |
56 | payload, | |
57 | null, | |
58 | 2 | |
59 | )}` | |
60 | ); | |
4198ad5c | 61 | } |
178ac666 | 62 | // Send the message response |
98a5256a | 63 | this.uiServer.sendResponse(this.buildProtocolMessage(messageId, command, messageResponse)); |
4198ad5c JB |
64 | } |
65 | ||
98a5256a JB |
66 | protected buildProtocolMessage( |
67 | messageId: string, | |
68 | command: ProtocolCommand, | |
69 | payload: JsonType | |
70 | ): string { | |
71 | return JSON.stringify([messageId, command, payload]); | |
4198ad5c JB |
72 | } |
73 | ||
178ac666 | 74 | private handleListChargingStations(): JsonType { |
675fa8e3 | 75 | return Array.from(this.uiServer.chargingStations); |
4198ad5c JB |
76 | } |
77 | } |