黄学彪
2020-11-19 9ef48d7b2da7c408b53f73be0f6eef3cbac1c84a
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -20,6 +20,10 @@
        /// </summary>
        public static bool IsSearchingGateway = false;
        /// <summary>
        /// 发送的时间间隔(单位:秒)
        /// </summary>
        public static int SendTime = 3;
        /// <summary>
        /// 上一次的住宅ID
        /// </summary>
        private static string oldHomeID = string.Empty;
@@ -64,7 +68,22 @@
        {
            new System.Threading.Thread(() =>
            {
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                var broadBytes = new byte[44];
                while (true)
                {
                    try
                    {
                        //检测能否发送广播 0:不能发送广播 1:可以发送广播 2:住宅ID变更
                        int result = CheckCanSendBroadcast();
                        if (result == 0)
                        {
                            continue;
                        }
                        if (result == 2)
                        {
                            //重新初始化广播数据
                            broadBytes = new byte[44];
                broadBytes[0] = 0xfe;
                broadBytes[1] = 0x29;
                broadBytes[2] = 0x00;
@@ -73,14 +92,8 @@
                broadBytes[5] = 0x00;
                broadBytes[6] = 0x00;
                broadBytes[43] = 0x02;
                while (true)
                {
                    try
                    {
                        //检测能否发送广播
                        if (CheckCanSendBroadcast(broadBytes) == false)
                        {
                            continue;
                            var tempBytes = System.Text.Encoding.UTF8.GetBytes(oldHomeID);
                            System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
@@ -93,7 +106,7 @@
                            FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                        }
                        //等待3秒,下面的接收方法会去接收广播
                        System.Threading.Thread.Sleep(3000);
                        System.Threading.Thread.Sleep(SendTime * 1000);
                        if (listReceiveIP.Count > 0)
                        {
@@ -137,7 +150,7 @@
                            System.Threading.Thread.Sleep(500);
                            continue;
                        }
                        var ipEndPoint = new System.Net.IPEndPoint(0, 0);
                        var ipEndPoint = new System.Net.IPEndPoint(IPAddress.Any, 0);
                        var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
                        if (bytes[43] == 0xA2)
                        {
@@ -187,15 +200,18 @@
                            var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                            if (gateWay == null)
                            {
                                zbGateWay.OnlineTime = DateTime.Now;
                                //刷新网关的在线时间点
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(zbGateWay.GwId);
                                ZbGateway.GateWayList.Add(zbGateWay);
                                await zbGateWay.StartLocalMqtt(ipAddress);
                            }
                            else
                            {
                                //刷新网关的在线时间点
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(gateWay.GwId);
                                gateWay.IsVirtual = false;
                                //将该网关标识为【可搜索到,即:在线】
                                gateWay.OnlineTime = DateTime.Now;
                                if (gateWay.GwIP != ipAddress)
                                {
                                    await gateWay.DisConnectLocalMqttClient("2");
@@ -260,6 +276,7 @@
                                Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
                            }
                        }
                        await System.Threading.Tasks.Task.Delay(20);
                    }
                    catch (Exception ex)
                    {
@@ -313,21 +330,24 @@
        }
        /// <summary>
        /// 检测能否发送广播
        /// 检测能否发送广播(0:不能发送广播 1:可以发送广播 2:住宅ID变更)
        /// </summary>
        /// <returns></returns>
        private static bool CheckCanSendBroadcast(byte[] broadBytes)
        private static int CheckCanSendBroadcast()
        {
            if (string.IsNullOrEmpty(Config.Instance.HomeId))
            {
                //住宅ID为空只有一种可能就是退出了登录,这里的上一次住宅ID要清空
                oldHomeID = "?";
                System.Threading.Thread.Sleep(1000);
                return false;
                return 0;
            }
            //首次进入网关,和切换住宅会清除网关列表,重新搜索存储
            if (Config.Instance.HomeId != oldHomeID)
            if (Config.Instance.HomeId == oldHomeID)
            {
                return 1;
            }
                //因为那一瞬间,有可能mqtt会加回来,所以先加缓存
                var list = new List<ZbGateway>();
                list.AddRange(ZbGateway.GateWayList);
@@ -339,10 +359,6 @@
                    list[i]?.DisConnectLocalMqttClient("1");
                }
                list.Clear();
                oldHomeID = Shared.Common.Config.Instance.HomeId;
                var tempBytes = System.Text.Encoding.UTF8.GetBytes(Shared.Common.Config.Instance.HomeId);
                System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
                //住宅中已经存在的网关如果局域网不存在,需要在当前住宅中虚拟一个网关ID相同的网关
                var gateWayFileList = Global.FileListByHomeId().FindAll(obj => obj.StartsWith("Gateway_"));
@@ -361,8 +377,10 @@
                        ZbGateway.GateWayList.Add(gateWay);
                    }
                }
            }
            return true;
            //住宅ID变更
            oldHomeID = Config.Instance.HomeId;
            return 2;
        }
        #endregion