- return Bootstrap.instance;
- }
-
- public async start(): Promise<void> {
- if (isMainThread && !this.started) {
- try {
- this.initialize();
- await this.storage?.open();
- await this.workerImplementation.start();
- this.uiServer?.start();
- const stationTemplateUrls = Configuration.getStationTemplateUrls();
- this.numberOfChargingStationTemplates = stationTemplateUrls.length;
- // Start ChargingStation object in worker thread
- if (!Utils.isEmptyArray(stationTemplateUrls)) {
- for (const stationTemplateUrl of stationTemplateUrls) {
- try {
- const nbStations = stationTemplateUrl.numberOfStations ?? 0;
- for (let index = 1; index <= nbStations; index++) {
- await this.startChargingStation(index, stationTemplateUrl);
- }
- } catch (error) {
- console.error(
- chalk.red(
- 'Error at starting charging station with template file ' +
- stationTemplateUrl.file +
- ': '
- ),
- error
- );
+ // Enable unconditionally for now
+ handleUnhandledRejection()
+ handleUncaughtException()
+ this.started = false
+ this.starting = false
+ this.stopping = false
+ this.chargingStationsByTemplate = new Map<string, TemplateChargingStations>()
+ this.uiServer = UIServerFactory.getUIServerImplementation(
+ Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
+ )
+ this.initializedCounters = false
+ this.initializeCounters()
+ Configuration.configurationChangeCallback = async () => {
+ if (isMainThread) {
+ await Bootstrap.getInstance().restart()
+ }
+ }
+ }
+
+ public static getInstance (): Bootstrap {
+ if (Bootstrap.instance === null) {
+ Bootstrap.instance = new Bootstrap()
+ }
+ return Bootstrap.instance
+ }
+
+ public get numberOfChargingStationTemplates (): number {
+ return this.chargingStationsByTemplate.size
+ }
+
+ public get numberOfConfiguredChargingStations (): number {
+ return [...this.chargingStationsByTemplate.values()].reduce(
+ (accumulator, value) => accumulator + value.configured,
+ 0
+ )
+ }
+
+ public getLastIndex (templateName: string): number {
+ return this.chargingStationsByTemplate.get(templateName)?.lastIndex ?? 0
+ }
+
+ public getPerformanceStatistics (): IterableIterator<Statistics> | undefined {
+ return this.storage?.getPerformanceStatistics()
+ }
+
+ private get numberOfAddedChargingStations (): number {
+ return [...this.chargingStationsByTemplate.values()].reduce(
+ (accumulator, value) => accumulator + value.added,
+ 0
+ )
+ }
+
+ private get numberOfStartedChargingStations (): number {
+ return [...this.chargingStationsByTemplate.values()].reduce(
+ (accumulator, value) => accumulator + value.started,
+ 0
+ )
+ }
+
+ public async start (): Promise<void> {
+ if (!this.started) {
+ if (!this.starting) {
+ this.starting = true
+ this.on(ChargingStationWorkerMessageEvents.added, this.workerEventAdded)
+ this.on(ChargingStationWorkerMessageEvents.started, this.workerEventStarted)
+ this.on(ChargingStationWorkerMessageEvents.stopped, this.workerEventStopped)
+ this.on(ChargingStationWorkerMessageEvents.updated, this.workerEventUpdated)
+ this.on(
+ ChargingStationWorkerMessageEvents.performanceStatistics,
+ this.workerEventPerformanceStatistics
+ )
+ this.on(
+ ChargingStationWorkerMessageEvents.workerElementError,
+ (eventError: ChargingStationWorkerEventError) => {
+ logger.error(
+ `${this.logPrefix()} ${moduleName}.start: Error occurred while handling '${eventError.event}' event on worker:`,
+ eventError
+ )
+ }
+ )
+ this.initializeCounters()
+ const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
+ ConfigurationSection.worker
+ )
+ this.initializeWorkerImplementation(workerConfiguration)
+ await this.workerImplementation?.start()
+ const performanceStorageConfiguration =
+ Configuration.getConfigurationSection<StorageConfiguration>(
+ ConfigurationSection.performanceStorage
+ )
+ if (performanceStorageConfiguration.enabled === true) {
+ this.storage = StorageFactory.getStorage(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ performanceStorageConfiguration.type!,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ performanceStorageConfiguration.uri!,
+ this.logPrefix()
+ )
+ await this.storage?.open()
+ }
+ Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
+ .enabled === true && this.uiServer?.start()
+ // Start ChargingStation object instance in worker thread
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) {
+ try {
+ const nbStations =
+ this.chargingStationsByTemplate.get(parse(stationTemplateUrl.file).name)
+ ?.configured ?? stationTemplateUrl.numberOfStations
+ for (let index = 1; index <= nbStations; index++) {
+ await this.addChargingStation(index, stationTemplateUrl.file)