From: Jérôme Benoit Date: Thu, 2 Dec 2021 07:28:05 +0000 (+0100) Subject: UI WS server: use a map to store UI services per protocol version X-Git-Tag: v1.1.36~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=de9136ae2018e1dec8b069184bd7dd5a5968d8d7;p=e-mobility-charging-stations-simulator.git UI WS server: use a map to store UI services per protocol version Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index ce20f0bf..0c398c57 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -117,9 +117,9 @@ export default class Bootstrap { }, messageHandler: async (msg: ChargingStationWorkerMessage) => { if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { - this.uiWebSocketServer.uiService.chargingStations.add(msg.data.id); + this.uiWebSocketServer.chargingStations.add(msg.data.id); } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) { - this.uiWebSocketServer.uiService.chargingStations.delete(msg.data.id); + this.uiWebSocketServer.chargingStations.delete(msg.data.id); } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { await this.storage.storePerformanceStatistics(msg.data); } diff --git a/src/charging-station/UIWebSocketServer.ts b/src/charging-station/UIWebSocketServer.ts index 5903b85c..59186a5e 100644 --- a/src/charging-station/UIWebSocketServer.ts +++ b/src/charging-station/UIWebSocketServer.ts @@ -10,11 +10,17 @@ 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); }); }); diff --git a/src/charging-station/UIWebSocketServices/AbstractUIService.ts b/src/charging-station/UIWebSocketServices/AbstractUIService.ts index 67512698..130c8a5a 100644 --- a/src/charging-station/UIWebSocketServices/AbstractUIService.ts +++ b/src/charging-station/UIWebSocketServices/AbstractUIService.ts @@ -5,12 +5,10 @@ import UIWebSocketServer from '../UIWebSocketServer'; import logger from '../../utils/Logger'; export default abstract class AbstractUIService { - public readonly chargingStations: Set; protected readonly uiWebSocketServer: UIWebSocketServer; protected readonly messageHandlers: Map; constructor(uiWebSocketServer: UIWebSocketServer) { - this.chargingStations = new Set(); this.uiWebSocketServer = uiWebSocketServer; this.messageHandlers = new Map([ [ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)], @@ -44,6 +42,6 @@ export default abstract class AbstractUIService { } private handleListChargingStations(): Set { - return this.chargingStations; + return this.uiWebSocketServer.chargingStations; } } diff --git a/src/charging-station/UIWebSocketServices/UIService001.ts b/src/charging-station/UIWebSocketServices/UIService001.ts index 5aba5a7b..3270aaf3 100644 --- a/src/charging-station/UIWebSocketServices/UIService001.ts +++ b/src/charging-station/UIWebSocketServices/UIService001.ts @@ -1,12 +1,13 @@ +import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UIProtocol'; + import AbstractUIService from './AbstractUIService'; -import { ProtocolCommand } from '../../types/UIProtocol'; import UIWebSocketServer from '../UIWebSocketServer'; export default class UIService001 extends AbstractUIService { constructor(uiWebSocketServer: UIWebSocketServer) { super(uiWebSocketServer); - this.messageHandlers.set(ProtocolCommand.START_TRANSACTION, this.handleStartTransaction.bind(this)); - this.messageHandlers.set(ProtocolCommand.STOP_TRANSACTION, this.handleStopTransaction.bind(this)); + this.messageHandlers.set(ProtocolCommand.START_TRANSACTION, this.handleStartTransaction.bind(this) as ProtocolRequestHandler); + this.messageHandlers.set(ProtocolCommand.STOP_TRANSACTION, this.handleStopTransaction.bind(this) as ProtocolRequestHandler); } private handleStartTransaction(payload: Record): void { } diff --git a/src/types/UIProtocol.ts b/src/types/UIProtocol.ts index 2abefe25..05ddaa23 100644 --- a/src/types/UIProtocol.ts +++ b/src/types/UIProtocol.ts @@ -4,7 +4,6 @@ export enum Protocol { export enum ProtocolVersion { '0.0.1' = '0.0.1', - '0.0.2' = '0.0.2', } export enum ProtocolCommand {