HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2019-10-12 c6b35c3138b944830b5336bf610f918154dd47c7
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -3,6 +3,7 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using Shared.Common;
using ZigBee.Device;
using static ZigBee.Device.ZbGateway;
@@ -11,7 +12,7 @@
{
    public static class Application
    {
        static bool isInited;
        public static bool isInited;
        static bool isEncry;
        /// <summary>
        /// 是否正在搜索网关
@@ -29,10 +30,11 @@
                return;
            }
            isInited = true;
            new System.Threading.Thread(async () =>
            {
                var gateWayList = new List<ZbGateway> { };
                var searchCount = 2;
                var searchCount = 0;
                var oldHomeID = string.Empty;
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                broadBytes[0] = 0xfe;
@@ -89,14 +91,12 @@
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        //网关通讯处理
                        System.Console.WriteLine($"Wifi{Shared.Application.IsWifi}");
                        if (Shared.Application.IsWifi)
                        {
                        if (0 < gateWayList.Count)
                        {
                            searchCount = 2;
                            gateWayList.Clear();
                            var aa = GateWayList;
                                searchCount = 4;
                            //关闭远程服务器
                            if (RemoteMqttClient!=null&& RemoteMqttClient.IsConnected==true)
@@ -109,12 +109,26 @@
                        {
                             if (searchCount <= 0)
                            {
                                    System.Console.WriteLine($"=====================3{System.DateTime.Now.ToString()}");
                                    gateWayList.Clear();
                                    //远程通讯连接,连接云端服务器
                                    await StartCloudMqtt();
                                }
                            }
                        }
                        else
                        {
                            if (searchCount <= 0)
                            {
                                System.Console.WriteLine($"=====================4{System.DateTime.Now.ToString()}");
                                gateWayList.Clear();
                                 //远程通讯连接,连接云端服务器
                                await StartCloudMqtt();
                            }
                        }
                        searchCount--;
                         #region 2秒搜索一次网关
                        //每0.5秒广播发现一次网关,共2s
                        int count = 4;
                        new System.Threading.Thread(() =>
@@ -124,7 +138,8 @@
                                try
                                {
                                    //点对点发送(先发一条已有的点播,回复几率高一点)
                                    for (int i = 0; i < gateWayList.Count;i++){
                                    for (int i = 0; i < gateWayList.Count; i++)
                                    {
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes);
                                    }
                                    //广播发送
@@ -171,7 +186,6 @@
                                    var pubKeyLengthByte1 = bytes[49 + gwNameLength + 1 + gwIdLength + 2];
                                    int pubKeyLength = ((pubKeyLengthByte1 & 0xff) << 8 | (pubKeyLengthByte0 & 0xff));
                                    var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
                                    var zbGateWay = new ZbGateway
                                    {
                                        getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
@@ -259,6 +273,7 @@
                                var mess = ex.Message;
                            }
                        }
#endregion
                    }
                    catch (Exception ex)
                    {