From: Jérôme Benoit Date: Tue, 24 Nov 2020 09:48:27 +0000 (+0100) Subject: Fix rounding helper X-Git-Tag: v1.0.1-0~181 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ad3de6c4ec07b788fe1c84a40081902c3abc5b3b;p=e-mobility-charging-stations-simulator.git Fix rounding helper Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/StationWorker.ts b/src/charging-station/StationWorker.ts index f54334e9..68c3ea24 100644 --- a/src/charging-station/StationWorker.ts +++ b/src/charging-station/StationWorker.ts @@ -3,6 +3,6 @@ import { isMainThread, workerData } from 'worker_threads'; import ChargingStation from './ChargingStation'; if (!isMainThread) { - const station = new ChargingStation(workerData.index, workerData.templateFile); + const station = new ChargingStation(workerData.index as number, workerData.templateFile as string); station.start(); } diff --git a/src/charging-station/Worker.ts b/src/charging-station/Worker.ts index 93aade50..d9194202 100644 --- a/src/charging-station/Worker.ts +++ b/src/charging-station/Worker.ts @@ -1,38 +1,37 @@ import Configuration from '../utils/Configuration'; import Pool from 'worker-threads-pool'; import { Worker } from 'worker_threads'; +import WorkerData from '../types/WorkerData'; export default class Wrk { - private _workerData; - private _workerScript; - private _pool; + private _workerScript: string; + private _workerData: WorkerData; + private _pool: Pool; private _concurrentWorkers: number; /** * Create a new `Wrk`. * - * @param {String} workerScript - * @param {Object} workerData - * @param {Number} numConcurrentWorkers + * @param {string} workerScript + * @param {WorkerData} workerData + * @param {number} numConcurrentWorkers */ - constructor(workerScript, workerData, numConcurrentWorkers) { + constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) { this._workerData = workerData; this._workerScript = workerScript; - this._numConcurrentWorkers = numConcurrentWorkers; if (Configuration.useWorkerPool()) { + this._concurrentWorkers = Configuration.getWorkerPoolSize(); this._pool = new Pool({ max: Configuration.getWorkerPoolSize() }); + } else { + this._concurrentWorkers = numConcurrentWorkers; } } /** - * @param {Number} numConcurrentWorkers - * @private + * @return {number} + * @public */ - set _numConcurrentWorkers(numConcurrentWorkers: number) { - this._concurrentWorkers = numConcurrentWorkers; - } - - get _numConcurrentWorkers(): number { + public get concurrentWorkers(): number { return this._concurrentWorkers; } diff --git a/src/start.ts b/src/start.ts index 1c91dea3..e841b57e 100644 --- a/src/start.ts +++ b/src/start.ts @@ -6,6 +6,7 @@ class Bootstrap { static start() { try { let numStationsTotal = 0; + let numConcurrentWorkers = 0; // Start each ChargingStation object in a worker thread if (Configuration.getStationTemplateURLs()) { Configuration.getStationTemplateURLs().forEach((stationURL: StationTemplateURL) => { @@ -18,6 +19,7 @@ class Bootstrap { templateFile: stationURL.file, }, numStationsTotal); worker.start().catch(() => {}); + numConcurrentWorkers = worker.concurrentWorkers; } } catch (error) { // eslint-disable-next-line no-console @@ -30,7 +32,7 @@ class Bootstrap { if (numStationsTotal === 0) { console.log('No charging station template enabled in configuration, exiting'); } else { - console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s)'); + console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running'); } } catch (error) { // eslint-disable-next-line no-console diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 4876f46d..69ccc318 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -102,7 +102,8 @@ export default class Utils { } static roundTo(number: number, scale: number): number { - return Utils.convertToFloat(number.toFixed(scale)); + const roundPower = Math.pow(10, scale); + return Math.round(number * roundPower) / roundPower; } static getRandomFloatRounded(max: number, min = 0, scale = 2): number {