From: Jérôme Benoit Date: Fri, 13 Aug 2021 20:44:30 +0000 (+0200) Subject: Ensure performance statistics is started before connection to the OCPP X-Git-Tag: v1.0.33~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7874b0b1fbf94d34e8c65ed3e669f1c97f74dd1d;p=e-mobility-charging-stations-simulator.git Ensure performance statistics is started before connection to the OCPP server. Signed-off-by: Jérôme Benoit --- diff --git a/src/assets/station-templates/evlink.station-template.json b/src/assets/station-templates/evlink.station-template.json index de6d2983..0b8e1506 100644 --- a/src/assets/station-templates/evlink.station-template.json +++ b/src/assets/station-templates/evlink.station-template.json @@ -37,6 +37,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/keba.station-template.json b/src/assets/station-templates/keba.station-template.json index 78e43df9..72ac62c4 100644 --- a/src/assets/station-templates/keba.station-template.json +++ b/src/assets/station-templates/keba.station-template.json @@ -34,6 +34,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/schneider-imredd.station-template.json b/src/assets/station-templates/schneider-imredd.station-template.json index 24f6daac..4609eac2 100644 --- a/src/assets/station-templates/schneider-imredd.station-template.json +++ b/src/assets/station-templates/schneider-imredd.station-template.json @@ -36,6 +36,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/siemens.station-template.json b/src/assets/station-templates/siemens.station-template.json index 4d78cca2..29141bf8 100644 --- a/src/assets/station-templates/siemens.station-template.json +++ b/src/assets/station-templates/siemens.station-template.json @@ -35,6 +35,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/virtual-simple-atg.station-template.json b/src/assets/station-templates/virtual-simple-atg.station-template.json index 306e03a1..79d0cb4b 100644 --- a/src/assets/station-templates/virtual-simple-atg.station-template.json +++ b/src/assets/station-templates/virtual-simple-atg.station-template.json @@ -35,6 +35,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/virtual-simple.station-template.json b/src/assets/station-templates/virtual-simple.station-template.json index 6eb68a46..d3939c8e 100644 --- a/src/assets/station-templates/virtual-simple.station-template.json +++ b/src/assets/station-templates/virtual-simple.station-template.json @@ -35,6 +35,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/assets/station-templates/virtual.station-template.json b/src/assets/station-templates/virtual.station-template.json index 31d52b59..d330a85c 100644 --- a/src/assets/station-templates/virtual.station-template.json +++ b/src/assets/station-templates/virtual.station-template.json @@ -35,6 +35,11 @@ "key": "AuthorizeRemoteTxRequests", "readonly": false, "value": "false" + }, + { + "key": "WebSocketPingInterval", + "readonly": false, + "value": "60" } ] }, diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index f5c0de9f..acfd9914 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -17,7 +17,7 @@ export default class Bootstrap { private constructor() { this.started = false; this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js'); - Configuration.setConfigurationChangeCallback(async () => await Bootstrap.getInstance().restart()); + Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart()); } public static getInstance(): Bootstrap { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 79c9e65a..4fc275bc 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -298,6 +298,9 @@ export default class ChargingStation { } public start(): void { + if (this.getEnableStatistics()) { + this.performanceStatistics.start(); + } this.openWSConnection(); // Monitor authorization file this.startAuthorizationFileMonitoring(); @@ -329,18 +332,20 @@ export default class ChargingStation { if (this.isWebSocketOpen()) { this.wsConnection.close(); } + if (this.getEnableStatistics()) { + this.performanceStatistics.stop(); + } this.bootNotificationResponse = null; this.hasStopped = true; } public getConfigurationKey(key: string | StandardParametersKey, caseInsensitive = false): ConfigurationKey | undefined { - const configurationKey: ConfigurationKey | undefined = this.configuration.configurationKey.find((configElement) => { + return this.configuration.configurationKey.find((configElement) => { if (caseInsensitive) { return configElement.key.toLowerCase() === key.toLowerCase(); } return configElement.key === key; }); - return configurationKey; } public addConfigurationKey(key: string | StandardParametersKey, value: string, readonly = false, visible = true, reboot = false): void { @@ -716,7 +721,7 @@ export default class ChargingStation { } private getTemplateChargingStationConfiguration(): ChargingStationConfiguration { - return this.stationInfo.Configuration ? this.stationInfo.Configuration : {} as ChargingStationConfiguration; + return this.stationInfo.Configuration ?? {} as ChargingStationConfiguration; } private getAuthorizationFile(): string | undefined { @@ -840,9 +845,6 @@ export default class ChargingStation { } // Start the ATG this.startAutomaticTransactionGenerator(); - if (this.getEnableStatistics()) { - this.performanceStatistics.start(); - } } private startAutomaticTransactionGenerator() { @@ -1012,6 +1014,8 @@ export default class ChargingStation { } private async reconnect(error: any): Promise { + // Stop WebSocket ping + this.stopWebSocketPing(); // Stop heartbeat this.stopHeartbeat(); // Stop the ATG if needed diff --git a/src/utils/PerformanceStatistics.ts b/src/utils/PerformanceStatistics.ts index e7cd58e8..49f75ade 100644 --- a/src/utils/PerformanceStatistics.ts +++ b/src/utils/PerformanceStatistics.ts @@ -75,7 +75,9 @@ export default class PerformanceStatistics { } public stop(): void { - clearInterval(this.displayInterval); + if (this.displayInterval) { + clearInterval(this.displayInterval); + } performance.clearMarks(); this.performanceObserver.disconnect(); } diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index 9141c2f0..8ab960f3 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -4,6 +4,7 @@ import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; import { WorkerData } from '../types/Worker'; +import { WorkerUtils } from './WorkerUtils'; export default class WorkerDynamicPool extends WorkerAbstract { private pool: DynamicPool; @@ -70,11 +71,7 @@ class DynamicPool extends DynamicThreadPool { public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions): DynamicPool { if (!DynamicPool.instance) { - opts.exitHandler = opts?.exitHandler ?? ((code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - }); + opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; DynamicPool.instance = new DynamicPool(min, max, workerScript, opts); } return DynamicPool.instance; diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 2ccd2796..9a6f457d 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -3,6 +3,7 @@ import { WorkerEvents, WorkerSetElement } from '../types/Worker'; import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; +import { WorkerUtils } from './WorkerUtils'; export default class WorkerSet extends WorkerAbstract { public maxElementsPerWorker: number; @@ -76,9 +77,7 @@ export default class WorkerSet extends WorkerAbstract { worker.on('message', () => { }); worker.on('error', () => { }); worker.on('exit', (code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } + WorkerUtils.defaultExitHandler(code); this.workerSet.delete(this.getWorkerSetElementByWorker(worker)); }); this.workerSet.add({ worker, numberOfWorkerElements: 0 }); diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 7a304b70..cf46cfa5 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -4,6 +4,7 @@ import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; import { WorkerData } from '../types/Worker'; +import { WorkerUtils } from './WorkerUtils'; export default class WorkerStaticPool extends WorkerAbstract { private pool: StaticPool; @@ -68,11 +69,7 @@ class StaticPool extends FixedThreadPool { public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions): StaticPool { if (!StaticPool.instance) { - opts.exitHandler = opts?.exitHandler ?? ((code) => { - if (code !== 0) { - console.error(`Worker stopped with exit code ${code}`); - } - }); + opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler; StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts); } return StaticPool.instance; diff --git a/src/worker/WorkerUtils.ts b/src/worker/WorkerUtils.ts new file mode 100644 index 00000000..7db9de9e --- /dev/null +++ b/src/worker/WorkerUtils.ts @@ -0,0 +1,7 @@ +export class WorkerUtils { + public static defaultExitHandler = (code: number): void => { + if (code !== 0) { + console.error(`Worker stopped with exit code ${code}`); + } + }; +}