X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIWebSocketServer.ts;h=be2961c522584172560a9736b15f6703f10599da;hb=a745e4127ed71e21b50d0397cd8ef79bf59a7573;hp=98dd52b2489fdeb1dc82670ed40339d2934893a5;hpb=675fa8e39afc1cc54c2dc68542300de95f5767e9;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 98dd52b2..be2961c5 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,26 +1,25 @@ -import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; -import WebSocket, { OPEN, Server, ServerOptions } from 'ws'; +import { IncomingMessage } from 'http'; + +import WebSocket from 'ws'; -import AbstractUIService from './ui-services/AbstractUIService'; +import { ServerOptions } from '../../types/ConfigurationData'; +import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; import Configuration from '../../utils/Configuration'; -import { IncomingMessage } from 'http'; -import UIServiceFactory from './ui-services/UIServiceFactory'; -import Utils from '../../utils/Utils'; import logger from '../../utils/Logger'; +import Utils from '../../utils/Utils'; +import { AbstractUIServer } from './AbstractUIServer'; +import UIServiceFactory from './ui-services/UIServiceFactory'; -export default class UIWebSocketServer extends Server { - public readonly chargingStations: Set; - public readonly uiServices: Map; +const moduleName = 'UIWebSocketServer'; - public constructor(options?: ServerOptions, callback?: () => void) { - // Create the WebSocket Server - super(options ?? Configuration.getUIServer().options, callback); - this.chargingStations = new Set(); - this.uiServices = new Map(); +export default class UIWebSocketServer extends AbstractUIServer { + public constructor(options?: ServerOptions) { + super(); + this.server = new WebSocket.Server(options ?? Configuration.getUIServer().options); } public start(): void { - this.on('connection', (socket: WebSocket, request: IncomingMessage): void => { + this.server.on('connection', (socket: WebSocket, request: IncomingMessage): void => { const protocolIndex = socket.protocol.indexOf(Protocol.UI); const version = socket.protocol.substring( protocolIndex + Protocol.UI.length @@ -29,35 +28,47 @@ export default class UIWebSocketServer extends Server { this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); } // FIXME: check connection validity - socket.on('message', (messageData) => { + socket.on('message', (rawData) => { this.uiServices .get(version) - .messageHandler(messageData) + .requestHandler(rawData) .catch(() => { - logger.error(`${this.logPrefix()} Error while handling message data: %j`, messageData); + /* Error caught by AbstractUIService */ }); }); socket.on('error', (error) => { - logger.error(`${this.logPrefix()} Error on WebSocket: %j`, error); + logger.error( + `${this.logPrefix(moduleName, 'start.socket.onerror')} Error on WebSocket:`, + error + ); }); }); } public stop(): void { - this.close(); + this.chargingStations.clear(); + } + + public sendRequest(request: string): void { + this.broadcastToClients(request); } - public sendResponse(message: string): void { - this.broadcastToClients(message); + public sendResponse(response: string): void { + // TODO: send response only to the client that sent the request + this.broadcastToClients(response); } - public logPrefix(): string { - return Utils.logPrefix(' UI WebSocket Server:'); + public logPrefix(modName?: string, methodName?: string): string { + const logMsg = + modName && methodName + ? ` UI WebSocket Server | ${modName}.${methodName}:` + : ' UI WebSocket Server |'; + return Utils.logPrefix(logMsg); } private broadcastToClients(message: string): void { - for (const client of this.clients) { - if (client?.readyState === OPEN) { + for (const client of (this.server as WebSocket.Server).clients) { + if (client?.readyState === WebSocket.OPEN) { client.send(message); } }