be2961c522584172560a9736b15f6703f10599da
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
1 import { IncomingMessage } from 'http';
2
3 import WebSocket from 'ws';
4
5 import { ServerOptions } from '../../types/ConfigurationData';
6 import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
7 import Configuration from '../../utils/Configuration';
8 import logger from '../../utils/Logger';
9 import Utils from '../../utils/Utils';
10 import { AbstractUIServer } from './AbstractUIServer';
11 import UIServiceFactory from './ui-services/UIServiceFactory';
12
13 const moduleName = 'UIWebSocketServer';
14
15 export default class UIWebSocketServer extends AbstractUIServer {
16 public constructor(options?: ServerOptions) {
17 super();
18 this.server = new WebSocket.Server(options ?? Configuration.getUIServer().options);
19 }
20
21 public start(): void {
22 this.server.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
23 const protocolIndex = socket.protocol.indexOf(Protocol.UI);
24 const version = socket.protocol.substring(
25 protocolIndex + Protocol.UI.length
26 ) as ProtocolVersion;
27 if (!this.uiServices.has(version)) {
28 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
29 }
30 // FIXME: check connection validity
31 socket.on('message', (rawData) => {
32 this.uiServices
33 .get(version)
34 .requestHandler(rawData)
35 .catch(() => {
36 /* Error caught by AbstractUIService */
37 });
38 });
39 socket.on('error', (error) => {
40 logger.error(
41 `${this.logPrefix(moduleName, 'start.socket.onerror')} Error on WebSocket:`,
42 error
43 );
44 });
45 });
46 }
47
48 public stop(): void {
49 this.chargingStations.clear();
50 }
51
52 public sendRequest(request: string): void {
53 this.broadcastToClients(request);
54 }
55
56 public sendResponse(response: string): void {
57 // TODO: send response only to the client that sent the request
58 this.broadcastToClients(response);
59 }
60
61 public logPrefix(modName?: string, methodName?: string): string {
62 const logMsg =
63 modName && methodName
64 ? ` UI WebSocket Server | ${modName}.${methodName}:`
65 : ' UI WebSocket Server |';
66 return Utils.logPrefix(logMsg);
67 }
68
69 private broadcastToClients(message: string): void {
70 for (const client of (this.server as WebSocket.Server).clients) {
71 if (client?.readyState === WebSocket.OPEN) {
72 client.send(message);
73 }
74 }
75 }
76 }