X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIWebSocketServer.ts;h=bc10db1cc785d5659c12418e91288e9a38a6beba;hb=5916df98e8d4c023b40e8c8c19b7f5734d7eb163;hp=78ca7516b89a07d53ee59f78e36e0d6ba5162098;hpb=4198ad5c76ad58e444c1dd2d3f81b5f8fd2846d4;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIWebSocketServer.ts b/src/charging-station/UIWebSocketServer.ts index 78ca7516..bc10db1c 100644 --- a/src/charging-station/UIWebSocketServer.ts +++ b/src/charging-station/UIWebSocketServer.ts @@ -1,24 +1,31 @@ -import { Protocol, ProtocolCommand, ProtocolRequest, ProtocolVersion } from '../types/UIProtocol'; +import { Protocol, ProtocolCommand, ProtocolRequest, ProtocolVersion } from '../types/UiProtocol'; +import WebSocket, { OPEN, Server, ServerOptions } from 'ws'; -import AbstractUIService from './UIWebSocketServices/AbstractUIService'; +import AbstractUIService from './ui-websocket-services/AbstractUIService'; import BaseError from '../exception/BaseError'; +import Configuration from '../utils/Configuration'; import { IncomingMessage } from 'http'; -import UIServiceFactory from './UIWebSocketServices/UIServiceFactory'; +import UIServiceFactory from './ui-websocket-services/UIServiceFactory'; import Utils from '../utils/Utils'; -import WebSocket from 'ws'; import logger from '../utils/Logger'; -export default class UIWebSocketServer extends WebSocket.Server { - public uiService: AbstractUIService; +export default class UIWebSocketServer extends Server { + public readonly chargingStations: Set; + public readonly uiServices: Map; - public constructor(options?: WebSocket.ServerOptions, callback?: () => void) { + public constructor(options?: ServerOptions, callback?: () => void) { // Create the WebSocket Server - super(options ?? { port: 80 }, callback); + super(options ?? Configuration.getUIWebSocketServer().options, callback); + this.chargingStations = new Set(); + this.uiServices = new Map(); + for (const version of Object.values(ProtocolVersion)) { + this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); + } } public broadcastToClients(message: string | Record): void { for (const client of this.clients) { - if (client?.readyState === WebSocket.OPEN) { + if (client?.readyState === OPEN) { client.send(message); } } @@ -28,9 +35,8 @@ export default class UIWebSocketServer extends WebSocket.Server { this.on('connection', (socket: WebSocket, request: IncomingMessage): void => { const protocolIndex = socket.protocol.indexOf(Protocol.UI); const version = socket.protocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion; - this.uiService = UIServiceFactory.getUIServiceImplementation(version, this); - if (!this.uiService) { - throw new BaseError(`Could not find a UI service implementation for protocol version ${version}`); + if (!this.uiServices.has(version)) { + throw new BaseError(`Could not find a UI service implementation for UI protocol version ${version}`); } // FIXME: check connection validity socket.on('message', (messageData) => { @@ -39,9 +45,9 @@ export default class UIWebSocketServer extends WebSocket.Server { if (Utils.isIterable(protocolRequest)) { [command, payload] = protocolRequest; } else { - throw new BaseError('Protocol request is not iterable'); + throw new BaseError('UI protocol request is not iterable'); } - this.uiService.handleMessage(command, payload).catch(() => { + this.uiServices.get(version).handleMessage(command, payload).catch(() => { logger.error(`${this.logPrefix()} Error while handling command %s message: %j`, command, payload); }); }); @@ -56,6 +62,6 @@ export default class UIWebSocketServer extends WebSocket.Server { } public logPrefix(): string { - return Utils.logPrefix('WebSocket Server:'); + return Utils.logPrefix(' UI WebSocket Server:'); } }