| | |
| | | using System.Text; |
| | | using ZigBee.Device; |
| | | using System.Linq; |
| | | |
| | | using Shared.Phone;
|
| | |
|
| | | namespace Shared.Common |
| | | { |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | [System.Serializable] |
| | | public class Room |
| | | { |
| | | #region ◆ 变量____________________________ |
| | | {
|
| | | #region ■ 变量声明___________________________ |
| | | |
| | | /// <summary> |
| | | /// 房间文件 |
| | | /// 是否是喜爱房间 |
| | | /// </summary> |
| | | /// <value>The name of the file.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public string FileName |
| | | { |
| | | get |
| | | { |
| | | return $"Room_{Id}.json"; |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 房间id--使用guid |
| | | /// Guid.NewGuid().ToString() |
| | | /// </summary> |
| | | public string Id = Guid.NewGuid().ToString(); |
| | | |
| | | /// <summary> |
| | | /// 房间名 |
| | | /// </summary> |
| | | public string Name = string.Empty; |
| | | |
| | | /// <summary> |
| | | /// 房间的时候背景图 |
| | | /// </summary> |
| | | public string BackgroundImage = string.Empty; |
| | | |
| | | /// <summary> |
| | | /// 图片来源 0--本地图库 1--拍照 2--系统图库 |
| | | /// </summary> |
| | | public int BackgroundImageType = 0; |
| | | |
| | | /// <summary> |
| | | /// 楼层--备用 |
| | | /// </summary> |
| | | public Dictionary<string, string> FloorList = new Dictionary<string, string> { }; |
| | | |
| | | /// <summary> |
| | | /// 当前选择的房间 |
| | | /// </summary> |
| | | public static Room CurrentRoom; |
| | | |
| | | /// <summary> |
| | | /// 房间里所有的设备列表 |
| | | /// </summary> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public List<DeviceUI> DeviceUIList = new List<DeviceUI> { }; |
| | | /// <summary> |
| | | ///设备文件路径列表 |
| | | /// </summary> |
| | | public readonly List<string> DeviceUIFilePathList = new List<string>(); |
| | | |
| | | /// <summary> |
| | | /// 是否是默认喜爱房间 |
| | | /// </summary> |
| | | /// <value><c>true</c> if is love; otherwise, <c>false</c>.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public bool IsLove |
| | | { |
| | |
| | | { |
| | | return Id == "Favorite"; |
| | | } |
| | | }
|
| | | /// <summary>
|
| | | /// 房间文件
|
| | | /// </summary>
|
| | | [Newtonsoft.Json.JsonIgnore]
|
| | | public string FileName
|
| | | {
|
| | | get
|
| | | {
|
| | | return $"Room_{Id}.json";
|
| | | }
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 房间id--使用guid |
| | | /// </summary> |
| | | public string Id = Guid.NewGuid().ToString(); |
| | | /// <summary> |
| | | /// 房间名 |
| | | /// </summary> |
| | | public string Name = string.Empty; |
| | | /// <summary> |
| | | /// 房间背景图 |
| | | /// </summary> |
| | | public string BackgroundImage = string.Empty; |
| | | /// <summary> |
| | | /// 图片来源 0--本地图库 1--拍照 2--系统图库 |
| | | /// </summary> |
| | | public int BackgroundImageType = 0; |
| | | /// <summary>
|
| | | /// 温度传感器(设备主键)
|
| | | /// </summary> |
| | | public string TemperatrueDevice = string.Empty; |
| | | /// <summary>
|
| | | /// 湿度传感器(设备主键)
|
| | | /// </summary> |
| | | public string HumidityDevice = string.Empty;
|
| | | /// <summary> |
| | | /// 是否是分享过来的房间 |
| | | /// 注:分享过来的房间不能删除,不能编辑该房间,不能对设备(功能)、场景进行增删改 |
| | | /// </summary> |
| | | public bool IsSharedRoom = false; |
| | | |
| | | public bool IsSharedRoom = false;
|
| | | /// <summary> |
| | | /// 是否可以刷新房间数据了 |
| | | /// 需要等初始化本地设备数据后在可以初始化房间数据 |
| | | /// 楼层Id |
| | | /// </summary> |
| | | public static bool canInitAllRoom; |
| | | |
| | | public string FloorId = string.Empty;
|
| | |
|
| | | /// <summary> |
| | | /// 获取喜爱房间的所有设备路径 |
| | | /// 房间里所有的设备列表(设备的主键:mac地址_端口) |
| | | /// </summary> |
| | | /// <value>The love room device list.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static List<string> LoveRoomDeviceUIFilePathList |
| | | { |
| | | get |
| | | { |
| | | if (Lists.Count == 0) |
| | | { |
| | | return new List<string> { }; |
| | | } |
| | | var loveRoom = Lists[0]; |
| | | return loveRoom.DeviceUIFilePathList; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 所有房间的所有设备类型 |
| | | /// 先调用 GetAllRoomDeviceTypeList() |
| | | /// </summary> |
| | | /// <value>All room device type list.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static List<DeviceType> AllRoomDeviceTypeList = new List<DeviceType> { }; |
| | | |
| | | /// <summary> |
| | | /// 获取所有房间的所有场景 |
| | | /// </summary> |
| | | /// <value>All room scene UIL ist.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static List<SceneUI> AllRoomSceneUIList |
| | | { |
| | | get |
| | | { |
| | | var sceneList = new List<SceneUI> { }; |
| | | 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; |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 场景列表---不再序列化 |
| | | /// </summary> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public List<SceneUI> SceneUIList = new List<SceneUI> { }; |
| | | /// <summary> |
| | | ///场景文件列表 |
| | | /// </summary> |
| | | public readonly List<string> SceneUIFilePathList = new List<string>(); |
| | | |
| | | /// <summary> |
| | | /// 所有的房间信息 |
| | | /// </summary> |
| | | public static List<Room> Lists = new List<Room>(); |
| | | |
| | | /// <summary> |
| | | /// 获取所有房间的所有设备 |
| | | /// 请先调用 GetAllRoomDeviceUIList()方法 |
| | | /// </summary> |
| | | /// <value>All room device UIL ist.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static List<DeviceUI> AllRoomDeviceUIList = new List<DeviceUI> { }; |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 构造方法________________________ |
| | | |
| | | /// <summary> |
| | | /// 构造方法 |
| | | /// </summary> |
| | | static Room() |
| | | { |
| | | InitAllRoom(); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 初始化__________________________ |
| | | |
| | | /// <summary> |
| | | /// 初始化房间信息 |
| | | /// 从文件中全部读取所有的房间数据到内存 |
| | | /// </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 = "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); |
| | | |
| | | if (null != room) |
| | | { |
| | | if (room.IsSharedRoom) |
| | | { |
| | | room.Name = $"({Language.StringByID(R.MyInternationalizationString.Shared)}){room.Name}"; |
| | | } |
| | | Lists.Add(room); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public List<string> ListDevice = new List<string>(); |
| | | /// <summary>
|
| | | /// 从本地重新加载全部的房间
|
| | | /// 房间内所有设备的Mac列表(物理设备所在的区域专用,含网关)
|
| | | /// </summary> |
| | | public static void RefreshAllRoomByLocation()
|
| | | {
|
| | | Lists.Clear();
|
| | | var homeTemp = Config.Instance.Home;
|
| | | homeTemp.RoomFilePathList.Clear();
|
| | | public List<string> ListDeviceMac = new List<string>(); |
| | | /// <summary> |
| | | ///场景列表(场景的ID) |
| | | /// </summary> |
| | | public List<int> ListSceneId = new List<int>();
|
| | |
|
| | | var listFile = Global.FileListByHomeId();
|
| | | #endregion
|
| | |
|
| | | //我的喜爱的房间必须要在第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 ◆ 添加房间_________________________ |
| | | |
| | | /// <summary> |
| | | /// 增加房间 |
| | | /// </summary> |
| | | /// <returns><c>true</c>, if room was added, <c>false</c> otherwise.</returns> |
| | | /// <param name="room">Room.</param> |
| | | 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 ◆ 删除房间_________________________ |
| | | |
| | | /// <summary> |
| | | /// 删除房间 |
| | | /// </summary> |
| | | 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 ◆ 获取房间________________________ |
| | | |
| | | /// <summary> |
| | | /// 通过路径获取房间 |
| | | /// </summary> |
| | | /// <returns>The room by file path.</returns> |
| | | /// <param name="roomFilePath">Room file path.</param> |
| | | public static Room GetRoomByFilePath(string roomFilePath) |
| | | { |
| | | try |
| | | { |
| | | var roomFile = Global.ReadFileByHomeId(roomFilePath); |
| | | var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(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<DeviceUI>(); |
| | | tempDeviceUIList.AddRange(beforeRoom.DeviceUIList); |
| | | var tempSceneUIList = new List<SceneUI>(); |
| | | 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<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(); |
| | | foreach (var sceneFilePath in beforeRoom.SceneUIFilePathList) |
| | | { |
| | | var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(sceneFilePath)); |
| | | var tempSceneUI = Newtonsoft.Json.JsonConvert.DeserializeObject<SceneUI>(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<SceneUI>(jsonInfo); |
| | | if (tempScene == null) |
| | | { |
| | | continue; |
| | | } |
| | | nowRoom.SceneUIList.Add(tempScene); |
| | | } |
| | | return nowRoom; |
| | | } |
| | | catch(Exception ex) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据房间名字,获取房间对象 |
| | | /// </summary> |
| | | /// <returns>The room by name.</returns> |
| | | /// <param name="roomName">房间名</param> |
| | | public Room GetRoomByName(string roomName) |
| | | { |
| | | return Lists.Find((obj) => obj.Name == roomName); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据设备获取房间名字 |
| | | /// </summary> |
| | | /// <returns>房间名</returns> |
| | | /// <param name="device">设备对象</param> |
| | | public List<string> GetRoomListNameByDevice(CommonDevice device) |
| | | { |
| | | var listName = new List<string>(); |
| | | var listroom = this.GetRoomByDevice(device); |
| | | foreach (var room in listroom) |
| | | { |
| | | listName.Add(room.Name); |
| | | } |
| | | |
| | | return listName; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据设备获取房间名字(多个房间的时候,用“,”分割) |
| | | /// </summary> |
| | | /// <returns>房间名</returns> |
| | | /// <param name="device">设备对象</param> |
| | | public string GetRoomNameByDevice(CommonDevice device) |
| | | { |
| | | var listName = this.GetRoomListNameByDevice(device); |
| | | |
| | | return this.GetRoomName(listName); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取多个房间的连接起来的名字 |
| | | /// </summary> |
| | | /// <param name="listName"></param> |
| | | /// <returns></returns> |
| | | public string GetRoomName(List<string> listName) |
| | | { |
| | | if (listName.Count == 0) |
| | | { |
| | | //未分配区域 |
| | | return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom); |
| | | } |
| | | return string.Join(",", listName); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取所有房间的所有设备 |
| | | /// </summary> |
| | | /// <returns>The room by device.</returns> |
| | | /// <param name="device">设备对象</param> |
| | | public List<Room> GetRoomByDevice(CommonDevice device) |
| | | { |
| | | var deviceUI = new DeviceUI { };
|
| | | deviceUI.DeviceAddr = device.DeviceAddr;
|
| | | deviceUI.DeviceEpoint = device.DeviceEpoint; |
| | | |
| | | return Lists.FindAll((obj) => obj.DeviceUIFilePathList.Contains(deviceUI.FileName)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 通过场景id获取房间名 |
| | | /// </summary> |
| | | /// <returns>The room name by scene identifier.</returns> |
| | | /// <param name="sceneId">Scene identifier.</param> |
| | | public string GetRoomNameBySceneId(int sceneId) |
| | | { |
| | | var room = GetRoomBySceneId(sceneId); |
| | | if (room == null) |
| | | { |
| | | return null; |
| | | } |
| | | return room.Name; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 通过场景id获取房间对象 |
| | | /// </summary> |
| | | /// <returns>The room by scene identifier.</returns> |
| | | /// <param name="sceneId">Scene identifier.</param> |
| | | 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 ◆ 房间方法________________________ |
| | | |
| | | /// <summary> |
| | | /// 设备的房间变更 |
| | | /// </summary> |
| | | /// <param name="device">设备对象</param> |
| | | /// <param name="listnewName">新房间名字列表</param> |
| | | public void ChangedRoom(CommonDevice device, List<string> listnewName) |
| | | { |
| | | //房间是否修改 |
| | | if (this.IsRoomChanged(device, listnewName) == false) |
| | | { |
| | | return; |
| | | } |
| | | var list = new List<string>(); |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 房间名字是否有修改 |
| | | /// </summary> |
| | | /// <param name="device">设备对象</param> |
| | | /// <param name="listnewName">新房间名字列表</param> |
| | | /// <returns></returns> |
| | | public bool IsRoomChanged(CommonDevice device, List<string> 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 ◆ 更新房间_________________________ |
| | | |
| | | /// <summary> |
| | | /// Updates the memorry. |
| | | /// </summary> |
| | | /// <param name="roomFilePath">Room file path.</param> |
| | | public static void UpdateMemorry(string roomFilePath) |
| | | { |
| | | GetRoomByFilePath(roomFilePath); |
| | | } |
| | | |
| | | #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> |
| | | /// 移动背景图片到住宅目录下 |
| | | /// </summary> |
| | | /// <param name="oldFile">需要移动的文件(直接文件名)</param> |
| | | /// <param name="newFile">要移动到的位置-(包含住宅路径:住宅/文件名)</param> |
| | | 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 ◆ 添加设备________________________ |
| | | |
| | | /// <summary> |
| | | /// 添加设备 |
| | | /// </summary> |
| | | /// <param name="deviceUIFilePath">Device UIF ile path.</param> |
| | | public void AddDevice(string deviceUIFilePath) |
| | | { |
| | | if (string.IsNullOrEmpty(deviceUIFilePath)) |
| | | { |
| | | return; |
| | | } |
| | | var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceUIFilePath)); |
| | | var deviceUI = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceUI>(jsonInfo); |
| | | if (null == deviceUI || null == deviceUI.CommonDevice) |
| | | { |
| | | //当前对象数据无效 |
| | | return; |
| | | } |
| | | if (!DeviceUIFilePathList.Contains(deviceUIFilePath) && !DeviceUIList.Contains(deviceUI)) |
| | | { |
| | | DeviceUIFilePathList.Add(deviceUIFilePath); |
| | | DeviceUIList.Add(deviceUI); |
| | | } |
| | | //保存到本地 |
| | | Save(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 添加设备 |
| | | /// </summary> |
| | | /// <param name="device">要添加的设备对象</param> |
| | | /// <param name="backUpUI">是否要覆盖UI文件</param> |
| | | 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); |
| | | if (DeviceUIFilePathList.Contains(deviceUI.FileName) == false) |
| | | { |
| | | DeviceUIFilePathList.Add(deviceUI.FileName); |
| | | DeviceUIList.Add(deviceUI); |
| | | //保存到本地 |
| | | Save(); |
| | | |
| | | //添加自动备份 |
| | | HdlAutoBackupLogic.AddOrEditorFile(FileName); |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 删除设备_________________________ |
| | | |
| | | /// <summary> |
| | | /// 删除功能-设备 |
| | | /// </summary> |
| | | /// <param name="deviceUIFilePath">Device UIF ile path.</param> |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 删除设备 |
| | | /// </summary> |
| | | /// <param name="device">要删除的设备对象</param> |
| | | /// <param name="DeleteUi">是否删除UI文件</param> |
| | | public void DeleteDevice(CommonDevice device, bool DeleteUi = true) |
| | | { |
| | | List<string> listNotDelRoom = new List<string>(); |
| | | //删除设备 |
| | | this.DeleteDevice(device, ref listNotDelRoom, DeleteUi); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 删除设备(特殊函数,现阶段是给设备变更房间使用的) |
| | | /// </summary> |
| | | /// <param name="device">要删除的设备对象</param> |
| | | /// <param name="listNotDelRoom">不执行删除的房间,当指定的不能删除的房间存在时,会从这个列表中移除,最终只剩下新的房间</param> |
| | | /// <param name="DeleteUi">是否删除UI文件</param> |
| | | public void DeleteDevice(CommonDevice device, ref List<string> listNotDelRoom, bool DeleteUi) |
| | | { |
| | | if (device == null) |
| | | { |
| | | return; |
| | | } |
| | | DeviceUI deviceUI = new DeviceUI(); |
| | | deviceUI.DeviceAddr = device.DeviceAddr;
|
| | | deviceUI.DeviceEpoint = device.DeviceEpoint; |
| | | |
| | | //根据设备,获取所在的房间,因为它删的不一定是当前房间 |
| | | List<Room> 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 ◆ 获取设备_________________________ |
| | | |
| | | /// <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> |
| | | /// <param name="device">设备对象</param> |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 添加场景_________________________ |
| | | |
| | | /// <summary> |
| | | /// 添加场景 0失败 1成功 -1已经存在 |
| | | /// </summary> |
| | | /// <returns>The scene.</returns> |
| | | /// <param name="sceneName">Scene name.</param> |
| | | /// <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) |
| | | { |
| | | 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; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 添加场景 |
| | | /// </summary> |
| | | /// <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(); |
| | | Save(); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 修改场景________________________ |
| | | |
| | | /// <summary> |
| | | /// 修改场景设备 0失败 1成功 |
| | | /// </summary> |
| | | /// <returns>The scene.</returns> |
| | | /// <param name="sceneUI">Scene user interface.</param> |
| | | /// <param name="sceneRemoveMemberData">Scene remove member data.</param> |
| | | /// <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; |
| | | } |
| | | |
| | | 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 ◆ 删除场景________________________ |
| | | |
| | | /// <summary> |
| | | /// 移除场景--该仅进行了对本地场景数据的删除 |
| | | /// </summary> |
| | | /// <param name="sceneUI">Scene user interface.</param> |
| | | 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 ◆ 获取场景________________________ |
| | | |
| | | /// <summary> |
| | | /// 通过场景id获取场景 |
| | | /// </summary> |
| | | /// <returns>The scene UIB y scene identifier.</returns> |
| | | /// <param name="sceneId">Scene identifier.</param> |
| | | 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 ◆ 刷新场景_________________________ |
| | | |
| | | /// <summary> |
| | | /// 刷新房间的场景列表 |
| | | /// </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; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ◆ 保存____________________________ |
| | | |
| | | /// <summary> |
| | | /// 保存 |
| | | /// </summary> |
| | | /// <param name="autoBackup">是否自动备份,默认true</param> |
| | | 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 ◆ 克隆房间对象_____________________
|
| | | #region ■ 保存_______________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 克隆房间对象
|
| | | /// 保存
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public Room CloneRoomClass()
|
| | | /// <param name="autoBackup">是否自动备份,默认true</param>
|
| | | public void Save(bool autoBackup = true)
|
| | | {
|
| | | var newRoom = new Room();
|
| | | //克隆属性
|
| | | newRoom.Id = this.Id;
|
| | | newRoom.Name = this.Name;
|
| | | newRoom.BackgroundImage = this.BackgroundImage;
|
| | | newRoom.BackgroundImageType = this.BackgroundImageType;
|
| | |
|
| | | return newRoom;
|
| | | //保存房间信息
|
| | | Global.WriteFileByBytesByHomeId(FileName, Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
|
| | | if (autoBackup == true)
|
| | | {
|
| | | HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(FileName);
|
| | | }
|
| | | }
|
| | |
|
| | | #endregion |