Refine lint staged configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
CommitLineData
daa6505e 1import type { IncomingMessage } from 'http';
eb3abc4f 2import type internal from 'stream';
8114d10e 3
eb3abc4f 4import { StatusCodes } from 'http-status-codes';
5dea4c94 5import * as uuid from 'uuid';
976d11ec 6import WebSocket, { type RawData, WebSocketServer } from 'ws';
8114d10e 7
eb3abc4f 8import type { UIServerConfiguration } from '../../types/ConfigurationData';
5e3cb728 9import type { ProtocolRequest, ProtocolResponse } from '../../types/UIProtocol';
8b0088bb 10import { WebSocketCloseEventStatusCode } from '../../types/WebSocket';
675fa8e3 11import logger from '../../utils/Logger';
8114d10e
JB
12import Utils from '../../utils/Utils';
13import { AbstractUIServer } from './AbstractUIServer';
ed3d2808 14import { UIServerUtils } from './UIServerUtils';
4198ad5c 15
32de5a57
LM
16const moduleName = 'UIWebSocketServer';
17
fe94fce0 18export default class UIWebSocketServer extends AbstractUIServer {
eb3abc4f
JB
19 private readonly webSocketServer: WebSocketServer;
20
21 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
22 super(uiServerConfiguration);
eb3abc4f 23 this.webSocketServer = new WebSocketServer({
ed3d2808 24 handleProtocols: UIServerUtils.handleProtocols,
eb3abc4f
JB
25 noServer: true,
26 });
4198ad5c
JB
27 }
28
29 public start(): void {
fd3c56d1 30 // eslint-disable-next-line @typescript-eslint/no-unused-vars
eb3abc4f 31 this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => {
ed3d2808 32 if (UIServerUtils.isProtocolAndVersionSupported(ws.protocol) === false) {
a92929f1
JB
33 logger.error(
34 `${this.logPrefix(
35 moduleName,
36 'start.server.onconnection'
7cb5b17f 37 )} Unsupported UI protocol version: '${ws.protocol}'`
a92929f1 38 );
5e3cb728 39 ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
a92929f1 40 }
ed3d2808 41 const [, version] = UIServerUtils.getProtocolAndVersion(ws.protocol);
143498c8 42 this.registerProtocolVersionUIService(version);
5e3cb728 43 ws.on('message', (rawData) => {
5dea4c94
JB
44 const request = this.validateRawDataRequest(rawData);
45 if (request === false) {
46 ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
47 return;
48 }
94dc3080
JB
49 const [requestId] = request as ProtocolRequest;
50 this.responseHandlers.set(requestId, ws);
e7aeea18
JB
51 this.uiServices
52 .get(version)
94dc3080 53 .requestHandler(request)
6c8f5d90
JB
54 .catch(() => {
55 /* Error caught by AbstractUIService */
e7aeea18 56 });
4198ad5c 57 });
5e3cb728
JB
58 ws.on('error', (error) => {
59 logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error);
60 });
61 ws.on('close', (code, reason) => {
62 logger.debug(
63 `${this.logPrefix(
64 moduleName,
65 'start.ws.onclose'
66 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
67 code
68 )}' - '${reason.toString()}'`
32de5a57 69 );
4198ad5c
JB
70 });
71 });
eb3abc4f
JB
72 this.httpServer.on(
73 'upgrade',
74 (req: IncomingMessage, socket: internal.Duplex, head: Buffer): void => {
75 this.authenticate(req, (err) => {
76 if (err) {
77 socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
4055bcb9 78 socket.destroy();
eb3abc4f
JB
79 return;
80 }
81 this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
82 this.webSocketServer.emit('connection', ws, req);
83 });
84 });
85 }
86 );
a307349b 87 this.startHttpServer();
4198ad5c
JB
88 }
89
5e3cb728
JB
90 public sendRequest(request: ProtocolRequest): void {
91 this.broadcastToClients(JSON.stringify(request));
02a6943a
JB
92 }
93
5e3cb728 94 public sendResponse(response: ProtocolResponse): void {
94dc3080 95 const responseId = response[0];
976d11ec
JB
96 try {
97 if (this.responseHandlers.has(responseId)) {
98 const ws = this.responseHandlers.get(responseId) as WebSocket;
99 if (ws?.readyState === WebSocket.OPEN) {
100 ws.send(JSON.stringify(response));
e2c77f10
JB
101 } else {
102 logger.error(
103 `${this.logPrefix(
104 moduleName,
105 'sendResponse'
106 )} Error at sending response id '${responseId}', WebSocket is not open: ${
107 ws?.readyState
108 }`
109 );
976d11ec 110 }
976d11ec
JB
111 } else {
112 logger.error(
113 `${this.logPrefix(
114 moduleName,
115 'sendResponse'
116 )} Response for unknown request id: ${responseId}`
117 );
94dc3080 118 }
976d11ec 119 } catch (error) {
94dc3080
JB
120 logger.error(
121 `${this.logPrefix(
122 moduleName,
123 'sendResponse'
976d11ec
JB
124 )} Error at sending response id '${responseId}':`,
125 error
94dc3080 126 );
e2c77f10
JB
127 } finally {
128 this.responseHandlers.delete(responseId);
94dc3080 129 }
178ac666
JB
130 }
131
0d2cec76
JB
132 public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
133 const logMsgPrefix = prefixSuffix
134 ? `UI WebSocket Server ${prefixSuffix}`
135 : 'UI WebSocket Server';
32de5a57 136 const logMsg =
0d2cec76 137 modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
32de5a57 138 return Utils.logPrefix(logMsg);
4198ad5c 139 }
178ac666
JB
140
141 private broadcastToClients(message: string): void {
eb3abc4f 142 for (const client of this.webSocketServer.clients) {
0d8140bd 143 if (client?.readyState === WebSocket.OPEN) {
178ac666
JB
144 client.send(message);
145 }
146 }
147 }
5e3cb728 148
5dea4c94 149 private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
5e3cb728
JB
150 // logger.debug(
151 // `${this.logPrefix(
152 // moduleName,
153 // 'validateRawDataRequest'
154 // )} Raw data received in string format: ${rawData.toString()}`
155 // );
156
157 const request = JSON.parse(rawData.toString()) as ProtocolRequest;
158
159 if (Array.isArray(request) === false) {
5dea4c94
JB
160 logger.error(
161 `${this.logPrefix(
162 moduleName,
163 'validateRawDataRequest'
164 )} UI protocol request is not an array:`,
165 request
166 );
167 return false;
5e3cb728
JB
168 }
169
170 if (request.length !== 3) {
5dea4c94
JB
171 logger.error(
172 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
173 request
174 );
175 return false;
176 }
177
178 if (uuid.validate(request[0]) === false) {
179 logger.error(
180 `${this.logPrefix(
181 moduleName,
182 'validateRawDataRequest'
183 )} UI protocol request UUID field is invalid:`,
184 request
185 );
186 return false;
5e3cb728
JB
187 }
188
189 return request;
190 }
4198ad5c 191}