X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FBootstrap.ts;h=fe344407dc03400970fc89fb911f45011ce422b5;hb=d8ff9d67d91a9aa8ac525de54ed0248066ac94f2;hp=4f49a3d9ab06565fb21e9c89ad67e1456ac880e3;hpb=a4bc29426a2f56c987ec161d86baf9a83c0398ca;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 4f49a3d9..fe344407 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -6,6 +6,7 @@ import Configuration from '../utils/Configuration'; import { Storage } from '../performance/storage/Storage'; import { StorageFactory } from '../performance/storage/StorageFactory'; import Utils from '../utils/Utils'; +import WebSocketServer from './WebSocketServer'; import WorkerAbstract from '../worker/WorkerAbstract'; import WorkerFactory from '../worker/WorkerFactory'; import chalk from 'chalk'; @@ -16,6 +17,7 @@ import { version } from '../../package.json'; export default class Bootstrap { private static instance: Bootstrap | null = null; private workerImplementation: WorkerAbstract | null = null; + private readonly webSocketServer: WebSocketServer; private readonly storage: Storage; private numberOfChargingStations: number; private readonly version: string = version; @@ -26,6 +28,7 @@ export default class Bootstrap { this.started = false; this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'ChargingStationWorker.js'); this.initWorkerImplementation(); + this.webSocketServer = new WebSocketServer(); this.storage = StorageFactory.getStorage(Configuration.getPerformanceStorage().type, Configuration.getPerformanceStorage().URI, this.logPrefix()); Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); } @@ -43,6 +46,7 @@ export default class Bootstrap { this.numberOfChargingStations = 0; await this.storage.open(); await this.workerImplementation.start(); + this.webSocketServer.start(); // Start ChargingStation object in worker thread if (Configuration.getStationTemplateURLs()) { for (const stationURL of Configuration.getStationTemplateURLs()) { @@ -80,6 +84,7 @@ export default class Bootstrap { public async stop(): Promise { if (isMainThread && this.started) { await this.workerImplementation.stop(); + this.webSocketServer.stop(); await this.storage.close(); } else { console.error(chalk.red('Trying to stop the charging stations simulator while not started')); @@ -104,7 +109,11 @@ export default class Bootstrap { workerChoiceStrategy: Configuration.getWorkerPoolStrategy() }, messageHandler: async (msg: ChargingStationWorkerMessage) => { - if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { + if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { + this.webSocketServer.webSocketServerService.chargingStations.add(msg.data.id); + } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) { + this.webSocketServer.webSocketServerService.chargingStations.delete(msg.data.id); + } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { await this.storage.storePerformanceStatistics(msg.data); } }