From e22e6699a4ec73e81f45526eb8249379963e22d7 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期三, 14 九月 2022 13:49:05 +0800
Subject: [PATCH] 光伏更新

---
 HDL_ON/DAL/DriverLayer/Control.cs |  207 ++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 168 insertions(+), 39 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index 08092a6..701af66 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,27 @@
         /// 璁板綍鎺ユ敹鍒扮殑娑堟伅锛屾柟渚縵b鐨勫伐绋嬪笀璋冭瘯浠栦滑鐨勮澶�
         /// </summary>
         public List<string> MsgInfoList = new List<string>();
+        private bool _LocalTcpClientLogin = false;
+        /// <summary>
+        /// 鏈湴tcp瀹㈡埛绔繛鎺ユ槸鍚︽垚鍔�
+        /// 鏄惁鐧诲綍缃戝叧鎴愬姛
+        /// </summary>
+        public bool LocalTcpClientLogin
+        {
+            get
+            {
+                return _LocalTcpClientLogin;
+            }
+            set
+            {
+                _LocalTcpClientLogin = value;
+                if (!value)
+                {
+                    loginGatewayThread?.Abort();
+                    loginGatewayThread = null;
+                }
+            }
+        }
 
         int _msg_id = 1;
         /// <summary>
@@ -200,11 +207,6 @@
         public Control_Udp myUdp1 = null;
 
         /// <summary>
-        /// 閫氳鏂瑰紡
-        /// </summary>
-        public CommunicationMode communicationMode;
-
-        /// <summary>
         /// 鎵撳紑tcp鏈嶅姟绔�
         /// </summary>
         public void OpenTcpServer()
@@ -236,8 +238,9 @@
             if (myTcpClient == null)
             {
                 myTcpClient = new Control_TcpClient(reportIp);
-                myTcpClient.Connect();
             }
+            myTcpClient.Connect();
+
         }
 
         /// <summary>
@@ -297,6 +300,57 @@
             }
         }
 
+
+        private System.Threading.Thread loginGatewayThread;
+
+        /// <summary>
+        /// 鐧诲綍缃戝叧
+        /// </summary>
+        public void LoginGateway()
+        {
+            if(loginGatewayThread== null)
+            {
+                loginGatewayThread = new System.Threading.Thread(() => {
+                    while (!Ins.myTcpClient.isConnected)
+                    {
+                        if (Ins.GatewayOnline_Local && myTcpClient.isConnected)
+                        {
+                            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);
+                    }
+                    //loginGatewayThread?.Abort();
+                    //loginGatewayThread = null;
+
+                });
+                loginGatewayThread.Start();
+            }
+            else
+            {
+                try
+                {
+                    loginGatewayThread?.Abort();
+                }catch (Exception ex)
+                {
+                    MainPage.Log($"閲嶅惎鐧诲綍缃戝叧绾跨▼寮傚父:{ex.Message}");
+                }
+                finally
+                {
+                    loginGatewayThread = null;
+                    LoginGateway();
+                }
+            }
+            
+        }
+
+
         /// <summary>
         /// 鍦烘櫙鎺у埗鍏ュ彛
         /// </summary>
@@ -342,15 +396,6 @@
                 }
             }
         }
-        /// <summary>
-        /// 瀹夐槻鎺у埗
-        /// </summary>
-        public void ControlArm()
-        {
-            DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
-            //var pack = httpServer.GetSecurityAlarmLogList
-        }
-
 
         /// <summary>
         /// 鍙戦�佸懡浠�
@@ -871,7 +916,6 @@
         {
             LocalCommunicationData receiveObj = new LocalCommunicationData();
 
-            MainPage.Log($"灞�鍩熺綉淇℃伅: \r\n{receiveString}");
 
             var res = receiveString.Split("\r\n\r\n");
 
@@ -903,13 +947,19 @@
                 //}
                 receiveObj.BodyDataString = res[1];
 
+                if (receiveObj.Topic == CommunicationTopic.ct.HeartBeat + "_reply")
+                {
+                    Ins.myTcpClient.ClearHeartBeatLog();
+                    MainPage.Log("tcp蹇冭烦鍥炲");
+                    return null;
+                }
+
                 //2021-09-23 杩囨护涓嶉渶瑕佽В瀵嗙殑涓婚 鐩墠鎼滅储缃戝叧涓婚涓嶅姞瀵�
                 if (receiveObj.Topic != CommunicationTopic.SearchLoaclGatewayReply)
                 {
                     //鍒ゆ柇褰撳墠缃戝叧鏄惁寮�鍚簡鏈湴鍔犲瘑
                     if (IsLocalEncryptAndGetAesKey)
                     {
-                        MainPage.Log($"灞�鍩熺綉淇℃伅 寮�濮嬭В瀵�");
                         if (originalReceiveBytes != null)
                         {
                             //鎷垮埌鍘熷Bytes鏁版嵁鍘昏В瀵�
@@ -941,8 +991,27 @@
                         }
 
                     }
+                    else
+                    {
+                        MainPage.Log($"灞�鍩熺綉淇℃伅: 鏄庢枃锛�" + receiveObj.BodyDataString);
+                    }
                 }
 
+                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 +1044,21 @@
                         //2021-09-23 鏂板鑾峰彇褰撳墠缃戝叧鏄惁鏈湴鍔犲瘑
                         Ins.IsLocalEncrypt = device.isLocalEncrypt;
                         //MainPage.Log("缃戝叧鏈湴鍔犲瘑鐘舵�侊細" + device.local_encrypt.ToString());
+
+                        OpenTcpClent();
+
+
                     }
                 }
                 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 +1066,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 +1086,29 @@
                             }
                         }
                     }
-                    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;
+                        try
+                        {
+                            loginGatewayThread?.Abort();
+                        }
+                        catch (Exception ex)
+                        {
+                            MainPage.Log($"缁堟鐧诲綍缃戝叧tcp寮傚父锛歿ex.Message}");
+                        }
+                        finally
+                        {
+                            loginGatewayThread = null;
+                        }
+                        MainPage.Log("灞�鍩熺綉tcp杩炴帴鐧诲綍缃戝叧鎴愬姛");
                     }
                 }
                 else
@@ -1025,6 +1120,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鍗忚鏁版嵁
@@ -1032,6 +1150,12 @@
         /// <param name="updateBytes"></param>
         public void UpdataFunctionStatus(string revString, byte[] usefulBytes,bool isCloudData = false)
         {
+            ////test 浜戠杩炴帴鎴愬姛鏃讹紝涓嶉�傜敤鏈湴鏁版嵁鏇存柊
+            //if (Ins.GatewayOnline_Cloud && !isCloudData)
+            //{
+            //    return;
+            //}
+
             var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<AlinkFunctionStatusObj>(revString);
             if (temp != null)
             {
@@ -1044,11 +1168,15 @@
 
                         if (Ins.GatewayOnline_Local && isCloudData)//鏈湴閾炬帴锛岄櫎浜嗘秱楦﹁澶囨暟鎹箣澶栫殑浜戠鏁版嵁涓嶅鐞�
                         {
-                            if (FunctionList.List.OtherBrandFunction.Count != 0)
+                            if (FunctionList.List.OtherBrandFunction.Count == 0)
+                            {
+                                MainPage.Log($"A鍗忚鏇存柊鐘舵��:鏈湴閾炬帴锛岄櫎浜嗘秱楦﹁澶囨暟鎹箣澶栫殑浜戠鏁版嵁涓嶅鐞�...");
+                                return;
+                            }
+                            else
                             {
                                 if (FunctionList.List.OtherBrandFunction.Find((obj) => obj.sid == updateTemp.sid) == null)
                                 {
-                                    //MainPage.Log($"A鍗忚鏇存柊鐘舵��:鏈湴閾炬帴锛岄櫎浜嗘秱楦﹁澶囨暟鎹箣澶栫殑浜戠鏁版嵁涓嶅鐞�...");
                                     return;
                                 }
                             }
@@ -1271,6 +1399,7 @@
                             case SPK.HvacAirFresh:
                             case SPK.SensorGas:
                             case SPK.HvacCac:
+                            case SPK.SensorHelp:
                                 //璁惧鐘舵�佹帹閫�
                                 //鐘舵�佹洿鏂�
                                 Stan.HdlDeviceStatuPushLogic.Current.UpdateDeviceStatu(updateTemp.sid, updateTemp.status);

--
Gitblit v1.8.0