import Configuration from '../utils/Configuration';
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';
export default class Bootstrap {
private static instance: Bootstrap | null = null;
private workerImplementation: WorkerAbstract | null = null;
- private readonly webSocketServer: WebSocketServer;
- private readonly storage: Storage;
+ private readonly uiWebSocketServer!: UIWebSocketServer;
+ private readonly storage!: Storage;
private numberOfChargingStations: number;
private readonly version: string = version;
private started: boolean;
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());
}
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'));
public async stop(): Promise<void> {
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'));
}
},
messageHandler: async (msg: ChargingStationWorkerMessage) => {
if (msg.id === ChargingStationWorkerMessageEvents.STARTED) {
- this.webSocketServer.webSocketServerService.chargingStations.add(msg.data.id);
+ this.uiWebSocketServer.chargingStations.add(msg.data.id);
} else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) {
- this.webSocketServer.webSocketServerService.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);
}