From 25429f085093d89d543a0b90e30d0d62d1b7dac9 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期二, 30 八月 2022 09:37:38 +0800 Subject: [PATCH] 合并了IOS的代码 --- ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 271 +++++++++++++++++++++++++++++------------------------ 1 files changed, 149 insertions(+), 122 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs index ca6fd17..d5f60c1 100755 --- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs +++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs @@ -19,6 +19,10 @@ /// 鏄惁姝e湪鎼滅储缃戝叧 /// </summary> public static bool IsSearchingGateway = false; + /// <summary> + /// 鍙戦�佺殑鏃堕棿闂撮殧(鍗曚綅:绉�) + /// </summary> + public static int SendTime = 3; /// <summary> /// 涓婁竴娆$殑浣忓畢ID /// </summary> @@ -64,78 +68,68 @@ { new System.Threading.Thread(() => { - var searchCount = 3; - var broadBytes = new byte[44];// byteHomeId[0] ,//H - broadBytes[0] = 0xfe; - broadBytes[1] = 0x29; - broadBytes[2] = 0x00; - broadBytes[3] = 0x00; - broadBytes[4] = 0x00; - broadBytes[5] = 0x00; - broadBytes[6] = 0x00; - broadBytes[43] = 0x02; + var broadBytes = new byte[44]; + var waitCount = 0; + while (true) { try - { - //妫�娴嬭兘鍚﹀彂閫佸箍鎾� - if (CheckCanSendBroadcast(broadBytes) == false) - { - continue; + { + //妫�娴嬭兘鍚﹀彂閫佸箍鎾� 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; + broadBytes[3] = 0x00; + broadBytes[4] = 0x00; + broadBytes[5] = 0x00; + broadBytes[6] = 0x00; + broadBytes[43] = 0x02; + 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; - 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(SendTime * 1000); + + if (listReceiveIP.Count > 0) + { + waitCount = 0; + //灞�鍩熺綉鑳藉骞挎挱寰楀埌缃戝叧 + ZbGateway.IsRemote = false; + //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮� + Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI); } else { + waitCount++; + if (waitCount >= 2) + { + //涓ゆ閮芥悳涓嶅埌,鎵嶆爣璁颁负杩滅▼ + continue; + } + waitCount = 0; + //灞�鍩熺綉骞挎挱涓嶅埌缃戝叧 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"); - } - //闈瀢ifi鎯呭喌涓�,涓嶉渶瑕佸箍鎾� - 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 { } @@ -165,9 +159,9 @@ 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) + if (bytes.Length > 43 && bytes[43] == 0xA2) { //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞� var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}"; @@ -190,7 +184,6 @@ HomeId = homeID, IsMainGateWay = isMainGateWay, GwId = id, - GatewayOnlineFlage = true, PubKey = pubKey }; //閫氳鍒扮綉鍏冲垪琛� @@ -216,14 +209,18 @@ var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId); if (gateWay == null) { + //鍒锋柊缃戝叧鐨勫湪绾挎椂闂寸偣 + 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.GatewayOnlineFlage = true; if (gateWay.GwIP != ipAddress) { await gateWay.DisConnectLocalMqttClient("2"); @@ -288,6 +285,7 @@ Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33); } } + await System.Threading.Tasks.Task.Delay(20); } catch (Exception ex) { @@ -338,57 +336,60 @@ { //涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊� oldHomeID = "**"; - } - - /// <summary> - /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾� - /// </summary> - /// <returns></returns> - private static bool CheckCanSendBroadcast(byte[] broadBytes) - { - if (string.IsNullOrEmpty(Config.Instance.HomeId)) - { - //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌� - oldHomeID = "?"; - System.Threading.Thread.Sleep(1000); - return false; - } - //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍 - if (Config.Instance.HomeId != oldHomeID) - { - //鍥犱负閭d竴鐬棿锛屾湁鍙兘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); - //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷D鐩稿悓鐨勭綉鍏� - 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; + } + + /// <summary> + /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾�(0:涓嶈兘鍙戦�佸箍鎾� 1:鍙互鍙戦�佸箍鎾� 2:浣忓畢ID鍙樻洿) + /// </summary> + /// <returns></returns> + private static int CheckCanSendBroadcast() + { + if (string.IsNullOrEmpty(Config.Instance.HomeId)) + { + //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌� + oldHomeID = "?"; + System.Threading.Thread.Sleep(1000); + return 0; + } + //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍 + if (Config.Instance.HomeId == oldHomeID) + { + return 1; + } + + //鍥犱负閭d竴鐬棿锛屾湁鍙兘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(); + + //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷D鐩稿悓鐨勭綉鍏� + 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); + } + } + //浣忓畢ID鍙樻洿 + oldHomeID = Config.Instance.HomeId; + + return 2; } #endregion @@ -408,13 +409,29 @@ /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳� /// </summary> /// <param name="port"></param> - public static void Start() + public static bool Start() { - if (udpClient!=null) + if (udpClient != null) { - return; + return true; } - udpClient = new UdpClient { EnableBroadcast = true }; + + //鑾峰彇鎵嬫満鐨剋ifi鍦板潃 +#if Android + var netwifi = new Shared.Net.NetWiFi(); + string phoneIp = netwifi.GetIPAddress(); +#endif +#if iOS + string phoneIp = Shared.Net.NetWiFi.GetIPAddress(); +#endif + //"127.0.0.1"鏄簳灞傚簱閲岄潰璁剧疆鐨勫垵濮嬮粯璁ゅ�� + if (phoneIp != "127.0.0.1") + { + udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(phoneIp), 7624)); + udpClient.EnableBroadcast = true; + return true; + } + return false; } /// <summary> @@ -440,11 +457,21 @@ { try { - if (udpClient == null) + if (Start() == false) { + //璋冭瘯鐢� + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1) + { + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "udp New涓嶈捣鏉�"); + } 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) { -- Gitblit v1.8.0