UI Server: dedupe some code in helpers
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
index 0d890687828cb6b86ac450c48a3610f5e283c893..42448275432337772fe80a691a5364840680f068 100644 (file)
@@ -17,7 +17,7 @@ import {
 } from '../types/WorkerBroadcastChannel';
 import { ResponseStatus } from '../ui/web/src/type/UIProtocol';
 import logger from '../utils/Logger';
-import ChargingStation from './ChargingStation';
+import type ChargingStation from './ChargingStation';
 import WorkerBroadcastChannel from './WorkerBroadcastChannel';
 
 const moduleName = 'ChargingStationWorkerBroadcastChannel';
@@ -38,12 +38,27 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
     if (this.isResponse(messageEvent.data)) {
       return;
     }
+    this.validateMessageEvent(messageEvent);
 
     const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest;
 
-    if (requestPayload?.hashId !== this.chargingStation.hashId) {
+    if (
+      requestPayload?.hashId === undefined &&
+      (requestPayload?.hashIds as string[])?.includes(this.chargingStation.hashId) === false
+    ) {
       return;
     }
+    if (
+      requestPayload?.hashIds === undefined &&
+      requestPayload?.hashId !== this.chargingStation.hashId
+    ) {
+      return;
+    }
+    if (requestPayload?.hashId !== undefined) {
+      logger.warn(
+        `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead`
+      );
+    }
 
     let responsePayload: BroadcastChannelResponsePayload;
     let commandResponse: CommandResponse;
@@ -109,9 +124,15 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
       case BroadcastChannelProcedureName.STOP_CHARGING_STATION:
         await this.chargingStation.stop();
         break;
+      case BroadcastChannelProcedureName.OPEN_CONNECTION:
+        this.chargingStation.openWSConnection();
+        break;
+      case BroadcastChannelProcedureName.CLOSE_CONNECTION:
+        this.chargingStation.closeWSConnection();
+        break;
       default:
         // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
-        throw new BaseError(`Unknown broadcast channel command: ${command}`);
+        throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
     }
   }