From c6b35c3138b944830b5336bf610f918154dd47c7 Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期六, 12 十月 2019 15:45:10 +0800 Subject: [PATCH] 合并门锁,完成住宅管理 --- ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 69 +++++++++++++++++++++------------- 1 files changed, 42 insertions(+), 27 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs index 3c400c1..50321cc 100755 --- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs +++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs @@ -3,6 +3,7 @@ using System.Net; using System.Net.Sockets; using System.Text; + using Shared.Common; using ZigBee.Device; using static ZigBee.Device.ZbGateway; @@ -11,7 +12,7 @@ { public static class Application { - static bool isInited; + public static bool isInited; static bool isEncry; /// <summary> /// 鏄惁姝e湪鎼滅储缃戝叧 @@ -29,10 +30,11 @@ return; } isInited = true; + new System.Threading.Thread(async () => { var gateWayList = new List<ZbGateway> { }; - var searchCount = 2; + var searchCount = 0; var oldHomeID = string.Empty; var broadBytes = new byte[44];// byteHomeId[0] ,//H broadBytes[0] = 0xfe; @@ -89,32 +91,44 @@ } var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress; - - //缃戝叧閫氳澶勭悊 - - if (0 < gateWayList.Count) + System.Console.WriteLine($"Wifi{Shared.Application.IsWifi}"); + if (Shared.Application.IsWifi) { - searchCount = 2; - gateWayList.Clear(); - var aa = GateWayList; - - //鍏抽棴杩滅▼鏈嶅姟鍣� - if (RemoteMqttClient!=null&& RemoteMqttClient.IsConnected==true) + if (0 < gateWayList.Count) { - await RemoteMqttClient?.DisconnectAsync(); - System.Console .WriteLine ($"鏀跺埌缃戝叧杩滅▼杩炴帴涓诲姩鏂紑_{System.DateTime.Now.ToString()}"); + searchCount = 4; + + //鍏抽棴杩滅▼鏈嶅姟鍣� + if (RemoteMqttClient != null && RemoteMqttClient.IsConnected == true) + { + await RemoteMqttClient?.DisconnectAsync(); + System.Console.WriteLine($"鏀跺埌缃戝叧杩滅▼杩炴帴涓诲姩鏂紑_{System.DateTime.Now.ToString()}"); + } + } + else + { + if (searchCount <= 0) + { + System.Console.WriteLine($"=====================3{System.DateTime.Now.ToString()}"); + gateWayList.Clear(); + //杩滅▼閫氳杩炴帴锛岃繛鎺ヤ簯绔湇鍔″櫒 + await StartCloudMqtt(); + } } } else - { - if (searchCount <= 0) - { - //杩滅▼閫氳杩炴帴锛岃繛鎺ヤ簯绔湇鍔″櫒 + { + if (searchCount <= 0) + { + System.Console.WriteLine($"=====================4{System.DateTime.Now.ToString()}"); + gateWayList.Clear(); + //杩滅▼閫氳杩炴帴锛岃繛鎺ヤ簯绔湇鍔″櫒 await StartCloudMqtt(); } } searchCount--; + #region 2绉掓悳绱竴娆$綉鍏� //姣�0.5绉掑箍鎾彂鐜颁竴娆$綉鍏筹紝鍏�2s int count = 4; new System.Threading.Thread(() => @@ -124,7 +138,8 @@ try { //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級 - for (int i = 0; i < gateWayList.Count;i++){ + for (int i = 0; i < gateWayList.Count; i++) + { FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes); } //骞挎挱鍙戦�� @@ -171,7 +186,6 @@ var pubKeyLengthByte1 = bytes[49 + gwNameLength + 1 + gwIdLength + 2]; int pubKeyLength = ((pubKeyLengthByte1 & 0xff) << 8 | (pubKeyLengthByte0 & 0xff)); var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength); - var zbGateWay = new ZbGateway { getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo @@ -192,24 +206,24 @@ //缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧 if (Shared.Common.Config.Instance.HomeId == homeID) { - gateWayList.Add(zbGateWay); + gateWayList.Add(zbGateWay); } //UI鐣岄潰姝e湪鎼滅储锛屼笉蹇呴厤褰撳墠浣忓畢鍒板埌缃戝叧姝ゆ椂涔熼�氳 else if (IsSearchingGateway) { - gateWayList.Add(zbGateWay); + gateWayList.Add(zbGateWay); } //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳 else if (homeID == string.Empty) { - gateWayList.Add(zbGateWay); + gateWayList.Add(zbGateWay); } } //缃戝叧鍒楄〃瀛樺偍澶勭悊 var gateWay = GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID); if (gateWay == null) { - await zbGateWay.StartLocalMqtt(ipAddress); + await zbGateWay.StartLocalMqtt(ipAddress); GateWayList.Add(zbGateWay); NewGateWayAction?.Invoke(zbGateWay); } @@ -223,7 +237,7 @@ gateWay.DisConnect("2"); GateWayList.Remove(gateWay); gateWay = zbGateWay; - await zbGateWay.StartLocalMqtt(ipAddress); + await zbGateWay.StartLocalMqtt(ipAddress); GateWayList.Add(gateWay); } else @@ -232,7 +246,7 @@ gateWay.getGatewayBaseInfo.Time = time; gateWay.getGatewayBaseInfo.GwName = gwName; gateWay.getGatewayBaseInfo.HomeId = homeID; - await gateWay.StartLocalMqtt(ipAddress); + await gateWay.StartLocalMqtt(ipAddress); } //涓荤綉鍏宠缃� @@ -259,6 +273,7 @@ var mess = ex.Message; } } +#endregion } catch (Exception ex) { @@ -305,7 +320,7 @@ { gateWayList[j].getGatewayBaseInfo.IsMainGateWay = false; } - break; + break; } } } -- Gitblit v1.8.0