From f8f87a4fe67a1539b6fb85a54e4c178474792285 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期二, 20 九月 2022 13:22:22 +0800
Subject: [PATCH] 完善后退功能

---
 HDL_ON/DAL/DriverLayer/Control.cs |   73 ++++++++++++++++++++++++++++++++----
 1 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index ab30fc6..b347f34 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -29,11 +29,27 @@
         /// 璁板綍鎺ユ敹鍒扮殑娑堟伅锛屾柟渚縵b鐨勫伐绋嬪笀璋冭瘯浠栦滑鐨勮澶�
         /// </summary>
         public List<string> MsgInfoList = new List<string>();
+        private bool _LocalTcpClientLogin = false;
         /// <summary>
         /// 鏈湴tcp瀹㈡埛绔繛鎺ユ槸鍚︽垚鍔�
         /// 鏄惁鐧诲綍缃戝叧鎴愬姛
         /// </summary>
-        public bool LocalTcpClientLogin = false;
+        public bool LocalTcpClientLogin
+        {
+            get
+            {
+                return _LocalTcpClientLogin;
+            }
+            set
+            {
+                _LocalTcpClientLogin = value;
+                if (!value)
+                {
+                    loginGatewayThread?.Abort();
+                    loginGatewayThread = null;
+                }
+            }
+        }
 
         int _msg_id = 1;
         /// <summary>
@@ -222,8 +238,9 @@
             if (myTcpClient == null)
             {
                 myTcpClient = new Control_TcpClient(reportIp);
-                myTcpClient.Connect();
             }
+            myTcpClient.Connect();
+
         }
 
         /// <summary>
@@ -294,9 +311,9 @@
             if(loginGatewayThread== null)
             {
                 loginGatewayThread = new System.Threading.Thread(() => {
-                    while (true)
+                    while (!Ins.myTcpClient.isConnected)
                     {
-                        if (Ins.GatewayOnline_Local && myTcpClient.isConnected && LocalTcpClientLogin)
+                        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 };
@@ -309,9 +326,28 @@
                         }
                         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();
+                }
+            }
+            
         }
 
 
@@ -880,7 +916,6 @@
         {
             LocalCommunicationData receiveObj = new LocalCommunicationData();
 
-            MainPage.Log($"灞�鍩熺綉淇℃伅: \r\n{receiveString}");
 
             var res = receiveString.Split("\r\n\r\n");
 
@@ -912,9 +947,10 @@
                 //}
                 receiveObj.BodyDataString = res[1];
 
-                if(receiveObj.Topic == CommunicationTopic.ct.HeartBeat)
+                if (receiveObj.Topic == CommunicationTopic.ct.HeartBeat + "_reply")
                 {
-
+                    Ins.myTcpClient.ClearHeartBeatLog();
+                    MainPage.Log("tcp蹇冭烦鍥炲");
                     return null;
                 }
 
@@ -924,7 +960,6 @@
                     //鍒ゆ柇褰撳墠缃戝叧鏄惁寮�鍚簡鏈湴鍔犲瘑
                     if (IsLocalEncryptAndGetAesKey)
                     {
-                        MainPage.Log($"灞�鍩熺綉淇℃伅 寮�濮嬭В瀵�");
                         if (originalReceiveBytes != null)
                         {
                             //鎷垮埌鍘熷Bytes鏁版嵁鍘昏В瀵�
@@ -955,6 +990,10 @@
                             //MainPage.Log($"灞�鍩熺綉淇℃伅: 瑙e瘑鍚庯細" + receiveObj.BodyDataString);
                         }
 
+                    }
+                    else
+                    {
+                        MainPage.Log($"灞�鍩熺綉淇℃伅: 鏄庢枃锛�" + receiveObj.BodyDataString);
                     }
                 }
 
@@ -1008,7 +1047,6 @@
 
                         OpenTcpClent();
 
-                        LoginGateway();
 
                     }
                 }
@@ -1058,6 +1096,19 @@
                     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
@@ -1402,6 +1453,10 @@
                                 //Console.WriteLine("闊充箰鎾斁鍣ㄦ樉绀虹姸鎬�======="+ revString);
                                 //wjc
                                 break;
+                            case SPK.Inverter:
+                                var status = new Dictionary<string, object> { ["method"] = "status", ["data"] = temp.objects };
+                                Inverter.Ins.H5Page.CallJS($"nativeToJs(\'{Newtonsoft.Json.JsonConvert.SerializeObject(status)}\')");
+                                break;
                         }
 
                         HomePage.UpdataFunctionStates(localFunction);

--
Gitblit v1.8.0