Robert 1 년 전
부모
커밋
9caaab534a

+ 6 - 2
EVCB_OCPP.WSServer/HostedProtalServer.cs

@@ -34,16 +34,20 @@ namespace EVCB_OCPP.WSServer
             //services.AddTransient<OCPPWSServer>();
             //services.AddTransient<IOCPPWSServerFactory, OCPPWSServerFactory>();
             services.AddHeaderRecordService();
-            services.AddOcppWsServer();
 
-            services.AddSingleton<ServerMessageService>();
             services.AddSingleton<MeterValueDbService>();
             services.AddSingleton<WebDbService>();
             services.AddSingleton<IMainDbService, MainDbService>();
             services.AddSingleton<IConnectionLogdbService, ConnectionLogdbService>();
 
+            services.AddOcppWsServer();
+
+            services.AddSingleton<ServerMessageService>();
+            services.AddSingleton<StationConfigService>();
+
             services.AddSingleton<ConfirmWaitingMessageSerevice>();
             services.AddTransient<ProfileHandler>();
+
             services.AddSingleton<ProtalServer>();
             services.AddHostedService<ProtalServer>(p => p.GetRequiredService<ProtalServer>());
 

+ 4 - 53
EVCB_OCPP.WSServer/ProtalServer.cs

@@ -70,6 +70,7 @@ namespace EVCB_OCPP.WSServer
             , IServiceProvider serviceProvider
             , OcppWebsocketService websocketService
             , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
+            , StationConfigService stationConfigService
             , OuterHttpClient httpClient)
         {
             _ct = _cts.Token;
@@ -85,6 +86,7 @@ namespace EVCB_OCPP.WSServer
             this.messageService = serverMessageService;
             this.websocketService = websocketService;
             this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
+            this.stationConfigService = stationConfigService;
             this.httpClient = httpClient;
             isInDocker = !string.IsNullOrEmpty(configuration["DOTNET_RUNNING_IN_CONTAINER"]);
 
@@ -115,6 +117,7 @@ namespace EVCB_OCPP.WSServer
         private readonly ServerMessageService messageService;
         private readonly OcppWebsocketService websocketService;
         private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
+        private readonly StationConfigService stationConfigService;
         private readonly ProfileHandler profileHandler;//= new ProfileHandler();
         //private readonly string webConnectionString;// = ConfigurationManager.ConnectionStrings["WebDBContext"].ConnectionString;
         private readonly bool isInDocker;
@@ -1038,7 +1041,7 @@ namespace EVCB_OCPP.WSServer
                     session.ChargeBoxId, key: "StopTransactionOnInvalidId", value: "True");
             }
 
-            await GetnChecknUpdateConfig(session.ChargeBoxId, session.DisconnetCancellationToken);
+            await stationConfigService.CheckAndUpdateEvseConfig(session.ChargeBoxId, session.DisconnetCancellationToken);
 
             sendTask = async () => await messageService.SendDataTransferRequest(
                 session.ChargeBoxId,
@@ -1048,58 +1051,6 @@ namespace EVCB_OCPP.WSServer
             await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, session.DisconnetCancellationToken);
         }
 
-        internal async Task GetnChecknUpdateConfig(string chargeBoxId, CancellationToken token = default)
-        {
-            MessageResult response = await GetEvseCurrentConfig(chargeBoxId, token);
-            if (!response.Success || response.Message is not GetConfigurationConfirmation confirmation)
-            {
-                return;
-            }
-
-            Dictionary<string,string> configs = await GetEvseDBCurrentConfig(chargeBoxId, token);
-            if (configs == null)
-            {
-                logger.LogInformation("{0} get station config failed", chargeBoxId);
-                return;
-            }
-
-            await ComparenUpdateConfig(chargeBoxId, 
-                evseCurrentConfigs: confirmation.configurationKey.ToDictionary(x => x.key, x => x.value), 
-                evseDbConfigs: configs, 
-                token);
-        }
-
-        private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
-        {
-            var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
-            var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
-            return response;
-        }
-
-        private Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
-        {
-            return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
-        }
-
-        internal async Task ComparenUpdateConfig(string chargeBoxId, 
-            Dictionary<string,string> evseCurrentConfigs,
-            Dictionary<string, string> evseDbConfigs,
-            CancellationToken token = default)
-        {
-            foreach(var config in evseDbConfigs)
-            {
-                if (evseCurrentConfigs.Keys.Contains(config.Key) &&
-                    evseCurrentConfigs[config.Key] == config.Value)
-                {
-                    continue;
-                }
-
-                MessageResult response = null;
-                var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
-                response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
-            }
-        }
-
         private void Send(WsClientData session, string msg, string messageType, string errorMsg = "")
         {
             try

+ 19 - 0
EVCB_OCPP.WSServer/Service/DbService/WebDbService.cs

@@ -144,6 +144,25 @@ public class WebDbService
         return configs.ToDictionary(x=>x.ConfigureName, x=>x.ConfigureSetting);
     }
 
+    internal async Task<Dictionary<string, string>> GetEvseStationConfig(int stationId, CancellationToken token = default)
+    {
+
+    }
+
+    internal async Task<int?> GetEvseStation(string chargeboxIds, CancellationToken token = default)
+    {
+        string getStationStrSql = """
+                SELECT [StationId]
+                FROM [dbo].[StationMachine]
+                WHERE [ChargeBoxId] = @ChargeBoxIds;
+                """;
+        var parameters = new DynamicParameters();
+        parameters.Add("@ChargeBoxIds", chargeboxIds, direction: ParameterDirection.Input, size: 25);
+        using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
+        var stationId = await conn.QuerySingleAsync<int?>(new CommandDefinition(getStationStrSql, parameters, token));
+        return stationId;
+    }
+
     internal async Task<Dictionary<string, int>> GetEvseStationPair(List<string> chargeboxIds)
     {
         string getStationStrSql = """

+ 98 - 0
EVCB_OCPP.WSServer/Service/StationConfigService.cs

@@ -0,0 +1,98 @@
+using EVCB_OCPP.Packet.Messages.Core;
+using EVCB_OCPP.WSServer.Message;
+using EVCB_OCPP.WSServer.Service.DbService;
+using log4net.Core;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EVCB_OCPP.WSServer.Service;
+
+internal class StationConfigService
+{
+    public StationConfigService(
+        WebDbService webDbService
+        , ServerMessageService messageService
+        , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
+        , ILogger<StationConfigService> logger)
+    {
+        this.webDbService = webDbService;
+        this.messageService = messageService;
+        this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
+        this.logger = logger;
+    }
+    private readonly WebDbService webDbService;
+    private readonly ServerMessageService messageService;
+    private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
+    private readonly ILogger<StationConfigService> logger;
+    private readonly Dictionary<int, Dictionary<string, string>> stationConfigRecord = new();
+
+    public async Task CheckAndUpdateEvseConfig(string chargeBoxId, CancellationToken token = default)
+    {
+        MessageResult response = await GetEvseCurrentConfig(chargeBoxId, token);
+        if (!response.Success || response.Message is not GetConfigurationConfirmation confirmation)
+        {
+            return;
+        }
+
+        Dictionary<string, string> configs = await GetEvseDBCurrentConfig(chargeBoxId, token);
+        if (configs == null)
+        {
+            logger.LogInformation("{0} get station config failed", chargeBoxId);
+            return;
+        }
+
+        await ComparenUpdateConfig(chargeBoxId,
+            evseCurrentConfigs: confirmation.configurationKey.ToDictionary(x => x.key, x => x.value),
+            evseDbConfigs: configs,
+            token);
+    }
+
+    private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
+    {
+        var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
+        var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
+        return response;
+    }
+
+    private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
+    {
+        var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
+        if (receivedStaionID is null)
+        {
+            logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
+            return null;
+        }
+        var staionID = receivedStaionID.Value;
+
+        if (!stationConfigRecord.Keys.Contains(staionID))
+        {
+            await webDbService.GetEvseStationConfig(staionID);
+        }
+
+        return stationConfigRecord[staionID];
+        return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
+    }
+
+    internal async Task ComparenUpdateConfig(string chargeBoxId,
+        Dictionary<string, string> evseCurrentConfigs,
+        Dictionary<string, string> evseDbConfigs,
+        CancellationToken token = default)
+    {
+        foreach (var config in evseDbConfigs)
+        {
+            if (evseCurrentConfigs.Keys.Contains(config.Key) &&
+                evseCurrentConfigs[config.Key] == config.Value)
+            {
+                continue;
+            }
+
+            MessageResult response = null;
+            var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
+            response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
+        }
+    }
+}