黄学彪
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,32 +91,44 @@
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        //网关通讯处理
                        if (0 < gateWayList.Count)
                        System.Console.WriteLine($"Wifi{Shared.Application.IsWifi}");
                        if (Shared.Application.IsWifi)
                        {
                            searchCount = 2;
                            gateWayList.Clear();
                            var aa = GateWayList;
                            //关闭远程服务器
                            if (RemoteMqttClient!=null&& RemoteMqttClient.IsConnected==true)
                            if (0 < gateWayList.Count)
                            {
                                await RemoteMqttClient?.DisconnectAsync();
                                System.Console .WriteLine ($"收到网关远程连接主动断开_{System.DateTime.Now.ToString()}");
                                searchCount = 4;
                                //关闭远程服务器
                                if (RemoteMqttClient != null && RemoteMqttClient.IsConnected == true)
                                {
                                    await RemoteMqttClient?.DisconnectAsync();
                                    System.Console.WriteLine($"收到网关远程连接主动断开_{System.DateTime.Now.ToString()}");
                                }
                            }
                            else
                            {
                                if (searchCount <= 0)
                                {
                                    System.Console.WriteLine($"=====================3{System.DateTime.Now.ToString()}");
                                    gateWayList.Clear();
                                    //远程通讯连接,连接云端服务器
                                    await StartCloudMqtt();
                                }
                            }
                        }
                        else
                        {
                             if (searchCount <= 0)
                            {
                                 //远程通讯连接,连接云端服务器
                        {
                            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
@@ -192,24 +206,24 @@
                                        //网关匹配当前住宅中到网关
                                        if (Shared.Common.Config.Instance.HomeId == homeID)
                                        {
                                             gateWayList.Add(zbGateWay);
                                            gateWayList.Add(zbGateWay);
                                        }
                                        //UI界面正在搜索,不必配当前住宅到到网关此时也通讯
                                        else if (IsSearchingGateway)
                                        {
                                             gateWayList.Add(zbGateWay);
                                            gateWayList.Add(zbGateWay);
                                        }
                                        //网关中到住宅ID为空此时也通讯
                                        else if (homeID == string.Empty)
                                        {
                                             gateWayList.Add(zbGateWay);
                                            gateWayList.Add(zbGateWay);
                                        }
                                    }
                                    //网关列表存储处理
                                    var gateWay = GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
                                    if (gateWay == null)
                                    {
                                         await zbGateWay.StartLocalMqtt(ipAddress);
                                        await zbGateWay.StartLocalMqtt(ipAddress);
                                        GateWayList.Add(zbGateWay);
                                        NewGateWayAction?.Invoke(zbGateWay);
                                    }
@@ -223,7 +237,7 @@
                                            gateWay.DisConnect("2");
                                            GateWayList.Remove(gateWay);
                                            gateWay = zbGateWay;
                                             await zbGateWay.StartLocalMqtt(ipAddress);
                                            await zbGateWay.StartLocalMqtt(ipAddress);
                                            GateWayList.Add(gateWay);
                                        }
                                        else
@@ -232,7 +246,7 @@
                                            gateWay.getGatewayBaseInfo.Time = time;
                                            gateWay.getGatewayBaseInfo.GwName = gwName;
                                            gateWay.getGatewayBaseInfo.HomeId = homeID;
                                             await gateWay.StartLocalMqtt(ipAddress);
                                            await gateWay.StartLocalMqtt(ipAddress);
                                        }
                                        //主网关设置
@@ -259,6 +273,7 @@
                                var mess = ex.Message;
                            }
                        }
#endregion
                    }
                    catch (Exception ex)
                    {
@@ -305,7 +320,7 @@
                                    {
                                        gateWayList[j].getGatewayBaseInfo.IsMainGateWay = false;
                                    }
                                     break;
                                    break;
                                }
                            }
                        }