Jelajahi Sumber

remove public Station access, prevent unexpect change by other process

Robert 1 tahun lalu
induk
melakukan
af26e83dd2

+ 34 - 10
EVCB_OCPP.WSServer/Service/StationConfigService.cs

@@ -41,6 +41,8 @@ public class StationConfigService
         this.logger = logger;
     }
 
+    private static string Session_Station_Key = "StationConfigService_Station";
+
     private readonly ProtalServer portalServer;
     private readonly WebDbService webDbService;
     private readonly ServerMessageService messageService;
@@ -63,9 +65,10 @@ public class StationConfigService
             return;
         }
 
-        if (session.StationId != stationId)
+        int? sessionStationId = GetSessionStation(session);
+        if (sessionStationId != stationId)
         {
-            session.StationId = stationId;
+            SetSessionStation(session, stationId);
             await UpdateEvseConfig(chargeBoxId, stationId.Value, token);
         }
         return;
@@ -97,13 +100,14 @@ public class StationConfigService
 
         stationConfigRecord = dbStationEvseConfig;
 
-        var connectedEvses = portalServer.GetClientDic().Values;
-        foreach (var evse in connectedEvses)
+        Dictionary<string, WsClientData>.ValueCollection connectedEvses = portalServer.GetClientDic().Values;
+        foreach (WsClientData evse in connectedEvses)
         {
-            if (evse.StationId is not null &&
-                modifiedStations.Contains(evse.StationId.Value))
+            int? sessionStationId = GetSessionStation(evse);
+            if (sessionStationId is not null &&
+                modifiedStations.Contains(sessionStationId.Value))
             {
-                await UpdateEvseConfig(evse.ChargeBoxId, evse.StationId.Value);
+                await UpdateEvseConfig(evse.ChargeBoxId, sessionStationId.Value);
             }
         }
     }
@@ -119,13 +123,14 @@ public class StationConfigService
             int? currentStation = evseStationPair.ContainsKey(evse.ChargeBoxId) ? evseStationPair[evse.ChargeBoxId] : null;
             if (currentStation is null)
             {
-                evse.StationId = null;
+                SetSessionStation(evse, null);
                 continue;
             }
 
-            if (evse.StationId != currentStation)
+            int? sessionStationId = GetSessionStation(evse);
+            if (sessionStationId != currentStation)
             {
-                evse.StationId = currentStation;
+                sessionStationId = currentStation;
                 await UpdateEvseConfig(evse.ChargeBoxId, currentStation.Value);
             }
         }
@@ -207,4 +212,23 @@ public class StationConfigService
                   d1KV.Value?.Equals(d2Value) == true)
         );
     }
+
+    private int? GetSessionStation(WsClientData session)
+    {
+        if (session is null ||
+            !session.Data.ContainsKey(Session_Station_Key))
+        {
+            return null;
+        }
+        return (int?)session.Data[Session_Station_Key];
+    }
+
+    private void SetSessionStation(WsClientData session, int? stationId)
+    {
+        if (session is null)
+        {
+            return;
+        }
+        session.Data[Session_Station_Key] = stationId;
+    }
 }

+ 2 - 1
EVCB_OCPP.WSServer/Service/WsService/WsClientData.cs

@@ -82,7 +82,8 @@ public class WsClientData : WsSession
 
     public string CustomerName { get; set; }
 
-    public int? StationId { set; get; } = null;
+    //public int? StationId { set; get; } = null;
+    public Dictionary<string, object> Data = new Dictionary<string, object>();
 
     public event EventHandler<string> m_ReceiveData;