From 05ce435c3b58e53eeab04c672affdeeab75f3036 Mon Sep 17 00:00:00 2001
From: gxc <guoxuecheng@guoxuechengdeMacBook-Pro.local>
Date: 星期五, 15 十一月 2019 14:41:39 +0800
Subject: [PATCH] 2019.11.15-1

---
 ZigbeeApp/Shared/Common/Room.cs |  304 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 190 insertions(+), 114 deletions(-)

diff --git a/ZigbeeApp/Shared/Common/Room.cs b/ZigbeeApp/Shared/Common/Room.cs
old mode 100755
new mode 100644
index 363128b..dbaa949
--- a/ZigbeeApp/Shared/Common/Room.cs
+++ b/ZigbeeApp/Shared/Common/Room.cs
@@ -76,10 +76,26 @@
         /// </summary>
         public string HumidityDevice = string.Empty;
 
+        /// <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)
+                {
+                    return Lists[0];
+                }
+                return m_CurrentRoom;
+            }
+            set { m_CurrentRoom = value; }
+        }
 
         /// <summary>
         /// 鎴块棿閲屾墍鏈夌殑璁惧鍒楄〃
@@ -140,11 +156,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>
         /// 鑾峰彇鎵�鏈夋埧闂寸殑鎵�鏈夊満鏅�
@@ -191,11 +224,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)
+                    {
+                        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,10 +282,10 @@
         /// </summary>
         public static void InitAllRoom()
         {
-            if (CanInitAllRoom == false)
-            {
-                return;
-            }
+            //if (CanInitAllRoom == false)
+            //{
+            //    return;
+            //}
             Lists.Clear();
             if (Config.Instance.Home.RoomFilePathList.Contains("Room_Favorite.json") == false)
             {
@@ -382,8 +445,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();
@@ -446,6 +522,10 @@
         /// <param name="roomId">鎴块棿ID</param>
         public Room GetRoomById(string roomId)
         {
+            if (string.IsNullOrEmpty(roomId))
+            {
+                return null;
+            }
             return Lists.Find((obj) => obj.Id == roomId);
         }
 
@@ -487,11 +567,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>
@@ -574,6 +651,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;
@@ -697,12 +778,12 @@
         public void AddDevice(string deviceUIFilePath)
         {
             if (string.IsNullOrEmpty(deviceUIFilePath))
-            {
-                return;
+
+             {
+                 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;
@@ -732,7 +813,7 @@
             //娣诲姞鑷姩澶囦唤
             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);
@@ -755,11 +836,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();
             }
         }
@@ -774,24 +854,21 @@
             {
                 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);
@@ -802,39 +879,6 @@
         #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瀵硅薄锛屽鏋滀笉瀛樺湪鍒欐柊寤�
         /// </summary>
         /// <returns>The device user interface.</returns>
@@ -842,25 +886,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>
@@ -878,11 +903,7 @@
                 }
                 listDevice.Add(device.CommonDevice);
             }
-            if (listDevice.Count == 0)
-            {
-                return listDevice;
-            }
-            return Common.LocalDevice.Current.SortDevice(listDevice);
+            return listDevice;
         }
 
         /// <summary>
@@ -934,6 +955,36 @@
             return typeList;
         }
 
+        /// <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;
+        }
 
         #endregion
 
@@ -949,25 +1000,21 @@
         /// <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)
         {
-            if (AllRoomSceneUIList.Find(s => s.Name == sceneName) != null)
+            var scenes = GetSceneUIsByFloorId(FloorId);
+            if(scenes!=null && scenes.Count>0)
             {
-                return -1;
+                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)
@@ -975,12 +1022,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;
@@ -1030,17 +1078,30 @@
         /// <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(sceneUI);
-            SceneUIFilePathList.Add(sceneUI.FileName);
-            sceneUI.Save();
+            //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();
+        }
+
+        /// <summary>
+        /// 鍒犻櫎鍦烘櫙
+        /// </summary>
+        /// <param name="scene"></param>
+        public void DeleteScene(SceneUI scene)
+        {
+            //scene.IsCollected = false;
+            scene.Save();
+            SceneUIList.Remove(scene);
+            SceneUIFilePathList.Remove(scene.FileName);
             Save();
         }
 
@@ -1112,7 +1173,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);
@@ -1183,7 +1245,21 @@
             }
             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;
+        }
         #endregion
 
         #region 鈼� 鍒锋柊鍦烘櫙_________________________
@@ -1418,6 +1494,6 @@
         }
 
         #endregion
-       
+
     }
 }

--
Gitblit v1.8.0