黄学彪
2020-07-20 2029e64ec0491f7e511dc15bffaf821bfbabe2de
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -12,19 +12,25 @@
{
    public static class Application
    {
        #region ■ 变量声明___________________________
        public static bool isInited;
        /// <summary>
        /// 是否正在搜索网关
        /// </summary>
        public static bool IsSearchingGateway = false;
        /// <summary>
        /// 新发现的网关通知
        /// </summary>
        public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
        /// <summary>
        /// 上一次的住宅ID
        /// </summary>
        private static string oldHomeID = string.Empty;
        /// <summary>
        /// 接收的IP
        /// </summary>
        private static List<string> listReceiveIP = new List<string>();
        #endregion
        #region ■ 初始化_____________________________
        public static void Init()
        {
@@ -37,10 +43,28 @@
            //初始化Socket
            FindGateWaySocket.Start();
            new System.Threading.Thread(async () =>
            //发送广播数据
            StartSendBroadcastData();
            //接收广播数据
            StartReceiveBroadcastData();
            ///开启远程连接
            StartRemoteMqtt();
        }
        #endregion
        #region ■ 发送广播数据_______________________
        /// <summary>
        /// 发送广播数据
        /// </summary>
        private static void StartSendBroadcastData()
            {
                var gateWayList = new List<ZbGateway> { };
                var searchCount = 6;
            new System.Threading.Thread(() =>
            {
                var searchCount = 3;
                var broadBytes = new byte[44];// byteHomeId[0] ,//H
                broadBytes[0] = 0xfe;
                broadBytes[1] = 0x29;
@@ -54,56 +78,18 @@
                {
                    try
                    {
                        if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
                        //检测能否发送广播
                        if (CheckCanSendBroadcast(broadBytes) == false)
                        {
                            //住宅ID为空只有一种可能就是退出了登录,这里的上一次住宅ID要清空
                            oldHomeID = "?";
                            System.Threading.Thread.Sleep(1000);
                            continue;
                        }
                        //首次进入网关,和切换住宅会清除网关列表,重新搜索存储
                        if (Shared.Common.Config.Instance.HomeId != oldHomeID)
                        {
                            //因为那一瞬间,有可能mqtt会加回来,所以先加缓存
                            var list = new List<ZbGateway>();
                            list.AddRange(ZbGateway.GateWayList);
                            //然后清空掉
                            ZbGateway.GateWayList.Clear();
                            //最后再断开mqtt连接
                            for (int i = 0; i < list.Count; i++)
                            {
                                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_"));
                            foreach (var filePath in gateWayFileList)
                            {
                                var paths = filePath.Split('_');
                                if (paths.Length < 3)
                                    continue;
                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
                                if (gateWay == null)
                                {
                                    gateWay = new ZbGateway { IsVirtual = true };
                                    gateWay.GwId = paths[2];
                                    gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
                                    ZbGateway.GateWayList.Add(gateWay);
                                }
                            }
                        }
                        var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
                        if (Shared.Application.IsWifi)
                        {
                            if (0 < gateWayList.Count)
                            if (0 < listReceiveIP.Count)
                            {
                                searchCount = 6;
                                searchCount = 3;
                                ZbGateway.IsRemote = false;
                                //当网关的连接方式改变时,记录当前的连接方式
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
@@ -128,48 +114,58 @@
                            {
                                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
                            }
                            //非wifi情况下,不需要广播
                            System.Threading.Thread.Sleep(1000);
                            continue;
                        }
                        searchCount--;
                        #region 1秒搜索一次网关
                        //每0.5秒广播发现一次网关,共1s
                        //每秒广播发现一次网关
                        int count = 2;
                        new System.Threading.Thread(() =>
                        {
                            gateWayList.Clear();
                        listReceiveIP.Clear();
                            while (0 < count--)
                            {
                                try
                                {
                                    //点对点发送(先发一条已有的点播,回复几率高一点)
                                    for (int i = 0; i < gateWayList.Count; i++)
                            for (int i = 0; i < listReceiveIP.Count; i++)
                                    {
                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes);
                                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{}
                                System.Threading.Thread.Sleep(500);
                            }
                        })
                        { IsBackground = true }.Start();
        }
                        while (0 < count)
        #endregion
        #region ■ 接收广播数据_______________________
        /// <summary>
        /// 接收广播数据
        /// </summary>
        private static void StartReceiveBroadcastData()
        {
            //数据接收
            new System.Threading.Thread(async () =>
            {
                while (true)
                        {
                            try
                            {
                                if (FindGateWaySocket.busSocket == null)
                        if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0)
                                {
                                    System.Threading.Thread.Sleep(100);
                            System.Threading.Thread.Sleep(500);
                                    continue;
                                }
                                var bytes = new byte[1024];
                                var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
                                if (bytes[43] == 0xA2)
                                {
@@ -198,23 +194,18 @@
                                        PubKey = pubKey
                                    };
                                    //通讯到网关列表
                                    if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null)
                            if (listReceiveIP.Find(obj => obj == zbGateWay.GwId) == null)
                                    {
                                        //网关匹配当前住宅中到网关
                                        if (Shared.Common.Config.Instance.HomeId == homeID)
                                        {
                                            gateWayList.Add(zbGateWay);
                                    listReceiveIP.Add(ipAddress);
                                        }
                                        //UI界面正在搜索,不必配当前住宅到到网关此时也通讯
                                        else if (IsSearchingGateway)
                                        {
                                            gateWayList.Add(zbGateWay);
                                    listReceiveIP.Add(ipAddress);
                                        }
                                        //网关中到住宅ID为空此时也通讯 2020.01.14变更:没这个必要,上面那个变量就能处理
                                        //else if (homeID == string.Empty)
                                        //{
                                        //    gateWayList.Add(zbGateWay);
                                        //}
                                    }
                                    //网关列表存储处理
                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
@@ -222,7 +213,6 @@
                                    {
                                        ZbGateway.GateWayList.Add(zbGateWay);
                                        await zbGateWay.StartLocalMqtt(ipAddress);
                                        NewGateWayAction?.Invoke(zbGateWay);
                                    }
                                    else
                                    {
@@ -265,6 +255,12 @@
                                    //测试能否广播得到网关,通常情况下不检测
                                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
                                    {
                                if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest == null)
                                {
                                    //防止异常,虽然几率很低
                                    Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway = 0;
                                    continue;
                                }
                                        if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
                                        {
                                            if (gateWay == null)
@@ -290,15 +286,19 @@
                            }
                            catch{}
                        }
                        #endregion
                    }
                    catch { }
                    System.Threading.Thread.Sleep(500);
                }
            })
            { IsBackground = true }.Start();
        }
            ///远程主网关更新
        #endregion
        #region ■ 开启远程连接_______________________
        /// <summary>
        /// 开启远程连接
        /// </summary>
        private static void StartRemoteMqtt()
        {
            new System.Threading.Thread(async () =>
            {
                while (true)
@@ -315,6 +315,10 @@
            { IsBackground = true }.Start();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 重新搜索,注意:调用该方法,则促使全部网关的mqtt全部断开,然后重新搜索(可能会有1秒延迟)
        /// </summary>
@@ -323,6 +327,61 @@
            //为了那么多少万分之一的几率,这里设置成别的另一类的值
            oldHomeID = "**";
        }
        /// <summary>
        /// 检测能否发送广播
        /// </summary>
        /// <returns></returns>
        private static bool CheckCanSendBroadcast(byte[] broadBytes)
        {
            if (string.IsNullOrEmpty(Config.Instance.HomeId))
            {
                //住宅ID为空只有一种可能就是退出了登录,这里的上一次住宅ID要清空
                oldHomeID = "?";
                System.Threading.Thread.Sleep(1000);
                return false;
            }
            //首次进入网关,和切换住宅会清除网关列表,重新搜索存储
            if (Config.Instance.HomeId != oldHomeID)
            {
                //因为那一瞬间,有可能mqtt会加回来,所以先加缓存
                var list = new List<ZbGateway>();
                list.AddRange(ZbGateway.GateWayList);
                //然后清空掉
                ZbGateway.GateWayList.Clear();
                //最后再断开mqtt连接
                for (int i = 0; i < list.Count; i++)
                {
                    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_"));
                foreach (var filePath in gateWayFileList)
                {
                    var paths = filePath.Split('_');
                    if (paths.Length < 3)
                        continue;
                    var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
                    if (gateWay == null)
                    {
                        gateWay = new ZbGateway { IsVirtual = true };
                        gateWay.GwId = paths[2];
                        gateWay.HomeId = Config.Instance.HomeId;
                        ZbGateway.GateWayList.Add(gateWay);
                    }
                }
            }
            return true;
        }
        #endregion
        #region ■ Socket_____________________________
        /// <summary>
        /// 接收处理UDP数据包
@@ -356,13 +415,9 @@
            /// </summary>
            public static void Stop()
            {
                if (!IsRunning)
                {
                    return;
                }
                try
                {
                    busSocket.Close();
                    busSocket?.Close();
                }
                catch { }
                busSocket = null;
@@ -425,5 +480,7 @@
                }
            }
        }
        #endregion
    }
}