]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: cleanup up internal messaging code
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 17 Aug 2025 18:41:32 +0000 (20:41 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 17 Aug 2025 18:41:32 +0000 (20:41 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts
src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts

index 649c057f24482a6799798a3fadb3144878014542..eb591836d2d5e9c4634a062fe2e0e00568e820f1 100644 (file)
@@ -173,6 +173,9 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
             chargingStation,
             StandardParametersKey.MeterValueSampleInterval
           )
+          const connectorId = requestPayload?.connectorId
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          const transactionId = this.chargingStation.getConnectorStatus(connectorId!)?.transactionId
           return await this.chargingStation.ocppRequestService.requestHandler<
             MeterValuesRequest,
             MeterValuesResponse
@@ -184,10 +187,9 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
                 buildMeterValue(
                   this.chargingStation,
                   // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                  requestPayload!.connectorId!,
+                  connectorId!,
                   // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-                  this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)!
-                    .transactionId!,
+                  transactionId!,
                   configuredMeterValueSampleInterval != null
                     ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value))
                     : Constants.DEFAULT_METER_VALUES_INTERVAL
@@ -208,7 +210,13 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
       [
         BroadcastChannelProcedureName.SET_SUPERVISION_URL,
         (requestPayload?: BroadcastChannelRequestPayload) => {
-          this.chargingStation.setSupervisionUrl(requestPayload?.url as string)
+          const url = requestPayload?.url
+          if (typeof url !== 'string' || isEmpty(url)) {
+            throw new BaseError(
+              `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'url' field is required`
+            )
+          }
+          this.chargingStation.setSupervisionUrl(url)
         },
       ],
       [
@@ -266,7 +274,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
         async (requestPayload?: BroadcastChannelRequestPayload) =>
           await this.chargingStation.ocppRequestService.requestHandler<
             StopTransactionRequest,
-            StartTransactionResponse
+            StopTransactionResponse
           >(
             this.chargingStation,
             RequestCommand.STOP_TRANSACTION,
@@ -416,6 +424,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
       return
     }
     let responsePayload: BroadcastChannelResponsePayload | undefined
+    // eslint-disable-next-line promise/catch-or-return
     this.commandHandler(command, requestPayload)
       .then(commandResponse => {
         if (commandResponse == null || isEmpty(commandResponse)) {
@@ -432,10 +441,6 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
         }
         return undefined
       })
-      .finally(() => {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        this.sendResponse([uuid, responsePayload!])
-      })
       .catch((error: unknown) => {
         logger.error(
           `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`,
@@ -450,6 +455,11 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
           requestPayload,
           status: ResponseStatus.FAILURE,
         } satisfies BroadcastChannelResponsePayload
+        return undefined
+      })
+      .finally(() => {
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        this.sendResponse([uuid, responsePayload!])
       })
   }
 }
index 12bc6824871d0ba10d42e252200e44ebd0a8111d..b1bd85c6f0369e3a425464c752132aa4e931c74f 100644 (file)
@@ -31,51 +31,44 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
   }
 
   private buildResponsePayload (uuid: string): ResponsePayload {
+    const responsesArray = this.responses.get(uuid)?.responses ?? []
     const responsesStatus =
-      this.responses
-        .get(uuid)
-        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
-        ?.responses.every(response => response?.status === ResponseStatus.SUCCESS) === true
+      responsesArray.length > 0 &&
+      responsesArray.every(response => response.status === ResponseStatus.SUCCESS)
         ? ResponseStatus.SUCCESS
         : ResponseStatus.FAILURE
     return {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
-      hashIdsSucceeded: this.responses
-        .get(uuid)
-        ?.responses.map(response => {
+      hashIdsSucceeded: responsesArray
+        .map(response => {
           // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
           if (response?.hashId != null && response?.status === ResponseStatus.SUCCESS) {
             return response.hashId
           }
           return undefined
         })
-        .filter(hashId => hashId != null)!,
+        .filter((hashId): hashId is string => hashId != null),
       status: responsesStatus,
       ...(responsesStatus === ResponseStatus.FAILURE && {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
-        hashIdsFailed: this.responses
-          .get(uuid)
-          ?.responses.map(response => {
+        hashIdsFailed: responsesArray
+          .map(response => {
             // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
             if (response?.hashId != null && response?.status === ResponseStatus.FAILURE) {
               return response.hashId
             }
             return undefined
           })
-          .filter(hashId => hashId != null)!,
+          .filter((hashId): hashId is string => hashId != null),
       }),
       ...(responsesStatus === ResponseStatus.FAILURE && {
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
-        responsesFailed: this.responses
-          .get(uuid)
-          ?.responses.map(response => {
+        responsesFailed: responsesArray
+          .map(response => {
             // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
             if (response?.status === ResponseStatus.FAILURE) {
               return response
             }
             return undefined
           })
-          .filter(response => response != null)!,
+          .filter((response): response is BroadcastChannelResponsePayload => response != null),
       }),
     }
   }
@@ -102,17 +95,22 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
         responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid),
         responsesReceived: 1,
       })
-    } else if (
+    } else {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      this.responses.get(uuid)!.responsesReceived <= this.responses.get(uuid)!.responsesExpected
-    ) {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      ++this.responses.get(uuid)!.responsesReceived
-      this.responses.get(uuid)?.responses.push(responsePayload)
+      const responses = this.responses.get(uuid)!
+      if (responses.responsesReceived < responses.responsesExpected) {
+        ++responses.responsesReceived
+        responses.responses.push(responsePayload)
+      } else {
+        logger.debug(
+          `${this.uiService.logPrefix(moduleName, 'responseHandler')} Received response after all expected responses:`,
+          { responsePayload, uuid }
+        )
+      }
     }
-    if (
-      this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected
-    ) {
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    const responses = this.responses.get(uuid)!
+    if (responses.responsesReceived >= responses.responsesExpected) {
       this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid))
       this.responses.delete(uuid)
       this.uiService.deleteBroadcastChannelRequest(uuid)
index cb371457816222c32a53038c17f5318b67577385..0846ed05b770c059d531d5e4eae6d6b1fec79e1d 100644 (file)
@@ -33,7 +33,8 @@ export abstract class WorkerBroadcastChannel extends BroadcastChannel {
   }
 
   protected validateMessageEvent (messageEvent: MessageEvent): false | MessageEvent {
-    if (!Array.isArray(messageEvent.data)) {
+    const data = messageEvent.data
+    if (!Array.isArray(data)) {
       logger.error(
         `${this.logPrefix(
           moduleName,
@@ -42,7 +43,7 @@ export abstract class WorkerBroadcastChannel extends BroadcastChannel {
       )
       return false
     }
-    if (!validateUUID(messageEvent.data[0])) {
+    if (!validateUUID(data[0])) {
       logger.error(
         `${this.logPrefix(
           moduleName,