陈嘉乐
2020-07-10 48ba446936b51fffafa7c3600c0dadc6ac0e8c20
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -13,7 +13,6 @@
    public static class Application
    {
        public static bool isInited;
        static bool isEncry;
        /// <summary>
        /// 是否正在搜索网关
        /// </summary>
@@ -22,6 +21,10 @@
        /// 新发现的网关通知
        /// </summary>
        public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
        /// <summary>
        /// 上一次的住宅ID
        /// </summary>
        private static string oldHomeID = string.Empty;
        public static void Init()
        {
@@ -35,7 +38,6 @@
            {
                var gateWayList = new List<ZbGateway> { };
                var searchCount = 6;
                var oldHomeID = string.Empty;
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                broadBytes[0] = 0xfe;
                broadBytes[1] = 0x29;
@@ -81,12 +83,12 @@
                                if (paths.Length < 3)
                                    continue;
                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.getGatewayBaseInfo != null) && (obj.getGatewayBaseInfo.gwID == paths[2]));
                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
                                if (gateWay == null)
                                {
                                    gateWay = new ZbGateway { IsVirtual = true };
                                    gateWay.getGatewayBaseInfo.gwID = paths[2];
                                    gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
                                    gateWay.GwId = paths[2];
                                    gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
                                    ZbGateway.GateWayList.Add(gateWay);
                                }
                            }
@@ -101,21 +103,28 @@
                            {
                                searchCount = 6;
                                ZbGateway.IsRemote = false;
                                //当网关的连接方式改变时,记录当前的连接方式
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                            }
                            else
                            {
                                if (searchCount < 0)
                                {
                                    ZbGateway.IsRemote = true;
                                    //当网关的连接方式改变时,记录当前的连接方式
                                    Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                                }
                            }
                        }
                        else
                        {
                            ZbGateway.IsRemote = true;
                            //当网关的连接方式改变时,记录当前的连接方式
                            Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
                            {
                                 ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
                                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
                            }
                        }
@@ -134,7 +143,7 @@
                                    //点对点发送(先发一条已有的点播,回复几率高一点)
                                    for (int i = 0; i < gateWayList.Count; i++)
                                    {
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes);
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes);
                                    }
                                    //广播发送
                                    if (broadcastIpAddress.ToString() != "0.0.0.0")
@@ -142,7 +151,11 @@
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                                    }
                                }
                                catch { }
                                catch
                                {
                                    //关闭Socket,下次发送会自动连接
                                    FindGateWaySocket.Stop();
                                }
                                System.Threading.Thread.Sleep(500);
                            }
                        })
@@ -179,20 +192,16 @@
                                    var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
                                    var zbGateWay = new ZbGateway
                                    {
                                        getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
                                        {
                                            IpAddress = ipAddress,
                                            Time = time,
                                            GwName = gwName,
                                            HomeId = homeID,
                                            IsMainGateWay = isMainGateWay,
                                            gwID = id,
                                        },
                                        GwIP = ipAddress,
                                        GwName = gwName,
                                        HomeId = homeID,
                                        IsMainGateWay = isMainGateWay,
                                        GwId = id,
                                        GatewayOnlineFlage = true,
                                        PubKey = pubKey
                                    };
                                    //通讯到网关列表
                                    if (gateWayList.Find(obj => obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID) == null)
                                    if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null)
                                    {
                                        //网关匹配当前住宅中到网关
                                        if (Shared.Common.Config.Instance.HomeId == homeID)
@@ -204,14 +213,14 @@
                                        {
                                            gateWayList.Add(zbGateWay);
                                        }
                                        //网关中到住宅ID为空此时也通讯
                                        else if (homeID == string.Empty)
                                        {
                                            gateWayList.Add(zbGateWay);
                                        }
                                        //网关中到住宅ID为空此时也通讯 2020.01.14变更:没这个必要,上面那个变量就能处理
                                        //else if (homeID == string.Empty)
                                        //{
                                        //    gateWayList.Add(zbGateWay);
                                        //}
                                    }
                                    //网关列表存储处理
                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                                    if (gateWay == null)
                                    {
                                        ZbGateway.GateWayList.Add(zbGateWay);
@@ -223,7 +232,7 @@
                                        gateWay.IsVirtual = false;
                                        //将该网关标识为【可搜索到,即:在线】
                                        gateWay.GatewayOnlineFlage = true;
                                        if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress)
                                        if (gateWay.GwIP != ipAddress)
                                        {
                                            await gateWay.DisConnectLocalMqttClient("2");
                                            ZbGateway.GateWayList.Remove(gateWay);
@@ -234,31 +243,50 @@
                                        else
                                        {
                                            gateWay.PubKey = pubKey;
                                            gateWay.getGatewayBaseInfo.Time = time;
                                            gateWay.getGatewayBaseInfo.GwName = gwName;
                                            gateWay.getGatewayBaseInfo.HomeId = homeID;
                                            gateWay.GwName = gwName;
                                            gateWay.HomeId = homeID;
                                            await gateWay.StartLocalMqtt(ipAddress);
                                        }
                                        //主网关设置
                                        if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId)
                                        if (isMainGateWay && oldHomeID == gateWay.HomeId)
                                        {
                                            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
                                            {
                                                var gw = ZbGateway.GateWayList[i];
                                                //网关ID不是当前的网关,则把网关列表中其他网关标记为不是主网关
                                                if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId)
                                                if (gw.GwId != id && oldHomeID == gw.HomeId)
                                                {
                                                    gw.getGatewayBaseInfo.IsMainGateWay = false;
                                                    gw.IsMainGateWay = false;
                                                }
                                            }
                                            //标记当前网关是主网关
                                            gateWay.getGatewayBaseInfo.IsMainGateWay = true;
                                            gateWay.IsMainGateWay = true;
                                        }
                                    }
                                    //测试能否广播得到网关,通常情况下不检测
                                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
                                    {
                                        if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
                                        {
                                            if (gateWay == null)
                                            {
                                                Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = zbGateWay;
                                            }
                                            else
                                            {
                                                Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = gateWay;
                                            }
                                        }
                                    }
                                }
                            }
                            catch { }
                            catch
                            {
                                //关闭Socket,下次发送会自动连接
                                FindGateWaySocket.Stop();
                            }
                        }
                        #endregion
                    }
@@ -276,48 +304,7 @@
                    try
                    {
                        //定时检测远程连接情况
                        await ZbGateway.StartRemoteMqtt();
                        if (!ZbGateway.IsRemote)
                        {
                            System.Threading.Thread.Sleep(500);
                            continue;
                        }
                        var gateWayList =ZbGateway.GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
                        if (gateWayList.Count == 1)
                        {
                            gateWayList[0].getGatewayBaseInfo.IsMainGateWay = true;
                        }
                        else
                        {
                            if (gateWayList.Find((obj) => obj.getGatewayBaseInfo.IsMainGateWay) == null)
                            {
                                for (int i = 0; i < gateWayList.Count; i++)
                                {
                                    var gateWay = gateWayList[i];
                                    var info = await gateWay.GetZbGwInfoAsync();
                                    if (info == null || info.getGwData == null)
                                    {
                                        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;
                                    }
                                }
                            }
                        }
                        await ZbGateway.StartRemoteMqtt();
                    }
                    catch { }
                    System.Threading.Thread.Sleep(2000);
@@ -326,6 +313,14 @@
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 重新搜索,注意:调用该方法,则促使全部网关的mqtt全部断开,然后重新搜索(可能会有1秒延迟)
        /// </summary>
        public static void ReSearch()
        {
            //为了那么多少万分之一的几率,这里设置成别的另一类的值
            oldHomeID = "**";
        }
        /// <summary>
        /// 接收处理UDP数据包
@@ -334,7 +329,7 @@
        public static class FindGateWaySocket
        {
            //本地Socket
            public static System.Net.Sockets.Socket busSocket;
            public static Socket busSocket;
            /// <summary>
            /// 启动Socket接收和发送功能
@@ -380,7 +375,7 @@
            {
                get
                {
                    return null == busSocket ? false : true;
                    return busSocket == null ? false : true;
                }
            }