From d6578b10542226650e263815dea75e598a7090f9 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期三, 17 八月 2022 13:22:02 +0800
Subject: [PATCH] tcp状态更新,iOS扫描

---
 HDL_ON/DAL/DriverLayer/Control.cs |  135 +++++++++++++++++++++++++++++++++-----------
 1 files changed, 101 insertions(+), 34 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index 2b0623c..ab30fc6 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -4,26 +4,12 @@
 using HDL_ON.DAL.Server;
 using HDL_ON.Entity;
 using HDL_ON.UI;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
 using Shared;
 
 namespace HDL_ON.DriverLayer
 {
-    /// <summary>
-    /// 閫氳鏂瑰紡
-    /// </summary>
-    public enum CommunicationMode
-    {
-        none,
-        /// <summary>
-        /// 鏈湴udp
-        /// </summary>
-        local_BusUdp,
-        /// <summary>
-        /// 鏈湴tcp瀹㈡埛绔�
-        /// </summary>
-        tcp_local_client,
-    }
-
     public class Control
     {
 
@@ -43,6 +29,11 @@
         /// 璁板綍鎺ユ敹鍒扮殑娑堟伅锛屾柟渚縵b鐨勫伐绋嬪笀璋冭瘯浠栦滑鐨勮澶�
         /// </summary>
         public List<string> MsgInfoList = new List<string>();
+        /// <summary>
+        /// 鏈湴tcp瀹㈡埛绔繛鎺ユ槸鍚︽垚鍔�
+        /// 鏄惁鐧诲綍缃戝叧鎴愬姛
+        /// </summary>
+        public bool LocalTcpClientLogin = false;
 
         int _msg_id = 1;
         /// <summary>
@@ -200,11 +191,6 @@
         public Control_Udp myUdp1 = null;
 
         /// <summary>
-        /// 閫氳鏂瑰紡
-        /// </summary>
-        public CommunicationMode communicationMode;
-
-        /// <summary>
         /// 鎵撳紑tcp鏈嶅姟绔�
         /// </summary>
         public void OpenTcpServer()
@@ -297,6 +283,38 @@
             }
         }
 
+
+        private System.Threading.Thread loginGatewayThread;
+
+        /// <summary>
+        /// 鐧诲綍缃戝叧
+        /// </summary>
+        public void LoginGateway()
+        {
+            if(loginGatewayThread== null)
+            {
+                loginGatewayThread = new System.Threading.Thread(() => {
+                    while (true)
+                    {
+                        if (Ins.GatewayOnline_Local && myTcpClient.isConnected && LocalTcpClientLogin)
+                        {
+                            var sendData = new { clientType = "app", version = "1.0" };
+                            var sendJob = new { id = Control.Ins.msg_id.ToString(), time_stamp = Utlis.GetTimestamp(), objects = sendData };
+
+                            var bodyString = JsonConvert.SerializeObject(sendJob);
+
+                            var sendBytes = ConvertSendBodyData($"/user/{GatewayId}/custom/gateway/login", bodyString);
+
+                            Ins.myTcpClient.SendMessage(sendBytes);
+                        }
+                        System.Threading.Thread.Sleep(2000);
+                    }
+
+                });
+            }
+        }
+
+
         /// <summary>
         /// 鍦烘櫙鎺у埗鍏ュ彛
         /// </summary>
@@ -342,15 +360,6 @@
                 }
             }
         }
-        /// <summary>
-        /// 瀹夐槻鎺у埗
-        /// </summary>
-        public void ControlArm()
-        {
-            DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
-            //var pack = httpServer.GetSecurityAlarmLogList
-        }
-
 
         /// <summary>
         /// 鍙戦�佸懡浠�
@@ -903,6 +912,12 @@
                 //}
                 receiveObj.BodyDataString = res[1];
 
+                if(receiveObj.Topic == CommunicationTopic.ct.HeartBeat)
+                {
+
+                    return null;
+                }
+
                 //2021-09-23 杩囨护涓嶉渶瑕佽В瀵嗙殑涓婚 鐩墠鎼滅储缃戝叧涓婚涓嶅姞瀵�
                 if (receiveObj.Topic != CommunicationTopic.SearchLoaclGatewayReply)
                 {
@@ -943,6 +958,21 @@
                     }
                 }
 
+                try
+                {
+                    var idMsg = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]);
+                    var hasProcess = hasItBeenProcessed(idMsg.id);
+                    if (hasProcess)
+                    {
+                        return null;
+                    }
+                    MainPage.Log($"澶勭悊灞�鍩熺綉鏁版嵁id:{idMsg.id}");
+
+                }
+                catch (Exception ex)
+                {
+                    MainPage.Log($"瑙f瀽灞�鍩熺綉鏁版嵁寮傚父{ex.Message}");
+                }
 
                 if (receiveObj.Topic == CommunicationTopic.SearchLoaclGatewayReply || receiveObj.Topic == CommunicationTopic.GatewayBroadcast)
                 {
@@ -975,17 +1005,22 @@
                         //2021-09-23 鏂板鑾峰彇褰撳墠缃戝叧鏄惁鏈湴鍔犲瘑
                         Ins.IsLocalEncrypt = device.isLocalEncrypt;
                         //MainPage.Log("缃戝叧鏈湴鍔犲瘑鐘舵�侊細" + device.local_encrypt.ToString());
+
+                        OpenTcpClent();
+
+                        LoginGateway();
+
                     }
                 }
                 else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
                     receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                     receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus ||
-                    receiveObj.Topic.Contains( CommunicationTopic.ct.GatewayUpSortTopic))
+                    receiveObj.Topic.Contains(CommunicationTopic.ct.GatewayUpSortTopic))
                 {
                     //TODO 鏆傛椂涓嶄紶姝g‘鐨勬暟鎹笂鍘伙紝濡傛灉鍚庨潰瑕佷紭鍖栧墠闈㈣繖浜涗唬鐮�
                     UpdataFunctionStatus(receiveObj.BodyDataString, null);
                 }
-                else if (receiveObj.Topic == CommunicationTopic.ct.ControlSeurity +"_reply"
+                else if (receiveObj.Topic == CommunicationTopic.ct.ControlSeurity + "_reply"
                     || receiveObj.Topic == CommunicationTopic.ct.ReadSecurityStatus + "_reply"
                     || receiveObj.Topic == CommunicationTopic.ct.SecurityStatusUp)
                 {
@@ -993,7 +1028,8 @@
                     {
                         MainPage.Log($"灞�鍩熺綉瀹夐槻淇℃伅: {receiveObj.Topic}  : 鍐呭: {res[1]}");
                         var tt = "";
-                        lock (tt) {
+                        lock (tt)
+                        {
                             var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<SecurityStatusObj>(receiveObj.BodyDataString);
                             if (temp != null)
                             {
@@ -1012,8 +1048,16 @@
                             }
                         }
                     }
-                    catch (Exception ex){
+                    catch (Exception ex)
+                    {
                         MainPage.Log($"瀹夐槻灞�鍩熺綉寮傚父锛歿ex.Message}");
+                    }
+                }
+                else if (receiveObj.Topic == $"/user/{GatewayId}/custom/gateway/login_reply") {
+                    var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(receiveObj.BodyDataString);
+                    if (temp.GetValue("objects").ToString().Contains("success"))
+                    {
+                        LocalTcpClientLogin = true;
                     }
                 }
                 else
@@ -1025,6 +1069,29 @@
             return receiveObj;
         }
 
+
+        private List<string> processedDataList = new List<string>();
+        /// <summary>
+        /// 鏄惁宸茬粡澶勭悊杩囨暟鎹�
+        /// </summary>
+        /// <returns></returns>
+        private bool hasItBeenProcessed(string msgId)
+        {
+            if (processedDataList.Contains(msgId))
+            {
+                return true;
+            }
+            else
+            {
+                if(processedDataList.Count> 50)
+                {
+                    processedDataList.RemoveAt(0);
+                }
+                return false;
+            }
+
+        }
+
         /// <summary>
         /// 鏇存柊璁惧鐘舵��
         /// A鍗忚鏁版嵁

--
Gitblit v1.8.0