黄学彪
2020-05-11 2e7e5f9af5b32cfe1fc3c6ba40bf7eb984bbd0a4
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -22,6 +22,10 @@
        /// 新发现的网关通知
        /// </summary>
        public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
        /// <summary>
        /// 上一次的住宅ID
        /// </summary>
        private static string oldHomeID = string.Empty;
        public static void Init()
        {
@@ -35,7 +39,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;
@@ -50,7 +53,9 @@
                    try
                    {
                        if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
                        {
                        {
                            //住宅ID为空只有一种可能就是退出了登录,这里的上一次住宅ID要清空
                            oldHomeID = "?";
                            System.Threading.Thread.Sleep(1000);
                            continue;
                        }
@@ -99,21 +104,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");
                            }
                        }
@@ -140,7 +152,11 @@
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                                    }
                                }
                                catch { }
                                catch
                                {
                                    //关闭Socket,下次发送会自动连接
                                    FindGateWaySocket.Stop();
                                }
                                System.Threading.Thread.Sleep(500);
                            }
                        })
@@ -180,7 +196,6 @@
                                        getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
                                        {
                                            IpAddress = ipAddress,
                                            Time = time,
                                            GwName = gwName,
                                            HomeId = homeID,
                                            IsMainGateWay = isMainGateWay,
@@ -202,11 +217,11 @@
                                        {
                                            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);
@@ -232,7 +247,6 @@
                                        else
                                        {
                                            gateWay.PubKey = pubKey;
                                            gateWay.getGatewayBaseInfo.Time = time;
                                            gateWay.getGatewayBaseInfo.GwName = gwName;
                                            gateWay.getGatewayBaseInfo.HomeId = homeID;
                                            await gateWay.StartLocalMqtt(ipAddress);
@@ -253,10 +267,30 @@
                                            //标记当前网关是主网关
                                            gateWay.getGatewayBaseInfo.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
                    }
@@ -274,48 +308,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);
@@ -324,6 +317,14 @@
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 重新搜索,注意:调用该方法,则促使全部网关的mqtt全部断开,然后重新搜索(可能会有1秒延迟)
        /// </summary>
        public static void ReSearch()
        {
            //为了那么多少万分之一的几率,这里设置成别的另一类的值
            oldHomeID = "**";
        }
        /// <summary>
        /// 接收处理UDP数据包