From b9cb076fe6127160c96c35bf9c8cebcffe1d5ccd Mon Sep 17 00:00:00 2001
From: gxc <guoxuecheng@guoxuechengdeMacBook-Pro.local>
Date: 星期五, 10 一月 2020 17:35:23 +0800
Subject: [PATCH] 2020.1.10

---
 ZigbeeApp/Shared/Common/Room.cs |  209 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 155 insertions(+), 54 deletions(-)

diff --git a/ZigbeeApp/Shared/Common/Room.cs b/ZigbeeApp/Shared/Common/Room.cs
old mode 100755
new mode 100644
index 3cac5f9..d324988
--- a/ZigbeeApp/Shared/Common/Room.cs
+++ b/ZigbeeApp/Shared/Common/Room.cs
@@ -98,6 +98,7 @@
             {
                 if (m_CurrentRoom == null && Lists.Count > 0)
                 {
+                    if (m_CurrentRoom == null) { m_CurrentRoom = Lists[0]; }
                     return Lists[0];
                 }
                 return m_CurrentRoom;
@@ -193,6 +194,10 @@
                 var sceneList = new List<SceneUI> { };
                 foreach (var r in Shared.Common.Room.Lists)
                 {
+                    if(r.IsLove)
+                    {
+                        continue;
+                    }
                     if (r.SceneUIList.Count == 0)
                     {
                         continue;
@@ -222,6 +227,11 @@
                 var pathList = new List<string> { };
                 foreach (var r in Lists)
                 {
+                    if(r.IsLove)
+                    {
+                        continue;
+                    }
+
                     if (r.SceneUIFilePathList.Count == 0)
                     {
                         continue;
@@ -263,7 +273,7 @@
                 for (int i = 0; i < Lists.Count; i++)
                 {
                     var room = Lists[i];
-                    if (room.IsSharedRoom)
+                    if (room.IsSharedRoom || room.IsLove)
                     {
                         continue;
                     }
@@ -335,15 +345,16 @@
 
                 }
             }
+
             Config.Instance.Home.InitFloor();
 
-            RefreshRoomListView();
+            CurrentRoom.RefreshRoomListView();
         }
 
         /// <summary>
         /// 鍒锋柊鎴块棿瑙嗗浘鍒楄〃
         /// </summary>
-        public static void RefreshRoomListView()
+        public void RefreshRoomListView()
         {
             Application.RunOnMainThread(() =>
             {
@@ -356,7 +367,6 @@
         /// </summary>
         public static void RefreshAllRoomByLocation()
         {
-            Lists.Clear();
             var homeTemp = Config.Instance.Home;
             homeTemp.RoomFilePathList.Clear();
 
@@ -367,7 +377,7 @@
             if (listFile.Contains(fRoom) == true)
             {
                 listFile.Remove(fRoom);
-                homeTemp.AddRoomListFilePath(fRoom);
+                homeTemp.RoomFilePathList.Add(fRoom);
             }
 
             var listRoomFile = new List<string>();
@@ -375,7 +385,7 @@
             {
                 if (fileName.StartsWith("Room_"))
                 {
-                    homeTemp.AddRoomListFilePath(fileName);
+                    homeTemp.RoomFilePathList.Add(fileName);
                     listRoomFile.Add(fileName);
                 }
             }
@@ -494,6 +504,9 @@
             Global.DeleteFilebyHomeId(roomFilePath);
             Lists.Remove(room);
             HdlAutoBackupLogic.DeleteFile(roomFilePath);
+
+            CurrentRoom.RefreshRoomListView();
+
             return true;
         }
 
@@ -538,8 +551,21 @@
                     //璁惧锛坉eviceUI锛�
                     beforeRoom.DeviceUIList.Clear();
                     foreach (var deviceFilePath in beforeRoom.DeviceUIFilePathList)
-                    {
-                        beforeRoom.DeviceUIList.Add(Common.LocalDevice.Current.GetDeviceUI(deviceFilePath));
+                    {
+                        var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceFilePath));
+                        var tempDeviceUI = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceUI>(jsonInfo);
+                        if (tempDeviceUI != null)
+                        {
+                            var delCommon = tempDeviceUIList.Find((obj) => obj.CommonDevice != null && tempDeviceUI.CommonDevice != null && obj.CommonDevice.Type == tempDeviceUI.CommonDevice.Type && obj.CommonDevice.CommonDeviceAddrEpoint == tempDeviceUI.CommonDevice.CommonDeviceAddrEpoint);
+                            if (delCommon != null)
+                            {
+                                beforeRoom.DeviceUIList.Add(delCommon);
+                            }
+                            else
+                            {
+                                beforeRoom.AddDevice(deviceFilePath);
+                            }
+                        }
                     }
                     //鍦烘櫙(SceneUI)
                     beforeRoom.SceneUIList.Clear();
@@ -693,11 +719,24 @@
         /// <returns></returns>
         public List<Room> GetRoomsByFloorId(string id)
         {
-            if (Config.Instance.Home.FloorDics.Count == 0)
+            try
             {
-                return Lists;
+                if (Lists == null || Lists.Count == 0 || Lists.Count == 1)
+                {
+                    return null;
+                }
+                if (Config.Instance.Home.FloorDics.Count == 0)
+                {
+                    return Lists;
+                }
+                return Lists.FindAll((obj) => obj.FloorId == id);
             }
-            return Lists.FindAll((obj) => obj.FloorId == id);
+            catch(Exception ex)
+            {
+                System.Console.WriteLine(ex.Message);
+                return null;
+            }
+
         }
         /// <summary>
         /// 鑾峰彇褰撳墠妤煎眰鐨勬埧闂村悕绉�
@@ -792,6 +831,25 @@
             return false;
         }
 
+        /// <summary>
+        /// 鑾峰彇鎴块棿鎵�鍦ㄥ尯鍩�
+        /// 妤煎眰,鎴块棿鍚�
+        /// </summary>
+        /// <returns></returns>
+        public string GetZoneName()
+        {
+            if (string.IsNullOrEmpty(FloorId))
+            {
+                return Name;
+            }
+            var floorName = Config.Instance.Home.GetFloorNameById(FloorId);
+            if (floorName == null)
+            {
+                return Name;
+            }
+            return $"{floorName},{Name}";
+        }
+
         #endregion
 
         #region 鈼� 鏇存柊鎴块棿_________________________
@@ -882,8 +940,6 @@
                 //淇濆瓨鍒版湰鍦�
                 Save();
 
-                //娣诲姞鑷姩澶囦唤
-                HdlAutoBackupLogic.AddOrEditorFile(FileName);
                 if (saveRealRoom == true && LocalDevice.Current.GetDevicesCountByMac(device.DeviceAddr) == 1)
                 {
                     //濡傛灉鍙湁涓�涓洖璺�,鍒欎慨鏀圭湡瀹炵墿鐞嗚澶囩殑鎴块棿
@@ -1091,16 +1147,16 @@
         /// <param name="sceneIconPath">鑳屾櫙鍥剧墖锛屼笉鍖呭惈浣忓畢璺緞 濡傛灉iconPathType=1鎴栬��2 闇�瑕佹嫾鎺ヤ綇瀹� 鍙樻垚 浣忓畢/sceneIconPath</param>
         /// <param name="commons">Commons.</param>
         /// <param name="iconPathType">I鍦烘櫙鑳屾櫙鍥剧墖鏉ユ簮绫诲瀷 鍥剧墖鏉ユ簮 0--鏈湴鍥惧簱 1--鎷嶇収 2--绯荤粺鍥惧簱 榛樿0</param>
-        public async System.Threading.Tasks.Task<int> AddScene(string sceneName, string sceneIconPath, List<ZigBee.Device.Scene.AddSceneMemberData> commons, int iconPathType = 0)
+        public async System.Threading.Tasks.Task<int> AddScene(string sceneName, string sceneIconPath, List<ZigBee.Device.Scene.AddSceneMemberData> commons, int iconPathType)
         {
-            var scenes = GetSceneUIsByFloorId(FloorId);
-            if(scenes!=null && scenes.Count>0)
-            {
-                if (scenes.Find(s => s.Name == sceneName) != null)
-                {
-                    return -1;
-                }
-            }
+            //var scenes = GetSceneUIsByFloorId(FloorId);
+            //if(scenes!=null && scenes.Count>0)
+            //{
+            //    if (scenes.Find(s => s.Name == sceneName) != null)
+            //    {
+            //        return -1;
+            //    }
+            //}
 
             var getSceneIdAllData = await ZigBee.Device.Scene.GetSceneNewIdAsync(sceneName);
             if (getSceneIdAllData == null || getSceneIdAllData.getSceneIdData == null)
@@ -1140,26 +1196,18 @@
             //鍔犲叆鎴愬姛
             if (result)
             {
-                //iconPathType=0 鐩存帴浼犲�硷紝 iconPathType=1鍜宨conPathType=2闇�瑕佹嫾鎺ヤ綇瀹呰矾寰�
-                var fullPath = sceneIconPath;
-                if (iconPathType == 1 || iconPathType == 2)
-                {
-                    Common.Room.CurrentRoom.MoveBackGroundIamageFileToDirectory(sceneIconPath, $"{Config.Instance.FullPath}/{sceneIconPath}");
-                    fullPath = $"{Config.Instance.FullPath}/{sceneIconPath}";
-                }
                 var sceneUI = new SceneUI
                 {
                     Name = sceneName,
                     Id = getSceneIdData.NewScenesId,
-                    IconPath = fullPath,
+                    IconPath = sceneIconPath,
                     IconPathType = iconPathType,
                     AddSceneMemberDataList= commons
                 };
+                sceneUI.Save();
                 SceneUIList.Add(sceneUI);
                 SceneUIFilePathList.Add(sceneUI.FileName);
-                sceneUI.Save();
                 Save();
-                HdlAutoBackupLogic.AddOrEditorFile(sceneUI.FileName);
                 return 1;
             }
             return 0;
@@ -1171,17 +1219,8 @@
         /// <param name="scene">Scene.</param>
         public void AddScene(SceneUI scene)
         {
-            //var sceneUI = new SceneUI
-            //{
-            //    Name = scene.Name,
-            //    Id = scene.Id,
-            //    IconPath = scene.IconPath,
-            //    IconPathType = scene.IconPathType,
-            //    AddSceneMemberDataList = scene.AddSceneMemberDataList
-            //};
             SceneUIList.Add(scene);
             SceneUIFilePathList.Add(scene.FileName);
-            scene.Save();
             Save();
         }
 
@@ -1191,10 +1230,13 @@
         /// <param name="scene"></param>
         public void DeleteScene(SceneUI scene)
         {
-            //scene.IsCollected = false;
-            scene.Save();
-            SceneUIList.Remove(scene);
-            SceneUIFilePathList.Remove(scene.FileName);
+            var curScene = SceneUIList.Find((obj) => obj.Id == scene.Id);
+            if (curScene == null)
+            {
+                return;
+            }
+            SceneUIList.Remove(curScene);
+            SceneUIFilePathList.Remove(curScene.FileName);
             Save();
         }
 
@@ -1205,15 +1247,10 @@
         /// <summary>
         /// 鏄惁鏄敹钘忚澶�
         /// </summary>
-        /// <param name="room"></param>
         /// <param name="filePath"></param>
         /// <returns></returns>
-        public bool IsCollectInRoom(Room room,string filePath)
+        public bool IsCollectInRoom(string filePath)
         {
-            if(room.IsLove)
-            {
-                return true;
-            }
             if (GetLoveRoom().DeviceUIFilePathList.Find((obj) => obj == filePath) == null)
             {
                 return false;
@@ -1288,9 +1325,61 @@
             {
                 sceneUI.AddSceneMemberDataList = addCommons;
                 sceneUI.Save();
+                if (IsLove == false)
+                {
+                    var curScene = Common.Room.CurrentRoom.GetLoveRoom().SceneUIList.Find((obj) => obj.Id == sceneUI.Id);
+                    if (curScene != null)
+                    {
+                        curScene.Name = sceneUI.Name;
+                        curScene.IconPath = sceneUI.IconPath;
+                        curScene.IconPathType = sceneUI.IconPathType;
+                        curScene.AddSceneMemberDataList = sceneUI.AddSceneMemberDataList;
+                        curScene.SceneDelayTime = sceneUI.SceneDelayTime;
+                        curScene.Save(false);
+                        Common.Room.CurrentRoom.GetLoveRoom().Save();
+                    }
+                }
                 return 1;
             }
             return 0;
+        }
+
+        /// <summary>
+        /// 璁剧疆銆佸悓姝ュ欢鏃舵椂闂�
+        /// </summary>
+        /// <param name="scene"></param>
+        public void ModifySceneDelayTime(SceneUI scene)
+        {
+            if (IsLove)
+            {
+                foreach (var r in Lists)
+                {
+                    if (r.IsLove || r.SceneUIList.Count == 0)
+                    {
+                        continue;
+                    }
+                    foreach (var sce in r.SceneUIList)
+                    {
+                        if (sce.Id == scene.Id)
+                        {
+                            sce.SceneDelayTime = scene.SceneDelayTime;
+                            sce.Save(false);
+                            r.Save(false);
+                            break;
+                        }
+                    }
+                }
+            }
+            else
+            {
+                var curScene = Common.Room.CurrentRoom.GetLoveRoom().SceneUIList.Find((obj) => obj.Id == scene.Id);
+                if (curScene != null)
+                {
+                    curScene.SceneDelayTime = scene.SceneDelayTime;
+                    curScene.Save(false);
+                    Common.Room.CurrentRoom.GetLoveRoom().Save(false);
+                }
+            }
         }
 
         #endregion
@@ -1307,9 +1396,16 @@
             {
                 return;
             }
-            SceneUIList.Remove(sceneUI);
-            SceneUIFilePathList.Remove(sceneUI.FileName);
-            Save();
+
+            if (sceneUI.IconPathType == 1 || sceneUI.IconPathType == 2)
+            {
+                if (Global.IsExistsByHomeId(sceneUI.IconPath))
+                {
+                    Global.DeleteFilebyHomeId(sceneUI.IconPath);
+                    HdlAutoBackupLogic.DeleteFile(sceneUI.IconPath);
+                }
+            }
+
             if (IsLove == false)
             {
                 if (CurrentRoom.GetLoveRoom().SceneUIList.Find((obj) => obj.Id == sceneUI.Id) != null)
@@ -1317,6 +1413,11 @@
                     CurrentRoom.GetLoveRoom().DeleteScene(sceneUI);
                 }
             }
+
+            SceneUIList.Remove(sceneUI);
+            SceneUIFilePathList.Remove(sceneUI.FileName);
+            Save();
+
             Global.DeleteFilebyHomeId(sceneUI.FileName);
             HdlAutoBackupLogic.DeleteFile(sceneUI.FileName);
         }

--
Gitblit v1.8.0