fix: ensure configuration key visibility test does not alter configuration
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 30 Dec 2023 14:31:06 +0000 (15:31 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 30 Dec 2023 14:31:06 +0000 (15:31 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 54741e67b8780439e69528af6860a88b9c29a2e8..e2e8dcba7ed846a4de3ecf8a25bc84bca00e11f4 100644 (file)
@@ -502,27 +502,21 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     const unknownKey: string[] = []
     if (key == null) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      for (const configuration of chargingStation.ocppConfiguration!.configurationKey!) {
-        if (configuration.visible == null) {
-          configuration.visible = true
-        }
-        if (!configuration.visible) {
+      for (const configKey of chargingStation.ocppConfiguration!.configurationKey!) {
+        if (!OCPP16ServiceUtils.isConfigurationKeyVisible(configKey)) {
           continue
         }
         configurationKey.push({
-          key: configuration.key,
-          readonly: configuration.readonly,
-          value: configuration.value
+          key: configKey.key,
+          readonly: configKey.readonly,
+          value: configKey.value
         })
       }
     } else if (isNotEmptyArray(key)) {
       for (const k of key) {
         const keyFound = getConfigurationKey(chargingStation, k, true)
         if (keyFound != null) {
-          if (keyFound.visible == null) {
-            keyFound.visible = true
-          }
-          if (!keyFound.visible) {
+          if (!OCPP16ServiceUtils.isConfigurationKeyVisible(keyFound)) {
             continue
           }
           configurationKey.push({
index 2d31c0512d9503cad411659805a3ae378054b70f..d99e721e2d153a58a55b56090dc5731ffaef6da3 100644 (file)
@@ -18,6 +18,7 @@ import {
   hasReservationExpired
 } from '../../../charging-station/index.js'
 import {
+  type ConfigurationKey,
   type GenericResponse,
   type JsonType,
   OCPP16AuthorizationStatus,
@@ -37,7 +38,7 @@ import {
   type OCPP16SupportedFeatureProfiles,
   OCPPVersion
 } from '../../../types/index.js'
-import { isNotEmptyArray, logger, roundTo } from '../../../utils/index.js'
+import { cloneObject, isNotEmptyArray, logger, roundTo } from '../../../utils/index.js'
 import { OCPPServiceUtils } from '../OCPPServiceUtils.js'
 
 export class OCPP16ServiceUtils extends OCPPServiceUtils {
@@ -431,6 +432,14 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     }
   }
 
+  public static isConfigurationKeyVisible (key: ConfigurationKey): boolean {
+    if (key.visible == null) {
+      key = cloneObject(key)
+      key.visible = true
+    }
+    return key.visible
+  }
+
   public static hasReservation = (
     chargingStation: ChargingStation,
     connectorId: number,