-import { AuthorizationStatus, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction';
+import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction';
import { PerformanceObserver, performance } from 'perf_hooks';
import ChargingStation from './ChargingStation';
if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
skip = 0;
// Start transaction
- let startResponse: StartTransactionResponse;
+ let startResponse: StartTransactionResponse | AuthorizeResponse;
if (this._chargingStation.getEnableStatistics()) {
const startTransaction = performance.timerify(this.startTransaction);
this._performanceObserver.observe({ entryTypes: ['function'] });
}
// eslint-disable-next-line consistent-this
- private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse> {
+ private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse | AuthorizeResponse> {
if (self._chargingStation.hasAuthorizedTags()) {
const tagId = self._chargingStation.getRandomTagId();
if (self._chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) {
// Start transaction
return await self._chargingStation.sendStartTransaction(connectorId, tagId);
}
- return authorizeResponse as StartTransactionResponse;
+ return authorizeResponse;
}
logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
// Start transaction
export default class Wrk {
private _workerScript: string;
private _workerData: WorkerData;
- private _index: number;
- private _maxWorkerElements: number;
private _worker: Worker;
/**
*
* @param {string} workerScript
* @param {WorkerData} workerData
- * @param {number} maxWorkerElements
*/
- constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) {
+ constructor(workerScript: string, workerData: WorkerData) {
this._workerData = workerData;
- this._index = workerData.index;
this._workerScript = workerScript;
if (Configuration.useWorkerPool()) {
WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize();
}
- this._maxWorkerElements = maxWorkerElements;
- }
-
- /**
- * @return {number}
- * @public
- */
- public get maxWorkerElements(): number {
- return this._maxWorkerElements;
}
/**
return;
}
this._workerData = workerData;
- this._index = workerData.index;
this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData });
}
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
- reject(new Error(`Worker id ${this._index} stopped with exit code ${code}`));
+ reject(new Error(`Worker stopped with exit code ${code}`));
}
});
this._worker = worker;
try {
let numStationsTotal = 0;
let numConcurrentWorkers = 0;
+ const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
+ let chargingStationsPerWorkerCounter = 0;
let worker: Wrk;
// Start each ChargingStation object in a worker thread
if (Configuration.getStationTemplateURLs()) {
- for await (const stationURL of Configuration.getStationTemplateURLs()) {
+ for (const stationURL of Configuration.getStationTemplateURLs()) {
try {
const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
numStationsTotal += nbStations;
worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
worker.start().catch(() => { });
numConcurrentWorkers = Configuration.getWorkerPoolSize();
- } else {
- const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
- let chargingStationsPerWorkerCounter = 0;
- if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker) {
- // Start new Wrk with one charging station
- worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
- worker.start().catch(() => { });
- numConcurrentWorkers++;
- chargingStationsPerWorkerCounter = 1;
- // Start Wrk sequentially to optimize memory at start time
- await Utils.sleep(Constants.START_WORKER_DELAY);
- } else {
- // Add charging station to existing Wrk
- worker.addWorkerElement(workerData);
- chargingStationsPerWorkerCounter++;
- }
+ } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) {
+ // Start new Wrk with one charging station
+ worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
+ worker.start().catch(() => { });
+ numConcurrentWorkers++;
+ chargingStationsPerWorkerCounter = 1;
+ // Start Wrk sequentially to optimize memory at start time
+ await Utils.sleep(Constants.START_WORKER_DELAY);
+ } else if (!Configuration.useWorkerPool()) {
+ // Add charging station to existing Wrk
+ worker.addWorkerElement(workerData);
+ chargingStationsPerWorkerCounter++;
}
}
} catch (error) {
}
}
-Bootstrap.start();
+Bootstrap.start().catch(
+ (error) => {
+ console.error(error);
+ }
+);