From 8b9ce384b26c414db32f98e94e088f5334869c2d Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期三, 13 十一月 2019 15:36:28 +0800
Subject: [PATCH] 全部合并了代码,安卓和 IOS 都测试通过了

---
 ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs |  377 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 332 insertions(+), 45 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
index 7bfa527..168cc2b 100755
--- a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
+++ b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -15,11 +15,11 @@
         #region 鈻� 鍙橀噺澹版槑___________________________
 
         /// <summary>
-        /// 澶囦唤涓氬姟鐨勯�昏緫
+        /// 缃戝叧涓氬姟鐨勯�昏緫绫�
         /// </summary>
         private static HdlGatewayLogic m_Current = null;
         /// <summary>
-        /// 澶囦唤涓氬姟鐨勯�昏緫
+        /// 缃戝叧涓氬姟鐨勯�昏緫绫�
         /// </summary>
         public static HdlGatewayLogic Current
         {
@@ -179,20 +179,34 @@
         #region 鈻� 娣诲姞缃戝叧___________________________
 
         /// <summary>
-        /// 娣诲姞鏂扮綉鍏�(浠呴檺杩藉姞鏂扮殑缃戝叧  1:姝e父  -1:寮傚父  0:褰撳墠鐨勭綉鍏崇粦瀹氬湪浜嗗綋鍓嶈处鍙蜂笅鐨勪笉鍚屼綇瀹呴噷闈�)
+        /// 娣诲姞鏂扮綉鍏�(浠呴檺杩藉姞鏂扮殑缃戝叧)
         /// </summary>
         /// <param name="zbGateway">缃戝叧</param>
         /// <param name="mode">鏄惁鏄剧ず閿欒</param>
-        public async Task<int> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
+        public async Task<bool> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
         {
             //鎵ц娣诲姞缃戝叧鍒板唴瀛�
             var result = await this.DoAddGatewayToMemory(zbGateway, mode);
+            //鍓嶇殑缃戝叧缁戝畾鍦ㄤ簡褰撳墠璐﹀彿涓嬬殑涓嶅悓浣忓畢閲岄潰
+            if (result == 0)
+            {
+                if (mode == ShowErrorMode.YES)
+                {
+                    //缃戝叧缁戝畾鍦ㄥ綋鍓嶈处鍙蜂笅鐨勫叾浠栦綇瀹呴噷\r\n璇疯В闄ょ粦瀹氬悗鍐嶈瘯
+                    string msg = Language.StringByID(R.MyInternationalizationString.uTheGatewayInOtherResidenceMsg);
+                    if (msg.Contains("{0}") == true)
+                    {
+                        msg = string.Format(msg, "\r\n");
+                    }
+                    this.ShowTipMsg(msg);
+                }
+                return false;
+            }
             if (result != 1)
             {
-                return result;
+                return false;
             }
-
-            return 1;
+            return true;
         }
 
         /// <summary>
@@ -1013,6 +1027,198 @@
 
         #endregion
 
+        #region 鈻� 鑾峰彇鍗忚皟鍣ㄥ綋鍓嶄俊閬揰________________
+
+        /// <summary>
+        /// 鑾峰彇鍗忚皟鍣ㄥ綋鍓嶄俊閬�(浼氭湁绛夊緟寤惰繜,杩斿洖-1浠h〃閿欒)
+        /// </summary>
+        /// <param name="zbGateway"></param>
+        /// <returns></returns>
+        public int GetGatewayChannelId(ZbGateway zbGateway)
+        {
+            ZbGateway realWay = null;
+            if (this.GetRealGateway(ref realWay, zbGateway) == false)
+            {
+                //閿欒:缃戝叧瀵硅薄涓㈠け
+                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
+                return -1;
+            }
+            ChannelIdInfo data = null;
+            Action<string, string> action = (topic, message) =>
+            {
+                var gatewayID = topic.Split('/')[0];
+                if (topic == gatewayID + "/" + "ZbGw/GetChannel_Respon")
+                {
+                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<ChannelIdInfo>(jobject["Data"].ToString());
+                }
+            };
+            realWay.Actions += action;
+
+            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 8 } };
+            realWay.Send("ZbGw/GetChannel", jObject.ToString());
+
+            int TimeOut = 0;
+            while (data == null && TimeOut < 30)
+            {
+                System.Threading.Thread.Sleep(100);
+                TimeOut++;
+            }
+
+            realWay.Actions -= action;
+            if (data == null)
+            {
+                //鑾峰彇鍗忚皟鍣ㄤ俊閬撳け璐�
+                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayChannelIdFail);
+                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂");
+                this.ShowTipMsg(msg);
+                return -1;
+            }
+
+            return data.Channel;
+        }
+
+        /// <summary>
+        /// 缃戝叧淇¢亾淇℃伅
+        /// </summary>
+        private class ChannelIdInfo
+        {
+            /// <summary>
+            /// 缃戝叧淇¢亾
+            /// </summary>
+            public int Channel = -1;
+        }
+
+        #endregion
+
+        #region 鈻� 鑾峰彇鍗忚皟鍣∕AC______________________
+
+        /// <summary>
+        /// 鑾峰彇鍗忚皟鍣∕AC鍦板潃(浼氭湁绛夊緟寤惰繜,杩斿洖null浠h〃閿欒)
+        /// </summary>
+        /// <param name="zbGateway"></param>
+        /// <returns></returns>
+        public string GetGatewayCoordinatorMac(ZbGateway zbGateway)
+        {
+            ZbGateway realWay = null;
+            if (this.GetRealGateway(ref realWay, zbGateway) == false)
+            {
+                //閿欒:缃戝叧瀵硅薄涓㈠け
+                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
+                return null;
+            }
+            CoordinatorMacInfo data = null;
+            Action<string, string> action = (topic, message) =>
+            {
+                var gatewayID = topic.Split('/')[0];
+                if (topic == gatewayID + "/" + "ZbGw/GetMac_Respon")
+                {
+                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordinatorMacInfo>(jobject["Data"].ToString());
+                }
+            };
+            realWay.Actions += action;
+
+            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 13 } };
+            realWay.Send("ZbGw/GetMac", jObject.ToString());
+
+            int TimeOut = 0;
+            while (data == null && TimeOut < 30)
+            {
+                System.Threading.Thread.Sleep(100);
+                TimeOut++;
+            }
+
+            realWay.Actions -= action;
+            if (data == null)
+            {
+                //鑾峰彇鍗忚皟鍣∕ac澶辫触
+                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayCoordinatorMacFail);
+                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂");
+                this.ShowTipMsg(msg);
+                return null;
+            }
+
+            return data.MacAddr;
+        }
+
+        /// <summary>
+        /// 缃戝叧鍗忚皟鍣∕ac淇℃伅
+        /// </summary>
+        private class CoordinatorMacInfo
+        {
+            /// <summary>
+            /// 璋冨櫒Mac
+            /// </summary>
+            public string MacAddr = string.Empty;
+        }
+
+        #endregion
+
+        #region 鈻� 鑾峰彇鍗忚皟鍣≒anID____________________
+
+        /// <summary>
+        /// 鑾峰彇鍗忚皟鍣≒anID(浼氭湁绛夊緟寤惰繜,杩斿洖-1浠h〃閿欒)
+        /// </summary>
+        /// <param name="zbGateway"></param>
+        /// <returns></returns>
+        public int GetGatewayPanId(ZbGateway zbGateway)
+        {
+            ZbGateway realWay = null;
+            if (this.GetRealGateway(ref realWay, zbGateway) == false)
+            {
+                //閿欒:缃戝叧瀵硅薄涓㈠け
+                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
+                return -1;
+            }
+            PanIdInfo data = null;
+            Action<string, string> action = (topic, message) =>
+            {
+                var gatewayID = topic.Split('/')[0];
+                if (topic == gatewayID + "/" + "ZbGw/GetPanId_Respon")
+                {
+                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<PanIdInfo>(jobject["Data"].ToString());
+                }
+            };
+            realWay.Actions += action;
+
+            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 12 } };
+            realWay.Send("ZbGw/GetPanId", jObject.ToString());
+
+            int TimeOut = 0;
+            while (data == null && TimeOut < 30)
+            {
+                System.Threading.Thread.Sleep(100);
+                TimeOut++;
+            }
+
+            realWay.Actions -= action;
+            if (data == null)
+            {
+                //鑾峰彇鍗忚皟鍣≒anID澶辫触
+                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayPanIDFail);
+                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂");
+                this.ShowTipMsg(msg);
+                return -1;
+            }
+
+            return data.PANID;
+        }
+
+        /// <summary>
+        /// 缃戝叧PanId淇℃伅
+        /// </summary>
+        private class PanIdInfo
+        {
+            /// <summary>
+            /// PanId
+            /// </summary>
+            public int PANID = -1;
+        }
+
+        #endregion
+
         #region 鈻� 鑾峰彇缃戝叧GwInfo閲岄潰鐨勫睘鎬__________
 
         /// <summary>
@@ -1044,44 +1250,6 @@
             {
                 //鑾峰彇鏈湴鐨勫睘鎬� 
                 objValue = localWay.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGwInfo, null);
-            }
-
-            if (objValue == null)
-            {
-                return defult;
-            }
-            return objValue;
-        }
-
-        /// <summary>
-        /// 鑾峰彇缃戝叧GatewayBaseInfo閲岄潰鐨勫睘鎬�
-        /// </summary>
-        /// <param name="zbGateway">缃戝叧瀵硅薄</param>
-        /// <param name="attributeName">GatewayBaseInfo閲岄潰灞炴�х殑鍚嶅瓧</param>
-        /// <param name="defult">濡傛灉鑾峰彇涓嶅埌鏃讹紝璁剧疆鐨勯粯璁ゅ��</param>
-        /// <returns></returns>
-        public object GetGatewayBaseInfoAttribute(ZbGateway zbGateway, string attributeName, string defult = "")
-        {
-            string gwID = this.GetGatewayId(zbGateway);
-            var localWay = this.GetLocalGateway(gwID);
-
-            object objValue = null;
-            if (localWay == null || localWay.getGatewayBaseInfo == null)
-            {
-                //鏈湴娌℃湁璁板綍鏈夎繖涓笢瑗�,鍒欑洿鎺ヨ繑鍥炲弬鏁扮殑鏁版嵁
-                if (zbGateway.getGatewayBaseInfo != null)
-                {
-                    objValue = zbGateway.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, zbGateway.getGatewayBaseInfo, null);
-                }
-                else
-                {
-                    return defult;
-                }
-            }
-            else
-            {
-                //鑾峰彇鏈湴灞炴�� 
-                objValue = localWay.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGatewayBaseInfo, null);
             }
 
             if (objValue == null)
@@ -1966,6 +2134,74 @@
 
         #endregion
 
+        #region 鈻� 璁剧疆缃戝叧缁忕含搴____________________
+
+        /// <summary>
+        /// 璁剧疆缃戝叧缁忕含搴�
+        /// </summary>
+        /// <param name="gateway">缃戝叧瀵硅薄</param>
+        /// <param name="Longitude">缁忓害</param>
+        /// <param name="Latitude">缁村害</param>
+        /// <returns></returns>
+        public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude)
+        {
+            ZbGateway realWay = null;
+            if (this.GetRealGateway(ref realWay, gateway) == false)
+            {
+                //閿欒:缃戝叧瀵硅薄涓㈠け
+                string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
+                this.ShowTipMsg(msg);
+                return false;
+            }
+
+            int result = -1;
+            Action<string, string> action = (topic, message) =>
+            {
+                var gatewayID = topic.Split('/')[0];
+                if (topic == gatewayID + "/" + "Logic/SetSite_Respon")
+                {
+                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+                    result = Convert.ToInt32(jobject["Data"]["Result"].ToString());
+                }
+            };
+            realWay.Actions += action;
+            //涓や綅灏忔暟
+            Longitude = Math.Round(Longitude, 2);
+            Latitude = Math.Round(Latitude, 2);
+
+            int intLongitude = Convert.ToInt32(Longitude.ToString().Replace(".", string.Empty));
+            int intLatitude = Convert.ToInt32(Latitude.ToString().Replace(".", string.Empty));
+
+            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 2013 } };
+            var data = new Newtonsoft.Json.Linq.JObject { { "Longitude", intLongitude }, { "Latitude", intLatitude } };
+            jObject.Add("Data", data);
+            realWay.Send("Logic/SetSite", jObject.ToString());
+
+            int TimeOut = 0;
+            while (result == -1 && TimeOut < 30)
+            {
+                System.Threading.Thread.Sleep(100);
+                TimeOut++;
+            }
+
+            realWay.Actions -= action;
+            if (result != 0)
+            {
+                //璁剧疆缃戝叧缁忕含搴﹀け璐�
+                string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail);
+                if (result == -1)
+                {
+                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂");
+                }
+                this.ShowTipMsg(msg);
+                return false;
+            }
+
+            return true;
+        }
+
+        #endregion
+
         #region 鈻� 瑙g粦浜戠缃戝叧_______________________
 
         /// <summary>
@@ -2120,6 +2356,7 @@
         {
             //鑾峰彇浠庝簯绔偅閲屽緱鏉ョ殑鍏ㄩ儴鏂囦欢
             var listBackFile = HdlAutoBackupLogic.GetFileFromDirectory(UserCenterLogic.CombinePath(backDirectory));
+            listBackFile.Sort();
             //娣诲姞闄勫姞鎯呮姤:杩樺師璁惧閰嶇疆
             ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uRecoverDeviceSettion));
             ProgressBar.SetMaxValue(listBackFile.Count);
@@ -2276,12 +2513,62 @@
             }
             else if (backType == GatewayBackupEnum.A绐楀笜鏂瑰悜)
             {
+                var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData));
+                result = await HdlDeviceCurtainLogic.Current.SetCurtainDirection((Rollershade)device, statu);
             }
             else if (backType == GatewayBackupEnum.A绐楀笜鎵嬫媺鎺у埗)
             {
+                var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData));
+                result = await HdlDeviceCurtainLogic.Current.SetHandPullControl((Rollershade)device, statu);
             }
             else if (backType == GatewayBackupEnum.A绐楀笜涓婁笅闄愪綅)
             {
+                var curtainDevice = (Rollershade)device;
+                var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(System.Text.Encoding.UTF8.GetString(byteData));
+                int upLimit = Convert.ToInt32(recoverData["upLimit"]);
+                int downLimit = Convert.ToInt32(recoverData["downLimit"]);
+                //鍏堥噸缃獥甯�
+                result = await HdlDeviceCurtainLogic.Current.RestoreCurtain(curtainDevice);
+                if (result == false) { return -1; }
+                await Task.Delay(5000);
+                //閲嶇疆涓婇檺浣�
+                result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.UpLimit);
+                if (result == false) { return -1; }
+                //灏嗙獥甯樿皟鏁村埌鎸囧畾鐧惧垎姣�
+                curtainDevice.WcdGoToTiltValue(upLimit);
+                await Task.Delay(3000);
+                //鎵ц纭鍙婅鐩栫獥甯橀檺浣嶇偣
+                result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.UpLimit, -1, -1);
+                if (result == false) { return -1; }
+
+                await Task.Delay(2000);
+                //閲嶇疆涓嬮檺浣�
+                result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.DownLimit);
+                if (result == false) { return -1; }
+                //灏嗙獥甯樿皟鏁村埌鎸囧畾鐧惧垎姣�
+                curtainDevice.WcdGoToTiltValue(downLimit);
+                await Task.Delay(3000);
+                //鎵ц纭鍙婅鐩栫獥甯橀檺浣嶇偣
+                result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.DownLimit, -1, -1);
+            }
+            else if (backType == GatewayBackupEnum.A绌鸿皟鑷畾涔夋ā寮�)
+            {
+                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(System.Text.Encoding.UTF8.GetString(byteData));
+                result = await HdlDeviceAirConditionerLogic.Current.SetAcModeSupport((AC)device, data);
+                if (result == true)
+                {
+                    //杞崲涓轰簩杩涘埗
+                    var value = Convert.ToString(data, 2).PadLeft(16, '0');
+                    //杩欎簲涓缃槸鏀惧湪鍚庨潰鐨�
+                    var fixValue = value.Substring(0, value.Length - 5);
+                    var reportValue = value.Substring(fixValue.Length);
+                    //鏇存敼缂撳瓨
+                    for (int i = 0; i < reportValue.Length; i++)
+                    {
+                        ((AC)device).listSupportMode[i] = Convert.ToInt32(reportValue[i]);
+                    }
+                    device.ReSave();
+                }
             }
             return result == true ? 1 : -1;
         }

--
Gitblit v1.8.0