X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-websocket-services%2FAbstractUIService.ts;h=42cfa428f1373cc36e1b67bca96661d1eebf6b19;hb=3e0905a14af7b7e0a96e859d6c4c615044845d54;hp=130c8a5a0d06629897e4b576036df214d1260c27;hpb=383cb2aee597e711f037c9f67151191bb233a91c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-websocket-services/AbstractUIService.ts b/src/charging-station/ui-websocket-services/AbstractUIService.ts index 130c8a5a..42cfa428 100644 --- a/src/charging-station/ui-websocket-services/AbstractUIService.ts +++ b/src/charging-station/ui-websocket-services/AbstractUIService.ts @@ -1,6 +1,7 @@ import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UIProtocol'; import BaseError from '../../exception/BaseError'; +import { JsonType } from '../../types/JsonType'; import UIWebSocketServer from '../UIWebSocketServer'; import logger from '../../utils/Logger'; @@ -15,12 +16,12 @@ export default abstract class AbstractUIService { ]); } - public async handleMessage(command: ProtocolCommand, payload: Record): Promise { - let messageResponse: Record; + public async messageHandler(command: ProtocolCommand, payload: JsonType): Promise { + let messageResponse: JsonType; if (this.messageHandlers.has(command)) { try { // Call the method to build the message response - messageResponse = await this.messageHandlers.get(command)(payload) as Record; + messageResponse = (await this.messageHandlers.get(command)(payload)) as JsonType; } catch (error) { // Log logger.error(this.uiWebSocketServer.logPrefix() + ' Handle message error: %j', error); @@ -28,20 +29,23 @@ export default abstract class AbstractUIService { } } else { // Throw exception - throw new BaseError(`${command} is not implemented to handle message payload ${JSON.stringify(payload, null, 2)}`); + throw new BaseError( + `${command} is not implemented to handle message payload ${JSON.stringify( + payload, + null, + 2 + )}` + ); } // Send the built message response this.uiWebSocketServer.broadcastToClients(this.buildProtocolMessage(command, messageResponse)); } - protected buildProtocolMessage( - command: ProtocolCommand, - payload: Record, - ): string { + protected buildProtocolMessage(command: ProtocolCommand, payload: JsonType): string { return JSON.stringify([command, payload]); } - private handleListChargingStations(): Set { - return this.uiWebSocketServer.chargingStations; + private handleListChargingStations(): string[] { + return Array.from(this.uiWebSocketServer.chargingStations); } }