Untangle charging station worker types from the generic ones
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 26 Sep 2021 09:29:48 +0000 (11:29 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 26 Sep 2021 09:29:48 +0000 (11:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationWorker.ts
src/performance/PerformanceStatistics.ts
src/types/ChargingStationWorker.ts [new file with mode: 0644]
src/types/Worker.ts

index 10344b5130026e4eb791758d13089eb294d5272b..511a4d7a452afa6cd651bc3deae8f8426af830b9 100644 (file)
@@ -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);
           }
         }
index 81e0296d8761eca012b63085553df13615b14980..cc0f62726a174350095ade3fd4805a0204c753bd 100644 (file)
@@ -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);
     }
   });
index 9762a3a07fcaf52c822de30ffffc40c288c8347f..70f9bafdbaedcc704c37eb5ae9d8447c68d489f8 100644 (file)
@@ -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 (file)
index 0000000..f016d8f
--- /dev/null
@@ -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<WorkerMessage, 'id'> {
+  id: ChargingStationWorkerMessageEvents;
+}
index 66f6d15a78e9665ab359ae3baa123bd0fdfe2782..8f47f57818e6e8b968bc10dce0c5bc21552df56e 100644 (file)
@@ -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'
 }