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/Device/ZbGateway.cs | 227 +++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 164 insertions(+), 63 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs b/ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs index e7b139f..eccff57 100755 --- a/ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs +++ b/ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs @@ -26,12 +26,26 @@ /// <summary> /// 涓荤綉鍏� /// </summary> - /// <value>The main gate way.</value> + [Newtonsoft.Json.JsonIgnore] public static ZbGateway MainGateWay { get - { - return GateWayList.Find((obj) => { return obj.HomeId == Shared.Common.Config.Instance.HomeId && obj.IsMainGateWay; }); + { + ZbGateway mainWay = null; + for (int i = 0; i < GateWayList.Count; i++) + { + if (GateWayList[i].HomeId == Config.Instance.HomeId) + { + //2020.07.16鍙樻洿:鍒閭d箞澶�,濡傛灉浣忓畢ID涓�鏍�,鍏堢‘瀹氬氨鏄畠浜�(涓嶇劧鏈夋椂鍊欑粡甯歌繑鍥瀗ull,鏈夊彲鑳芥槸鍒锋柊涓嶅埌) + mainWay = GateWayList[i]; + if (mainWay.IsMainGateWay == true) + { + //鐒跺悗濡傛灉瀹冪‘瀹炴槸涓荤綉鍏�,鐩存帴break + break; + } + } + } + return mainWay; } } @@ -2335,7 +2349,7 @@ /// </summary> static Guid currentGuid = Guid.NewGuid(); - public async System.Threading.Tasks.Task SendAesKey() + public async Task SendAesKey() { if (PubKey != null) { @@ -2354,12 +2368,11 @@ } } - public async System.Threading.Tasks.Task StartLocalMqtt(string brokerName) + public async Task StartLocalMqtt(string brokerName) { if (localMqttIsConnecting - || !Shared.Common.Config.Instance.IsLogin - || Shared.Common.Config.Instance.HomeId == "" - || localIsConnected) + || Shared.Common.Config.Instance.HomeId == "" + || localIsConnected) { return; } @@ -2400,6 +2413,11 @@ { DebugPrintLog($" 鏈湴杩炴帴鎴愬姛_缃戝叧IP:{brokerName}_缃戝叧鏄惁鍔�:{IsEncry}_褰撳墠瀵嗙爜:{Password}"); IsRemote = false; + //Log鍐欏叆(璋冭瘯鐢�) + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSendAndReceveDataToFile == 1) + { + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(2, "鏈湴杩炴帴鎴愬姛"); + } }); } @@ -2439,7 +2457,12 @@ } } catch (Exception ex) - { + { + //Log鍐欏叆(璋冭瘯鐢�) + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSendAndReceveDataToFile == 1) + { + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(2, "鏈湴杩炴帴寮傚父:\r\n" + ex.Message); + } DebugPrintLog($"灞�鍩熺綉閫氳杩炴帴鍑哄紓甯�:{ex.Message}"); } finally @@ -2449,22 +2472,15 @@ }); } - class GateWayBaseInfomation - { - //public string GatewayUniqueId;//"Emq缃戝叧1", <!-- 缃戝叧Mac --> - public bool MqttOnlineStatus;//: true, <!-- 缃戝叧鍦ㄤ簯绔疎mq鏄惁鍦ㄧ嚎 --> - public string AesKey;// : "a5d8da8a-ddea-48", <!-- 涓庢缃戝叧鍦ㄤ簯绔疎mq閫氫俊鏃讹紝璐熻浇鍔犺В鐨�16浣岮es瀵嗛挜 --> - public string MacMark;//: "61eaa0ea-4812-4a7a-86d6-3098c61e64ed" <!-- 缃戝叧瀵瑰簲鐨勫敮涓�鐮� --> - } /// <summary> /// 褰撳墠鏈夊笎鍙蜂笅鎵�鏈夌殑缃戝叧鍒楄〃鍙婁俊鎭� /// </summary> - static Dictionary<string, GateWayBaseInfomation> GateWayBaseInfomations = new Dictionary<string, GateWayBaseInfomation> { }; + public static Dictionary<string, Shared.Phone.UserCenter.GatewayResult> GateWayBaseInfomations = new Dictionary<string, Shared.Phone.UserCenter.GatewayResult> { }; /// <summary> /// 鑾峰彇褰撳墠甯愬彿鎵�鏈夌殑缃戝叧淇℃伅 /// </summary> /// <returns></returns> - static async System.Threading.Tasks.Task initGateWayBaseInfomation() + static async Task initGateWayBaseInfomation() { if (AllowRemoteCtrl == false) { @@ -2482,9 +2498,7 @@ MainAccountId = Config.Instance.Home.MainUserDistributedMark, SharedHid = Config.Instance.Home.Id }; - - var listNotShow = new List<string>() { "NotSetAgain" }; - var resultData = await Shared.Phone.UserCenter.UserCenterLogic.GetByteResponseDataByRequestHttps("App/GetSharedHomeApiControl", false, pra, listNotShow); + var resultData = Shared.Phone.UserCenter.UserCenterLogic.GetByteResponseDataByRequestHttps("App/GetSharedHomeApiControl", false, pra, new List<string> { "NotCheck" }); if (resultData == null) { return; @@ -2505,7 +2519,7 @@ pra2.ReqDto.LoginAccessToken = loginToken; var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(pra2)); - var result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/App/GetSingleHomeGatewayPagger", bytes, loginToken); + var result = CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/App/GetSingleHomeGatewayPagger", bytes, loginToken); if (result != null) { var jobject = Newtonsoft.Json.Linq.JObject.Parse(Encoding.UTF8.GetString(result)); @@ -2530,7 +2544,7 @@ var list = JArray.Parse(responseData["PageData"].ToString()); foreach (var v in list) { - GateWayBaseInfomations[v["GatewayUniqueId"].ToString()] = new GateWayBaseInfomation { AesKey = v["AesKey"].ToString(), MacMark = v["MacMark"].ToString(), MqttOnlineStatus = bool.Parse(v["MqttOnlineStatus"].ToString()) }; + GateWayBaseInfomations[v["GatewayUniqueId"].ToString()] = new Shared.Phone.UserCenter.GatewayResult { AesKey = v["AesKey"].ToString(), MacMark = v["MacMark"].ToString(), MqttOnlineStatus = bool.Parse(v["MqttOnlineStatus"].ToString()) }; //if (RemoteMqttClient.IsConnected) { await RemoteMqttClient.SubscribeAsync($"/ZigbeeGateWayToClient/{v["MacMark"].ToString()}/#", MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce); @@ -2557,23 +2571,37 @@ public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); static bool remoteIsConnected; + /// <summary> + /// 杩滅▼寮�濮嬭繛鎺ョ殑鏃堕棿鐐� + /// </summary> + private static DateTime RemoteConnectTime = DateTime.Now; /// <summary> /// 鍚姩杩滅▼Mqtt /// </summary> /// <returns>The start.</returns> /// <param name="brokerName">Broker name.</param> - public static async System.Threading.Tasks.Task StartRemoteMqtt() + public static async Task StartRemoteMqtt() { //杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺 if (AllowRemoteCtrl == false - || remoteMqttIsConnecting - || !Shared.Common.Config.Instance.IsLogin || Shared.Common.Config.Instance.HomeId == "" || remoteIsConnected) { return; } - await System.Threading.Tasks.Task.Factory.StartNew(async () => + //濡傛灉杩滅▼杩樺湪杩炴帴涓� + if (remoteMqttIsConnecting == true) + { + //濡傛灉杩欎釜鍙橀噺涓�鐩村浜庤繛鎺ヤ腑鐨勭姸鎬�,浣嗘槸宸茬粡杩囧幓浜�10绉掍簡,杩樻槸true鐨勮瘽,璇存槑杩欓噷鏄湁鐐归棶棰樼殑,闇�瑕侀噸鏂板垱寤� + if ((DateTime.Now - RemoteConnectTime).TotalMilliseconds < 10 * 1000) + { + return; + } + } + //璁板綍璧疯繖娆¤繙绋嬭繛鎺ョ殑鏃堕棿鐐� + RemoteConnectTime = DateTime.Now; + + await Task.Factory.StartNew(async () => { try { @@ -2636,44 +2664,50 @@ //璁㈤槄涓�涓尋涓嬬嚎鐨勪富棰� await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + Config.Instance.ConnEmqClientId + "/Push/NotifySqueeze"); - await initGateWayBaseInfomation(); - - //娌℃湁涓荤綉鍏虫椂涓诲姩璇诲彇锛岃幏鍙栦富缃戝叧淇℃伅 - var gateWayList = GateWayList.FindAll(obj => obj.HomeId == Shared.Common.Config.Instance.HomeId); - if (gateWayList.Find(obj => obj.IsMainGateWay == true) == null) + //濡傛灉杩欎釜鍑芥暟鍗′箙浜嗙殑璇濓紝浼氭帴鏀跺埌浜戠鎺ㄩ�佺殑鎸や笅绾夸富棰橈紝涓嶇煡閬撲负浠�涔� + new Thread(async () => { - if (gateWayList.Count == 1) + await initGateWayBaseInfomation(); + + //娌℃湁涓荤綉鍏虫椂涓诲姩璇诲彇锛岃幏鍙栦富缃戝叧淇℃伅 + var gateWayList = GateWayList.FindAll(obj => obj.HomeId == Shared.Common.Config.Instance.HomeId); + if (gateWayList.Find(obj => obj.IsMainGateWay == true) == null) { - gateWayList[0].IsMainGateWay = true; - } - else - { - for (int i = 0; i < gateWayList.Count; i++) + if (gateWayList.Count == 1) { - var gateWay = gateWayList[i]; - var info = await gateWay.GetZbGwInfoAsync(); - if (info == null || info.getGwData == null) + gateWayList[0].IsMainGateWay = true; + } + else + { + for (int i = 0; i < gateWayList.Count; i++) { - continue; - } - if (info.getGwData.IsDominant == 1) - { - for (int j = 0; j < gateWayList.Count; j++) + var gateWay = gateWayList[i]; + var info = await gateWay.GetZbGwInfoAsync(); + if (info == null || info.getGwData == null) { - if (gateWayList[i].GwId == info.getGwData.GwId) - { - gateWayList[i].IsMainGateWay = true; - } - else - { - gateWayList[i].IsMainGateWay = false; - } + continue; } - break; + if (info.getGwData.IsDominant == 1) + { + for (int j = 0; j < gateWayList.Count; j++) + { + if (gateWayList[i].GwId == info.getGwData.GwId) + { + gateWayList[i].IsMainGateWay = true; + } + else + { + gateWayList[i].IsMainGateWay = false; + } + } + break; + } } } } - } + }) + { IsBackground = true }.Start(); + }); } #endregion @@ -2695,7 +2729,7 @@ ["PublishPayloadJsonStr"] = "1" }; var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)); - result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/GetConnMqttInfo", bytes, Config.Instance.Token); + result = CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/GetConnMqttInfo", bytes, Config.Instance.Token); } else { @@ -2712,7 +2746,7 @@ ["PublishPayloadJsonStr"] = "1" }; var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)); - result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/ShareMemberConnMqttInfo", bytes, Config.Instance.Token); + result = CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/ShareMemberConnMqttInfo", bytes, Config.Instance.Token); } if (result != null) { @@ -2723,8 +2757,6 @@ AllowRemoteCtrl = false; return; } - //铏界劧涓嶇煡閬撳鎸や笅绾块偅涓�鍧楁湁娌℃湁鐢�,涓嶈繃鍏堣繖涔堝紕 - await System.Threading.Tasks.Task.Delay(1500); var responseData = jobject["ResponseData"]; if (responseData != null) @@ -2772,13 +2804,15 @@ /// <summary> /// 鏂紑鏈嶅姟鍣ㄨ繛鎺� /// </summary> - public async System.Threading.Tasks.Task DisConnectLocalMqttClient(string s) + public async Task DisConnectLocalMqttClient(string s) { try { if (localIsConnected) { localIsConnected = false; + //杩欎釜涓滆タ涔熻寮� + localMqttIsConnecting = false; DebugPrintLog($"Local涓诲姩鏂紑_{s}"); //await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None); await localMqttClient.DisconnectAsync(); @@ -2788,18 +2822,38 @@ { DebugPrintLog($"Local鏂紑閫氳杩炴帴鍑哄紓甯�:{ex.Message}"); } + } + + /// <summary> + /// 寮哄埗鏂紑鏈湴鐨勭綉鍏宠繛鎺� + /// </summary> + public async Task CloseLocalConnectionOnForce() + { + try + { + await localMqttClient.DisconnectAsync(); + } + catch { } + finally + { + localIsConnected = false; + //杩欎釜涓滆タ涔熻寮� + localMqttIsConnecting = false; + } } /// <summary> /// 鏂紑杩滅▼Mqtt鐨勯摼鎺� /// </summary> - public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient(string s = "") + public static async Task DisConnectRemoteMqttClient(string s = "") { try { if (remoteIsConnected) { remoteIsConnected = false; + //杩欎釜涓滆タ涔熻寮� + remoteMqttIsConnecting = false; DebugPrintLog($"Remote涓诲姩鏂紑_{s}"); //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None); await RemoteMqttClient.DisconnectAsync(); @@ -2808,6 +2862,25 @@ catch (Exception e) { DebugPrintLog($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}"); + } + } + + /// <summary> + /// 寮哄埗鏂紑杩滅▼Mqtt鐨勯摼鎺� + /// </summary> + /// <returns></returns> + public static async Task CloseRemoteConnectionOnForce() + { + try + { + await RemoteMqttClient.DisconnectAsync(); + } + catch { } + finally + { + remoteIsConnected = false; + //杩欎釜涓滆タ涔熻寮� + remoteMqttIsConnecting = false; } } @@ -2889,6 +2962,16 @@ if (Shared.Common.Config.Instance.HomeId == "") { return; + } + + //Log鍐欏叆(璋冭瘯鐢�) + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSendAndReceveDataToFile == 1) + { + string text = "杩滅▼鍙戦��:"; + if (IsRemote == false) { text = "鏈湴鍙戦��:"; } + text += topic + "\r\n"; + text += Encoding.UTF8.GetString(message) + "\r\n"; + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(2, text); } if (IsRemote) @@ -2923,6 +3006,11 @@ { DebugPrintLog($"灞�鍩熺綉鈥斺�斾簩娆″彂閫佸埌缃戝叧鐨勪富棰�:{topic}_鍙戦�佸埌缃戝叧鐨勬暟鎹�:{System.Text.Encoding.UTF8.GetString(message)}_鏄惁鍔犲瘑:{IsEncry}"); await localMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topic, Payload = message, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, Retain = retain }); + } + //Log鍐欏叆(璋冭瘯鐢�) + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSendAndReceveDataToFile == 1) + { + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(2, "鏈湴杩炴帴寮傚父鏂紑"); } } } @@ -3137,6 +3225,14 @@ epoint = topic.Split('/')[3]; cluID = topic.Split('/')[4]; attrId = topic.Split('/')[5]; + } + + //Log鍐欏叆(璋冭瘯鐢�) + if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSendAndReceveDataToFile == 1) + { + string text = "缃戝叧鍥炲:" + topic + "\r\n"; + text += message + "\r\n"; + Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(2, text); } //鍏ㄥ眬鎺ユ敹缃戝叧鎺ㄩ�佺殑鐨勯�昏緫(涓轰簡鎵ц閫熷害,灏藉彲鑳界殑鍒姞鑰楁椂鐨勬搷浣�) @@ -3484,6 +3580,11 @@ /// </summary> public void ReSave() { + if (Shared.Common.Config.Instance.Home.IsShowTemplate == true) + { + //灞曠ず妯℃澘鏃�,涓嶅厑璁镐繚瀛樻枃浠�(闃叉灞炴�т笂鎶ョ敤鐨�) + return; + } Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this))); } #endregion @@ -3500,7 +3601,7 @@ #if DEBUG if (flage == true) { - //if (msg.Contains("DeviceStatusReport") == false) + //if (msg.Contains("DeviceInfoRespon") == true) { System.Console.WriteLine(msg + " " + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond); } -- Gitblit v1.8.0