},
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);
}
import logger from '../utils/Logger';
export default class UIWebSocketServer extends Server {
- public uiService: AbstractUIService;
+ public readonly chargingStations: Set<string>;
+ public readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
public constructor(options?: ServerOptions, callback?: () => void) {
// Create the WebSocket Server
super(options ?? Configuration.getUIWebSocketServer().options, callback);
+ this.chargingStations = new Set<string>();
+ this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
+ for (const version of Object.values(ProtocolVersion)) {
+ this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
+ }
}
public broadcastToClients(message: string | Record<string, unknown>): void {
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
} 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);
});
});
import logger from '../../utils/Logger';
export default abstract class AbstractUIService {
- public readonly chargingStations: Set<string>;
protected readonly uiWebSocketServer: UIWebSocketServer;
protected readonly messageHandlers: Map<ProtocolCommand, ProtocolRequestHandler>;
constructor(uiWebSocketServer: UIWebSocketServer) {
- this.chargingStations = new Set<string>();
this.uiWebSocketServer = uiWebSocketServer;
this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([
[ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
}
private handleListChargingStations(): Set<string> {
- return this.chargingStations;
+ return this.uiWebSocketServer.chargingStations;
}
}
+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<string, unknown>): void { }
export enum ProtocolVersion {
'0.0.1' = '0.0.1',
- '0.0.2' = '0.0.2',
}
export enum ProtocolCommand {