From 66a9965c44ecc32a6696abca876ab9d1cd091584 Mon Sep 17 00:00:00 2001
From: gxc <guoxuecheng@guoxuechengdeMacBook-Pro.local>
Date: 星期五, 28 二月 2020 15:25:13 +0800
Subject: [PATCH] 2020.2.28

---
 ZigbeeApp/Shared/Common/Room.cs |  797 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 564 insertions(+), 233 deletions(-)

diff --git a/ZigbeeApp/Shared/Common/Room.cs b/ZigbeeApp/Shared/Common/Room.cs
index 64e0190..e5c8d07 100644
--- a/ZigbeeApp/Shared/Common/Room.cs
+++ b/ZigbeeApp/Shared/Common/Room.cs
@@ -75,11 +75,36 @@
         /// 婀垮害浼犳劅鍣�(璁惧涓婚敭)
         /// </summary>
         public string HumidityDevice = string.Empty;
+        /// <summary>
+        /// 娓╁害
+        /// </summary>
+        public decimal Temperatrue;
+        /// <summary>
+        /// 婀垮害
+        /// </summary>
+        public decimal Humidity;
 
+        /// <summary>
+        /// 褰撳墠閫夋嫨鐨勬埧闂�
+        /// </summary>
+        private static Room m_CurrentRoom = null;
         /// <summary>
         /// 褰撳墠閫夋嫨鐨勬埧闂�
         /// </summary>
-        public static Room CurrentRoom;
+        [Newtonsoft.Json.JsonIgnore]
+        public static Room CurrentRoom
+        {
+            get
+            {
+                if (m_CurrentRoom == null && Lists.Count > 0)
+                {
+                    if (m_CurrentRoom == null) { m_CurrentRoom = Lists[0]; }
+                    return Lists[0];
+                }
+                return m_CurrentRoom;
+            }
+            set { m_CurrentRoom = value; }
+        }
 
         /// <summary>
         /// 鎴块棿閲屾墍鏈夌殑璁惧鍒楄〃
@@ -116,12 +141,6 @@
         public bool IsSharedRoom = false;
 
         /// <summary>
-        /// 鏄惁鍙互鍒锋柊鎴块棿鏁版嵁浜�
-        /// 闇�瑕佺瓑鍒濆鍖栨湰鍦拌澶囨暟鎹悗鍦ㄥ彲浠ュ垵濮嬪寲鎴块棿鏁版嵁
-        /// </summary>
-        public static bool CanInitAllRoom;
-
-        /// <summary>
         /// 鑾峰彇鍠滅埍鎴块棿鐨勬墍鏈夎澶囪矾寰�
         /// </summary>
         /// <value>The love room device list.</value>
@@ -140,11 +159,28 @@
 
         /// <summary>
         /// 鎵�鏈夋埧闂寸殑鎵�鏈夎澶囩被鍨�
-        /// 鍏堣皟鐢� GetAllRoomDeviceTypeList()
         /// </summary>
         /// <value>All room device type list.</value>
         [Newtonsoft.Json.JsonIgnore]
-        public static List<DeviceType> AllRoomDeviceTypeList = new List<DeviceType> { };
+        public static List<DeviceType> AllRoomDeviceTypeList
+        {
+            get
+            {
+                var typeList = new List<DeviceType> { };
+                foreach (var deviceUI in AllRoomDeviceUIList)
+                {
+                    if (deviceUI == null || deviceUI.CommonDevice == null)
+                    {
+                        continue;
+                    }
+                    if (!typeList.Contains(deviceUI.CommonDevice.Type))
+                    {
+                        typeList.Add(deviceUI.CommonDevice.Type);
+                    }
+                }
+                return typeList;
+            }
+        }
 
         /// <summary>
         /// 鑾峰彇鎵�鏈夋埧闂寸殑鎵�鏈夊満鏅�
@@ -158,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;
@@ -174,6 +214,37 @@
                 return sceneList;
             }
         }
+
+        /// <summary>
+        /// 鑾峰彇鎵�鏈夋埧闂寸殑鎵�鏈夊満鏅矾寰�
+        /// </summary>
+        /// <value>All room scene UIL ist.</value>
+        [Newtonsoft.Json.JsonIgnore]
+        public  List<string> AllRoomSceneUIFilepathList
+        {
+            get
+            {
+                var pathList = new List<string> { };
+                foreach (var r in Lists)
+                {
+                    if(r.IsLove)
+                    {
+                        continue;
+                    }
+
+                    if (r.SceneUIFilePathList.Count == 0)
+                    {
+                        continue;
+                    }
+                    foreach (var path in r.SceneUIFilePathList)
+                    {
+                        pathList.Add(path);
+                    }
+                }
+                return pathList;
+            }
+        }
+
         /// <summary>
         /// 鍦烘櫙鍒楄〃---涓嶅啀搴忓垪鍖�
         /// </summary>
@@ -191,11 +262,41 @@
 
         /// <summary>
         /// 鑾峰彇鎵�鏈夋埧闂寸殑鎵�鏈夎澶�
-        /// 璇峰厛璋冪敤 GetAllRoomDeviceUIList()鏂规硶
         /// </summary>
         /// <value>All room device UIL ist.</value>
         [Newtonsoft.Json.JsonIgnore]
-        public static List<DeviceUI> AllRoomDeviceUIList = new List<DeviceUI> { };
+        public static List<DeviceUI> AllRoomDeviceUIList
+        {
+            get
+            {
+                List<DeviceUI> deviceList = new List<DeviceUI>();
+                for (int i = 0; i < Lists.Count; i++)
+                {
+                    var room = Lists[i];
+                    if (room.IsSharedRoom || room.IsLove)
+                    {
+                        continue;
+                    }
+                    if (room.DeviceUIList.Count == 0)
+                    {
+                        continue;
+                    }
+                    for (int j = 0; j < room.DeviceUIList.Count; j++)
+                    {
+                        var device = room.DeviceUIList[j];
+                        if (device == null || device.CommonDevice == null)
+                        {
+                            continue;
+                        }
+                        if (deviceList.Find((obj) => obj.FileName == device.FileName) == null)
+                        {
+                            deviceList.Add(device);
+                        }
+                    }
+                }
+                return deviceList;
+            }
+        }
 
         #endregion
 
@@ -219,20 +320,16 @@
         /// </summary>
         public static void InitAllRoom()
         {
-            if (CanInitAllRoom == false)
-            {
-                return;
-            }
             Lists.Clear();
             if (Config.Instance.Home.RoomFilePathList.Contains("Room_Favorite.json") == false)
             {
                 //榛樿娣诲姞鍠滅埍鐨勬埧闂�--绂佹淇敼鎴块棿鍚�
-                var love = new Room { Name = Language.StringByID(R.MyInternationalizationString.Favorite), BackgroundImage = "Room/r0.png", Id = LoveRoomId };
-                love.Save(true);
+                var love = new Room { Name = Language.StringByID(R.MyInternationalizationString.Favorite), BackgroundImage = "RoomIcon/0.JPG", Id = LoveRoomId };
+                love.Save(false);
                 //娣诲姞鍒癶ouse 鎴块棿璺緞鍒楄〃
                 var currentHome = Config.Instance.Home;
                 currentHome.RoomFilePathList.Insert(0, love.FileName);
-                currentHome.Save();
+                currentHome.Save(false);
             }
             foreach (var roomFilePath in Config.Instance.Home.RoomFilePathList)
             {
@@ -242,11 +339,27 @@
                 {
                     if (room.IsSharedRoom)
                     {
-                        room.Name = $"({Language.StringByID(R.MyInternationalizationString.Shared)}){room.Name}";
+                        room.Name = $"{room.Name}";
                     }
                     Lists.Add(room);
+
                 }
             }
+
+            Config.Instance.Home.InitFloor();
+
+            CurrentRoom.RefreshRoomListView();
+        }
+
+        /// <summary>
+        /// 鍒锋柊鎴块棿瑙嗗浘鍒楄〃
+        /// </summary>
+        public void RefreshRoomListView()
+        {
+            Application.RunOnMainThread(() =>
+            {
+                Phone.Device.Room.RoomManagement.Instance.Show();
+            });
         }
 
         /// <summary>
@@ -254,7 +367,6 @@
         /// </summary>
         public static void RefreshAllRoomByLocation()
         {
-            Lists.Clear();
             var homeTemp = Config.Instance.Home;
             homeTemp.RoomFilePathList.Clear();
 
@@ -265,19 +377,48 @@
             if (listFile.Contains(fRoom) == true)
             {
                 listFile.Remove(fRoom);
-                homeTemp.AddRoomListFilePath(fRoom);
+                homeTemp.RoomFilePathList.Add(fRoom);
             }
 
+            var listRoomFile = new List<string>();
             foreach (string fileName in listFile)
             {
                 if (fileName.StartsWith("Room_"))
                 {
-                    homeTemp.AddRoomListFilePath(fileName);
+                    homeTemp.RoomFilePathList.Add(fileName);
+                    listRoomFile.Add(fileName);
                 }
             }
+            //妫�娴嬫ゼ灞傛暟鎹殑鍚堟硶鎬�
+            CheckFloorData(listRoomFile);
+
             homeTemp.Save(false);
-            CanInitAllRoom = true;
             InitAllRoom();
+        }
+
+        /// <summary>
+        /// 妫�娴嬫ゼ灞傛暟鎹殑鍚堟硶鎬�
+        /// </summary>
+        /// <param name="listRoomFile"></param>
+        private static void CheckFloorData(List<string> listRoomFile)
+        {
+            for (int i = 0; i < listRoomFile.Count; i++)
+            {
+                try
+                {
+                    var byteData = Global.ReadFileByHomeId(listRoomFile[i]);
+                    string valueData = System.Text.Encoding.UTF8.GetString(byteData);
+                    var roomTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(valueData);
+
+                    //妫�娴嬪涓墜鏈烘潵鍥炲垱寤�,鐒跺悗鍙堝垹闄や箣鍚�,妤煎眰鏁版嵁涓嶈兘淇濊瘉100%鍚屾鐨勯棶棰�
+                    if (roomTemp.FloorId != string.Empty && Config.Instance.Home.FloorDics.ContainsKey(roomTemp.FloorId) == false)
+                    {
+                        //鏈煡妤煎眰
+                        Config.Instance.Home.FloorDics[roomTemp.FloorId] = Language.StringByID(R.MyInternationalizationString.uUnKnownFloor);
+                    }
+                }
+                catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex); }
+            }
         }
 
         #endregion
@@ -307,6 +448,7 @@
             }
             Save();
             HdlAutoBackupLogic.AddOrEditorFile(room.FileName);
+            CurrentRoom.RefreshRoomListView();
             return true;
         }
 
@@ -327,8 +469,33 @@
             //鍒犻櫎鏉ヨ嚜鎷嶇収鎴栬�呯郴缁熷浘搴撶殑鎴块棿鑳屾櫙鍥剧墖
             if (room.BackgroundImageType == 1 || room.BackgroundImageType == 2)
             {
-                DeleteBackGroundIamageFilebyHomeId(room.BackgroundImage);
+                //鍒犻櫎鎺夊師鏉ョ殑鑷畾涔夊浘鐗�
+                if (Global.IsExistsByHomeId(room.BackgroundImage) == true)
+                {
+                    Global.DeleteFilebyHomeId(room.BackgroundImage);
+                    //鍒犻櫎澶囦唤
+                    HdlAutoBackupLogic.DeleteFile(room.BackgroundImage);
+                }
             }
+            //鎴戠殑鍠滅埍
+            var loveRoom = this.GetLoveRoom();
+            if (loveRoom != null)
+            {
+                //绉婚櫎鎴戠殑鍠滅埍閲岄潰鐨勮澶�
+                for (int i = 0; i < room.DeviceUIFilePathList.Count; i++)
+                {
+                    loveRoom.DeviceUIFilePathList.Remove(room.DeviceUIFilePathList[i]);
+                    loveRoom.DeviceUIList.RemoveAll((obj) => { return room.DeviceUIFilePathList[i] == obj.FileName; });
+                }
+                //绉婚櫎鎴戠殑鍠滅埍閲岄潰鐨勫満鏅�
+                for (int i = 0; i < room.SceneUIFilePathList.Count; i++)
+                {
+                    loveRoom.SceneUIFilePathList.Remove(room.SceneUIFilePathList[i]);
+                    loveRoom.SceneUIList.RemoveAll((obj) => { return room.SceneUIFilePathList[i] == obj.FileName; });
+                }
+                loveRoom.Save(false);
+            }
+
             if (Global.IsExistsByHomeId(roomFilePath) == false)
             {
                 return false;
@@ -338,6 +505,9 @@
             Global.DeleteFilebyHomeId(roomFilePath);
             Lists.Remove(room);
             HdlAutoBackupLogic.DeleteFile(roomFilePath);
+
+            CurrentRoom.RefreshRoomListView();
+
             return true;
         }
 
@@ -349,7 +519,7 @@
         /// 鑾峰彇鍠滅埍鎴块棿
         /// </summary>
         /// <returns></returns>
-        public  Room GetLoveRoom()
+        public Room GetLoveRoom()
         {
             return CurrentRoom.GetRoomById(LoveRoomId);
         }
@@ -459,6 +629,10 @@
         /// <param name="roomId">鎴块棿ID</param>
         public Room GetRoomById(string roomId)
         {
+            if (string.IsNullOrEmpty(roomId))
+            {
+                return null;
+            }
             return Lists.Find((obj) => obj.Id == roomId);
         }
 
@@ -500,11 +674,8 @@
         /// <param name="device">璁惧瀵硅薄</param>
         public Room GetRoomByDevice(CommonDevice device)
         {
-            var deviceUI = new DeviceUI { };
-            deviceUI.DeviceAddr = device.DeviceAddr;
-            deviceUI.DeviceEpoint = device.DeviceEpoint;
-
-            return Lists.Find((obj) => obj.DeviceUIFilePathList.Contains(deviceUI.FileName));
+            string deviceFile = device.FilePath;
+            return Lists.Find((obj) => obj.IsLove == false && obj.DeviceUIFilePathList.Contains(deviceFile));
         }
 
         /// <summary>
@@ -531,6 +702,10 @@
         {
             foreach (var r in Lists)
             {
+                if(r.IsLove)
+                {
+                    continue;
+                }
                 foreach (var scene in r.SceneUIList)
                 {
                     if (scene.Id == sceneId)
@@ -547,21 +722,38 @@
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        public  List<Room> GetRoomsByFloorId(string id)
+        public List<Room> GetRoomsByFloorId(string id)
         {
-            return Lists.FindAll((obj) => obj.FloorId==id);
+            try
+            {
+                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);
+            }
+            catch(Exception ex)
+            {
+                System.Console.WriteLine(ex.Message);
+                return null;
+            }
+
         }
         /// <summary>
         /// 鑾峰彇褰撳墠妤煎眰鐨勬埧闂村悕绉�
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        public  List<string> GetRoomNamesByFloorId(string id)
+        public List<string> GetRoomNamesByFloorId(string id)
         {
             List<string> names = new List<string> { };
-            foreach(var r in Lists)
+            foreach (var r in Lists)
             {
-                if(r.FloorId==id)
+                if (r.FloorId == id)
                 {
                     names.Add(r.Name);
                 }
@@ -576,6 +768,10 @@
         /// <returns></returns>
         public  List<Room> GetRoomsByFloorIdAppendLoveRoom(string id)
         {
+            if (Config.Instance.Home.FloorDics.Count == 0)
+            {
+                return Lists;
+            }
             var r= Lists.FindAll((obj) => obj.FloorId == id);
             r.Insert(0, GetLoveRoom());
             return r;
@@ -587,6 +783,10 @@
         /// <returns></returns>
         public  List<Room> GetRoomsByCurrentFloorIdAppendLoveRoom()
         {
+            if(Config.Instance.Home.FloorDics.Count==0)
+            {
+                return Lists;
+            }
             var r = Lists.FindAll((obj) => obj.FloorId == Config.Instance.Home.CurrentFloorId);
             r.Insert(0, GetLoveRoom());
             return r;
@@ -601,7 +801,8 @@
         /// </summary>
         /// <param name="device">璁惧瀵硅薄</param>
         /// <param name="roomId">鏂版埧闂碔d</param>
-        public void ChangedRoom(CommonDevice device, string roomId)
+        /// <param name="saveRealRoom">鏄惁淇敼鐪熷疄鐗╃悊璁惧鐨勬埧闂�,涓嶅嚭鎰忓,杩欎釜鍊奸粯璁や负true鍗冲彲</param>
+        public void ChangedRoom(CommonDevice device, string roomId, bool saveRealRoom = true)
         {
             //鎴块棿鏄惁淇敼
             if (this.IsRoomChanged(device, roomId) == false)
@@ -615,7 +816,7 @@
             var room = this.GetRoomById(roomId);
             if (room != null)
             {
-                room.AddDevice(device);
+                room.AddDevice(device, saveRealRoom);
             }
         }
 
@@ -635,6 +836,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 鈼� 鏇存柊鎴块棿_________________________
@@ -651,31 +871,6 @@
         #endregion
 
         #region 鈼� 鎴块棿鑳屾櫙鍥剧殑鐩稿叧___________________
-        /// <summary>
-        /// 鍒犻櫎鑳屾櫙鍥剧墖
-        /// </summary>
-        /// <param name="fileName">fileName瀹為檯涓婂寘鍚簡浣忓畢璺姴 濡� 浣忓畢/鏂囦欢鍚�.</param>
-        public static void DeleteBackGroundIamageFilebyHomeId(string fileName)
-        {
-            if (fileName == null)
-            {
-                return;
-            }
-            var pathLists = fileName.Split('/');
-            if (pathLists == null || pathLists.Count() < 9 || pathLists[8] == null)
-            {
-                return;
-            }
-            var path = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
-            if (!Global.IsExistsByHomeId(pathLists[8]))
-            {
-                return;
-            }
-            //鍒犻櫎鏈湴鍥剧墖
-            System.IO.File.Delete(path);
-            //鍒犻櫎澶囦唤
-            Phone.UserCenter.HdlAutoBackupLogic.DeleteFile(pathLists[8]);
-        }
 
         /// <summary>
         /// 绉诲姩鑳屾櫙鍥剧墖鍒颁綇瀹呯洰褰曚笅
@@ -704,7 +899,7 @@
         #region 鈼� 娣诲姞璁惧________________________
 
         /// <summary>
-        /// 娣诲姞璁惧
+        /// 娣诲姞璁惧(姝ゆ柟娉曠洰鍓嶅彧缁欍�愭垜鐨勫枩鐖便�戜娇鐢�)
         /// </summary>
         /// <param name="deviceUIFilePath">Device UIF ile path.</param>
         public void AddDevice(string deviceUIFilePath)
@@ -713,9 +908,8 @@
             {
                 return;
             }
-            var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceUIFilePath));
-            var deviceUI = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceUI>(jsonInfo);
-            if (null == deviceUI || null == deviceUI.CommonDevice)
+            var deviceUI = Common.LocalDevice.Current.GetDeviceUI(deviceUIFilePath);
+            if (null == deviceUI.CommonDevice)
             {
                 //褰撳墠瀵硅薄鏁版嵁鏃犳晥
                 return;
@@ -730,10 +924,11 @@
         }
 
         /// <summary>
-        /// 娣诲姞璁惧
+        /// 娣诲姞璁惧(姝ゆ柟娉曠洰鍓嶅彧缁橰oom閲岄潰浣跨敤)
         /// </summary>
         /// <param name="device">瑕佹坊鍔犵殑璁惧瀵硅薄</param>
-        public void AddDevice(CommonDevice device)
+        /// <param name="saveRealRoom">鏄惁淇敼鐪熷疄鐗╃悊璁惧鐨勬埧闂�,涓嶅嚭鎰忓,杩欎釜鍊奸粯璁や负true鍗冲彲</param>
+        public void AddDevice(CommonDevice device, bool saveRealRoom)
         {
             if (device == null)
             {
@@ -742,10 +937,7 @@
             //璁惧淇℃伅淇濆瓨鍒版湰鍦�
             device.Save();
 
-            //娣诲姞鑷姩澶囦唤
-            HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
-
-            DeviceUI deviceUI = Common.LocalDevice.Current.GetDeviceUI(device);
+            var deviceUI = Common.LocalDevice.Current.GetDeviceUI(device);
             if (DeviceUIFilePathList.Contains(deviceUI.FileName) == false)
             {
                 DeviceUIFilePathList.Add(deviceUI.FileName);
@@ -753,8 +945,11 @@
                 //淇濆瓨鍒版湰鍦�
                 Save();
 
-                //娣诲姞鑷姩澶囦唤
-                HdlAutoBackupLogic.AddOrEditorFile(FileName);
+                if (saveRealRoom == true && LocalDevice.Current.GetDevicesCountByMac(device.DeviceAddr) == 1)
+                {
+                    //濡傛灉鍙湁涓�涓洖璺�,鍒欎慨鏀圭湡瀹炵墿鐞嗚澶囩殑鎴块棿
+                    LocalDevice.Current.SaveRealDeviceRoomId(new List<CommonDevice>() { device }, this.Id, false);
+                }
             }
         }
 
@@ -768,11 +963,10 @@
         public void DeleteDevice(string deviceUIFilePath)
         {
             if (deviceUIFilePath == null) return;
-            if (DeviceUIFilePathList.Contains(deviceUIFilePath) && Global.IsExistsByHomeId(deviceUIFilePath))
+            if (DeviceUIFilePathList.Contains(deviceUIFilePath))
             {
                 DeviceUIFilePathList.Remove(deviceUIFilePath);
                 DeviceUIList.RemoveAll((obj) => obj.FileName == deviceUIFilePath);
-                HdlAutoBackupLogic.DeleteFile(deviceUIFilePath);
                 Save();
             }
         }
@@ -787,65 +981,57 @@
             {
                 return;
             }
-            DeviceUI deviceUI = new DeviceUI();
-            deviceUI.DeviceAddr = device.DeviceAddr;
-            deviceUI.DeviceEpoint = device.DeviceEpoint;
-
             //鏍规嵁璁惧锛岃幏鍙栨墍鍦ㄧ殑鎴块棿
             var room = this.GetRoomByDevice(device);
             if (room == null)
             {
                 return;
             }
+            string deviceFile = device.FilePath;
             //绉婚櫎缂撳瓨
-            if (room.DeviceUIFilePathList.Contains(deviceUI.FileName) == false)
+            if (room.DeviceUIFilePathList.Contains(deviceFile) == false)
             {
                 return;
             }
-            room.DeviceUIFilePathList.Remove(deviceUI.FileName);
+            room.DeviceUIFilePathList.Remove(deviceFile);
 
-            room.DeviceUIList.RemoveAll((obj) => obj.FileName == deviceUI.FileName);
+            room.DeviceUIList.RemoveAll((obj) => obj.FileName == deviceFile);
             room.Save();
             //鏇存敼鑷姩澶囦唤
             HdlAutoBackupLogic.AddOrEditorFile(room.FileName);
+
+            //閫掑綊锛氬垹闄ゆ帀浠ュ墠鐨勬棫鏁版嵁瀵艰嚧鐨勫涓埧闂寸殑闂
+            this.DeleteDevice(device);
+        }
+
+        /// <summary>
+        /// 鍒犻櫎鎴戠殑鍠滅埍鐨勮澶�
+        /// </summary>
+        /// <param name="device">瑕佸垹闄ょ殑璁惧瀵硅薄</param>
+        public void DeleteLoveDevice(CommonDevice device)
+        {
+            if (device == null)
+            {
+                return;
+            }
+            //鎴戠殑鍠滅埍
+            var loveRoom = this.GetLoveRoom();
+            if (loveRoom != null)
+            {
+                string deviceFile = device.FilePath;
+                //绉婚櫎缂撳瓨
+                if (loveRoom.DeviceUIFilePathList.Contains(deviceFile) == false)
+                {
+                    return;
+                }
+                loveRoom.DeviceUIFilePathList.Remove(deviceFile);
+                loveRoom.DeviceUIList.RemoveAll((obj) => obj.FileName == deviceFile);
+            }
         }
 
         #endregion
 
         #region 鈼� 鑾峰彇璁惧_________________________
-
-        /// <summary>
-        /// 鑾峰彇鎵�鏈夋埧闂寸殑鎵�鏈夎澶�
-        /// </summary>
-        public static void GetAllRoomDeviceUIList()
-        {
-            List<string> devicePathList = new List<string>();
-            //鎵�鏈夋埧闂寸殑鎵�鏈夎澶�
-            AllRoomDeviceUIList.Clear();
-            foreach (var room in Shared.Common.Room.Lists)
-            {
-                if(room.IsSharedRoom)
-                {
-                    continue;
-                }
-                if (room.DeviceUIList.Count == 0)
-                {
-                    continue;
-                }
-                foreach (var device in room.DeviceUIList)
-                {
-                    if (device == null || device.CommonDevice == null)
-                    {
-                        continue;
-                    }
-                    if (!devicePathList.Contains(device.FileName))
-                    {
-                        AllRoomDeviceUIList.Add(device);
-                        devicePathList.Add(device.FileName);
-                    }
-                }
-            }
-        }
 
         /// <summary>
         /// 鏍规嵁璁惧鑾峰彇瀹冪殑UI瀵硅薄锛屽鏋滀笉瀛樺湪鍒欐柊寤�
@@ -855,25 +1041,6 @@
         public DeviceUI GetDeviceUI(CommonDevice device)
         {
             return Common.LocalDevice.Current.GetDeviceUI(device);
-        }
-
-        /// <summary>
-        /// 閲嶆柊鑾峰彇-鎵�鏈夋埧闂寸殑鎵�鏈夎澶囩被鍨�
-        /// </summary>
-        public static void GetAllRoomDeviceTypeList()
-        {
-            AllRoomDeviceTypeList.Clear();
-            foreach (var deviceUI in AllRoomDeviceUIList)
-            {
-                if (deviceUI == null || deviceUI.CommonDevice == null)
-                {
-                    continue;
-                }
-                if (!AllRoomDeviceTypeList.Contains(deviceUI.CommonDevice.Type))
-                {
-                    AllRoomDeviceTypeList.Add(deviceUI.CommonDevice.Type);
-                }
-            }
         }
 
         /// <summary>
@@ -891,11 +1058,7 @@
                 }
                 listDevice.Add(device.CommonDevice);
             }
-            if (listDevice.Count == 0)
-            {
-                return listDevice;
-            }
-            return Common.LocalDevice.Current.SortDevice(listDevice);
+            return listDevice;
         }
 
         /// <summary>
@@ -947,6 +1110,31 @@
             return typeList;
         }
 
+        /// <summary>
+        /// 鑾峰彇鏈垎閰嶅尯鍩熻澶�
+        /// </summary>
+        /// <returns></returns>
+        public List<DeviceUI> GetUnalloctedDeviceUIs()
+        {
+            List<DeviceUI> deviceUIs = new List<DeviceUI> { };
+            var dList = AllRoomDeviceUIList;
+            var commonDeviceList = Common.LocalDevice.Current.listAllDevice;
+            
+            foreach (var device in commonDeviceList)
+            {
+                if (dList.Find((obj) => obj.CommonDevice.DeviceEpoint == device.DeviceEpoint && obj.CommonDevice.DeviceAddr == device.DeviceAddr) == null)
+                {
+                    deviceUIs.Add(Common.LocalDevice.Current.GetDeviceUI(device));
+                }
+            }
+            if (deviceUIs.Count == 0)
+            {
+                return null;
+            }
+            return deviceUIs;
+        }
+
+        //public List<DeviceUI> GetUnalloctedDeviceUITypes
 
         #endregion
 
@@ -960,27 +1148,23 @@
         /// <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)
         {
-            if (AllRoomSceneUIList.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)
             {
                 return 0;
             }
             var getSceneIdData = getSceneIdAllData.getSceneIdData;
-            //寰幆鐩稿悓鎶ラ敊
-            foreach (var tempSceneUI in AllRoomSceneUIList)
-            {
-                if (tempSceneUI.Id == getSceneIdAllData.getSceneIdData.NewScenesId)
-                {
-                    //鍦烘櫙宸插瓨鍦�
-                    return -1;
-                }
-            }
 
             bool result = true;
             foreach (var common in commons)
@@ -988,12 +1172,13 @@
                 //娣诲姞鏂版垚鍛�
                 var addSceneMemberData = new ZigBee.Device.Scene.AddSceneMemberData
                 {
-                    DeviceAddr = common.DeviceAddr,
                     Type = common.Type,
+                    DeviceAddr = common.DeviceAddr,
                     Epoint = common.Epoint,
                     ScenesId = getSceneIdData.NewScenesId,
                     TaskList = common.TaskList,
                     DelayTime = common.DelayTime,
+                    MemberNumber=common.MemberNumber,
                     ElseScenesId = common.ElseScenesId
                 };
                 //common.ScenesId = getSceneIdData.NewScenesId;
@@ -1012,26 +1197,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;
@@ -1043,17 +1220,24 @@
         /// <param name="scene">Scene.</param>
         public void AddScene(SceneUI scene)
         {
-            var sceneUI = new SceneUI
+            SceneUIList.Add(scene);
+            SceneUIFilePathList.Add(scene.FileName);
+            Save();
+        }
+
+        /// <summary>
+        /// 鍒犻櫎鍦烘櫙
+        /// </summary>
+        /// <param name="scene"></param>
+        public void DeleteScene(SceneUI scene)
+        {
+            var curScene = SceneUIList.Find((obj) => obj.Id == scene.Id);
+            if (curScene == null)
             {
-                Name = scene.Name,
-                Id = scene.Id,
-                IconPath = scene.IconPath,
-                IconPathType = scene.IconPathType,
-                AddSceneMemberDataList = scene.AddSceneMemberDataList
-            };
-            SceneUIList.Add(sceneUI);
-            SceneUIFilePathList.Add(sceneUI.FileName);
-            sceneUI.Save();
+                return;
+            }
+            SceneUIList.Remove(curScene);
+            SceneUIFilePathList.Remove(curScene.FileName);
             Save();
         }
 
@@ -1064,15 +1248,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;
@@ -1092,10 +1271,10 @@
         /// <param name="addCommons">Add commons.</param>
         public async System.Threading.Tasks.Task<int> ModifyScene(SceneUI sceneUI, Scene.SceneRemoveMemberData sceneRemoveMemberData, List<Scene.AddSceneMemberData> addCommons)
         {
-            if (AllRoomSceneUIList.Find(s => s.Name == sceneUI.Name) == null)
-            {
-                return 0;
-            }
+            //if (AllRoomSceneUIList.Find(s => s.Name == sceneUI.Name) == null)
+            //{
+            //    return 0;
+            //}
 
             bool result = true;
             //绉婚櫎鎴愬憳 杩斿洖缁撴灉
@@ -1125,7 +1304,8 @@
                     ScenesId = sceneUI.Id,
                     TaskList = addCommon.TaskList,
                     DelayTime = addCommon.DelayTime,
-                    ElseScenesId = addCommon.ElseScenesId
+                    ElseScenesId = addCommon.ElseScenesId,
+                    MemberNumber=addCommon.MemberNumber
                 };
                 //娣诲姞鏂版垚鍛� 杩斿洖缁撴灉
                 var addSceneMemberResponseAllData = await ZigBee.Device.Scene.AddSceneMemberAsync(addSceneMemberData);
@@ -1146,9 +1326,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
@@ -1165,12 +1397,30 @@
             {
                 return;
             }
+
+            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)
+                {
+                    CurrentRoom.GetLoveRoom().DeleteScene(sceneUI);
+                }
+            }
+
             SceneUIList.Remove(sceneUI);
             SceneUIFilePathList.Remove(sceneUI.FileName);
             Save();
+
             Global.DeleteFilebyHomeId(sceneUI.FileName);
             HdlAutoBackupLogic.DeleteFile(sceneUI.FileName);
-            HdlAutoBackupLogic.AddOrEditorFile(FileName);
         }
 
         #endregion
@@ -1186,6 +1436,10 @@
         {
             foreach (var r in Lists)
             {
+                if (r.IsLove)
+                {
+                    continue;
+                }
                 foreach (var sceneUI in r.SceneUIList)
                 {
                     if (sceneUI.Id == sceneId)
@@ -1197,6 +1451,89 @@
             return null;
         }
 
+        /// <summary>
+        /// 鑾峰彇璇ユゼ灞傛墍鏈夊満鏅�
+        /// </summary>
+        /// <param name="floorId"></param>
+        /// <returns></returns>
+        public List<SceneUI> GetSameFloorScenes(string floorId)
+        {
+            List<SceneUI> sceneUIs = new List<SceneUI> { };
+            var rooms= CurrentRoom.GetRoomsByFloorId(floorId);
+            foreach(var r in rooms)
+            {
+                sceneUIs.AddRange(r.SceneUIList);
+            }
+            return sceneUIs;
+        }
+
+        /// <summary>
+        /// 鑾峰彇璇ユゼ灞傜殑鍦烘櫙
+        /// </summary>
+        /// <param name="floorId"></param>
+        /// <returns></returns>
+        public List<SceneUI> GetSceneUIsByFloorId(string floorId)
+        {
+            var rooms = GetRoomsByFloorId(floorId);
+            if (rooms == null)
+            {
+                return null;
+            }
+            var sceneList = new List<SceneUI> { };
+            foreach (var r in rooms)
+            {
+                if (r.SceneUIList.Count == 0)
+                {
+                    continue;
+                }
+                foreach (var sceneUI in r.SceneUIList)
+                {
+                    if (sceneUI == null)
+                    {
+                        continue;
+                    }
+                    sceneList.Add(sceneUI);
+                }
+            }
+            return sceneList;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鏈垎閰嶅尯鍩熷満鏅�
+        /// </summary>
+        /// <returns></returns>
+        public List<SceneUI> GetUnalloctedScenes()
+        {
+            List<SceneUI> sceneUIs = new List<SceneUI> { };
+            var sList = AllRoomSceneUIFilepathList;
+
+            List<string> sfile = new List<string> { };
+            foreach (var path in Global.FileListByHomeId())
+            {
+                if (path.StartsWith("Scene_", StringComparison.Ordinal))
+                {
+                    sfile.Add(path);
+                }
+            }
+            foreach (var path in sfile)
+            {
+                if (sList.Find((obj) => obj == path) == null)
+                {
+                    var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(path));
+                    var tempSceneUI = Newtonsoft.Json.JsonConvert.DeserializeObject<SceneUI>(jsonInfo);
+                    if (tempSceneUI != null)
+                    {
+                        sceneUIs.Add(tempSceneUI);
+                    }
+                }
+            }
+            if (sceneUIs.Count == 0)
+            {
+                return null;
+            }
+            return sceneUIs;
+        }
+
         #endregion
 
         #region 鈼� 鍒锋柊鍦烘櫙_________________________
@@ -1206,51 +1543,45 @@
         /// </summary>
         public async System.Threading.Tasks.Task<bool> RefreshSceneUIList()
         {
-            return false;
-            //System.Console.WriteLine($"寮�濮嬭姹傜綉鍏冲満鏅�****{DateTime.Now.ToString("yyMMdd hhmmss fff")}*****");
-            //bool result = true;
-            //var sceneList = await ZigBee.Device.Scene.GetSceneListAsync();
-            //System.Console.WriteLine($"缁撴潫璇锋眰缃戝叧鍦烘櫙****{DateTime.Now.ToString("yyMMdd hhmmss fff")}*****");
-            //if (sceneList == null)
-            //{
-            //    return false;
-            //}
-            //List<int> sceneIDList = new List<int> { };
-            //System.Console.WriteLine($"寮�濮嬫湰鍦板満鏅�****{DateTime.Now.ToString("yyMMdd hhmmss fff")}*****");
-            //foreach (var scene in sceneList)
-            //{
-            //    if (scene == null)
-            //    {
-            //        continue;
-            //    }
-            //    sceneIDList.Add(scene.ScenesId);
-            //}
-            //foreach(var r in Lists)
-            //{
-            //    if(r.SceneUIList==null || r.SceneUIList.Count==0)
-            //    {
-            //        continue;
-            //    }
-            //    foreach(var rScene in r.SceneUIList)
-            //    {
-            //        if(rScene==null)
-            //        {
-            //            continue;
-            //        }
-            //        //var scene = sceneList.Find((obj) => rScene.ScenesId == obj.ScenesId);
-            //        if(sceneIDList.Contains(rScene.ScenesId)==false)
-            //        {
-            //            var removeResult = r.SceneUIList.Remove(rScene);
-            //            r.Save();
-            //            if (removeResult == false)
-            //            {
-            //                result = false;
-            //            }
-            //        }
-            //    }
-            //}
-            //System.Console.WriteLine($"缁撴潫鏈湴鍦烘櫙****{DateTime.Now.ToString("yyMMdd hhmmss fff")}*****");
-            //return result;
+            bool result = true;
+            var sceneList = await ZigBee.Device.Scene.GetSceneListAsync();
+            if (sceneList == null)
+            {
+                return false;
+            }
+            List<int> sceneIDList = new List<int> { };
+            foreach (var scene in sceneList)
+            {
+                if (scene == null)
+                {
+                    continue;
+                }
+                sceneIDList.Add(scene.ScenesId);
+            }
+            foreach (var r in Lists)
+            {
+                if(r.IsLove)
+                {
+                    continue;
+                }
+                if (r.SceneUIList == null || r.SceneUIList.Count == 0)
+                {
+                    continue;
+                }
+                foreach (var rScene in r.SceneUIList)
+                {
+                    if (rScene == null)
+                    {
+                        continue;
+                    }
+                    if (sceneIDList.Contains(rScene.Id) == false)
+                    { 
+                        r.RemoveScene(rScene);
+                        result = true;
+                    }
+                }
+            }
+            return result;
         }
 
         #endregion
@@ -1431,6 +1762,6 @@
         }
 
         #endregion
-       
+
     }
 }

--
Gitblit v1.8.0