HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-10-12 dc6493db59dcb0893eac50b72122f94c24056b3f
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -64,7 +64,6 @@
        {
            new System.Threading.Thread(() =>
            {
                var searchCount = 3;
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                broadBytes[0] = 0xfe;
                broadBytes[1] = 0x29;
@@ -85,57 +84,30 @@
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        if (Shared.Application.IsWifi)
                        //清空接收到的IP
                        listReceiveIP.Clear();
                        if (broadcastIpAddress.ToString() != "0.0.0.0")
                        {
                            if (0 < listReceiveIP.Count)
                            {
                                searchCount = 3;
                                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);
                                }
                            }
                            //广播出去
                            FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                        }
                        //等待3秒,下面的接收方法会去接收广播
                        System.Threading.Thread.Sleep(3000);
                        if (listReceiveIP.Count > 0)
                        {
                            //局域网能够广播得到网关
                            ZbGateway.IsRemote = false;
                            //当网关的连接方式改变时,记录当前的连接方式
                            Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                        }
                        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");
                            }
                            //非wifi情况下,不需要广播
                            System.Threading.Thread.Sleep(1000);
                            continue;
                        }
                        searchCount--;
                        //每秒广播发现一次网关
                        int count = 2;
                        listReceiveIP.Clear();
                        while (0 < count--)
                        {
                            //点对点发送(先发一条已有的点播,回复几率高一点)
                            for (int i = 0; i < listReceiveIP.Count; i++)
                            {
                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(listReceiveIP[i]), 7624), broadBytes);
                            }
                            //广播发送
                            if (broadcastIpAddress.ToString() != "0.0.0.0")
                            {
                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                            }
                            System.Threading.Thread.Sleep(1000);
                        }
                    }
                    catch { }
@@ -190,7 +162,6 @@
                                HomeId = homeID,
                                IsMainGateWay = isMainGateWay,
                                GwId = id,
                                GatewayOnlineFlage = true,
                                PubKey = pubKey
                            };
                            //通讯到网关列表
@@ -216,6 +187,7 @@
                            var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                            if (gateWay == null)
                            {
                                zbGateWay.OnlineTime = DateTime.Now;
                                ZbGateway.GateWayList.Add(zbGateWay);
                                await zbGateWay.StartLocalMqtt(ipAddress);
                            }
@@ -223,7 +195,7 @@
                            {
                                gateWay.IsVirtual = false;
                                //将该网关标识为【可搜索到,即:在线】
                                gateWay.GatewayOnlineFlage = true;
                                gateWay.OnlineTime = DateTime.Now;
                                if (gateWay.GwIP != ipAddress)
                                {
                                    await gateWay.DisConnectLocalMqttClient("2");
@@ -368,8 +340,10 @@
                }
                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_"));
                foreach (var filePath in gateWayFileList)
@@ -444,7 +418,12 @@
                    {
                        return;
                    }
                    udpClient.Send(bytes, bytes.Length, iPEndPoint);
                    int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
                    //调试用
                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
                    {
                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "发送长度(BeginSendTo):" + value + "  发送内容:" + System.Text.Encoding.UTF8.GetString(bytes));
                    }
                }
                catch (Exception ex)
                {