1 import type { IncomingMessage
} from
'http';
3 import WebSocket
, { RawData
} from
'ws';
5 import BaseError from
'../../exception/BaseError';
6 import type { ServerOptions
} from
'../../types/ConfigurationData';
7 import type { ProtocolRequest
, ProtocolResponse
} from
'../../types/UIProtocol';
8 import { WebSocketCloseEventStatusCode
} from
'../../types/WebSocket';
9 import Configuration from
'../../utils/Configuration';
10 import logger from
'../../utils/Logger';
11 import Utils from
'../../utils/Utils';
12 import { AbstractUIServer
} from
'./AbstractUIServer';
13 import UIServiceFactory from
'./ui-services/UIServiceFactory';
14 import { UIServiceUtils
} from
'./ui-services/UIServiceUtils';
16 const moduleName
= 'UIWebSocketServer';
18 export default class UIWebSocketServer
extends AbstractUIServer
{
19 public constructor(options
?: ServerOptions
) {
21 this.server
= new WebSocket
.Server(options
?? Configuration
.getUIServer().options
);
24 public start(): void {
25 this.server
.on('connection', (ws
: WebSocket
, request
: IncomingMessage
): void => {
26 const [protocol
, version
] = UIServiceUtils
.getProtocolAndVersion(ws
.protocol
);
27 if (UIServiceUtils
.isProtocolAndVersionSupported(protocol
, version
) === false) {
31 'start.server.onconnection'
32 )} Unsupported UI protocol version: '${protocol}${version}'`
34 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
);
36 if (!this.uiServices
.has(version
)) {
37 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
39 ws
.on('message', (rawData
) => {
40 const [messageId
, procedureName
, payload
] = this.validateRawDataRequest(rawData
);
43 .requestHandler(this.buildProtocolRequest(messageId
, procedureName
, payload
))
45 /* Error caught by AbstractUIService */
48 ws
.on('error', (error
) => {
49 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
);
51 ws
.on('close', (code
, reason
) => {
56 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
58 )}' - '${reason.toString()}'`
65 this.chargingStations
.clear();
68 public sendRequest(request
: ProtocolRequest
): void {
69 this.broadcastToClients(JSON
.stringify(request
));
72 public sendResponse(response
: ProtocolResponse
): void {
73 // TODO: send response only to the client that sent the request
74 this.broadcastToClients(JSON
.stringify(response
));
77 public logPrefix(modName
?: string, methodName
?: string, prefixSuffix
?: string): string {
78 const logMsgPrefix
= prefixSuffix
79 ? `UI WebSocket Server ${prefixSuffix}`
80 : 'UI WebSocket Server';
82 modName
&& methodName
? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
83 return Utils
.logPrefix(logMsg
);
86 private broadcastToClients(message
: string): void {
87 for (const client
of (this.server
as WebSocket
.Server
).clients
) {
88 if (client
?.readyState
=== WebSocket
.OPEN
) {
94 private validateRawDataRequest(rawData
: RawData
): ProtocolRequest
{
98 // 'validateRawDataRequest'
99 // )} Raw data received in string format: ${rawData.toString()}`
102 const request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
;
104 if (Array.isArray(request
) === false) {
105 throw new BaseError('UI protocol request is not an array');
108 if (request
.length
!== 3) {
109 throw new BaseError('UI protocol request is malformed');