-import Configuration from '../utils/Configuration';
-import { StationWorkerData } from '../types/Worker';
-import Utils from '../utils/Utils';
-import WorkerAbstract from '../worker/WorkerAbstract';
-import WorkerFactory from '../worker/WorkerFactory';
-import { isMainThread } from 'worker_threads';
-import path from 'path';
-import { version } from '../../package.json';
-
-export default class Bootstrap {
- private static instance: Bootstrap;
- private version: string = version;
- private started: boolean;
- private workerScript: string;
- private workerImplementationInstance: WorkerAbstract | null = null;
-
- private constructor() {
- this.started = false;
- this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
- Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
- }
-
- public static getInstance(): Bootstrap {
- if (!Bootstrap.instance) {
- Bootstrap.instance = new Bootstrap();
- }
- return Bootstrap.instance;
- }
-
- public async start(): Promise<void> {
- if (isMainThread && !this.started) {
- try {
- let numStationsTotal = 0;
- await this.getWorkerImplementationInstance()?.start();
- // Start ChargingStation object in worker thread
- if (Configuration.getStationTemplateURLs()) {
- for (const stationURL of Configuration.getStationTemplateURLs()) {
- try {
- const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
- for (let index = 1; index <= nbStations; index++) {
- const workerData: StationWorkerData = {
- index,
- templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
- };
- await this.getWorkerImplementationInstance()?.addElement(workerData);
- numStationsTotal++;
- }
- } catch (error) {
- console.error('Charging station start with template file ' + stationURL.file + ' error ', error);
+// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
+
+import { EventEmitter } from 'node:events'
+import { dirname, extname, join } from 'node:path'
+import process, { exit } from 'node:process'
+import { fileURLToPath } from 'node:url'
+import { isMainThread } from 'node:worker_threads'
+
+import chalk from 'chalk'
+import { availableParallelism, type MessageHandler } from 'poolifier'
+import type { Worker } from 'worker_threads'
+
+import { version } from '../../package.json'
+import { BaseError } from '../exception/index.js'
+import { type Storage, StorageFactory } from '../performance/index.js'
+import {
+ type ChargingStationData,
+ type ChargingStationInfo,
+ type ChargingStationOptions,
+ type ChargingStationWorkerData,
+ type ChargingStationWorkerMessage,
+ type ChargingStationWorkerMessageData,
+ ChargingStationWorkerMessageEvents,
+ ConfigurationSection,
+ ProcedureName,
+ type SimulatorState,
+ type Statistics,
+ type StorageConfiguration,
+ type TemplateStatistics,
+ type UIServerConfiguration,
+ type WorkerConfiguration
+} from '../types/index.js'
+import {
+ Configuration,
+ Constants,
+ formatDurationMilliSeconds,
+ generateUUID,
+ handleUncaughtException,
+ handleUnhandledRejection,
+ isAsyncFunction,
+ isNotEmptyArray,
+ logger,
+ logPrefix
+} from '../utils/index.js'
+import { DEFAULT_ELEMENTS_PER_WORKER, type WorkerAbstract, WorkerFactory } from '../worker/index.js'
+import { buildTemplateName, waitChargingStationEvents } from './Helpers.js'
+import type { AbstractUIServer } from './ui-server/AbstractUIServer.js'
+import { UIServerFactory } from './ui-server/UIServerFactory.js'
+
+const moduleName = 'Bootstrap'
+
+enum exitCodes {
+ succeeded = 0,
+ missingChargingStationsConfiguration = 1,
+ duplicateChargingStationTemplateUrls = 2,
+ noChargingStationTemplates = 3,
+ gracefulShutdownError = 4
+}
+
+export class Bootstrap extends EventEmitter {
+ private static instance: Bootstrap | null = null
+ private workerImplementation?: WorkerAbstract<ChargingStationWorkerData, ChargingStationInfo>
+ private readonly uiServer: AbstractUIServer
+ private storage?: Storage
+ private readonly templateStatistics: Map<string, TemplateStatistics>
+ private readonly version: string = version
+ private started: boolean
+ private starting: boolean
+ private stopping: boolean
+ private uiServerStarted: boolean
+
+ private constructor () {
+ super()
+ for (const signal of ['SIGINT', 'SIGQUIT', 'SIGTERM']) {
+ process.on(signal, this.gracefulShutdown.bind(this))
+ }
+ // Enable unconditionally for now
+ handleUnhandledRejection()
+ handleUncaughtException()
+ this.started = false
+ this.starting = false
+ this.stopping = false
+ this.uiServerStarted = false
+ this.templateStatistics = new Map<string, TemplateStatistics>()
+ this.uiServer = UIServerFactory.getUIServerImplementation(
+ Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
+ )
+ this.initializeCounters()
+ this.initializeWorkerImplementation(
+ Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
+ )
+ 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.templateStatistics.size
+ }
+
+ public get numberOfConfiguredChargingStations (): number {
+ return [...this.templateStatistics.values()].reduce(
+ (accumulator, value) => accumulator + value.configured,
+ 0
+ )
+ }
+
+ public get numberOfProvisionedChargingStations (): number {
+ return [...this.templateStatistics.values()].reduce(
+ (accumulator, value) => accumulator + value.provisioned,
+ 0
+ )
+ }
+
+ public getState (): SimulatorState {
+ return {
+ version: this.version,
+ configuration: Configuration.getConfigurationData(),
+ started: this.started,
+ templateStatistics: this.templateStatistics
+ }
+ }
+
+ public getLastIndex (templateName: string): number {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ const indexes = [...this.templateStatistics.get(templateName)!.indexes]
+ .concat(0)
+ .sort((a, b) => a - b)
+ for (let i = 0; i < indexes.length - 1; i++) {
+ if (indexes[i + 1] - indexes[i] !== 1) {
+ return indexes[i]
+ }
+ }
+ return indexes[indexes.length - 1]
+ }
+
+ public getPerformanceStatistics (): IterableIterator<Statistics> | undefined {
+ return this.storage?.getPerformanceStatistics()
+ }
+
+ private get numberOfAddedChargingStations (): number {
+ return [...this.templateStatistics.values()].reduce(
+ (accumulator, value) => accumulator + value.added,
+ 0
+ )
+ }
+
+ private get numberOfStartedChargingStations (): number {
+ return [...this.templateStatistics.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.deleted, this.workerEventDeleted)
+ this.on(ChargingStationWorkerMessageEvents.started, this.workerEventStarted)
+ this.on(ChargingStationWorkerMessageEvents.stopped, this.workerEventStopped)
+ this.on(ChargingStationWorkerMessageEvents.updated, this.workerEventUpdated)
+ this.on(
+ ChargingStationWorkerMessageEvents.performanceStatistics,
+ this.workerEventPerformanceStatistics
+ )
+ // eslint-disable-next-line @typescript-eslint/unbound-method
+ if (isAsyncFunction(this.workerImplementation?.start)) {
+ await this.workerImplementation.start()
+ } else {
+ (this.workerImplementation?.start as () => void)()
+ }
+ 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()
+ }
+ if (
+ !this.uiServerStarted &&
+ Configuration.getConfigurationSection<UIServerConfiguration>(
+ ConfigurationSection.uiServer
+ ).enabled === true
+ ) {
+ this.uiServer.start()
+ this.uiServerStarted = true
+ }
+ // 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 = stationTemplateUrl.numberOfStations
+ for (let index = 1; index <= nbStations; index++) {
+ await this.addChargingStation(index, stationTemplateUrl.file)