From 98dc07faaf264c0263d2c8de382efa9db59cd5b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 26 Sep 2021 11:29:48 +0200 Subject: [PATCH] Untangle charging station worker types from the generic ones MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 6 ++--- src/charging-station/ChargingStationWorker.ts | 6 ++--- src/performance/PerformanceStatistics.ts | 4 ++-- src/types/ChargingStationWorker.ts | 22 +++++++++++++++++++ src/types/Worker.ts | 8 +------ 5 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/types/ChargingStationWorker.ts diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 10344b51..511a4d7a 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -1,6 +1,6 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. -import { ChargingStationWorkerData, WorkerMessage, WorkerMessageEvents } from '../types/Worker'; +import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import Configuration from '../utils/Configuration'; import { Storage } from '../performance/storage/Storage'; @@ -103,8 +103,8 @@ export default class Bootstrap { poolOptions: { workerChoiceStrategy: Configuration.getWorkerPoolStrategy() }, - messageHandler: async (msg: WorkerMessage) => { - if (msg.id === WorkerMessageEvents.PERFORMANCE_STATISTICS) { + messageHandler: async (msg: ChargingStationWorkerMessage) => { + if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) { await Bootstrap.storage.storePerformanceStatistics(msg.data); } } diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 81e0296d..cc0f6272 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -1,6 +1,6 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. -import { ChargingStationWorkerData, WorkerMessage, WorkerMessageEvents } from '../types/Worker'; +import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import { parentPort, workerData } from 'worker_threads'; import ChargingStation from './ChargingStation'; @@ -24,8 +24,8 @@ if (Utils.workerPoolInUse()) { * Listen messages send by the main thread */ function addMessageListener(): void { - parentPort?.on('message', (message: WorkerMessage) => { - if (message.id === WorkerMessageEvents.START_WORKER_ELEMENT) { + parentPort?.on('message', (message: ChargingStationWorkerMessage) => { + if (message.id === ChargingStationWorkerMessageEvents.START_WORKER_ELEMENT) { startChargingStation(message.data); } }); diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 9762a3a0..70f9bafd 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -5,11 +5,11 @@ import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests'; import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks'; import Statistics, { StatisticsData } from '../types/Statistics'; +import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import Configuration from '../utils/Configuration'; import { MessageType } from '../types/ocpp/MessageType'; import { URL } from 'url'; import Utils from '../utils/Utils'; -import { WorkerMessageEvents } from '../types/Worker'; import logger from '../utils/Logger'; import { parentPort } from 'worker_threads'; @@ -192,7 +192,7 @@ export default class PerformanceStatistics { this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95); this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries); if (Configuration.getPerformanceStorage().enabled) { - parentPort.postMessage({ id: WorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics }); + parentPort.postMessage({ id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics }); } } diff --git a/src/types/ChargingStationWorker.ts b/src/types/ChargingStationWorker.ts new file mode 100644 index 00000000..f016d8ff --- /dev/null +++ b/src/types/ChargingStationWorker.ts @@ -0,0 +1,22 @@ +import { WorkerData, WorkerMessage, WorkerMessageEvents } from './Worker'; + +export interface ChargingStationWorkerData extends WorkerData { + index: number; + templateFile: string; +} + +enum InternalChargingStationWorkerMessageEvents { + PERFORMANCE_STATISTICS = 'performanceStatistics' +} + +export type ChargingStationWorkerMessageEvents = WorkerMessageEvents | InternalChargingStationWorkerMessageEvents; + +export const ChargingStationWorkerMessageEvents = { + ...WorkerMessageEvents, + ...InternalChargingStationWorkerMessageEvents +}; + + +export interface ChargingStationWorkerMessage extends Omit { + id: ChargingStationWorkerMessageEvents; +} diff --git a/src/types/Worker.ts b/src/types/Worker.ts index 66f6d15a..8f47f578 100644 --- a/src/types/Worker.ts +++ b/src/types/Worker.ts @@ -19,11 +19,6 @@ export interface WorkerOptions { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface WorkerData {} -export interface ChargingStationWorkerData extends WorkerData { - index: number; - templateFile: string; -} - export interface WorkerSetElement { worker: Worker; numberOfWorkerElements: number; @@ -36,7 +31,6 @@ export interface WorkerMessage { export enum WorkerMessageEvents { START_WORKER_ELEMENT = 'startWorkerElement', - STOP_WORKER_ELEMENT = 'stopWorkerElement', - PERFORMANCE_STATISTICS = 'performanceStatistics' + STOP_WORKER_ELEMENT = 'stopWorkerElement' } -- 2.34.1