X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FBootstrap.ts;h=143bb5c4298ee229fe3c0d8d66ad69eebda37bf6;hb=6114e6f11b3fb12439d464e142fdf93866982b6c;hp=06bc50e391695090eb25d6e469a98c5e5be7ce3d;hpb=8244f5f080133602e63ce6d985f87782a8af9fbc;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 06bc50e3..143bb5c4 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -3,10 +3,12 @@ import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import Configuration from '../utils/Configuration'; +import Statistics from '../types/Statistics'; import { Storage } from '../performance/storage/Storage'; import { StorageFactory } from '../performance/storage/StorageFactory'; +import { UIServiceUtils } from './ui-websocket-services/UIServiceUtils'; +import UIWebSocketServer from './UIWebSocketServer'; import Utils from '../utils/Utils'; -import WebSocketServer from './WebSocketServer'; import WorkerAbstract from '../worker/WorkerAbstract'; import WorkerFactory from '../worker/WorkerFactory'; import chalk from 'chalk'; @@ -16,9 +18,9 @@ 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 workerImplementation: WorkerAbstract | null = null; + private readonly uiWebSocketServer!: UIWebSocketServer; + private readonly storage!: Storage; private numberOfChargingStations: number; private readonly version: string = version; private started: boolean; @@ -28,8 +30,14 @@ 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.getUIWebSocketServer().enabled && (this.uiWebSocketServer = new UIWebSocketServer({ + ...Configuration.getUIWebSocketServer().options, handleProtocols: UIServiceUtils.handleProtocols + })); + Configuration.getPerformanceStorage().enabled && (this.storage = StorageFactory.getStorage( + Configuration.getPerformanceStorage().type, + Configuration.getPerformanceStorage().uri, + this.logPrefix() + )); Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); } @@ -44,28 +52,29 @@ export default class Bootstrap { if (isMainThread && !this.started) { try { this.numberOfChargingStations = 0; - await this.storage.open(); + await this.storage?.open(); await this.workerImplementation.start(); - this.webSocketServer.start(); + this.uiWebSocketServer?.start(); + const stationTemplateUrls = Configuration.getStationTemplateUrls(); // Start ChargingStation object in worker thread - if (Configuration.getStationTemplateURLs()) { - for (const stationURL of Configuration.getStationTemplateURLs()) { + if (stationTemplateUrls) { + for (const stationTemplateUrl of stationTemplateUrls) { try { - const nbStations = stationURL.numberOfStations ?? 0; + const nbStations = stationTemplateUrl.numberOfStations ?? 0; for (let index = 1; index <= nbStations; index++) { const workerData: ChargingStationWorkerData = { index, - templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file)) + templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationTemplateUrl.file)) }; await this.workerImplementation.addElement(workerData); this.numberOfChargingStations++; } } catch (error) { - console.error(chalk.red('Charging station start with template file ' + stationURL.file + ' error '), error); + console.error(chalk.red('Charging station start with template file ' + stationTemplateUrl.file + ' error '), error); } } } else { - console.warn(chalk.yellow('No stationTemplateURLs defined in configuration, exiting')); + console.warn(chalk.yellow('No stationTemplateUrls defined in configuration, exiting')); } if (this.numberOfChargingStations === 0) { console.warn(chalk.yellow('No charging station template enabled in configuration, exiting')); @@ -84,8 +93,8 @@ export default class Bootstrap { public async stop(): Promise { if (isMainThread && this.started) { await this.workerImplementation.stop(); - this.webSocketServer.stop(); - await this.storage.close(); + this.uiWebSocketServer?.stop(); + await this.storage?.close(); } else { console.error(chalk.red('Trying to stop the charging stations simulator while not started')); } @@ -109,8 +118,12 @@ export default class Bootstrap { workerChoiceStrategy: Configuration.getWorkerPoolStrategy() }, messageHandler: async (msg: ChargingStationWorkerMessage) => { - if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { - await this.storage.storePerformanceStatistics(msg.data); + if (msg.id === ChargingStationWorkerMessageEvents.STARTED) { + this.uiWebSocketServer.chargingStations.add(msg.data.id as string); + } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) { + this.uiWebSocketServer.chargingStations.delete(msg.data.id as string); + } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { + await this.storage.storePerformanceStatistics(msg.data as unknown as Statistics); } } });