using Shared.Phone.UserCenter; using System; using System.Collections.Generic; using System.Text; using ZigBee.Device; using System.Linq; namespace Shared.Common { /// /// 房间对象 /// [System.Serializable] public class Room { #region ◆ 变量____________________________ /// /// 房间文件 /// /// The name of the file. [Newtonsoft.Json.JsonIgnore] public string FileName { get { return $"Room_{Id}.json"; } } /// /// 房间id--使用guid /// Guid.NewGuid().ToString() /// public string Id = Guid.NewGuid().ToString(); /// /// 房间名 /// public string Name = string.Empty; /// /// 房间的时候背景图 /// public string BackgroundImage = string.Empty; /// /// 图片来源 0--本地图库 1--拍照 2--系统图库 /// public int BackgroundImageType = 0; /// /// 楼层--备用 /// public Dictionary FloorList = new Dictionary { }; /// /// 当前选择的房间 /// public static Room CurrentRoom; /// /// 房间里所有的设备列表 /// [Newtonsoft.Json.JsonIgnore] public List DeviceUIList = new List { }; /// ///设备文件路径列表 /// public readonly List DeviceUIFilePathList = new List(); /// /// 是否是默认喜爱房间 /// /// true if is love; otherwise, false. [Newtonsoft.Json.JsonIgnore] public bool IsLove { get { return Id == "Favorite"; } } /// /// 是否是分享过来的房间 /// 注:分享过来的房间不能删除,不能编辑该房间,不能对设备(功能)、场景进行增删改 /// public bool IsSharedRoom = false; /// /// 是否可以刷新房间数据了 /// 需要等初始化本地设备数据后在可以初始化房间数据 /// public static bool canInitAllRoom; /// /// 获取喜爱房间的所有设备路径 /// /// The love room device list. [Newtonsoft.Json.JsonIgnore] public static List LoveRoomDeviceUIFilePathList { get { if (Lists.Count == 0) { return new List { }; } var loveRoom = Lists[0]; return loveRoom.DeviceUIFilePathList; } } /// /// 所有房间的所有设备类型 /// 先调用 GetAllRoomDeviceTypeList() /// /// All room device type list. [Newtonsoft.Json.JsonIgnore] public static List AllRoomDeviceTypeList = new List { }; /// /// 获取所有房间的所有场景 /// /// All room scene UIL ist. [Newtonsoft.Json.JsonIgnore] public static List AllRoomSceneUIList { get { var sceneList = new List { }; foreach (var r in Shared.Common.Room.Lists) { if (r.SceneUIList.Count == 0) { continue; } foreach (var sceneUI in r.SceneUIList) { if (sceneUI == null) { continue; } sceneList.Add(sceneUI); } } return sceneList; } } /// /// 场景列表---不再序列化 /// [Newtonsoft.Json.JsonIgnore] public List SceneUIList = new List { }; /// ///场景文件列表 /// public readonly List SceneUIFilePathList = new List(); /// /// 所有的房间信息 /// public static List Lists = new List(); /// /// 获取所有房间的所有设备 /// 请先调用 GetAllRoomDeviceUIList()方法 /// /// All room device UIL ist. [Newtonsoft.Json.JsonIgnore] public static List AllRoomDeviceUIList = new List { }; #endregion #region ◆ 构造方法________________________ /// /// 构造方法 /// static Room() { InitAllRoom(); } #endregion #region ◆ 初始化__________________________ /// /// 初始化房间信息 /// 从文件中全部读取所有的房间数据到内存 /// 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 = "Favorite" }; love.Save(true); //添加到house 房间路径列表 var currentHome = Config.Instance.Home; currentHome.RoomFilePathList.Insert(0, love.FileName); currentHome.Save(); } foreach (var roomFilePath in Config.Instance.Home.RoomFilePathList) { var room = GetRoomByFilePath(roomFilePath); System.Console.WriteLine(roomFilePath); System.Console.WriteLine(room?.FileName); System.Console.WriteLine(room?.Name); if (null != room) { if (room.IsSharedRoom) { var sharedName = $"{room.Name}({Language.StringByID(R.MyInternationalizationString.Shared)})"; if (Lists.Find((obj) => obj.Name == sharedName) == null) { } room.Name = $"{room.Name}({Language.StringByID(R.MyInternationalizationString.Shared)})"; room.Save(); } Lists.Add(room); } } } /// /// 从本地重新加载全部的房间 /// public static void RefreshAllRoomByLocation() { Lists.Clear(); var homeTemp = Config.Instance.Home; homeTemp.RoomFilePathList.Clear(); var listFile = Global.FileListByHomeId(); //我的喜爱的房间必须要在第0位才行 string fRoom = "Room_Favorite.json"; if (listFile.Contains(fRoom) == true) { listFile.Remove(fRoom); homeTemp.AddRoomListFilePath(fRoom); } foreach (string fileName in listFile) { if (fileName.StartsWith("Room_")) { homeTemp.AddRoomListFilePath(fileName); } } homeTemp.Save(false); canInitAllRoom = true; InitAllRoom(); } #endregion #region ◆ 添加房间_________________________ /// /// 增加房间 /// /// true, if room was added, false otherwise. /// Room. public bool AddRoom(Room room) { if (string.IsNullOrEmpty(room.FileName) || Global.IsExistsByHomeId(room.FileName)) { return false; } if (Config.Instance.Home.RoomFilePathList.Contains(room.FileName)) { return false; } Lists.Add(room); var r = Config.Instance.Home.AddRoomListFilePath(room.FileName); if (r == false) { return false; } Save(); HdlAutoBackupLogic.AddOrEditorFile(room.FileName); return true; } #endregion #region ◆ 删除房间_________________________ /// /// 删除房间 /// public bool Remove(string roomFilePath) { var room = GetRoomByFilePath(roomFilePath); if (null == room) { return false; } //删除来自拍照或者系统图库的房间背景图片 if (room.BackgroundImageType == 1 || room.BackgroundImageType == 2) { DeleteBackGroundIamageFilebyHomeId(room.BackgroundImage); } if (Global.IsExistsByHomeId(roomFilePath) == false) { return false; } Config.Instance.Home.RemoveRoomListFilePath(roomFilePath); //删除文件 Global.DeleteFilebyHomeId(roomFilePath); Lists.Remove(room); HdlAutoBackupLogic.DeleteFile(roomFilePath); return true; } #endregion #region ◆ 获取房间________________________ /// /// 通过路径获取房间 /// /// The room by file path. /// Room file path. public static Room GetRoomByFilePath(string roomFilePath) { try { var roomFile = Global.ReadFileByHomeId(roomFilePath); var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(roomFile)); if (null == nowRoom) { System.Console.WriteLine("房间文件路径不对,文件路径为:" + roomFilePath); return null; } var beforeRoom = Lists.Find((obj) => obj.Id == nowRoom.Id); if (beforeRoom != null) { var tempDeviceUIList = new List(); tempDeviceUIList.AddRange(beforeRoom.DeviceUIList); var tempSceneUIList = new List(); tempSceneUIList.AddRange(beforeRoom.SceneUIList); //设备(deviceUI) beforeRoom.DeviceUIList.Clear(); foreach (var deviceFilePath in beforeRoom.DeviceUIFilePathList) { var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceFilePath)); var tempDeviceUI = Newtonsoft.Json.JsonConvert.DeserializeObject(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(); foreach (var sceneFilePath in beforeRoom.SceneUIFilePathList) { var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(sceneFilePath)); var tempSceneUI = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonInfo); if (tempSceneUI != null) { var scene = tempSceneUIList.Find((obj) => obj.FileName == tempSceneUI.FileName); if (scene != null) { beforeRoom.SceneUIList.Add(scene); } else { beforeRoom.AddScene(scene); } } } return beforeRoom; } //设备(deviceUI) nowRoom.DeviceUIList.Clear(); foreach (var deviceFilePath in nowRoom.DeviceUIFilePathList) { var tempCommon = LocalDevice.Current.GetDeviceUI(deviceFilePath); if (tempCommon == null || tempCommon.CommonDevice == null) { continue; } nowRoom.DeviceUIList.Add(tempCommon); } //场景(SceneUI) nowRoom.SceneUIList.Clear(); foreach (var sceneUIFilePath in nowRoom.SceneUIFilePathList) { var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(sceneUIFilePath)); var tempScene = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonInfo); if (tempScene == null) { continue; } nowRoom.SceneUIList.Add(tempScene); } return nowRoom; } catch(Exception ex) { return null; } } /// /// 根据房间名字,获取房间对象 /// /// The room by name. /// 房间名 public Room GetRoomByName(string roomName) { return Lists.Find((obj) => obj.Name == roomName); } /// /// 根据设备获取房间名字 /// /// 房间名 /// 设备对象 public List GetRoomListNameByDevice(CommonDevice device) { var listName = new List(); var listroom = this.GetRoomByDevice(device); foreach (var room in listroom) { listName.Add(room.Name); } return listName; } /// /// 根据设备获取房间名字(多个房间的时候,用“,”分割) /// /// 房间名 /// 设备对象 public string GetRoomNameByDevice(CommonDevice device) { var listName = this.GetRoomListNameByDevice(device); return this.GetRoomName(listName); } /// /// 获取多个房间的连接起来的名字 /// /// /// public string GetRoomName(List listName) { if (listName.Count == 0) { //未分配区域 return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom); } return string.Join(",", listName); } /// /// 获取所有房间的所有设备 /// /// The room by device. /// 设备对象 public List GetRoomByDevice(CommonDevice device) { var deviceUI = new DeviceUI { }; deviceUI.DeviceFileName = device.FilePath; return Lists.FindAll((obj) => obj.DeviceUIFilePathList.Contains(deviceUI.FileName)); } /// /// 通过场景id获取房间名 /// /// The room name by scene identifier. /// Scene identifier. public string GetRoomNameBySceneId(int sceneId) { var room = GetRoomBySceneId(sceneId); if (room == null) { return null; } return room.Name; } /// /// 通过场景id获取房间对象 /// /// The room by scene identifier. /// Scene identifier. public Room GetRoomBySceneId(int sceneId) { foreach (var r in Lists) { foreach (var scene in r.SceneUIList) { if (scene.Id == sceneId) { return r; } } } return null; } #endregion #region ◆ 房间方法________________________ /// /// 设备的房间变更 /// /// 设备对象 /// 新房间名字列表 public void ChangedRoom(CommonDevice device, List listnewName) { //房间是否修改 if (this.IsRoomChanged(device, listnewName) == false) { return; } var list = new List(); list.AddRange(listnewName); //从原来的房间移除设备,但是不删除UI文件 this.DeleteDevice(device, ref list, false); //添加到新的房间 foreach (string newRoom in list) { var room = this.GetRoomByName(newRoom); if (room != null) { //不覆盖UI文件 room.AddDevice(device, false); } } } /// /// 房间名字是否有修改 /// /// 设备对象 /// 新房间名字列表 /// public bool IsRoomChanged(CommonDevice device, List listnewName) { var listOld = this.GetRoomListNameByDevice(device); if (listOld.Count != listnewName.Count) { return true; } //个数一样时,比较内容 foreach (string newstring in listnewName) { if (listOld.Contains(newstring) == false) { return true; } } return false; } #endregion #region ◆ 更新房间_________________________ /// /// Updates the memorry. /// /// Room file path. public static void UpdateMemorry(string roomFilePath) { GetRoomByFilePath(roomFilePath); } #endregion #region ◆ 房间背景图的相关___________________ /// /// 删除背景图片 /// /// fileName实际上包含了住宅路劲 如 住宅/文件名. 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]); } /// /// 移动背景图片到住宅目录下 /// /// 需要移动的文件(直接文件名) /// 要移动到的位置-(包含住宅路径:住宅/文件名) public void MoveBackGroundIamageFileToDirectory(string oldFile, string newFile) { try { var newPath = System.IO.Path.Combine(Config.Instance.FullPath, newFile); var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath,oldFile); Global.MoveFileToDirectory(path, newPath); //备份 Phone.UserCenter.HdlAutoBackupLogic.AddOrEditorFile(oldFile); //Save(); } catch (Exception ex) { System.Console.WriteLine("移动图片异常 " + ex.Message); } } #endregion #region ◆ 添加设备________________________ /// /// 添加设备 /// /// Device UIF ile path. public void AddDevice(string deviceUIFilePath) { if (string.IsNullOrEmpty(deviceUIFilePath)) { return; } var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceUIFilePath)); var deviceUI = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonInfo); if (null == deviceUI || null == deviceUI.CommonDevice) { //当前对象数据无效 return; } if (!DeviceUIFilePathList.Contains(deviceUIFilePath) && !DeviceUIList.Contains(deviceUI)) { DeviceUIFilePathList.Add(deviceUIFilePath); DeviceUIList.Add(deviceUI); } //保存到本地 Save(); } /// /// 添加设备 /// /// 要添加的设备对象 /// 是否要覆盖UI文件 public void AddDevice(CommonDevice device, bool backUpUI = true) { if (device == null) { return; } //设备信息保存到本地 device.Save(); //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(device.FilePath); DeviceUI deviceUI = Common.LocalDevice.Current.GetDeviceUI(device); deviceUI.DeviceFileName = device.FilePath; if (backUpUI == true) { deviceUI.ReSave(); } else { deviceUI.Save(); } //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(deviceUI.FileName); if (DeviceUIFilePathList.Contains(deviceUI.FileName) == false) { DeviceUIFilePathList.Add(deviceUI.FileName); DeviceUIList.Add(deviceUI); //保存到本地 Save(); //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(FileName); } } #endregion #region ◆ 删除设备_________________________ /// /// 删除功能-设备 /// /// Device UIF ile path. public void DeleteDevice(string deviceUIFilePath) { if (deviceUIFilePath == null) return; if (DeviceUIFilePathList.Contains(deviceUIFilePath) && Global.IsExistsByHomeId(deviceUIFilePath)) { DeviceUIFilePathList.Remove(deviceUIFilePath); DeviceUIList.RemoveAll((obj) => obj.FileName == deviceUIFilePath); HdlAutoBackupLogic.DeleteFile(deviceUIFilePath); Save(); } } /// /// 删除设备 /// /// 要删除的设备对象 /// 是否删除UI文件 public void DeleteDevice(CommonDevice device, bool DeleteUi = true) { List listNotDelRoom = new List(); //删除设备 this.DeleteDevice(device, ref listNotDelRoom, DeleteUi); } /// /// 删除设备(特殊函数,现阶段是给设备变更房间使用的) /// /// 要删除的设备对象 /// 不执行删除的房间,当指定的不能删除的房间存在时,会从这个列表中移除,最终只剩下新的房间 /// 是否删除UI文件 public void DeleteDevice(CommonDevice device, ref List listNotDelRoom, bool DeleteUi) { if (device == null) { return; } DeviceUI deviceUI = new DeviceUI(); deviceUI.DeviceFileName = device.FilePath; //根据设备,获取所在的房间,因为它删的不一定是当前房间 List listroom = this.GetRoomByDevice(device); //删除UI文件 if (DeleteUi == true && Global.IsExistsByHomeId(deviceUI.FileName) == true) { Global.DeleteFilebyHomeId(deviceUI.FileName); //删除自动备份 Phone.UserCenter.HdlAutoBackupLogic.DeleteFile(deviceUI.FileName); } foreach (Room room in listroom) { if (listNotDelRoom.Contains(room.Name) == true) { listNotDelRoom.Remove(room.Name); continue; } //移除缓存 if (room.DeviceUIFilePathList.Contains(deviceUI.FileName) == false) { return; } room.DeviceUIFilePathList.Remove(deviceUI.FileName); room.DeviceUIList.RemoveAll((obj) => obj.FileName == deviceUI.FileName); room.Save(); //更改自动备份 Phone.UserCenter.HdlAutoBackupLogic.AddOrEditorFile(room.FileName); } } #endregion #region ◆ 获取设备_________________________ /// /// 获取所有房间的所有设备 /// public static void GetAllRoomDeviceUIList() { List devicePathList = new List(); //所有房间的所有设备 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); } } } } /// /// 根据设备获取它的UI对象,如果不存在则新建 /// /// The device user interface. /// 设备对象 public DeviceUI GetDeviceUI(CommonDevice device) { return Common.LocalDevice.Current.GetDeviceUI(device); } /// /// 重新获取-所有房间的所有设备类型 /// 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); } } } #endregion #region ◆ 添加场景_________________________ /// /// 添加场景 0失败 1成功 -1已经存在 /// /// The scene. /// Scene name. /// 背景图片,不包含住宅路径 如果iconPathType=1或者2 需要拼接住宅 变成 住宅/sceneIconPath /// Commons. /// I场景背景图片来源类型 图片来源 0--本地图库 1--拍照 2--系统图库 默认0 public async System.Threading.Tasks.Task AddScene(string sceneName, string sceneIconPath, List commons, int iconPathType = 0) { if (AllRoomSceneUIList.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) { //添加新成员 var addSceneMemberData = new ZigBee.Device.Scene.AddSceneMemberData { DeviceAddr = common.DeviceAddr, Type = common.Type, Epoint = common.Epoint, ScenesId = getSceneIdData.NewScenesId, TaskList = common.TaskList, DelayTime = common.DelayTime, ElseScenesId = common.ElseScenesId }; //common.ScenesId = getSceneIdData.NewScenesId; //添加新成员 返回结果 var addSceneMemberResponseAllData = await ZigBee.Device.Scene.AddSceneMemberAsync(addSceneMemberData); if (addSceneMemberResponseAllData == null || addSceneMemberResponseAllData.addSceneMemberResponseData == null) { continue; } var addSceneMemberResponseData = addSceneMemberResponseAllData.addSceneMemberResponseData; if (addSceneMemberResponseData == null && addSceneMemberResponseData.Result != 1) { result = false; } } //加入成功 if (result) { //iconPathType=0 直接传值, iconPathType=1和iconPathType=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, IconPathType = iconPathType, AddSceneMemberDataList= commons }; SceneUIList.Add(sceneUI); SceneUIFilePathList.Add(sceneUI.FileName); sceneUI.Save(); Save(); HdlAutoBackupLogic.AddOrEditorFile(sceneUI.FileName); return 1; } return 0; } /// /// 添加场景 /// /// Scene. 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(); Save(); } #endregion #region ◆ 修改场景________________________ /// /// 修改场景设备 0失败 1成功 /// /// The scene. /// Scene user interface. /// Scene remove member data. /// Add commons. public async System.Threading.Tasks.Task ModifyScene(SceneUI sceneUI, Scene.SceneRemoveMemberData sceneRemoveMemberData, List addCommons) { if (AllRoomSceneUIList.Find(s => s.Name == sceneUI.Name) == null) { return 0; } bool result = true; //移除成员 返回结果 var removeSceneMemberResponseAllData = await ZigBee.Device.Scene.RemoveSceneMemberAsync(sceneRemoveMemberData); if (removeSceneMemberResponseAllData == null || removeSceneMemberResponseAllData.removeSceneMemberResponseData == null) { return 0; } var removeSceneMemberResponseData = removeSceneMemberResponseAllData.removeSceneMemberResponseData; if (removeSceneMemberResponseData == null) { return 0; } if (removeSceneMemberResponseData.Result != 0) { result = false; } //添加 foreach (var addCommon in addCommons) { //添加新成员 var addSceneMemberData = new ZigBee.Device.Scene.AddSceneMemberData { DeviceAddr = addCommon.DeviceAddr, Type = addCommon.Type, Epoint = addCommon.Epoint, ScenesId = sceneUI.Id, TaskList = addCommon.TaskList, DelayTime = addCommon.DelayTime, ElseScenesId = addCommon.ElseScenesId }; //添加新成员 返回结果 var addSceneMemberResponseAllData = await ZigBee.Device.Scene.AddSceneMemberAsync(addSceneMemberData); if (addSceneMemberResponseAllData == null || addSceneMemberResponseAllData.addSceneMemberResponseData == null) { result = false; System.Console.WriteLine("添加场景失败"); continue; } var addSceneMemberResponseData = addSceneMemberResponseAllData.addSceneMemberResponseData; if (addSceneMemberResponseData == null && addSceneMemberResponseData.Result != 1) { result = false; } } //加入成功 if (result) { sceneUI.AddSceneMemberDataList = addCommons; sceneUI.Save(); return 1; } return 0; } #endregion #region ◆ 删除场景________________________ /// /// 移除场景--该仅进行了对本地场景数据的删除 /// /// Scene user interface. public void RemoveScene(SceneUI sceneUI) { if(Global.IsExistsByHomeId(sceneUI.FileName)==false) { return; } SceneUIList.Remove(sceneUI); SceneUIFilePathList.Remove(sceneUI.FileName); Save(); Global.DeleteFilebyHomeId(sceneUI.FileName); HdlAutoBackupLogic.DeleteFile(sceneUI.FileName); HdlAutoBackupLogic.AddOrEditorFile(FileName); } #endregion #region ◆ 获取场景________________________ /// /// 通过场景id获取场景 /// /// The scene UIB y scene identifier. /// Scene identifier. public SceneUI GetSceneUIBySceneId(int sceneId) { foreach (var r in Lists) { foreach (var sceneUI in r.SceneUIList) { if (sceneUI.Id == sceneId) { return sceneUI; } } } return null; } #endregion #region ◆ 刷新场景_________________________ /// /// 刷新房间的场景列表 /// public async System.Threading.Tasks.Task 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 sceneIDList = new List { }; //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; } #endregion #region ◆ 保存____________________________ /// /// 保存 /// /// 是否自动备份,默认true public void Save(bool autoBackup = true) { //保存房间信息 Global.WriteFileByBytesByHomeId(FileName, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this))); if (autoBackup == true) { HdlAutoBackupLogic.AddOrEditorFile(FileName); } } #endregion #region ◆ 克隆房间对象_____________________ /// /// 克隆房间对象 /// /// public Room CloneRoomClass() { var newRoom = new Room(); //克隆属性 newRoom.Id = this.Id; newRoom.Name = this.Name; newRoom.BackgroundImage = this.BackgroundImage; newRoom.BackgroundImageType = this.BackgroundImageType; return newRoom; } #endregion } }