1 import { IncomingMessage
, createServer
} from
'http';
2 import type internal from
'stream';
4 import { StatusCodes
} from
'http-status-codes';
5 import * as uuid from
'uuid';
6 import WebSocket
, { RawData
, WebSocketServer
} from
'ws';
8 import type { UIServerConfiguration
} from
'../../types/ConfigurationData';
9 import type { ProtocolRequest
, ProtocolResponse
} from
'../../types/UIProtocol';
10 import { WebSocketCloseEventStatusCode
} from
'../../types/WebSocket';
11 import logger from
'../../utils/Logger';
12 import Utils from
'../../utils/Utils';
13 import { AbstractUIServer
} from
'./AbstractUIServer';
14 import UIServiceFactory from
'./ui-services/UIServiceFactory';
15 import { UIServiceUtils
} from
'./ui-services/UIServiceUtils';
17 const moduleName
= 'UIWebSocketServer';
19 export default class UIWebSocketServer
extends AbstractUIServer
{
20 private readonly webSocketServer
: WebSocketServer
;
22 public constructor(protected readonly uiServerConfiguration
: UIServerConfiguration
) {
23 super(uiServerConfiguration
);
24 this.httpServer
= createServer();
25 this.webSocketServer
= new WebSocketServer({
26 handleProtocols
: UIServiceUtils
.handleProtocols
,
31 public start(): void {
32 this.webSocketServer
.on('connection', (ws
: WebSocket
, req
: IncomingMessage
): void => {
33 const [protocol
, version
] = UIServiceUtils
.getProtocolAndVersion(ws
.protocol
);
34 if (UIServiceUtils
.isProtocolAndVersionSupported(protocol
, version
) === false) {
38 'start.server.onconnection'
39 )} Unsupported UI protocol version: '${protocol}${version}'`
41 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
);
43 if (this.uiServices
.has(version
) === false) {
44 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
46 ws
.on('message', (rawData
) => {
47 const request
= this.validateRawDataRequest(rawData
);
48 if (request
=== false) {
49 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_INVALID_PAYLOAD
);
52 const [messageId
, procedureName
, payload
] = request
as ProtocolRequest
;
55 .requestHandler(this.buildProtocolRequest(messageId
, procedureName
, payload
))
57 /* Error caught by AbstractUIService */
60 ws
.on('error', (error
) => {
61 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
);
63 ws
.on('close', (code
, reason
) => {
68 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
70 )}' - '${reason.toString()}'`
76 (req
: IncomingMessage
, socket
: internal
.Duplex
, head
: Buffer
): void => {
77 this.authenticate(req
, (err
) => {
79 socket
.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
83 this.webSocketServer
.handleUpgrade(req
, socket
, head
, (ws
: WebSocket
) => {
84 this.webSocketServer
.emit('connection', ws
, req
);
89 if (this.httpServer
.listening
=== false) {
90 this.httpServer
.listen(this.uiServerConfiguration
.options
);
95 this.chargingStations
.clear();
98 public sendRequest(request
: ProtocolRequest
): void {
99 this.broadcastToClients(JSON
.stringify(request
));
102 public sendResponse(response
: ProtocolResponse
): void {
103 // TODO: send response only to the client that sent the request
104 this.broadcastToClients(JSON
.stringify(response
));
107 public logPrefix(modName
?: string, methodName
?: string, prefixSuffix
?: string): string {
108 const logMsgPrefix
= prefixSuffix
109 ? `UI WebSocket Server ${prefixSuffix}`
110 : 'UI WebSocket Server';
112 modName
&& methodName
? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
113 return Utils
.logPrefix(logMsg
);
116 private broadcastToClients(message
: string): void {
117 for (const client
of this.webSocketServer
.clients
) {
118 if (client
?.readyState
=== WebSocket
.OPEN
) {
119 client
.send(message
);
124 private authenticate(req
: IncomingMessage
, next
: (err
?: Error) => void): void {
125 if (this.isBasicAuthEnabled() === true) {
126 if (this.isValidBasicAuth(req
) === false) {
127 next(new Error('Unauthorized'));
136 private validateRawDataRequest(rawData
: RawData
): ProtocolRequest
| false {
138 // `${this.logPrefix(
140 // 'validateRawDataRequest'
141 // )} Raw data received in string format: ${rawData.toString()}`
144 const request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
;
146 if (Array.isArray(request
) === false) {
150 'validateRawDataRequest'
151 )} UI protocol request is not an array:`,
157 if (request
.length
!== 3) {
159 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
165 if (uuid
.validate(request
[0]) === false) {
169 'validateRawDataRequest'
170 )} UI protocol request UUID field is invalid:`,