黄学彪
2019-11-25 160785587667cc0d927f85e44c139ec9dde13a9e
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -6,7 +6,7 @@
using Shared.Common;
using ZigBee.Device;
using static ZigBee.Device.ZbGateway;
namespace ZigBee.Common
{
@@ -34,7 +34,7 @@
            new System.Threading.Thread(async () =>
            {
                var gateWayList = new List<ZbGateway> { };
                var searchCount = 0;
                var searchCount = 6;
                var oldHomeID = string.Empty;
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                broadBytes[0] = 0xfe;
@@ -59,9 +59,9 @@
                        {
                            //因为那一瞬间,有可能mqtt会加回来,所以先加缓存
                            var list = new List<ZbGateway>();
                            list.AddRange(GateWayList);
                            list.AddRange(ZbGateway.GateWayList);
                            //然后清空掉
                            GateWayList.Clear();
                            ZbGateway.GateWayList.Clear();
                            //最后再断开mqtt连接
                            for (int i = 0; i < list.Count; i++)
                            {
@@ -90,49 +90,37 @@
                            }
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        System.Console.WriteLine($"Wifi{Shared.Application.IsWifi}");
                        if (Shared.Application.IsWifi)
                        {
                            if (0 < gateWayList.Count)
                            {
                                searchCount = 4;
                                //关闭远程服务器
                                if (RemoteMqttClient != null && RemoteMqttClient.IsConnected == true)
                                {
                                    await RemoteMqttClient?.DisconnectAsync();
                                    System.Console.WriteLine($"收到网关远程连接主动断开_{System.DateTime.Now.ToString()}");
                                }
                            }
                            else
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        if (Shared.Application.IsWifi)
                        {
                            if (0 < gateWayList.Count)
                            {
                                if (searchCount <= 0)
                                searchCount = 6;
                                ZbGateway.IsRemote = false;
                            }
                            else
                            {
                                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();
                            }
                        }
                                    ZbGateway.IsRemote = true;
                                }
                            }
                        }
                        else
                        {
                            ZbGateway.IsRemote = true;
                        }
                        searchCount--;
                         #region 2秒搜索一次网关
                        //每0.5秒广播发现一次网关,共2s
                        int count = 4;
                        #region 1秒搜索一次网关
                        //每0.5秒广播发现一次网关,共1s
                        int count = 2;
                        new System.Threading.Thread(() =>
                        {
                        {
                            gateWayList.Clear();
                            while (0 < count--)
                            {
                                try
@@ -148,10 +136,7 @@
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                                    }
                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("===" + e.Message);
                                }
                                catch { }
                                System.Threading.Thread.Sleep(500);
                            }
                        })
@@ -220,11 +205,11 @@
                                        }
                                    }
                                    //网关列表存储处理
                                    var gateWay = GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
                                    if (gateWay == null)
                                    {
                                        await zbGateWay.StartLocalMqtt(ipAddress);
                                        GateWayList.Add(zbGateWay);
                                        ZbGateway.GateWayList.Add(zbGateWay);
                                        NewGateWayAction?.Invoke(zbGateWay);
                                    }
                                    else
@@ -235,10 +220,10 @@
                                        if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress)
                                        {
                                            gateWay.DisConnect("2");
                                            GateWayList.Remove(gateWay);
                                            ZbGateway.GateWayList.Remove(gateWay);
                                            gateWay = zbGateWay;
                                            await zbGateWay.StartLocalMqtt(ipAddress);
                                            GateWayList.Add(gateWay);
                                            ZbGateway.GateWayList.Add(gateWay);
                                        }
                                        else
                                        {
@@ -252,9 +237,9 @@
                                        //主网关设置
                                        if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId)
                                        {
                                            for (int i = 0; i < GateWayList.Count; i++)
                                            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
                                            {
                                                var gw = GateWayList[i];
                                                var gw = ZbGateway.GateWayList[i];
                                                //网关ID不是当前的网关,则把网关列表中其他网关标记为不是主网关
                                                if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId)
                                                {
@@ -265,22 +250,14 @@
                                            gateWay.getGatewayBaseInfo.IsMainGateWay = true;
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                var mess = ex.Message;
                            }
                            catch { }
                        }
#endregion
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        var mess = ex.Message;
                    }
                    catch { }
                    System.Threading.Thread.Sleep(500);
                }
            })
            { IsBackground = true }.Start();
@@ -292,12 +269,14 @@
                {
                    try
                    {
                        //定时检测远程连接情况
                        await ZbGateway.StartCloudMqtt();
                        if (!ZbGateway.IsRemote)
                        {
                            System.Threading.Thread.Sleep(500);
                            continue;
                        }
                        var gateWayList = GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
                        var gateWayList =ZbGateway.GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
                        if (gateWayList.Count == 1)
                        {
@@ -305,28 +284,33 @@
                        }
                        else
                        {
                            for (int i = 0; i < gateWayList.Count; i++)
                            if (gateWayList.Find((obj) => obj.getGatewayBaseInfo.IsMainGateWay) == null)
                            {
                                var gateWay = gateWayList[i];
                                var info = await gateWay.GetZbGwInfoAsync();
                                if (info == null || info.getGwData == null)
                                for (int i = 0; i < gateWayList.Count; i++)
                                {
                                    continue;
                                }
                                if (info.getGwData.IsDominant == 1)
                                {
                                    gateWay.getGatewayBaseInfo.IsMainGateWay = true;
                                    for (int j = i + 1; j < gateWayList.Count; j++)
                                    var gateWay = gateWayList[i];
                                    var info = await gateWay.GetZbGwInfoAsync();
                                    if (info == null || info.getGwData == null)
                                    {
                                        gateWayList[j].getGatewayBaseInfo.IsMainGateWay = false;
                                    }
                                    break;
                                        continue;
                                    }
                                    if (info.getGwData.IsDominant == 1)
                                    {
                                        for (int j = 0; j < gateWayList.Count; j++)
                                        {
                                            if (gateWayList[i].getGatewayBaseInfo.gwID == info.getGwData.GwId)
                                            {
                                                gateWayList[i].getGatewayBaseInfo.IsMainGateWay = true;
                                            }
                                            else
                                            {
                                                gateWayList[i].getGatewayBaseInfo.IsMainGateWay = false;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        if (gateWayList.Find(obj => obj.getGatewayBaseInfo.IsMainGateWay == true) != null)
                        {
                            break;
                        }
                    }
                    catch { }