Use crypto unbiased random integer generator
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorkerBroadcastChannel.ts
index 1528d65c0983580e86fb99b7e0b0f314612dd7a7..8d6cb1c62ac7ff647f1cbbf8a6cf6a240c1520ea 100644 (file)
@@ -1,3 +1,7 @@
+import type ChargingStation from './ChargingStation';
+import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
+import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
+import WorkerBroadcastChannel from './WorkerBroadcastChannel';
 import BaseError from '../exception/BaseError';
 import type OCPPError from '../exception/OCPPError';
 import { StandardParametersKey } from '../types/ocpp/Configuration';
@@ -9,6 +13,7 @@ import {
   type HeartbeatRequest,
   type MeterValuesRequest,
   RequestCommand,
+  type RequestParams,
   type StatusNotificationRequest,
 } from '../types/ocpp/Requests';
 import {
@@ -42,10 +47,6 @@ import {
 import Constants from '../utils/Constants';
 import logger from '../utils/Logger';
 import Utils from '../utils/Utils';
-import type ChargingStation from './ChargingStation';
-import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
-import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
-import WorkerBroadcastChannel from './WorkerBroadcastChannel';
 
 const moduleName = 'ChargingStationWorkerBroadcastChannel';
 
@@ -71,6 +72,9 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
 
   constructor(chargingStation: ChargingStation) {
     super();
+    const requestParams: RequestParams = {
+      throwError: true,
+    };
     this.commandHandlers = new Map<BroadcastChannelProcedureName, CommandHandler>([
       [BroadcastChannelProcedureName.START_CHARGING_STATION, () => this.chargingStation.start()],
       [
@@ -101,7 +105,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             StartTransactionRequest,
             StartTransactionResponse
-          >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload),
+          >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload, requestParams),
       ],
       [
         BroadcastChannelProcedureName.STOP_TRANSACTION,
@@ -109,13 +113,18 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             StopTransactionRequest,
             StartTransactionResponse
-          >(this.chargingStation, RequestCommand.STOP_TRANSACTION, {
-            meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
-              requestPayload.transactionId,
-              true
-            ),
-            ...requestPayload,
-          }),
+          >(
+            this.chargingStation,
+            RequestCommand.STOP_TRANSACTION,
+            {
+              meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
+                requestPayload.transactionId,
+                true
+              ),
+              ...requestPayload,
+            },
+            requestParams
+          ),
       ],
       [
         BroadcastChannelProcedureName.AUTHORIZE,
@@ -123,7 +132,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             AuthorizeRequest,
             AuthorizeResponse
-          >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload),
+          >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload, requestParams),
       ],
       [
         BroadcastChannelProcedureName.BOOT_NOTIFICATION,
@@ -141,6 +150,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
               },
               {
                 skipBufferingOnError: true,
+                throwError: true,
               }
             );
           return this.chargingStation.bootNotificationResponse;
@@ -152,7 +162,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             StatusNotificationRequest,
             StatusNotificationResponse
-          >(this.chargingStation, RequestCommand.STATUS_NOTIFICATION, requestPayload),
+          >(
+            this.chargingStation,
+            RequestCommand.STATUS_NOTIFICATION,
+            requestPayload,
+            requestParams
+          ),
       ],
       [
         BroadcastChannelProcedureName.HEARTBEAT,
@@ -160,7 +175,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             HeartbeatRequest,
             HeartbeatResponse
-          >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload),
+          >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload, requestParams),
       ],
       [
         BroadcastChannelProcedureName.METER_VALUES,
@@ -173,19 +188,26 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           return this.chargingStation.ocppRequestService.requestHandler<
             MeterValuesRequest,
             MeterValuesResponse
-          >(this.chargingStation, RequestCommand.METER_VALUES, {
-            meterValue: [
-              OCPP16ServiceUtils.buildMeterValue(
-                this.chargingStation,
-                requestPayload.connectorId,
-                this.chargingStation.getConnectorStatus(requestPayload.connectorId)?.transactionId,
-                configuredMeterValueSampleInterval
-                  ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000
-                  : Constants.DEFAULT_METER_VALUES_INTERVAL
-              ),
-            ],
-            ...requestPayload,
-          });
+          >(
+            this.chargingStation,
+            RequestCommand.METER_VALUES,
+            {
+              meterValue: [
+                // FIXME: Implement OCPP version agnostic helpers
+                OCPP16ServiceUtils.buildMeterValue(
+                  this.chargingStation,
+                  requestPayload.connectorId,
+                  this.chargingStation.getConnectorStatus(requestPayload.connectorId)
+                    ?.transactionId,
+                  configuredMeterValueSampleInterval
+                    ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000
+                    : Constants.DEFAULT_METER_VALUES_INTERVAL
+                ),
+              ],
+              ...requestPayload,
+            },
+            requestParams
+          );
         },
       ],
       [
@@ -194,7 +216,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             DataTransferRequest,
             DataTransferResponse
-          >(this.chargingStation, RequestCommand.DATA_TRANSFER, requestPayload),
+          >(this.chargingStation, RequestCommand.DATA_TRANSFER, requestPayload, requestParams),
       ],
       [
         BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION,
@@ -202,7 +224,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             DiagnosticsStatusNotificationRequest,
             DiagnosticsStatusNotificationResponse
-          >(this.chargingStation, RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, requestPayload),
+          >(
+            this.chargingStation,
+            RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+            requestPayload,
+            requestParams
+          ),
       ],
       [
         BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION,
@@ -210,7 +237,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           this.chargingStation.ocppRequestService.requestHandler<
             FirmwareStatusNotificationRequest,
             FirmwareStatusNotificationResponse
-          >(this.chargingStation, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, requestPayload),
+          >(
+            this.chargingStation,
+            RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
+            requestPayload,
+            requestParams
+          ),
       ],
     ]);
     this.chargingStation = chargingStation;