From b71dfb3ca100340005d56e1298292807da82322d Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期一, 08 六月 2020 15:04:21 +0800
Subject: [PATCH] 20200608

---
 HDL_ON/DriverLayer/Control_Bus.cs |  160 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 104 insertions(+), 56 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control_Bus.cs b/HDL_ON/DriverLayer/Control_Bus.cs
index b5d7d40..dd8490b 100644
--- a/HDL_ON/DriverLayer/Control_Bus.cs
+++ b/HDL_ON/DriverLayer/Control_Bus.cs
@@ -1,8 +1,12 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.Net.Sockets;
+using System.Text;
 using HDL_ON.DAL;
 using HDL_ON.DAL.Net;
 using HDL_ON.Entity;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
 using Shared;
 using Shared.Net;
 
@@ -20,7 +24,7 @@
         /// <summary>
         /// 鍙戦�佹暟鎹紝涓嶉渶瑕佺瓑寰呭洖澶�
         /// </summary>
-        static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
+       public static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
         {
             Control control = new Control();
             control.Send(new Target()
@@ -32,8 +36,15 @@
                 AddData = gatewayBytes,
             }, sendCount, false);
             MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString());
-
         }
+
+        public static byte[] ReadGatewayIPAddress()
+        {
+            Control control = new Control();
+            control.ReadGatewayIP();
+            return control.UsefulBytes;
+        }
+
         /// <summary>
         /// 000E 鎼滅储鍥炲
         /// </summary>
@@ -48,51 +59,38 @@
                     return;
                 }
 
-                //if (!isHttpListenerStart)
-                //{
-                //    //鏁版嵁鎺ユ敹绔彛娌℃墦寮�锛屼笉鍥炲
-                //    return;
-                //}
 
-                var sendStr = MainPage.LoginUser.accountString;
-
-
-                //濡傛灉IP鍦板潃鑾峰彇鎴愬姛 鏄剧ず鏈�鍚庝竴浣�
-                var mAddressBytes = new Shared.Net.NetWiFi().IpAddress.GetAddressBytes();
-                if (mAddressBytes != null && mAddressBytes.Length == 4)
+                if (!CommonPage.isHttpListenerStart)
                 {
-                    sendStr = (mAddressBytes[3] & 0xFF).ToString() + ":" + sendStr;
+                    //鏁版嵁鎺ユ敹绔彛娌℃墦寮�锛屼笉鍥炲
+                    CommonPage.InitHttpListener();
                 }
-
-                if (usefullBytes.Length <= 2)
+                var sendStr = MainPage.LoginUser.accountString;
+                if (usefullBytes.Length == 0)
                 {
-                    byte[] sendBytes = new byte[20];
-                    byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
-                    Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
-                    Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1);
-
+                    SenRemark(sendStr);
                 }
                 else
                 {
+                    bool isExit = false;
 
-                    for (int i = 2, len = usefullBytes.Length; i < len; i++)
+                    for (int i = 0, len = usefullBytes.Length; i < len; i++)
                     {
                         if (i % 2 == 0)
                         {
-                            if ((usefullBytes[i] & 0xFF) == 0xFC
-                                    && (usefullBytes[i + 1] & 0xFF) == 0xFC
-                            )
+                            if ((usefullBytes[i] & 0xFF) == 252
+                                    && (usefullBytes[i + 1] & 0xFF) == 252)
                             {
-
-                                byte[] sendBytes = new byte[20];
-                                byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
-                                //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
-                                Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
-                                Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1);
-
+                                isExit = true;
                                 break;
                             }
                         }
+                    }
+
+                    if (!isExit)
+                    {
+                        //涓嶅瓨鍦紝浠h〃娌℃敹鍒版湰鏈虹殑鍙戦�侊紝缁х画鍥炲
+                        SenRemark(sendStr);
                     }
 
                 }
@@ -101,6 +99,27 @@
             catch { }
         }
 
+        /// <summary>
+        /// 000F鍥炲澶囨敞
+        /// </summary>
+        /// <param name="sendStr"></param>
+        static void SenRemark(string sendStr)
+        {
+            byte[] sendBytes = new byte[20];
+            byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
+            //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
+            Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
+
+            var control = new Control();
+            control.Send(new Target()
+            {
+                IPEndPoint = CommonPage.EndPoint,
+                Command = Command.ReadRemarkACK,
+                SubnetID = 0xFF,
+                DeviceID = 0xFF,
+                AddData = sendBytes,
+            }, 0, false); //璁剧疆褰撳墠鍙戦�佹寚浠ゆ柟寮忎负锛氫换浣曟儏鍐典笅鏈湴鍙戦�併�佷笉闄愬埗銆佷笉鍔犲瘑鏄庢枃鍙戦��
+        }
         /// <summary>
         /// 澶勭悊鎺ユ敹鍥炴潵鐨勬暟鎹�
         /// </summary>
@@ -111,6 +130,25 @@
         /// <param name="remoteEndPoint">婧愮綉缁滃鎺ュ瓧</param>
         public static void ManagerReceive(byte subnetID, byte deviceID, Command command, byte targetSubnetID, byte targetDeviceID, byte[] usefulBytes, System.Net.EndPoint remoteEndPoint)
         {
+            if (((System.Net.IPEndPoint)remoteEndPoint).Port == 6688)
+            {
+        
+                for (int i = 0; i < controlList.Count; i++)
+                {
+                    try
+                    {
+                        var control = controlList[i];
+                        control.LatestDateTime = DateTime.Now;
+                        control.UsefulBytes = usefulBytes;//
+                        control.run();
+                    }
+                    catch (Exception ex)
+                    {
+                        Console.WriteLine($"control error : {ex.Message}");
+                    }
+                }
+            }
+
             try
             {
                 string receiveFlag = string.Format("{0},{1},{2},", subnetID, deviceID, (int)command);
@@ -132,6 +170,7 @@
                         break;
                     case Command.ReadRemark:
                         //buspro 璇诲娉ㄥ洖澶�
+                        Console.WriteLine("buspro 璇诲娉ㄥ洖澶�");
                         ReceiveReadRemark(usefulBytes);
                         break;
                     //case Command.YIPanelDeviceInofACK:
@@ -353,9 +392,9 @@
                             control.run();
                         }
                     }
-                    catch
+                    catch (Exception ex)
                     {
-
+                        Console.WriteLine($"control error : {ex.Message}");
                     }
                 }
             }
@@ -460,20 +499,7 @@
             add();
             if (CommonPage.IsRemote)
             {
-                MqttCommon.MqttRemoteSend(Packet.Bytes);
-                Packet.FlagDateTime = DateTime.Now;
-                Packet.HaveSendCount--;
-                //杩欓噷鏄噸鍙戜袱娆�
-                while (Packet.HaveSendCount < 3)
-                {
-                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks)
-                    {
-                        MqttCommon.MqttRemoteSend(Packet.Bytes);
-                        Packet.FlagDateTime = DateTime.Now;
-                        Packet.HaveSendCount++;
-                    }
-                    System.Threading.Thread.Sleep(100);
-                }鈥�                allDone.Set();鈥�            }
+             鈥�            }
             else
             {
                 try
@@ -509,7 +535,7 @@
         /// </summary>
         Packet Packet;
 
-        private void ini(Target target, bool send = true)
+        private void ini(Target target)
         {
             this.SendFlag = string.Format("{0},{1},{2},", target.SubnetID, target.DeviceID, (int)target.Command + 1);
 
@@ -708,12 +734,10 @@
                     //return;
                     break;
             }
-            if (send)
-            {
-                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
-                thread.IsBackground = true;
-                thread.Start(Packet);
-            }
+
+            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
+            thread.IsBackground = true;
+            thread.Start(Packet);
         }
 
         /// <summary>
@@ -721,7 +745,7 @@
         /// </summary>
         /// <param name="target">鍙戦�佸璞�</param>
         /// <param name="sendCount">閲嶅彂娆℃暟</param>
-        public void Send(Target target, int sendCount, bool isWait)
+        void Send(Target target, int sendCount, bool isWait)
         {
             try
             {
@@ -741,5 +765,29 @@
             }
         }
 
+        /// <summary>
+        /// 璇诲彇缃戝叧IP
+        /// </summary>
+        void ReadGatewayIP()
+        {
+            try
+            {
+                var sendJob = new JObject { { "vendor_code", "HDL" }, { "command", "search" } };
+                var SearchGateway = JsonConvert.SerializeObject(sendJob);
+                var SearchGatewayPayload = Encoding.ASCII.GetBytes(SearchGateway);
+                Packet = new Packet(SearchGatewayPayload, CommonPage.GetGatewayIP_EndPoint);
+                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
+                thread.IsBackground = true;
+                thread.Start(Packet);
+
+                wait();
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log($"Send bus data error {ex.Message}");
+            }
+        }
+
+
     }
 }

--
Gitblit v1.8.0