소스 검색

1. 加入偵測拒絕連線機種名單功能(每五分鐘一次)
名單來源 Web DB KernelConfig Table

Jessica Tseng 2 년 전
부모
커밋
8d29f79499

+ 2 - 0
EVCB_OCPP.WSServer/GlobalConfig.cs

@@ -15,6 +15,8 @@ namespace EVCB_OCPP.WSServer
             "OCPP20_WSSUrl"
         };
 
+        public static List<string> DenyModelNames = new List<string>();
+
         public static  string UTC_DATETIMEFORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'Z'";
 
         public static string TCC_API_URL = string.Empty;

+ 1 - 1
EVCB_OCPP.WSServer/Properties/AssemblyInfo.cs

@@ -35,4 +35,4 @@ using System.Runtime.InteropServices;
 [assembly: AssemblyVersion("1.2.1.0")]
 [assembly: AssemblyFileVersion("1.2.1.0")]
 
-[assembly: AssemblyInformationalVersion("0c503b3")]
+[assembly: AssemblyInformationalVersion("7b6fbe5")]

+ 74 - 4
EVCB_OCPP.WSServer/ProtalServer.cs

@@ -62,6 +62,7 @@ namespace EVCB_OCPP.WSServer
         private DateTime checkUpdateDt = DateTime.UtcNow;
         private DateTime _CheckFeeDt = DateTime.UtcNow;
         private DateTime _CheckLBDt = DateTime.UtcNow;
+        private DateTime _CheckDenyListDt = DateTime.UtcNow.AddDays(-1);
         private LoadingBalanceService _loadingBalanceService = new LoadingBalanceService();
         private List<StationInfoDto> _StationInfo = new List<StationInfoDto>();
         private List<string> needConfirmActions = new List<string>()
@@ -108,6 +109,7 @@ namespace EVCB_OCPP.WSServer
         {
             _ct = _cts.Token;
             WarmUpLog();
+            DenyModelCheckTrigger(true);
         }
 
         internal void Start()
@@ -137,6 +139,10 @@ namespace EVCB_OCPP.WSServer
             Task smartChargingTask = new Task(SmartChargingTrigger, _ct);
             smartChargingTask.Start();
 
+
+            Task denyModelCheckTask = new Task(()=>DenyModelCheckTrigger(false), _ct);
+            denyModelCheckTask.Start();
+
             while (true)
             {
                 var input = Console.ReadLine();
@@ -290,7 +296,7 @@ namespace EVCB_OCPP.WSServer
             //設定server config
             var serverConfig = new ServerConfig
             {
-                SendingQueueSize=10,
+                SendingQueueSize = 10,
                 //Port = Convert.ToInt32(2012),
                 //Ip = "172.17.40.13",
                 MaxRequestLength = 204800,
@@ -990,6 +996,70 @@ namespace EVCB_OCPP.WSServer
 
         }
 
+        async private void DenyModelCheckTrigger(bool warmup)
+        {
+            Console.WriteLine("DenyModelCheckTrigger " + warmup);
+            for (; ; )
+            {
+                if (_ct.IsCancellationRequested)
+                {
+                    break;
+                }
+
+                try
+                {
+                    var min_Interval = (DateTime.UtcNow - _CheckDenyListDt).TotalMinutes;
+
+                    if (min_Interval > 5)
+                    {
+                        using (SqlConnection conn = new SqlConnection(webConnectionString))
+                        {
+                            string strSql = "SELECT [Value] FROM[StandardOCPP_Web].[dbo].[KernelConfig]" +
+                             "where SystemKey = 'DenyModelNames'; ";
+                            var result = await conn.QueryAsync<string>(strSql);
+                           
+                            GlobalConfig.DenyModelNames = result.FirstOrDefault().Split(',').ToList();
+                           
+                        }
+                        _CheckDenyListDt = DateTime.UtcNow;
+
+                        if (!string.IsNullOrEmpty(GlobalConfig.DenyModelNames[0]))
+                        {
+                            Dictionary<string, ClientData> _copyClientDic = null;
+                            lock (_lockClientDic)
+                            {
+                                _copyClientDic = new Dictionary<string, ClientData>(clientDic);
+                            }
+                            foreach (var denyName in GlobalConfig.DenyModelNames)
+                            {
+                                var removeClients = _copyClientDic.Where(x => x.Key.StartsWith(denyName)).Select(x => x.Value).ToList();
+                                foreach (var session in removeClients)
+                                {
+
+                                    Console.WriteLine(string.Format("Server forced to shut down ChargeBox ({0}: Reason: DenyModelName-{1}", session.ChargeBoxId, denyName));
+                                    RemoveClient(session);
+                                }
+                            }
+                            
+
+                           
+                        }
+
+                    }
+                   
+
+                    await Task.Delay(500);
+
+                }
+                catch (Exception ex)
+                {
+                    logger.Error(string.Format("DenyModelCheckTrigger  Ex:{0}", ex.ToString()));
+                }
+
+                if (warmup) break;
+            }
+        }
+
         async private void ServerUpdateTrigger()
         {
             for (; ; )
@@ -1441,8 +1511,8 @@ namespace EVCB_OCPP.WSServer
                     foreach (var station in stations)
                     {
                         var compareStation = _StationInfo.Where(x => x.Id == station.Id).FirstOrDefault();
-                       
-                        if (compareStation == null ||(station.Id == compareStation.Id && station.Availability != compareStation.Availability))
+
+                        if (compareStation == null || (station.Id == compareStation.Id && station.Availability != compareStation.Availability))
                         {
                             var _powerDic = await _loadingBalanceService.GetSettingPower(station.Id);
                             if (_powerDic != null)
@@ -1450,7 +1520,7 @@ namespace EVCB_OCPP.WSServer
                                 foreach (var kv in _powerDic)
                                 {
                                     try
-                                    {           
+                                    {
 
                                         if (kv.Value.HasValue)
                                         {

+ 23 - 0
EVCB_OCPP.WSServer/SuperSocket.Protocol/OCPPWSServer.cs

@@ -1,5 +1,6 @@
 
 using EVCB_OCPP.Domain;
+using EVCB_OCPP.WSServer;
 using NLog;
 using OCPPPackage.Profiles;
 using SuperWebSocket;
@@ -78,6 +79,28 @@ namespace OCPPServer.Protocol
 
             string[] words = session.Path.Split('/');
             session.ChargeBoxId = words.Last();
+           
+            foreach(var denyModel in GlobalConfig.DenyModelNames)
+            {
+                if (string.IsNullOrEmpty(denyModel)) break;
+                if (session.ChargeBoxId.StartsWith(denyModel))
+                {                 
+
+                    StringBuilder responseBuilder = new StringBuilder();
+
+                    responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
+                    (int)HttpStatusCode.Unauthorized, @"Unauthorized");
+
+                    responseBuilder.AppendWithCrCf();
+                    string sb = responseBuilder.ToString();
+                    byte[] data = Encoding.UTF8.GetBytes(sb);
+
+                    ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
+                    logger.Info(sb);
+                    return false;
+                }
+            }
+
 
             if (ConfigurationManager.AppSettings["MaintainMode"] == "1")
             {

BIN
SuperWebSocket/bin/Debug/SuperWebSocket.dll


BIN
SuperWebSocket/bin/Debug/SuperWebSocket.pdb