From 6c8946b21635246355ec41f216426783685a14b2 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期三, 14 十月 2020 09:14:09 +0800
Subject: [PATCH] 20201013

---
 HDL_ON/DriverLayer/Control_Bus.cs |  112 +++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 82 insertions(+), 30 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control_Bus.cs b/HDL_ON/DriverLayer/Control_Bus.cs
index e4eadf7..cc437dc 100644
--- a/HDL_ON/DriverLayer/Control_Bus.cs
+++ b/HDL_ON/DriverLayer/Control_Bus.cs
@@ -1,14 +1,11 @@
 锘縰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;
 
 namespace HDL_ON
 {
@@ -39,9 +36,8 @@
                 DeviceID = deviceID,
                 AddData = gatewayBytes,
             }, sendCount, false);
-            MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString());
         }
-
+        //鑾峰彇A鍗忚缃戝叧IP
         public static byte[] ReadGatewayIPAddress()
         {
             Control control = new Control();
@@ -49,7 +45,47 @@
             return control.UsefulBytes;
         }
 
-        static DAL.Net.TcpListener tcpListener;
+        static bool searchLocalGateway = false;
+        /// <summary>
+        /// 璇诲彇灞�鍩熺綉缃戝叧鍒楄〃
+        /// </summary>
+        public static void SearchLocalGateway()
+        {
+            searchLocalGateway = true;
+            var control = new Control();
+            control.Send(new Target()
+            {
+                IPEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(new Shared.Net.NetWiFi().BroadcastIpAddress.ToString()), 6000),
+                Command = Command.ReadGateway,
+                SubnetID = 255,
+                DeviceID = 255,
+                AddData = new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) },
+            }, 3, true);
+        }
+        /// <summary>
+        /// 楠岃瘉缃戝叧鍥炲淇℃伅
+        /// </summary>
+        public static void VerGateway(byte[] result)
+        {
+            if(searchLocalGateway)
+            {
+                if (result != null)
+                {
+                    var mac0 = CommonPage.byteToHex16(result[5]) + "." + CommonPage.byteToHex16(result[6]) + "." + CommonPage.byteToHex16(result[7]) + "." + CommonPage.byteToHex16(result[8]) + "." + CommonPage.byteToHex16(result[9]) + "." + CommonPage.byteToHex16(result[10]) + "." + CommonPage.byteToHex16(result[11]) + "." + CommonPage.byteToHex16(result[12]);
+                    // 
+                    if (DB_ResidenceData.residenceData.residenceGatewayMAC.Replace(".", "").Replace(":", "").ToUpper() == mac0.Replace(".", "").Replace(":", "").ToUpper())
+                    {
+                        searchLocalGateway = false;
+                    }
+                    else
+                    {
+                        Mqtt_Cloud.StartCloudMqtt();
+                    }
+                }
+            }
+        }
+
+        static TcpListener tcpListener;
 
         /// <summary>
         /// 000E 鎼滅储鍥炲
@@ -67,15 +103,15 @@
 
                 if (tcpListener == null)
                 {
-                    tcpListener = new DAL.Net.TcpListener();
+                    tcpListener = new TcpListener();
                     tcpListener.OpenServer(8586);
                 }
 
-                if (!CommonPage.isHttpListenerStart)
-                {
-                    //鏁版嵁鎺ユ敹绔彛娌℃墦寮�锛屼笉鍥炲
-                    CommonPage.InitHttpListener();
-                }
+                //if (!CommonPage.isHttpListenerStart)
+                //{
+                //    //鏁版嵁鎺ユ敹绔彛娌℃墦寮�锛屼笉鍥炲
+                //    CommonPage.InitHttpListener();
+                //}
                 var sendStr = MainPage.LoginUser.accountString;
                 if (usefullBytes.Length == 0)
                 {
@@ -84,7 +120,6 @@
                 else
                 {
                     bool isExit = false;
-
                     for (int i = 0, len = usefullBytes.Length; i < len; i++)
                     {
                         if (i % 2 == 0)
@@ -97,17 +132,16 @@
                             }
                         }
                     }
-
                     if (!isExit)
                     {
                         //涓嶅瓨鍦紝浠h〃娌℃敹鍒版湰鏈虹殑鍙戦�侊紝缁х画鍥炲
                         SenRemark(sendStr);
                     }
-
                 }
-
             }
-            catch { }
+            catch (Exception ex) {
+                Console.WriteLine($"鍥炲bus鎼滅储寮傚父 : {ex.Message}");
+            }
         }
 
         /// <summary>
@@ -143,7 +177,6 @@
         {
             if (((System.Net.IPEndPoint)remoteEndPoint).Port == 6688)
             {
-        
                 for (int i = 0; i < controlList.Count; i++)
                 {
                     try
@@ -183,6 +216,9 @@
                         //buspro 璇诲娉ㄥ洖澶�
                         Console.WriteLine("buspro 璇诲娉ㄥ洖澶�");
                         ReceiveReadRemark(usefulBytes);
+                        break;
+                    case Command.ReadLightAllLoopBrightnessACK:
+                        receiveFlag += "";
                         break;
                     //case Command.YIPanelDeviceInofACK:
                     //    for (int i = 0; i < 4; i++)
@@ -234,8 +270,6 @@
                     //case Command.ReadSensorPushValuesACK:
                     //    receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                     //    break;
-                    //case Command.ReadLightEquipmentAllLoopBrightnessACK:
-                    //    break;
                     //case Command.UpLoadInfraredACK:
                     //    receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                     //    break;
@@ -254,10 +288,10 @@
                     //    receiveFlag += string.Format("{0}", usefulBytes[0]);
                     //    break;
                     //case Command.ReadDeviceModulACK:
-                    //case Command.ReadGatewayACK:
                     //case Command.ReadSecurityStatusACK:
-                    //    receiveFlag = string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
-                    //    break;
+                    case Command.ReadGatewayACK:
+                        receiveFlag = string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
+                        break;
                     //case Command.UpdataLightDimmingTheLargestLevelACK:
                     //case Command.UpdataLightDimmingMaximumOrMinimumACK:
                     //case Command.WriteManageWirelessNetACK:
@@ -508,9 +542,24 @@
         void managerSendCount(object o)
         {
             add();
-            if (CommonPage.IsRemote)
+            if (MainPage.IsRemote)
             {
-             鈥�            }
+                System.Console.WriteLine("杩滅▼鍙戦�佹暟鎹�:" + SendFlag);
+                Mqtt_Cloud.MqttRemoteSend(Packet.Bytes);
+                Packet.FlagDateTime = DateTime.Now;
+                Packet.HaveSendCount--;
+                //杩欓噷鏄噸鍙戜袱娆�
+                while (Packet.HaveSendCount < 3)
+                {
+                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks)
+                    {
+                        Mqtt_Cloud.MqttRemoteSend(Packet.Bytes);
+                        System.Console.WriteLine("閲嶅彂杩滅▼鍙戦�佹暟鎹�:" + SendFlag);
+                        Packet.FlagDateTime = DateTime.Now;
+                        Packet.HaveSendCount++;
+                    }
+                    System.Threading.Thread.Sleep(100);
+                }鈥�                allDone.Set();鈥�            }
             else
             {
                 //Bus socket鏃犳硶鎺у埗锛岄噸鍚満鍒�
@@ -577,6 +626,9 @@
                 case Command.SetLogicLoopColor:
                     this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
                     break;
+                case Command.ReadLightAllLoopBrightness:
+                    this.SendFlag += "";
+                    break;
                 //case Command.YIPanelDeviceInof:
                 //    for (int i = 0; i < 4; i++)
                 //    {
@@ -619,11 +671,11 @@
                 //    this.SendFlag += string.Format("{0}", target.AddData[0]);
                 //    break;
                 //case Command.ReadDeviceModul:
-                //case Command.ReadGateway:
                 //case Command.ReadSecurityStatus:
                 //case Command.ReadHornTargetState:
-                //    this.SendFlag = string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
-                //    break;
+                case Command.ReadGateway:
+                    this.SendFlag = string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
+                    break;
                 //case Command.SetScene:
                 //case Command.SetSeries:
                 //case Command.SetHotelCurtain:
@@ -797,10 +849,10 @@
         {
             try
             {
-                var sendJob = new JObject { { "vendor_code", "HDL" }, { "command", "search" } };
+                var sendJob = new JObject {{ "command", "search" } };
                 var SearchGateway = JsonConvert.SerializeObject(sendJob);
                 var SearchGatewayPayload = Encoding.ASCII.GetBytes(SearchGateway);
-                Packet = new Packet(SearchGatewayPayload, CommonPage.GetGatewayIP_EndPoint);
+                Packet = new Packet(SearchGatewayPayload, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("224.0.168.188"), 6688));
                 System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                 thread.IsBackground = true;
                 thread.Start(Packet);

--
Gitblit v1.8.0