repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Apply dependencies update
[e-mobility-charging-stations-simulator.git]
/
src
/
charging-station
/
Bootstrap.ts
diff --git
a/src/charging-station/Bootstrap.ts
b/src/charging-station/Bootstrap.ts
index 52e1242f3973d45d6a6b430260509ba8f5e3b780..4f49a3d9ab06565fb21e9c89ad67e1456ac880e3 100644
(file)
--- a/
src/charging-station/Bootstrap.ts
+++ b/
src/charging-station/Bootstrap.ts
@@
-1,23
+1,32
@@
+// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+
+import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker';
+
import Configuration from '../utils/Configuration';
import Configuration from '../utils/Configuration';
-import { StationWorkerData } from '../types/Worker';
+import { Storage } from '../performance/storage/Storage';
+import { StorageFactory } from '../performance/storage/StorageFactory';
import Utils from '../utils/Utils';
import WorkerAbstract from '../worker/WorkerAbstract';
import WorkerFactory from '../worker/WorkerFactory';
import Utils from '../utils/Utils';
import WorkerAbstract from '../worker/WorkerAbstract';
import WorkerFactory from '../worker/WorkerFactory';
+import chalk from 'chalk';
import { isMainThread } from 'worker_threads';
import path from 'path';
import { version } from '../../package.json';
export default class Bootstrap {
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 static instance: Bootstrap | null = null;
private workerImplementation: WorkerAbstract | null = null;
private workerImplementation: WorkerAbstract | null = null;
+ private readonly storage: Storage;
+ private numberOfChargingStations: number;
+ private readonly version: string = version;
+ private started: boolean;
+ private readonly workerScript: string;
private constructor() {
this.started = false;
private constructor() {
this.started = false;
- this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
+ this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', '
Charging
StationWorker.js');
this.initWorkerImplementation();
this.initWorkerImplementation();
+ this.storage = StorageFactory.getStorage(Configuration.getPerformanceStorage().type, Configuration.getPerformanceStorage().URI, this.logPrefix());
Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
}
Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
}
@@
-31,55
+40,61
@@
export default class Bootstrap {
public async start(): Promise<void> {
if (isMainThread && !this.started) {
try {
public async start(): Promise<void> {
if (isMainThread && !this.started) {
try {
- let numStationsTotal = 0;
+ this.numberOfChargingStations = 0;
+ await this.storage.open();
await this.workerImplementation.start();
// Start ChargingStation object in worker thread
if (Configuration.getStationTemplateURLs()) {
for (const stationURL of Configuration.getStationTemplateURLs()) {
try {
await this.workerImplementation.start();
// Start ChargingStation object in worker thread
if (Configuration.getStationTemplateURLs()) {
for (const stationURL of Configuration.getStationTemplateURLs()) {
try {
- const nbStations = stationURL.numberOfStations ?
stationURL.numberOfStations :
0;
+ const nbStations = stationURL.numberOfStations ?
?
0;
for (let index = 1; index <= nbStations; index++) {
for (let index = 1; index <= nbStations; index++) {
- const workerData: StationWorkerData = {
+ const workerData:
Charging
StationWorkerData = {
index,
templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
};
await this.workerImplementation.addElement(workerData);
index,
templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
};
await this.workerImplementation.addElement(workerData);
-
numStationsTotal
++;
+
this.numberOfChargingStations
++;
}
} catch (error) {
}
} catch (error) {
- console.error(
'Charging station start with template file ' + stationURL.file + ' error '
, error);
+ console.error(
chalk.red('Charging station start with template file ' + stationURL.file + ' error ')
, error);
}
}
} else {
}
}
} else {
- console.
log('No stationTemplateURLs defined in configuration, exiting'
);
+ console.
warn(chalk.yellow('No stationTemplateURLs defined in configuration, exiting')
);
}
}
- if (
numStationsTotal
=== 0) {
- console.
log('No charging station template enabled in configuration, exiting'
);
+ if (
this.numberOfChargingStations
=== 0) {
+ console.
warn(chalk.yellow('No charging station template enabled in configuration, exiting')
);
} else {
} else {
- console.log(
`Charging station simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.workerImplementation.maxElementsPerWorker ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`
);
+ console.log(
chalk.green(`Charging stations simulator ${this.version} started with ${this.numberOfChargingStations.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.workerImplementation.maxElementsPerWorker ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`)
);
}
this.started = true;
} catch (error) {
}
this.started = true;
} catch (error) {
- console.error(
'Bootstrap start error '
, error);
+ console.error(
chalk.red('Bootstrap start error ')
, error);
}
}
+ } else {
+ console.error(chalk.red('Cannot start an already started charging stations simulator'));
}
}
public async stop(): Promise<void> {
if (isMainThread && this.started) {
await this.workerImplementation.stop();
}
}
public async stop(): Promise<void> {
if (isMainThread && this.started) {
await this.workerImplementation.stop();
+ await this.storage.close();
+ } else {
+ console.error(chalk.red('Trying to stop the charging stations simulator while not started'));
}
this.started = false;
}
public async restart(): Promise<void> {
await this.stop();
}
this.started = false;
}
public async restart(): Promise<void> {
await this.stop();
- this.initWorkerImplementation(
true
);
+ this.initWorkerImplementation();
await this.start();
}
await this.start();
}
- private initWorkerImplementation(
forceInstantiation = false)
{
- this.workerImplementation = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
+ private initWorkerImplementation(
): void
{
+ this.workerImplementation = WorkerFactory.getWorkerImplementation<
Charging
StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
{
startDelay: Configuration.getWorkerStartDelay(),
poolMaxSize: Configuration.getWorkerPoolMaxSize(),
{
startDelay: Configuration.getWorkerStartDelay(),
poolMaxSize: Configuration.getWorkerPoolMaxSize(),
@@
-87,10
+102,16
@@
export default class Bootstrap {
elementsPerWorker: Configuration.getChargingStationsPerWorker(),
poolOptions: {
workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
elementsPerWorker: Configuration.getChargingStationsPerWorker(),
poolOptions: {
workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
+ },
+ messageHandler: async (msg: ChargingStationWorkerMessage) => {
+ if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) {
+ await this.storage.storePerformanceStatistics(msg.data);
+ }
}
}
- }, forceInstantiation);
- if (!this.workerImplementation) {
- throw new Error('Worker implementation not found');
- }
+ });
+ }
+
+ private logPrefix(): string {
+ return Utils.logPrefix(' Bootstrap |');
}
}
}
}