refactor: cleanup log messages
[e-mobility-charging-stations-simulator.git] / src / charging-station / broadcast-channel / ChargingStationWorkerBroadcastChannel.ts
index a7c6e4ad867331a82979cd188df36c69f60e854b..422c964bcbedd232d28f1a7005efc09c7e5e989b 100644 (file)
@@ -15,6 +15,7 @@ import {
   DataTransferStatus,
   type DiagnosticsStatusNotificationRequest,
   type DiagnosticsStatusNotificationResponse,
+  type EmptyObject,
   type FirmwareStatusNotificationRequest,
   type FirmwareStatusNotificationResponse,
   type HeartbeatRequest,
@@ -36,22 +37,19 @@ import {
 } from '../../types';
 import { Constants, convertToInt, isEmptyObject, isNullOrUndefined, logger } from '../../utils';
 import type { ChargingStation } from '../ChargingStation';
-import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils';
+import { getConfigurationKey } from '../ChargingStationConfigurationUtils';
 import { OCPP16ServiceUtils } from '../ocpp';
 
 const moduleName = 'ChargingStationWorkerBroadcastChannel';
 
 type CommandResponse =
+  | EmptyObject
   | StartTransactionResponse
   | StopTransactionResponse
   | AuthorizeResponse
   | BootNotificationResponse
-  | StatusNotificationResponse
   | HeartbeatResponse
-  | MeterValuesResponse
-  | DataTransferResponse
-  | DiagnosticsStatusNotificationResponse
-  | FirmwareStatusNotificationResponse;
+  | DataTransferResponse;
 
 type CommandHandler = (
   requestPayload?: BroadcastChannelRequestPayload,
@@ -114,7 +112,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
             RequestCommand.STOP_TRANSACTION,
             {
               meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
-                requestPayload.transactionId,
+                requestPayload!.transactionId!,
                 true,
               ),
               ...requestPayload,
@@ -176,11 +174,10 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
       [
         BroadcastChannelProcedureName.METER_VALUES,
         async (requestPayload?: BroadcastChannelRequestPayload) => {
-          const configuredMeterValueSampleInterval =
-            ChargingStationConfigurationUtils.getConfigurationKey(
-              chargingStation,
-              StandardParametersKey.MeterValueSampleInterval,
-            );
+          const configuredMeterValueSampleInterval = getConfigurationKey(
+            chargingStation,
+            StandardParametersKey.MeterValueSampleInterval,
+          );
           return this.chargingStation.ocppRequestService.requestHandler<
             MeterValuesRequest,
             MeterValuesResponse
@@ -192,9 +189,9 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
                 // FIXME: Implement OCPP version agnostic helpers
                 OCPP16ServiceUtils.buildMeterValue(
                   this.chargingStation,
-                  requestPayload.connectorId,
-                  this.chargingStation.getConnectorStatus(requestPayload.connectorId)
-                    ?.transactionId,
+                  requestPayload!.connectorId!,
+                  this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)!
+                    .transactionId!,
                   configuredMeterValueSampleInterval
                     ? convertToInt(configuredMeterValueSampleInterval.value) * 1000
                     : Constants.DEFAULT_METER_VALUES_INTERVAL,
@@ -242,8 +239,8 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
       ],
     ]);
     this.chargingStation = chargingStation;
-    this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
-    this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void;
+    this.onmessage = this.requestHandler.bind(this) as (message: unknown) => void;
+    this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void;
   }
 
   private async requestHandler(messageEvent: MessageEvent): Promise<void> {
@@ -256,19 +253,19 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
     }
     const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest;
     if (
-      !isNullOrUndefined(requestPayload?.hashIds) &&
-      requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
+      !isNullOrUndefined(requestPayload.hashIds) &&
+      requestPayload.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
     ) {
       return;
     }
-    if (!isNullOrUndefined(requestPayload?.hashId)) {
+    if (!isNullOrUndefined(requestPayload.hashId)) {
       logger.error(
         `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`,
       );
       return;
     }
-    let responsePayload: BroadcastChannelResponsePayload;
-    let commandResponse: CommandResponse | void;
+    let responsePayload: BroadcastChannelResponsePayload | undefined;
+    let commandResponse: CommandResponse | void | undefined;
     try {
       commandResponse = await this.commandHandler(command, requestPayload);
       if (isNullOrUndefined(commandResponse) || isEmptyObject(commandResponse as CommandResponse)) {
@@ -299,7 +296,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
         errorDetails: (error as OCPPError).details,
       };
     } finally {
-      this.sendResponse([uuid, responsePayload]);
+      this.sendResponse([uuid, responsePayload!]);
     }
   }
 
@@ -316,7 +313,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
   ): Promise<CommandResponse | void> {
     if (this.commandHandlers.has(command) === true) {
       this.cleanRequestPayload(command, requestPayload);
-      return this.commandHandlers.get(command)(requestPayload);
+      return this.commandHandlers.get(command)!(requestPayload);
     }
     throw new BaseError(`Unknown worker broadcast channel command: ${command}`);
   }