feat: add performance statistics to UI protocol
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
CommitLineData
66a7748d
JB
1import type { IncomingMessage } from 'node:http'
2import type { Duplex } from 'node:stream'
8114d10e 3
66a7748d
JB
4import { StatusCodes } from 'http-status-codes'
5import { type RawData, WebSocket, WebSocketServer } from 'ws'
8114d10e 6
66a7748d
JB
7import { AbstractUIServer } from './AbstractUIServer.js'
8import { UIServerUtils } from './UIServerUtils.js'
268a74bb
JB
9import {
10 type ProtocolRequest,
11 type ProtocolResponse,
12 type UIServerConfiguration,
66a7748d
JB
13 WebSocketCloseEventStatusCode
14} from '../../types/index.js'
9bf0ef23
JB
15import {
16 Constants,
a66bbcfe 17 JSONStringifyWithMapSupport,
9bf0ef23
JB
18 getWebSocketCloseEventStatusString,
19 isNotEmptyString,
9bf0ef23
JB
20 logPrefix,
21 logger,
66a7748d
JB
22 validateUUID
23} from '../../utils/index.js'
4198ad5c 24
66a7748d 25const moduleName = 'UIWebSocketServer'
32de5a57 26
268a74bb 27export class UIWebSocketServer extends AbstractUIServer {
66a7748d 28 private readonly webSocketServer: WebSocketServer
eb3abc4f 29
66a7748d
JB
30 public constructor (protected readonly uiServerConfiguration: UIServerConfiguration) {
31 super(uiServerConfiguration)
eb3abc4f 32 this.webSocketServer = new WebSocketServer({
ed3d2808 33 handleProtocols: UIServerUtils.handleProtocols,
66a7748d
JB
34 noServer: true
35 })
4198ad5c
JB
36 }
37
66a7748d
JB
38 public start (): void {
39 this.webSocketServer.on('connection', (ws: WebSocket, _req: IncomingMessage): void => {
40 if (!UIServerUtils.isProtocolAndVersionSupported(ws.protocol)) {
a92929f1
JB
41 logger.error(
42 `${this.logPrefix(
43 moduleName,
66a7748d
JB
44 'start.server.onconnection'
45 )} Unsupported UI protocol version: '${ws.protocol}'`
46 )
47 ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR)
a92929f1 48 }
66a7748d
JB
49 const [, version] = UIServerUtils.getProtocolAndVersion(ws.protocol)
50 this.registerProtocolVersionUIService(version)
a974c8e4 51 ws.on('message', rawData => {
66a7748d 52 const request = this.validateRawDataRequest(rawData)
5dea4c94 53 if (request === false) {
66a7748d
JB
54 ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD)
55 return
5dea4c94 56 }
97608fbd 57 const [requestId] = request
66a7748d 58 this.responseHandlers.set(requestId, ws)
0b22144c
JB
59 this.uiServices
60 .get(version)
61 ?.requestHandler(request)
4a3807d1 62 .then((protocolResponse?: ProtocolResponse) => {
66a7748d
JB
63 if (protocolResponse != null) {
64 this.sendResponse(protocolResponse)
0b22144c
JB
65 }
66 })
66a7748d
JB
67 .catch(Constants.EMPTY_FUNCTION)
68 })
a974c8e4 69 ws.on('error', error => {
66a7748d
JB
70 logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error)
71 })
5e3cb728
JB
72 ws.on('close', (code, reason) => {
73 logger.debug(
74 `${this.logPrefix(
75 moduleName,
66a7748d 76 'start.ws.onclose'
9bf0ef23 77 )} WebSocket closed: '${getWebSocketCloseEventStatusString(
66a7748d
JB
78 code
79 )}' - '${reason.toString()}'`
80 )
81 })
82 })
83 this.httpServer.on('connect', (req: IncomingMessage, socket: Duplex, _head: Buffer) => {
5199f9fd 84 if (req.headers.connection !== 'Upgrade' || req.headers.upgrade !== 'websocket') {
66a7748d
JB
85 socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`)
86 socket.destroy()
cbf9b878 87 }
66a7748d 88 })
60a74391 89 this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
a974c8e4 90 this.authenticate(req, err => {
66a7748d
JB
91 if (err != null) {
92 socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`)
93 socket.destroy()
94 return
60a74391
JB
95 }
96 try {
97 this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
66a7748d
JB
98 this.webSocketServer.emit('connection', ws, req)
99 })
60a74391
JB
100 } catch (error) {
101 logger.error(
102 `${this.logPrefix(
103 moduleName,
66a7748d 104 'start.httpServer.on.upgrade'
60a74391 105 )} Error at handling connection upgrade:`,
66a7748d
JB
106 error
107 )
60a74391 108 }
66a7748d
JB
109 })
110 })
111 this.startHttpServer()
4198ad5c
JB
112 }
113
66a7748d
JB
114 public sendRequest (request: ProtocolRequest): void {
115 this.broadcastToClients(JSON.stringify(request))
02a6943a
JB
116 }
117
66a7748d 118 public sendResponse (response: ProtocolResponse): void {
5199f9fd 119 const responseId = response[0]
976d11ec 120 try {
1ca4a038 121 if (this.hasResponseHandler(responseId)) {
66a7748d 122 const ws = this.responseHandlers.get(responseId) as WebSocket
5199f9fd 123 if (ws.readyState === WebSocket.OPEN) {
a66bbcfe 124 ws.send(JSONStringifyWithMapSupport(response))
e2c77f10
JB
125 } else {
126 logger.error(
127 `${this.logPrefix(
128 moduleName,
66a7748d 129 'sendResponse'
5199f9fd
JB
130 )} Error at sending response id '${responseId}', WebSocket is not open: ${
131 ws.readyState
132 }`
66a7748d 133 )
976d11ec 134 }
976d11ec
JB
135 } else {
136 logger.error(
137 `${this.logPrefix(
138 moduleName,
66a7748d
JB
139 'sendResponse'
140 )} Response for unknown request id: ${responseId}`
141 )
94dc3080 142 }
976d11ec 143 } catch (error) {
94dc3080
JB
144 logger.error(
145 `${this.logPrefix(
146 moduleName,
66a7748d 147 'sendResponse'
976d11ec 148 )} Error at sending response id '${responseId}':`,
66a7748d
JB
149 error
150 )
e2c77f10 151 } finally {
66a7748d 152 this.responseHandlers.delete(responseId)
94dc3080 153 }
178ac666
JB
154 }
155
8b7072dc 156 public logPrefix = (modName?: string, methodName?: string, prefixSuffix?: string): string => {
66a7748d
JB
157 const logMsgPrefix =
158 prefixSuffix != null ? `UI WebSocket Server ${prefixSuffix}` : 'UI WebSocket Server'
32de5a57 159 const logMsg =
9bf0ef23 160 isNotEmptyString(modName) && isNotEmptyString(methodName)
1b271a54 161 ? ` ${logMsgPrefix} | ${modName}.${methodName}:`
66a7748d
JB
162 : ` ${logMsgPrefix} |`
163 return logPrefix(logMsg)
164 }
178ac666 165
66a7748d 166 private broadcastToClients (message: string): void {
eb3abc4f 167 for (const client of this.webSocketServer.clients) {
5199f9fd 168 if (client.readyState === WebSocket.OPEN) {
66a7748d 169 client.send(message)
178ac666
JB
170 }
171 }
172 }
5e3cb728 173
66a7748d 174 private validateRawDataRequest (rawData: RawData): ProtocolRequest | false {
5e3cb728
JB
175 // logger.debug(
176 // `${this.logPrefix(
177 // moduleName,
66a7748d 178 // 'validateRawDataRequest'
e1d9a0f4 179 // // eslint-disable-next-line @typescript-eslint/no-base-to-string
66a7748d
JB
180 // )} Raw data received in string format: ${rawData.toString()}`
181 // )
5e3cb728 182
e1d9a0f4 183 // eslint-disable-next-line @typescript-eslint/no-base-to-string
66a7748d 184 const request = JSON.parse(rawData.toString()) as ProtocolRequest
5e3cb728 185
66a7748d 186 if (!Array.isArray(request)) {
5dea4c94
JB
187 logger.error(
188 `${this.logPrefix(
189 moduleName,
66a7748d 190 'validateRawDataRequest'
5dea4c94 191 )} UI protocol request is not an array:`,
66a7748d
JB
192 request
193 )
194 return false
5e3cb728
JB
195 }
196
5199f9fd 197 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
5e3cb728 198 if (request.length !== 3) {
5dea4c94
JB
199 logger.error(
200 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
66a7748d
JB
201 request
202 )
203 return false
5dea4c94
JB
204 }
205
5199f9fd 206 if (!validateUUID(request[0])) {
5dea4c94
JB
207 logger.error(
208 `${this.logPrefix(
209 moduleName,
66a7748d 210 'validateRawDataRequest'
5dea4c94 211 )} UI protocol request UUID field is invalid:`,
66a7748d
JB
212 request
213 )
214 return false
5e3cb728
JB
215 }
216
66a7748d 217 return request
5e3cb728 218 }
4198ad5c 219}