1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 ChargingStationWorkerData
,
5 ChargingStationWorkerMessage
,
6 ChargingStationWorkerMessageEvents
,
7 } from
'../types/ChargingStationWorker';
9 import { AbstractUIServer
} from
'./ui-server/AbstractUIServer';
10 import { ApplicationProtocol
} from
'../types/UIProtocol';
11 import { ChargingStationUtils
} from
'./ChargingStationUtils';
12 import Configuration from
'../utils/Configuration';
13 import { StationTemplateUrl
} from
'../types/ConfigurationData';
14 import Statistics from
'../types/Statistics';
15 import { Storage
} from
'../performance/storage/Storage';
16 import { StorageFactory
} from
'../performance/storage/StorageFactory';
17 import UIServerFactory from
'./ui-server/UIServerFactory';
18 import { UIServiceUtils
} from
'./ui-server/ui-services/UIServiceUtils';
19 import Utils from
'../utils/Utils';
20 import WorkerAbstract from
'../worker/WorkerAbstract';
21 import WorkerFactory from
'../worker/WorkerFactory';
22 import chalk from
'chalk';
23 import { isMainThread
} from
'worker_threads';
24 import path from
'path';
25 import { version
} from
'../../package.json';
27 export default class Bootstrap
{
28 private static instance
: Bootstrap
| null = null;
29 private workerImplementation
: WorkerAbstract
<ChargingStationWorkerData
> | null = null;
30 private readonly uiServer
!: AbstractUIServer
;
31 private readonly storage
!: Storage
;
32 private numberOfChargingStationTemplates
!: number;
33 private numberOfChargingStations
!: number;
34 private readonly version
: string = version
;
35 private started
: boolean;
36 private readonly workerScript
: string;
38 private constructor() {
40 this.workerScript
= path
.join(
41 path
.resolve(__dirname
, '../'),
43 'ChargingStationWorker.js'
46 this.initWorkerImplementation();
47 Configuration
.getUIServer().enabled
&&
48 (this.uiServer
= UIServerFactory
.getUIServerImplementation(ApplicationProtocol
.WS
, {
49 ...Configuration
.getUIServer().options
,
50 handleProtocols
: UIServiceUtils
.handleProtocols
,
52 Configuration
.getPerformanceStorage().enabled
&&
53 (this.storage
= StorageFactory
.getStorage(
54 Configuration
.getPerformanceStorage().type,
55 Configuration
.getPerformanceStorage().uri
,
58 Configuration
.setConfigurationChangeCallback(async () => Bootstrap
.getInstance().restart());
61 public static getInstance(): Bootstrap
{
62 if (!Bootstrap
.instance
) {
63 Bootstrap
.instance
= new Bootstrap();
65 return Bootstrap
.instance
;
68 public async start(): Promise
<void> {
69 if (isMainThread
&& !this.started
) {
72 await this.storage
?.open();
73 await this.workerImplementation
.start();
74 this.uiServer
?.start();
75 const stationTemplateUrls
= Configuration
.getStationTemplateUrls();
76 this.numberOfChargingStationTemplates
= stationTemplateUrls
.length
;
77 // Start ChargingStation object in worker thread
78 if (stationTemplateUrls
) {
79 for (const stationTemplateUrl
of stationTemplateUrls
) {
81 const nbStations
= stationTemplateUrl
.numberOfStations
?? 0;
82 for (let index
= 1; index
<= nbStations
; index
++) {
83 await this.startChargingStation(index
, stationTemplateUrl
);
88 'Error at starting charging station with template file ' +
89 stationTemplateUrl
.file
+
97 console
.warn(chalk
.yellow("No 'stationTemplateUrls' defined in configuration, exiting"));
99 if (this.numberOfChargingStations
=== 0) {
101 chalk
.yellow('No charging station template enabled in configuration, exiting')
106 `Charging stations simulator ${
108 } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
109 ChargingStationUtils.workerDynamicPoolInUse()
110 ? `${Configuration.getWorkerPoolMinSize().toString()}
/`
112 }${this.workerImplementation.size}${
113 ChargingStationUtils.workerPoolInUse()
114 ? `/${Configuration.getWorkerPoolMaxSize().toString()}
`
116 } worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${
117 this.workerImplementation.maxElementsPerWorker
118 ? ` (${this.workerImplementation.maxElementsPerWorker} charging
station(s
) per worker
)`
126 console
.error(chalk
.red('Bootstrap start error '), error
);
129 console
.error(chalk
.red('Cannot start an already started charging stations simulator'));
133 public async stop(): Promise
<void> {
134 if (isMainThread
&& this.started
) {
135 await this.workerImplementation
.stop();
136 this.uiServer
?.stop();
137 await this.storage
?.close();
139 console
.error(chalk
.red('Trying to stop the charging stations simulator while not started'));
141 this.started
= false;
144 public async restart(): Promise
<void> {
147 this.initWorkerImplementation();
151 private initWorkerImplementation(): void {
152 this.workerImplementation
= WorkerFactory
.getWorkerImplementation
<ChargingStationWorkerData
>(
154 Configuration
.getWorkerProcess(),
156 workerStartDelay
: Configuration
.getWorkerStartDelay(),
157 elementStartDelay
: Configuration
.getElementStartDelay(),
158 poolMaxSize
: Configuration
.getWorkerPoolMaxSize(),
159 poolMinSize
: Configuration
.getWorkerPoolMinSize(),
160 elementsPerWorker
: Configuration
.getChargingStationsPerWorker(),
162 workerChoiceStrategy
: Configuration
.getWorkerPoolStrategy(),
164 messageHandler
: async (msg
: ChargingStationWorkerMessage
) => {
165 if (msg
.id
=== ChargingStationWorkerMessageEvents
.STARTED
) {
166 this.uiServer
.chargingStations
.add(msg
.data
.id
as string);
167 } else if (msg
.id
=== ChargingStationWorkerMessageEvents
.STOPPED
) {
168 this.uiServer
.chargingStations
.delete(msg
.data
.id
as string);
169 } else if (msg
.id
=== ChargingStationWorkerMessageEvents
.PERFORMANCE_STATISTICS
) {
170 await this.storage
.storePerformanceStatistics(msg
.data
as unknown
as Statistics
);
177 private initialize() {
178 this.numberOfChargingStations
= 0;
179 this.numberOfChargingStationTemplates
= 0;
182 private async startChargingStation(
184 stationTemplateUrl
: StationTemplateUrl
186 const workerData
: ChargingStationWorkerData
= {
188 templateFile
: path
.join(
189 path
.resolve(__dirname
, '../'),
192 path
.basename(stationTemplateUrl
.file
)
195 await this.workerImplementation
.addElement(workerData
);
196 this.numberOfChargingStations
++;
199 private logPrefix(): string {
200 return Utils
.logPrefix(' Bootstrap |');