Simplify stop transaction payload handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
index 159743a049104cd3d9d210b6a799c374c7614aa7..dfb50ab1dd67d318f251ed88a953aae67011e276 100644 (file)
@@ -1,10 +1,11 @@
 import BaseError from '../exception/BaseError';
-import { RequestCommand } from '../types/ocpp/Requests';
+import type OCPPError from '../exception/OCPPError';
+import { RequestCommand, type StatusNotificationRequest } from '../types/ocpp/Requests';
+import type { StatusNotificationResponse } from '../types/ocpp/Responses';
 import {
   AuthorizationStatus,
   StartTransactionRequest,
   StartTransactionResponse,
-  StopTransactionReason,
   StopTransactionRequest,
   StopTransactionResponse,
 } from '../types/ocpp/Transaction';
@@ -17,12 +18,16 @@ import {
 } from '../types/WorkerBroadcastChannel';
 import { ResponseStatus } from '../ui/web/src/types/UIProtocol';
 import logger from '../utils/Logger';
+import Utils from '../utils/Utils';
 import type ChargingStation from './ChargingStation';
 import WorkerBroadcastChannel from './WorkerBroadcastChannel';
 
 const moduleName = 'ChargingStationWorkerBroadcastChannel';
 
-type CommandResponse = StartTransactionResponse | StopTransactionResponse;
+type CommandResponse =
+  | StartTransactionResponse
+  | StopTransactionResponse
+  | StatusNotificationResponse;
 
 export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
   private readonly chargingStation: ChargingStation;
@@ -35,30 +40,30 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
   }
 
   private async requestHandler(messageEvent: MessageEvent): Promise<void> {
-    if (this.isResponse(messageEvent.data)) {
+    if (this.isResponse(messageEvent.data) === true) {
       return;
     }
-    this.validateMessageEvent(messageEvent);
+    const [uuid, command, requestPayload] = this.validateMessageEvent(messageEvent)
+      .data as BroadcastChannelRequest;
 
-    const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest;
-
-    if (
-      requestPayload?.hashId === undefined &&
-      (requestPayload?.hashIds as string[])?.includes(this.chargingStation.stationInfo.hashId) ===
-        false
-    ) {
-      return;
-    }
-    if (
-      requestPayload?.hashIds === undefined &&
-      requestPayload?.hashId !== this.chargingStation.stationInfo.hashId
-    ) {
-      return;
-    }
-    if (requestPayload?.hashId !== undefined) {
-      logger.warn(
-        `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead`
-      );
+    if (requestPayload?.hashIds !== undefined || requestPayload?.hashId !== undefined) {
+      if (
+        requestPayload?.hashId === undefined &&
+        requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
+      ) {
+        return;
+      }
+      if (
+        requestPayload?.hashIds === undefined &&
+        requestPayload?.hashId !== this.chargingStation.stationInfo.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;
@@ -89,6 +94,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
         commandResponse,
         errorMessage: (error as Error).message,
         errorStack: (error as Error).stack,
+        errorDetails: (error as OCPPError).details,
       };
     }
     this.sendResponse([uuid, responsePayload]);
@@ -97,7 +103,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
   private messageErrorHandler(messageEvent: MessageEvent): void {
     logger.error(
       `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`,
-      { messageEvent, messageEventData: messageEvent.data }
+      { messageEvent }
     );
   }
 
@@ -136,8 +142,8 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
             requestPayload.transactionId,
             true
           ),
-          idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId),
-          reason: StopTransactionReason.NONE,
+          idTag: requestPayload.idTag,
+          reason: requestPayload.reason,
         });
       case BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR:
         this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds);
@@ -145,6 +151,21 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
       case BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR:
         this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds);
         break;
+      case BroadcastChannelProcedureName.STATUS_NOTIFICATION:
+        return this.chargingStation.ocppRequestService.requestHandler<
+          StatusNotificationRequest,
+          StatusNotificationResponse
+        >(this.chargingStation, RequestCommand.STATUS_NOTIFICATION, {
+          connectorId: requestPayload.connectorId,
+          errorCode: requestPayload.errorCode,
+          status: requestPayload.status,
+          ...(requestPayload.info && { info: requestPayload.info }),
+          ...(requestPayload.timestamp && { timestamp: requestPayload.timestamp }),
+          ...(requestPayload.vendorId && { vendorId: requestPayload.vendorId }),
+          ...(requestPayload.vendorErrorCode && {
+            vendorErrorCode: requestPayload.vendorErrorCode,
+          }),
+        });
       default:
         // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
         throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
@@ -152,7 +173,10 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
   }
 
   private commandResponseToResponseStatus(commandResponse: CommandResponse): ResponseStatus {
-    if (commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+    if (
+      Utils.isEmptyObject(commandResponse) ||
+      commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED
+    ) {
       return ResponseStatus.SUCCESS;
     }
     return ResponseStatus.FAILURE;