X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FUIWebSocketServer.ts;h=dba59e9206b4a32583c049bc64d4154abb5d5fd4;hb=d0294d4394caa65c7dac8336fcdf30fd800c1a0a;hp=5903b85c48cad0112b0eacee9a64dc6f09e9ed0d;hpb=6a49ad23c5b2b65424a2374c4eeca027beb77e79;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/UIWebSocketServer.ts b/src/charging-station/UIWebSocketServer.ts index 5903b85c..dba59e92 100644 --- a/src/charging-station/UIWebSocketServer.ts +++ b/src/charging-station/UIWebSocketServer.ts @@ -1,20 +1,26 @@ 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 logger from '../utils/Logger'; export default class UIWebSocketServer extends Server { - public uiService: AbstractUIService; + public readonly chargingStations: Set; + public readonly uiServices: Map; public constructor(options?: ServerOptions, callback?: () => void) { // Create the WebSocket Server 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 { @@ -29,8 +35,7 @@ export default class UIWebSocketServer extends 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) { + if (!this.uiServices.has(version)) { throw new BaseError(`Could not find a UI service implementation for UI protocol version ${version}`); } // FIXME: check connection validity @@ -42,7 +47,7 @@ export default class UIWebSocketServer extends Server { } else { 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); }); });