ChargingStationWorkerMessageEvents,
} from '../types/ChargingStationWorker';
+import { AbstractUIServer } from './ui-server/AbstractUIServer';
+import { ApplicationProtocol } from '../types/UIProtocol';
import Configuration from '../utils/Configuration';
import { StationTemplateUrl } from '../types/ConfigurationData';
import Statistics from '../types/Statistics';
import { Storage } from '../performance/storage/Storage';
import { StorageFactory } from '../performance/storage/StorageFactory';
+import UIServerFactory from './ui-server/UIServerFactory';
import { UIServiceUtils } from './ui-server/ui-services/UIServiceUtils';
-import UIWebSocketServer from './ui-server/UIWebSocketServer';
import Utils from '../utils/Utils';
import WorkerAbstract from '../worker/WorkerAbstract';
import WorkerFactory from '../worker/WorkerFactory';
export default class Bootstrap {
private static instance: Bootstrap | null = null;
private workerImplementation: WorkerAbstract<ChargingStationWorkerData> | null = null;
- private readonly uiServer!: UIWebSocketServer;
+ private readonly uiServer!: AbstractUIServer;
private readonly storage!: Storage;
private numberOfChargingStations: number;
private readonly version: string = version;
);
this.initWorkerImplementation();
Configuration.getUIServer().enabled &&
- (this.uiServer = new UIWebSocketServer({
+ (this.uiServer = UIServerFactory.getUIServerImplementation(ApplicationProtocol.WS, {
...Configuration.getUIServer().options,
handleProtocols: UIServiceUtils.handleProtocols,
}));
--- /dev/null
+import AbstractUIService from './ui-services/AbstractUIService';
+import { Server as HttpServer } from 'http';
+import { ProtocolVersion } from '../../types/UIProtocol';
+import { Server as WSServer } from 'ws';
+
+export abstract class AbstractUIServer {
+ public readonly chargingStations: Set<string>;
+ protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
+ protected uiServer: WSServer | HttpServer;
+
+ public constructor() {
+ this.chargingStations = new Set<string>();
+ this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
+ }
+
+ public abstract start(): void;
+ public abstract stop(): void;
+ public abstract sendResponse(message: string): void;
+ public abstract logPrefix(): string;
+}
--- /dev/null
+import { AbstractUIServer } from './AbstractUIServer';
+import { ApplicationProtocol } from '../../types/UIProtocol';
+import Configuration from '../../utils/Configuration';
+import { ServerOptions } from '../../types/ConfigurationData';
+import UIWebSocketServer from './UIWebSocketServer';
+
+export default class UIServerFactory {
+ private constructor() {
+ // This is intentional
+ }
+
+ public static getUIServerImplementation(
+ applicationProtocol: ApplicationProtocol,
+ options?: ServerOptions,
+ callback?: () => void
+ ): AbstractUIServer | null {
+ switch (applicationProtocol) {
+ case ApplicationProtocol.WS:
+ return new UIWebSocketServer(options ?? Configuration.getUIServer().options, callback);
+ default:
+ return null;
+ }
+ }
+}
import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
import WebSocket, { OPEN, Server } from 'ws';
-import AbstractUIService from './ui-services/AbstractUIService';
+import { AbstractUIServer } from './AbstractUIServer';
import Configuration from '../../utils/Configuration';
import { IncomingMessage } from 'http';
import { ServerOptions } from '../../types/ConfigurationData';
import Utils from '../../utils/Utils';
import logger from '../../utils/Logger';
-export default class UIWebSocketServer extends Server {
- public readonly chargingStations: Set<string>;
- private readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
-
+export default class UIWebSocketServer extends AbstractUIServer {
public constructor(options?: ServerOptions, callback?: () => void) {
- // Create the WebSocket server
- super(options ?? Configuration.getUIServer().options, callback);
- this.chargingStations = new Set<string>();
- this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
+ super();
+ this.uiServer = new Server(options ?? Configuration.getUIServer().options, callback);
}
public start(): void {
- this.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
+ this.uiServer.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
const protocolIndex = socket.protocol.indexOf(Protocol.UI);
const version = socket.protocol.substring(
protocolIndex + Protocol.UI.length
}
public stop(): void {
- this.close();
+ this.uiServer.close();
}
public sendResponse(message: string): void {
}
private broadcastToClients(message: string): void {
- for (const client of this.clients) {
+ for (const client of (this.uiServer as Server).clients) {
if (client?.readyState === OPEN) {
client.send(message);
}
ProtocolRequestHandler,
} from '../../../types/UIProtocol';
+import { AbstractUIServer } from '../AbstractUIServer';
import BaseError from '../../../exception/BaseError';
import { JsonType } from '../../../types/JsonType';
import { RawData } from 'ws';
-import UIWebSocketServer from '../UIWebSocketServer';
import Utils from '../../../utils/Utils';
import logger from '../../../utils/Logger';
export default abstract class AbstractUIService {
- protected readonly uiServer: UIWebSocketServer;
+ protected readonly uiServer: AbstractUIServer;
protected readonly messageHandlers: Map<ProtocolCommand, ProtocolRequestHandler>;
- constructor(uiServer: UIWebSocketServer) {
+ constructor(uiServer: AbstractUIServer) {
this.uiServer = uiServer;
this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([
[ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
import { ProtocolCommand, ProtocolRequestHandler } from '../../../types/UIProtocol';
+import { AbstractUIServer } from '../AbstractUIServer';
import AbstractUIService from './AbstractUIService';
import { JsonType } from '../../../types/JsonType';
-import UIWebSocketServer from '../UIWebSocketServer';
export default class UIService001 extends AbstractUIService {
- constructor(uiServer: UIWebSocketServer) {
+ constructor(uiServer: AbstractUIServer) {
super(uiServer);
this.messageHandlers.set(
ProtocolCommand.START_TRANSACTION,
+import { AbstractUIServer } from '../AbstractUIServer';
import AbstractUIService from './AbstractUIService';
import { ProtocolVersion } from '../../../types/UIProtocol';
import UIService001 from './UIService001';
-import UIWebSocketServer from '../UIWebSocketServer';
export default class UIServiceFactory {
private constructor() {
public static getUIServiceImplementation(
version: ProtocolVersion,
- uiServer: UIWebSocketServer
+ uiServer: AbstractUIServer
): AbstractUIService | null {
switch (version) {
case ProtocolVersion['0.0.1']: