Cleanup Array.from() usage
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
index 18cbadefdaf63d4915e6bb497abe0628072949c2..6fd226fa326632d42ef251ae4e59707207c9590f 100644 (file)
@@ -15,7 +15,7 @@ import {
   BroadcastChannelResponsePayload,
   MessageEvent,
 } from '../types/WorkerBroadcastChannel';
-import { ResponseStatus } from '../ui/web/src/type/UIProtocol';
+import { ResponseStatus } from '../ui/web/src/types/UIProtocol';
 import logger from '../utils/Logger';
 import type ChargingStation from './ChargingStation';
 import WorkerBroadcastChannel from './WorkerBroadcastChannel';
@@ -38,21 +38,42 @@ 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;
     try {
       commandResponse = await this.commandHandler(command, requestPayload);
       if (commandResponse === undefined) {
-        responsePayload = { status: ResponseStatus.SUCCESS };
+        responsePayload = {
+          hashId: this.chargingStation.hashId,
+          status: ResponseStatus.SUCCESS,
+        };
       } else {
-        responsePayload = { status: this.commandResponseToResponseStatus(commandResponse) };
+        responsePayload = {
+          hashId: this.chargingStation.hashId,
+          status: this.commandResponseToResponseStatus(commandResponse),
+        };
       }
     } catch (error) {
       logger.error(
@@ -60,6 +81,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
         error
       );
       responsePayload = {
+        hashId: this.chargingStation.hashId,
         status: ResponseStatus.FAILURE,
         command,
         requestPayload,
@@ -117,7 +139,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
         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}`);
     }
   }