From f1c3921b08bb22ac6f5db22d620e01d7e8e5c49f Mon Sep 17 00:00:00 2001
From: WJC <wjc@hdlchina.com.cn>
Date: 星期一, 30 十二月 2019 13:32:33 +0800
Subject: [PATCH] 2019-12-30-1

---
 ZigbeeApp/Shared/Common/Device.cs |  243 ++++++++++++++++++++++-------------------------
 1 files changed, 114 insertions(+), 129 deletions(-)

diff --git a/ZigbeeApp/Shared/Common/Device.cs b/ZigbeeApp/Shared/Common/Device.cs
index 85ac646..7259cdb 100755
--- a/ZigbeeApp/Shared/Common/Device.cs
+++ b/ZigbeeApp/Shared/Common/Device.cs
@@ -37,22 +37,21 @@
             }
         }
         /// <summary>
-        /// 鏈湴鎵�鏈夎澶囩殑缂撳瓨
+        /// 鏈湴鎵�鏈夎澶囩殑缂撳瓨(鎺掑簭)
         /// </summary>
         public List<CommonDevice> listAllDevice
         {
             get
             {
-                var list = new List<CommonDevice>();
-                foreach (var listMac in this.dicDeviceSort.Values)
+                lock (dicAllDevice)
                 {
-                    foreach (var strMac in listMac)
+                    var list = new List<CommonDevice>();
+                    foreach (var device in dicAllDevice.Values)
                     {
-                        var listTemp = this.GetDevicesByMac(strMac);
-                        list.AddRange(listTemp);
+                        list.Add(device);
                     }
+                    return list;
                 }
-                return list;
             }
         }
 
@@ -85,13 +84,9 @@
         /// </summary>
         private Dictionary<string, HashSet<int>> dicDeviceEpoint = new Dictionary<string, HashSet<int>>();
         /// <summary>
-        /// 璁惧Mac椤哄簭(閲岄潰鏄疢ac鍦板潃)
+        /// 鐗╃悊璁惧灞炰簬鍝釜鎴块棿鐨勮褰�
         /// </summary>
-        private Dictionary<string, List<string>> dicDeviceSort = null;
-        /// <summary>
-        /// 璁惧鏂囦欢鐨勫墠缂�鍚嶅瓧
-        /// </summary>
-        public const string deviceFirstName = "Device_";
+        private Dictionary<string, string> dicDeviceRoomId = null;
 
         #endregion
 
@@ -175,9 +170,9 @@
                 }
             }
             //鎴愬憳韬唤鐨勬椂鍊�,鍒犻櫎鎺夐潪娉曠殑缃戝叧鏂囦欢
-            this.DeleteGatewayFileByMemberModel();
-            //鍒濆鍖栬澶囬『搴�
-            this.InitDeviceSort();
+            this.DeleteGatewayFileByMemberModel();
+            //鍒濆鍖栫墿鐞嗚澶囨墍灞炴埧闂寸殑璁板綍
+            this.InitRealDeviceRoomId();
         }
 
         /// <summary>
@@ -223,11 +218,11 @@
         /// <param name="zbGateway">缃戝叧瀵硅薄</param>
         /// <param name="deviceComingAction">鎺ユ敹鍒拌澶囨椂鐨勪簨浠�,璁惧瀵硅薄涓簄ull鏃�,浠h〃鎺ユ敹瀹屾垚</param>
         /// <returns>涓�鐩磋繑鍥瀟rue</returns>
-        public async Task<bool> SetDeviceToMemmoryByGateway(ZbGateway zbGateway, Action<CommonDevice> deviceComingAction = null)
+        public bool SetDeviceToMemmoryByGateway(ZbGateway zbGateway, Action<CommonDevice> deviceComingAction = null)
         {
             //浠庣綉鍏宠幏鍙栧叏閮ㄧ殑璁惧
             List<CommonDevice> listDevice = new List<CommonDevice>();
-            List<CommonDevice> list = await this.GetDeviceListFromGateway(zbGateway, true, deviceComingAction);
+            List<CommonDevice> list = this.GetDeviceListFromGateway(zbGateway, true, deviceComingAction);
 
             if (list == null)
             {
@@ -312,16 +307,6 @@
         /// <param name="device">璁惧瀵硅薄(杩欎釜涓滆タ鏈夊彲鑳戒細琚洿鏀�)</param>
         public void AddDeviceToMemory(ref CommonDevice device)
         {
-            if (dicDeviceSort.ContainsKey(device.CurrentGateWayId) == false)
-            {
-                dicDeviceSort[device.CurrentGateWayId] = new List<string>();
-            }
-            if (dicDeviceSort[device.CurrentGateWayId].Contains(device.DeviceAddr) == false)
-            {
-                //淇濆瓨椤哄簭
-                dicDeviceSort[device.CurrentGateWayId].Add(device.DeviceAddr);
-                UserCenterLogic.SaveFileContent(UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DeviceSortFile), dicDeviceSort);
-            }
             string mainKeys = this.GetDeviceMainKeys(device);
             //濡傛灉瀹冩槸鍗囩骇鐨勯《绔鐐�,鍒欎笉鑳借瀹冨姞鍏ュ埌缂撳瓨锛屼絾鏄彲浠ヨ浠栫敓鎴愭枃浠�
             if (device is OTADevice)
@@ -469,6 +454,11 @@
                 }
                 //澶囦唤鏁版嵁
                 await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(device, GatewayBackupEnum.AMac鍚嶇О, newMacName);
+                //濡傛灉瀹冨彧鏈変竴涓洖璺�,鍒欐洿鏀圭鐐瑰悕瀛�
+                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == true && this.dicDeviceEpoint[device.DeviceAddr].Count == 1)
+                {
+                    return await this.ReName(device, newMacName);
+                }
             }
             return true;
         }
@@ -521,18 +511,17 @@
                 this.ShowErrorMsg(msg);
                 return false;
             }
-            if (dicDeviceSort.ContainsKey(listdevice[0].CurrentGateWayId) == true &&
-                dicDeviceSort[listdevice[0].CurrentGateWayId].Contains(info.DeviceAddr) == true)
-            {
-                //淇濆瓨椤哄簭
-                dicDeviceSort[listdevice[0].CurrentGateWayId].Remove(info.DeviceAddr);
-                UserCenterLogic.SaveFileContent(UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DeviceSortFile), dicDeviceSort);
-            }
 
             //鍒犻櫎鏂囦欢
             foreach (CommonDevice device in listdevice)
             {
                 this.DeleteMemmoryDevice(device);
+            }
+            if (this.dicDeviceRoomId.ContainsKey(listdevice[0].DeviceAddr) == true)
+            {
+                //绉婚櫎鐪熷疄璁惧鐨勬埧闂寸储寮�
+                this.dicDeviceRoomId.Remove(listdevice[0].DeviceAddr);
+                this.SaveRealDeviceRoomId(null, null);
             }
             return true;
         }
@@ -683,32 +672,14 @@
             List<CommonDevice> list = new List<CommonDevice>();
             lock (dicAllDevice)
             {
-                List<string> listSort = null;
-                if (dicDeviceSort.ContainsKey(gwId) == false)
-                {
-                    dicDeviceSort[gwId] = new List<string>();
-                }
-                listSort = dicDeviceSort[gwId];
-
                 //鍚勭綉鍏崇殑鎵�鏈夎澶�
                 foreach (CommonDevice device in this.dicAllDevice.Values)
                 {
                     if (gwId == device.CurrentGateWayId)
                     {
-                        if (listSort.Contains(device.DeviceAddr) == false)
-                        {
-                            listSort.Add(device.DeviceAddr);
-                        }
+                        list.Add(device);
                     }
                 }
-                //鎺掑簭
-                foreach (var strMac in listSort)
-                {
-                    var listTemp = this.GetDevicesByMac(strMac);
-                    list.AddRange(listTemp);
-                }
-                //淇濆瓨椤哄簭
-                UserCenterLogic.SaveFileContent(UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DeviceSortFile), dicDeviceSort);
             }
 
             return list;
@@ -776,6 +747,20 @@
         }
 
         /// <summary>
+        /// 鏍规嵁MAC鍦板潃,鑾峰彇鍏ㄩ儴鍥炶矾鐨勬暟閲�
+        /// </summary>
+        /// <param name="DeviceAddr">Mac鍦板潃</param>
+        /// <returns></returns>
+        public int GetDevicesCountByMac(string DeviceAddr)
+        {
+            if (dicDeviceEpoint.ContainsKey(DeviceAddr) == false)
+            {
+                return 0;
+            }
+            return dicDeviceEpoint[DeviceAddr].Count;
+        }
+
+        /// <summary>
         /// 鑾峰彇鏈湴鍏ㄩ儴鐨勮澶囨枃浠�
         /// </summary>
         /// <returns></returns>
@@ -783,9 +768,10 @@
         {
             List<string> listDeviceFile = new List<string>();
             List<string> listAllFile = Global.FileListByHomeId();
+
             foreach (string file in listAllFile)
             {
-                if (file.StartsWith(deviceFirstName) == false)
+                if (file.StartsWith("Device_") == false)
                 {
                     //濡傛灉涓嶆槸璁惧鏂囦欢
                     continue;
@@ -1000,6 +986,11 @@
         /// <returns></returns>
         public void SetDeviceIconToControl(Button btnIcon, CommonDevice device)
         {
+            if (device == null)
+            {
+                btnIcon.UnSelectedImagePath = "Device/ThirdPartyDevice.png";
+                return;
+            }
             string unSelectFilePath = string.Empty;
             string selectFilePath = string.Empty;
 
@@ -1007,6 +998,7 @@
             this.GetDeviceIcon(device, ref unSelectFilePath, ref selectFilePath);
 
             btnIcon.UnSelectedImagePath = unSelectFilePath;
+            btnIcon.SelectedImagePath = selectFilePath;
         }
 
         /// <summary>
@@ -1162,14 +1154,14 @@
         public DeviceUI GetDeviceUI(string filePath)
         {
             string[] arry = filePath.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
-            if (arry.Length != 5)
+            if (arry.Length != 4)
             {
                 return null;
             }
             //鍒涘缓涓�涓柊鐨勪笢瑗跨粰杩囧幓
             var deviceUi = new DeviceUI();
-            deviceUi.DeviceAddr = arry[3];
-            deviceUi.DeviceEpoint = Convert.ToInt32(arry[4]);
+            deviceUi.DeviceAddr = arry[2];
+            deviceUi.DeviceEpoint = Convert.ToInt32(arry[3]);
             return deviceUi;
         }
 
@@ -1511,89 +1503,82 @@
 
         #endregion
 
-        #region 鈻� 璁惧鎺掑簭___________________________
+        #region 鈻� 鐗╃悊璁惧鎵�灞炴埧闂確__________________
 
         /// <summary>
-        /// 鍒濆鍖栬澶囬『搴�
+        /// 鍒濆鍖栫墿鐞嗚澶囨墍灞炴埧闂寸殑璁板綍
         /// </summary>
-        private void InitDeviceSort()
+        private void InitRealDeviceRoomId()
         {
-            //璇诲彇璁惧椤哄簭
-            this.dicDeviceSort = new Dictionary<string, List<string>>();
-            string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DeviceSortFile);
+            this.dicDeviceRoomId = new Dictionary<string, string>();
+            string fullName = UserCenterLogic.CombinePath(DirNameResourse.DeviceRoomIdFile);
             var strData = UserCenterLogic.LoadFileContent(fullName);
             if (strData != null)
             {
-                this.dicDeviceSort = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(strData);
+                this.dicDeviceRoomId = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(strData);
             }
-            foreach (var device in dicAllDevice.Values)
-            {
-                if (dicDeviceSort.ContainsKey(device.CurrentGateWayId) == false)
-                {
-                    dicDeviceSort[device.CurrentGateWayId] = new List<string>();
-                }
-                if (dicDeviceSort[device.CurrentGateWayId].Contains(device.DeviceAddr) == false)
-                {
-                    //鏂版坊鍔犵殑璁惧
-                    dicDeviceSort[device.CurrentGateWayId].Add(device.DeviceAddr);
-                }
-            }
-            foreach (var keys in dicDeviceSort.Keys)
-            {
-                var list = dicDeviceSort[keys];
-                for (int i = 0; i < list.Count; i++)
-                {
-                    if (this.dicDeviceEpoint.ContainsKey(list[i]) == false)
-                    {
-                        //杩欎釜涓滆タ鑾悕鐨勪笉瑙佷簡锛熷拰璁惧鍒楄〃鍖归厤涓嶄笂?
-                        list.RemoveAt(i);
-                        i--;
-                    }
-                }
-            }
-
-            //淇濆瓨椤哄簭
-            UserCenterLogic.SaveFileContent(fullName, dicDeviceEpoint);
         }
 
         /// <summary>
-        /// 璁惧鎺掑簭
+        /// 淇濆瓨鐗╃悊璁惧鎵�灞炴埧闂寸殑璁板綍
         /// </summary>
-        /// <param name="listDevice">闇�瑕佹帓搴忕殑璁惧鍒楄〃</param>
-        /// <returns></returns>
-        public List<CommonDevice> SortDevice(List<CommonDevice> listDevice)
+        /// <param name="listDevice">闇�瑕佷繚瀛樼殑璁惧瀵硅薄</param>
+        /// <param name="roomId">闇�瑕佷繚瀛樼殑鍝釜璁惧鐨勬埧闂碔D</param>
+        /// <param name="saveRoadDevice">濡傛灉鍙湁涓�涓洖璺�,鏄惁鎶婂洖璺殑鎴块棿涓�璧蜂慨鏀�</param>
+        public void SaveRealDeviceRoomId(List<CommonDevice> listDevice, string roomId, bool saveRoadDevice = true)
         {
-            var dic = new Dictionary<string, List<CommonDevice>>();
-            for (int i = 0; i < listDevice.Count; i++)
+            if (listDevice != null)
             {
-                if (dic.ContainsKey(listDevice[i].DeviceAddr) == false)
-                {
-                    dic[listDevice[i].DeviceAddr] = new List<CommonDevice>();
-                }
-                dic[listDevice[i].DeviceAddr].Add(listDevice[i]);
+                this.dicDeviceRoomId[listDevice[0].DeviceAddr] = roomId;
             }
-            var list = new List<CommonDevice>();
-            var listCheck = new HashSet<string>();
-            foreach (var listSort in this.dicDeviceSort.Values)
+
+            //淇濆瓨璁板綍
+            string fullName = UserCenterLogic.CombinePath(DirNameResourse.DeviceRoomIdFile);
+            UserCenterLogic.SaveFileContent(fullName, this.dicDeviceRoomId);
+
+            //娣诲姞鑷姩澶囦唤
+            HdlAutoBackupLogic.AddOrEditorFile(DirNameResourse.DeviceRoomIdFile);
+
+            //濡傛灉璁惧鍙湁涓�涓洖璺紝濡傛灉鏀瑰彉浜嗙湡瀹炶澶囧尯鍩燂紝鍒欏畠鐨勫洖璺殑鍖哄煙涔熶竴璧锋敼浜�
+            if (saveRoadDevice == true && listDevice != null && listDevice.Count == 1)
             {
-                for (int i = 0; i < listSort.Count; i++)
-                {
-                    if (dic.ContainsKey(listSort[i]) == true && listCheck.Contains(listSort[i]) == false)
-                    {
-                        listCheck.Add(listSort[i]);
-                        dic[listSort[i]].Sort((obj1, obj2) =>
-                        {
-                            if (obj1.DeviceEpoint > obj2.DeviceEpoint)
-                            {
-                                return 1;
-                            }
-                            return -1;
-                        });
-                        list.AddRange(dic[listSort[i]]);
-                    }
-                }
+                Common.Room.CurrentRoom.ChangedRoom(listDevice[0], roomId, false);
             }
-            return list;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鐪熷疄鐗╃悊璁惧鐨勬埧闂村悕瀛�
+        /// </summary>
+        /// <param name="device">璁惧鐨勬煇涓�涓洖璺�</param>
+        /// <returns></returns>
+        public string GeteRealDeviceRoomName(CommonDevice device)
+        {
+            if (this.dicDeviceRoomId.ContainsKey(device.DeviceAddr) == false)
+            {
+                //鏈垎閰嶅尯鍩�
+                return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
+            }
+            var room = Room.CurrentRoom.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
+            if (room != null)
+            {
+                return room.Name;
+            }
+            //鏈垎閰嶅尯鍩�
+            return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
+        }
+
+        /// <summary>
+        /// 鑾峰彇鐪熷疄鐗╃悊璁惧灞炰簬鍝釜鎴块棿
+        /// </summary>
+        /// <param name="device">璁惧鐨勬煇涓�涓洖璺�</param>
+        /// <returns></returns>
+        public Room GeteRealDeviceRoom(CommonDevice device)
+        {
+            if (this.dicDeviceRoomId.ContainsKey(device.DeviceAddr) == false)
+            {
+                return null;
+            }
+            return Room.CurrentRoom.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
         }
 
         #endregion
@@ -1706,7 +1691,7 @@
         /// <param name="deviceComingAction">鎺ユ敹鍒拌澶囨椂鐨勪簨浠�</param>
         /// <param name="mode">鏄惁鏄剧ず閿欒</param>
         /// <returns></returns>
-        public async Task<List<CommonDevice>> GetDeviceListFromGateway(ZbGateway zbGateway, bool ignoreTime, Action<CommonDevice> deviceComingAction = null, ShowErrorMode mode = ShowErrorMode.YES)
+        public List<CommonDevice> GetDeviceListFromGateway(ZbGateway zbGateway, bool ignoreTime, Action<CommonDevice> deviceComingAction = null, ShowErrorMode mode = ShowErrorMode.YES)
         {
             if (ignoreTime == false)
             {
@@ -1813,13 +1798,13 @@
             try
             {
                 var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 93 } };
-                await realWay.Send("GetDeviceInfo", jObject.ToString());
+                realWay.Send("GetDeviceInfo", jObject.ToString());
             }
             catch { canBreak = true; }
 
             while (canBreak == false && TimeOut < 60)
             {
-                await Task.Delay(100);
+                System.Threading.Thread.Sleep(100);
                 TimeOut++;
             }
 
@@ -1837,7 +1822,7 @@
                         string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceListFail);
                         msg += "\r\n[" + HdlGatewayLogic.Current.GetGatewayName(zbGateway).ToString() + "]";
                         msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "鍥炲瓒呮椂", false);
-                        this.ShowErrorMsg(msg);
+                        this.ShowTipMsg(msg);
                     }
                     return null;
                 }
@@ -2074,7 +2059,7 @@
             this.dicDeviceModelIdEnum["H06C"] = "2800-2800";//鏅鸿兘闂ㄩ攣(H06C)
 
             //=========鈽呪槄绌鸿皟绫�(3600-3899)鈽呪槄=========
-            this.dicDeviceModelIdEnum["MAC/GW-ZB.431"] = "3600-3600";//zigbee绌鸿皟缃戝叧妯″潡
+            this.dicDeviceModelIdEnum["MAC/GW-ZB.10"] = "3600-3600";//zigbee绌鸿皟缃戝叧妯″潡
 
             //=========鈽呪槄涓户鍣ㄧ被(3900-3999)鈽呪槄=========
             this.dicDeviceModelIdEnum["MSR-ZB.10"] = "3900-3900"; //zigbee涓户鍣�

--
Gitblit v1.8.0