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();
///
/// 楼层Id
/// 新增时使用Guid
///
public string FloorId = string.Empty;
///
/// 楼层名称
///
public string FloorName
{
get
{
return Config.Instance.Home.GetFloorNameById(FloorId);
}
}
///
/// 房间名
///
public string Name = string.Empty;
///
/// 房间背景图
///
public string BackgroundImage = string.Empty;
///
/// 图片来源 0--本地图库 1--拍照 2--系统图库
///
public int BackgroundImageType = 0;
///
/// 楼层--备用
///
public Dictionary FloorList = new Dictionary { };
///
/// 温度传感器(设备主键)
///
public string TemperatrueDevice = string.Empty;
///
/// 湿度传感器(设备主键)
///
public string HumidityDevice = string.Empty;
///
/// 温度
///
public decimal Temperatrue;
///
/// 湿度
///
public decimal Humidity;
///
/// 当前选择的房间
///
private static Room m_CurrentRoom = null;
///
/// 当前选择的房间
///
[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; }
}
///
/// 房间里所有的设备列表
///
[Newtonsoft.Json.JsonIgnore]
public List DeviceUIList = new List { };
///
///设备文件路径列表
///
public readonly List DeviceUIFilePathList = new List();
///
/// 喜爱房间id
///
public const string LoveRoomId= "Favorite";
///
/// 是否是默认喜爱房间
///
/// true if is love; otherwise, false.
[Newtonsoft.Json.JsonIgnore]
public bool IsLove
{
get
{
return Id == LoveRoomId;
}
}
///
/// 是否是分享过来的房间
/// 注:分享过来的房间不能删除,不能编辑该房间,不能对设备(功能)、场景进行增删改
///
public bool IsSharedRoom = false;
///
/// 获取喜爱房间的所有设备路径
///
/// The love room device list.
[Newtonsoft.Json.JsonIgnore]
public static List LoveRoomDeviceUIFilePathList
{
get
{
if (Lists.Count == 0)
{
return new List { };
}
return CurrentRoom.GetLoveRoom().DeviceUIFilePathList;
}
}
///
/// 所有房间的所有设备类型
///
/// All room device type list.
[Newtonsoft.Json.JsonIgnore]
public static List AllRoomDeviceTypeList
{
get
{
var typeList = new List { };
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;
}
}
///
/// 获取所有房间的所有场景
///
/// 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.IsLove)
{
continue;
}
if (r.SceneUIList.Count == 0)
{
continue;
}
foreach (var sceneUI in r.SceneUIList)
{
if (sceneUI == null)
{
continue;
}
sceneList.Add(sceneUI);
}
}
return sceneList;
}
}
///
/// 获取所有房间的所有场景路径
///
/// All room scene UIL ist.
[Newtonsoft.Json.JsonIgnore]
public List AllRoomSceneUIFilepathList
{
get
{
var pathList = new List { };
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;
}
}
///
/// 场景列表---不再序列化
///
[Newtonsoft.Json.JsonIgnore]
public List SceneUIList = new List { };
///
///场景文件列表
///
public readonly List SceneUIFilePathList = new List();
///
/// 所有的房间信息
///
public static List Lists = new List();
///
/// 获取所有房间的所有设备
///
/// All room device UIL ist.
[Newtonsoft.Json.JsonIgnore]
public static List AllRoomDeviceUIList
{
get
{
List deviceList = new List();
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
#region ◆ 构造方法________________________
///
/// 构造方法
///
static Room()
{
InitAllRoom();
}
#endregion
#region ◆ 初始化__________________________
///
/// 初始化房间信息
/// 从文件中全部读取所有的房间数据到内存
///
public static void InitAllRoom()
{
Lists.Clear();
if (Config.Instance.Home.RoomFilePathList.Contains("Room_Favorite.json") == false)
{
//默认添加喜爱的房间--禁止修改房间名
var love = new Room { Name = Language.StringByID(R.MyInternationalizationString.Favorite), BackgroundImage = "RoomIcon/0.JPG", Id = LoveRoomId };
love.Save(false);
//添加到house 房间路径列表
var currentHome = Config.Instance.Home;
currentHome.RoomFilePathList.Insert(0, love.FileName);
currentHome.Save(false);
}
foreach (var roomFilePath in Config.Instance.Home.RoomFilePathList)
{
var room = GetRoomByFilePath(roomFilePath);
if (null != room)
{
if (room.IsSharedRoom)
{
room.Name = $"{room.Name}";
}
Lists.Add(room);
}
}
Config.Instance.Home.InitFloor();
CurrentRoom.RefreshRoomListView();
}
///
/// 刷新房间视图列表
///
public void RefreshRoomListView()
{
Application.RunOnMainThread(() =>
{
Phone.Device.Room.RoomManagement.Instance.Show();
});
}
///
/// 从本地重新加载全部的房间
///
public static void RefreshAllRoomByLocation()
{
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.RoomFilePathList.Add(fRoom);
}
var listRoomFile = new List();
foreach (string fileName in listFile)
{
if (fileName.StartsWith("Room_"))
{
homeTemp.RoomFilePathList.Add(fileName);
listRoomFile.Add(fileName);
}
}
//检测楼层数据的合法性
CheckFloorData(listRoomFile);
homeTemp.Save(false);
InitAllRoom();
}
///
/// 检测楼层数据的合法性
///
///
private static void CheckFloorData(List 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(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
#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);
CurrentRoom.RefreshRoomListView();
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)
{
//删除掉原来的自定义图片
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;
}
Config.Instance.Home.RemoveRoomListFilePath(roomFilePath);
//删除文件
Global.DeleteFilebyHomeId(roomFilePath);
Lists.Remove(room);
HdlAutoBackupLogic.DeleteFile(roomFilePath);
CurrentRoom.RefreshRoomListView();
return true;
}
#endregion
#region ◆ 获取房间________________________
///
/// 获取喜爱房间
///
///
public Room GetLoveRoom()
{
return CurrentRoom.GetRoomById(LoveRoomId);
}
///
/// 通过路径获取房间
///
/// 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;
}
}
///
/// 根据房间Id,获取房间对象
///
/// The room by name.
/// 房间ID
public Room GetRoomById(string roomId)
{
if (string.IsNullOrEmpty(roomId))
{
return null;
}
return Lists.Find((obj) => obj.Id == roomId);
}
///
/// 根据房间名字,获取房间对象
///
/// The room by name.
/// 房间名
public Room GetRoomByName(string roomName)
{
return Lists.Find((obj) => obj.Name == roomName);
}
///
/// 根据设备获取房间名字(楼层+房间名)
///
/// 房间名
/// 设备对象
public string GetRoomNameByDevice(CommonDevice device)
{
var room = this.GetRoomByDevice(device);
if (room == null)
{
//未分配区域
return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
}
if (Config.Instance.Home.FloorDics.ContainsKey(room.FloorId) == true)
{
//(楼层+房间名)
return Config.Instance.Home.FloorDics[room.FloorId] + " " + room.Name;
}
return room.Name;
}
///
/// 获取设备所在的房间
///
/// The room by device.
/// 设备对象
public Room GetRoomByDevice(CommonDevice device)
{
string deviceFile = device.FilePath;
return Lists.Find((obj) => obj.IsLove == false && obj.DeviceUIFilePathList.Contains(deviceFile));
}
///
/// 通过场景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)
{
if(r.IsLove)
{
continue;
}
foreach (var scene in r.SceneUIList)
{
if (scene.Id == sceneId)
{
return r;
}
}
}
return null;
}
///
/// 获取当前楼层的房间
///
///
///
public List GetRoomsByFloorId(string 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;
}
}
///
/// 获取当前楼层的房间名称
///
///
///
public List GetRoomNamesByFloorId(string id)
{
List names = new List { };
foreach (var r in Lists)
{
if (r.FloorId == id)
{
names.Add(r.Name);
}
}
return names;
}
///
/// 获取当前楼层的房间(拼接了【常用】在第一位)
///
///
///
public List 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;
}
///
/// 获取当前楼层的房间(拼接了【常用】在第一位)
///
///
public List 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;
}
#endregion
#region ◆ 房间方法________________________
///
/// 设备的房间变更
///
/// 设备对象
/// 新房间Id
/// 是否修改真实物理设备的房间,不出意外,这个值默认为true即可
public void ChangedRoom(CommonDevice device, string roomId, bool saveRealRoom = true)
{
//房间是否修改
if (this.IsRoomChanged(device, roomId) == false)
{
return;
}
//从原来的房间移除设备
this.DeleteDevice(device);
//添加到新的房间
var room = this.GetRoomById(roomId);
if (room != null)
{
room.AddDevice(device, saveRealRoom);
}
}
///
/// 房间名字是否有修改
///
/// 设备对象
/// 新房间Id
///
public bool IsRoomChanged(CommonDevice device, string roomId)
{
var room = this.GetRoomByDevice(device);
if (room == null || room.Id != roomId)
{
return true;
}
return false;
}
///
/// 获取房间所在区域
/// 楼层,房间名
///
///
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 ◆ 更新房间_________________________
///
/// Updates the memorry.
///
/// Room file path.
public static void UpdateMemorry(string roomFilePath)
{
GetRoomByFilePath(roomFilePath);
}
#endregion
#region ◆ 房间背景图的相关___________________
///
/// 移动背景图片到住宅目录下
///
/// 需要移动的文件(直接文件名)
/// 要移动到的位置-(包含住宅路径:住宅/文件名)
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 deviceUI = Common.LocalDevice.Current.GetDeviceUI(deviceUIFilePath);
if (null == deviceUI.CommonDevice)
{
//当前对象数据无效
return;
}
if (!DeviceUIFilePathList.Contains(deviceUIFilePath) && !DeviceUIList.Contains(deviceUI))
{
DeviceUIFilePathList.Add(deviceUIFilePath);
DeviceUIList.Add(deviceUI);
}
//保存到本地
Save();
}
///
/// 添加设备(此方法目前只给Room里面使用)
///
/// 要添加的设备对象
/// 是否修改真实物理设备的房间,不出意外,这个值默认为true即可
public void AddDevice(CommonDevice device, bool saveRealRoom)
{
if (device == null)
{
return;
}
//设备信息保存到本地
device.Save();
var deviceUI = Common.LocalDevice.Current.GetDeviceUI(device);
if (DeviceUIFilePathList.Contains(deviceUI.FileName) == false)
{
DeviceUIFilePathList.Add(deviceUI.FileName);
DeviceUIList.Add(deviceUI);
//保存到本地
Save();
if (saveRealRoom == true && LocalDevice.Current.GetDevicesCountByMac(device.DeviceAddr) == 1)
{
//如果只有一个回路,则修改真实物理设备的房间
LocalDevice.Current.SaveRealDeviceRoomId(new List() { device }, this.Id, false);
}
}
}
#endregion
#region ◆ 删除设备_________________________
///
/// 删除功能-设备
///
/// Device UIF ile path.
public void DeleteDevice(string deviceUIFilePath)
{
if (deviceUIFilePath == null) return;
if (DeviceUIFilePathList.Contains(deviceUIFilePath))
{
DeviceUIFilePathList.Remove(deviceUIFilePath);
DeviceUIList.RemoveAll((obj) => obj.FileName == deviceUIFilePath);
Save();
}
}
///
/// 删除设备
///
/// 要删除的设备对象
public void DeleteDevice(CommonDevice device)
{
if (device == null)
{
return;
}
//根据设备,获取所在的房间
var room = this.GetRoomByDevice(device);
if (room == null)
{
return;
}
string deviceFile = device.FilePath;
//移除缓存
if (room.DeviceUIFilePathList.Contains(deviceFile) == false)
{
return;
}
room.DeviceUIFilePathList.Remove(deviceFile);
room.DeviceUIList.RemoveAll((obj) => obj.FileName == deviceFile);
room.Save();
//更改自动备份
HdlAutoBackupLogic.AddOrEditorFile(room.FileName);
//递归:删除掉以前的旧数据导致的多个房间的问题
this.DeleteDevice(device);
}
///
/// 删除我的喜爱的设备
///
/// 要删除的设备对象
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 ◆ 获取设备_________________________
///
/// 根据设备获取它的UI对象,如果不存在则新建
///
/// The device user interface.
/// 设备对象
public DeviceUI GetDeviceUI(CommonDevice device)
{
return Common.LocalDevice.Current.GetDeviceUI(device);
}
///
/// 获取当前房间下的全部设备
///
///
public List GetRoomListDevice()
{
var listDevice = new List();
foreach (var device in this.DeviceUIList)
{
if (device == null || device.CommonDevice == null)
{
continue;
}
listDevice.Add(device.CommonDevice);
}
return listDevice;
}
///
/// 获取房间设备类型
///
///
///
public static List GetdeviceTypes(Room room)
{
List typeList = new List { };
foreach (var deviceUI in room.DeviceUIList)
{
if (deviceUI == null || deviceUI.CommonDevice == null)
{
continue;
}
if (!typeList.Contains(deviceUI.CommonDevice.Type))
{
typeList.Add(deviceUI.CommonDevice.Type);
}
}
return typeList;
}
///
/// 获取该类型的设备
///
///
///
///
public static List GetDeviceUIs(Room room ,DeviceType deviceType)
{
List typeList = new List { };
foreach (var deviceUI in room.DeviceUIList)
{
if (deviceUI == null || deviceUI.CommonDevice == null)
{
continue;
}
if(deviceUI.CommonDevice.Type!=deviceType)
{
continue;
}
if (!typeList.Contains(deviceUI))
{
typeList.Add(deviceUI);
}
}
return typeList;
}
///
/// 获取未分配区域设备
///
///
public List GetUnalloctedDeviceUIs()
{
List deviceUIs = new List { };
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 GetUnalloctedDeviceUITypes
#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)
{
//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;
bool result = true;
foreach (var common in commons)
{
//添加新成员
var addSceneMemberData = new ZigBee.Device.Scene.AddSceneMemberData
{
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;
//添加新成员 返回结果
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)
{
var sceneUI = new SceneUI
{
Name = sceneName,
Id = getSceneIdData.NewScenesId,
IconPath = sceneIconPath,
IconPathType = iconPathType,
AddSceneMemberDataList= commons
};
sceneUI.Save();
SceneUIList.Add(sceneUI);
SceneUIFilePathList.Add(sceneUI.FileName);
Save();
return 1;
}
return 0;
}
///
/// 添加场景
///
/// Scene.
public void AddScene(SceneUI scene)
{
SceneUIList.Add(scene);
SceneUIFilePathList.Add(scene.FileName);
Save();
}
///
/// 删除场景
///
///
public void DeleteScene(SceneUI scene)
{
var curScene = SceneUIList.Find((obj) => obj.Id == scene.Id);
if (curScene == null)
{
return;
}
SceneUIList.Remove(curScene);
SceneUIFilePathList.Remove(curScene.FileName);
Save();
}
#endregion
#region ◆ 设备是否收藏______________________
///
/// 是否是收藏设备
///
///
///
public bool IsCollectInRoom(string filePath)
{
if (GetLoveRoom().DeviceUIFilePathList.Find((obj) => obj == filePath) == null)
{
return false;
}
return true;
}
#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,
MemberNumber=addCommon.MemberNumber
};
//添加新成员 返回结果
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();
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;
}
///
/// 设置、同步延时时间
///
///
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
#region ◆ 删除场景________________________
///
/// 移除场景--该仅进行了对本地场景数据的删除
///
/// Scene user interface.
public void RemoveScene(SceneUI sceneUI)
{
if(Global.IsExistsByHomeId(sceneUI.FileName)==false)
{
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);
}
#endregion
#region ◆ 获取场景________________________
///
/// 通过场景id获取场景
///
/// The scene UIB y scene identifier.
/// Scene identifier.
public SceneUI GetSceneUIBySceneId(int sceneId)
{
foreach (var r in Lists)
{
if (r.IsLove)
{
continue;
}
foreach (var sceneUI in r.SceneUIList)
{
if (sceneUI.Id == sceneId)
{
return sceneUI;
}
}
}
return null;
}
///
/// 获取该楼层所有场景
///
///
///
public List GetSameFloorScenes(string floorId)
{
List sceneUIs = new List { };
var rooms= CurrentRoom.GetRoomsByFloorId(floorId);
foreach(var r in rooms)
{
sceneUIs.AddRange(r.SceneUIList);
}
return sceneUIs;
}
///
/// 获取该楼层的场景
///
///
///
public List GetSceneUIsByFloorId(string floorId)
{
var rooms = GetRoomsByFloorId(floorId);
if (rooms == null)
{
return null;
}
var sceneList = new List { };
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;
}
///
/// 获取未分配区域场景
///
///
public List GetUnalloctedScenes()
{
List sceneUIs = new List { };
var sList = AllRoomSceneUIFilepathList;
List sfile = new List { };
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(jsonInfo);
if (tempSceneUI != null)
{
sceneUIs.Add(tempSceneUI);
}
}
}
if (sceneUIs.Count == 0)
{
return null;
}
return sceneUIs;
}
#endregion
#region ◆ 刷新场景_________________________
///
/// 刷新房间的场景列表
///
public async System.Threading.Tasks.Task RefreshSceneUIList()
{
bool result = true;
var sceneList = await ZigBee.Device.Scene.GetSceneListAsync();
if (sceneList == null)
{
return false;
}
List sceneIDList = new List { };
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
#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 List GetFloorSortRoom(string i_floorKeys, bool getShard = true)
{
Dictionary> dicAllSort = new Dictionary>();
//读取房间顺序
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.RoomSortFile);
var strData = UserCenterLogic.LoadFileContent(fullName);
if (strData != null)
{
dicAllSort = Newtonsoft.Json.JsonConvert.DeserializeObject>>(strData);
}
var listRoomSort = new List();
if (dicAllSort.ContainsKey(i_floorKeys) == true)
{
listRoomSort = dicAllSort[i_floorKeys];
}
else
{
dicAllSort[i_floorKeys] = listRoomSort;
}
var dicRoom = new Dictionary();
foreach (var room in Common.Room.Lists)
{
if (room.FloorId != i_floorKeys || room.IsLove == true)
{
//不是同一个楼层
continue;
}
if (listRoomSort.Contains(room.Id) == false)
{
//新添加的房间
listRoomSort.Add(room.Id);
}
if (getShard == false && room.IsSharedRoom == true)
{
//不要分享的房间
continue;
}
dicRoom[room.Id] = room;
}
var listSortRoom = new List();
for (int i = 0; i < listRoomSort.Count; i++)
{
if (dicRoom.ContainsKey(listRoomSort[i]) == true)
{
listSortRoom.Add(dicRoom[listRoomSort[i]]);
}
}
//保存顺序
UserCenterLogic.SaveFileContent(fullName, dicAllSort);
return listSortRoom;
}
///
/// 保存房间的顺序
///
/// 楼层主键
/// 房间顺序(房间的主键)
public void SaveRoomSort(string i_floorKeys, List listSort)
{
Dictionary> dicAllSort = new Dictionary>();
//读取房间顺序
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.RoomSortFile);
var strData = UserCenterLogic.LoadFileContent(fullName);
if (strData != null)
{
dicAllSort = Newtonsoft.Json.JsonConvert.DeserializeObject>>(strData);
}
//保存顺序
dicAllSort[i_floorKeys] = listSort;
UserCenterLogic.SaveFileContent(fullName, dicAllSort);
dicAllSort.Clear();
}
///
/// 获取排序后的楼层
///
///
public Dictionary GetFloorSortList()
{
//读取楼层顺序
var listFloorSort = new List();
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.FloorSortFile);
var strData = UserCenterLogic.LoadFileContent(fullName);
if (strData != null)
{
listFloorSort = Newtonsoft.Json.JsonConvert.DeserializeObject>(strData);
}
foreach (string keys in Common.Config.Instance.Home.FloorDics.Keys)
{
if (listFloorSort.Contains(keys) == false)
{
//新添加的楼层
listFloorSort.Add(keys);
}
}
var dic = new Dictionary();
for (int i = 0; i < listFloorSort.Count; i++)
{
if (Config.Instance.Home.FloorDics.ContainsKey(listFloorSort[i]) == true)
{
dic[listFloorSort[i]] = Config.Instance.Home.FloorDics[listFloorSort[i]];
}
}
//保存顺序
UserCenterLogic.SaveFileContent(fullName, listFloorSort);
return dic;
}
///
/// 保存楼层的顺序
///
/// 楼层的主键
public void SaveFloorSort(List listSort)
{
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.FloorSortFile);
//保存顺序
UserCenterLogic.SaveFileContent(fullName, listSort);
}
#endregion
#region ◆ 克隆房间对象_____________________
///
/// 克隆房间对象
///
///
public Room CloneRoomClass()
{
var newRoom = new Room();
//克隆属性
newRoom.Id = this.Id;
newRoom.FloorId = this.FloorId;
newRoom.TemperatrueDevice = this.TemperatrueDevice;
newRoom.HumidityDevice = this.HumidityDevice;
newRoom.Name = this.Name;
newRoom.BackgroundImage = this.BackgroundImage;
newRoom.BackgroundImageType = this.BackgroundImageType;
return newRoom;
}
#endregion
}
}