using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using Newtonsoft.Json.Linq;
|
using Shared.SimpleControl;
|
|
namespace Shared
|
{
|
[System.Serializable]
|
/// <summary>
|
/// 房间对象
|
/// </summary>
|
public class Room
|
{
|
public static string FavoriteRoom = "FavoriteRoom";
|
|
/// <summary>
|
/// 房间里面灯光设备的延迟时间
|
/// </summary>
|
public Dictionary<string, byte> DelayTime = new Dictionary<string, byte> ();
|
|
public static void UpdateMemorry (string roomFilePath)
|
{
|
GetRoomByFilePath (roomFilePath);
|
}
|
|
// 房间命名规则 typeof (Room).Name + "_" + etNameBox.Text.Trim ();
|
/// <summary>
|
/// 根据房间路径恢复房间对象
|
/// </summary>
|
/// <returns>The by file path.</returns>
|
/// <param name="roomFilePath">Room file path.</param>
|
public static Room GetRoomByFilePath (string roomFilePath)
|
{
|
try {
|
var sdf = IO.FileUtils.ReadFile (roomFilePath);
|
var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject<Room> (System.Text.Encoding.UTF8.GetString (sdf));
|
|
if (null == nowRoom) {
|
System.Console.WriteLine ("房间文件路径不对,文件路径为:" + roomFilePath);
|
return null;
|
}
|
|
var beforeRoom = Lists.Find ((obj) => obj.Name == nowRoom.Name);
|
if (beforeRoom != null) {
|
List<Common> tempCommomList = new List<Common> ();
|
tempCommomList.AddRange (beforeRoom.DeviceList);
|
beforeRoom.DeviceList.Clear ();
|
foreach (var filePath in beforeRoom.DeviceFilePathList) {
|
var jsonInfo = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (filePath));
|
var tempCommon = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (jsonInfo);
|
if (tempCommon != null) {
|
Common delCommon = tempCommomList.Find ((obj) => obj.Type == tempCommon.Type && obj.SubnetID == tempCommon.SubnetID && obj.DeviceID == tempCommon.DeviceID && obj.LoopID == tempCommon.LoopID);
|
if (delCommon != null) {
|
beforeRoom.DeviceList.Add (delCommon);
|
} else {
|
addDevice (beforeRoom, filePath);
|
}
|
}
|
}
|
|
beforeRoom.SceneFilePathList.Clear ();
|
foreach (var filePath in nowRoom.SceneFilePathList) {
|
beforeRoom.SceneFilePathList.Add (filePath);
|
}
|
|
return beforeRoom;
|
}
|
|
nowRoom.DeviceList.Clear ();
|
foreach (string deviceFilePath in nowRoom.DeviceFilePathList) {
|
addDevice (nowRoom, deviceFilePath);
|
}
|
|
for (int i = UserConfig.Instance.HideDeviceTypes.Count - 1; i >= 0; i--) {
|
var hideType = UserConfig.Instance.HideDeviceTypes [i];
|
var devices = nowRoom.DeviceList.Find ((obj) => {
|
return obj.DeviceTextID == hideType;
|
});
|
if (devices != null) {
|
UserConfig.Instance.HideDeviceTypes.Remove (hideType);
|
UserConfig.Instance.SaveUserConfig ();
|
break;
|
}
|
}
|
return nowRoom;
|
} catch {
|
return null;
|
}
|
}
|
|
static void addDevice (Room room, string deviceFilePath)
|
{
|
if (deviceFilePath.Split ('_').Length < 2) {
|
return;
|
}
|
var jsonInfo = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath));
|
var jsonInfoCommon = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (jsonInfo);
|
if (null == jsonInfoCommon) {
|
//当前对象数据无效
|
return;
|
}
|
var deviceType = deviceFilePath.Split ('_') [1];
|
|
//判断设备,使其在用户界面数量和从设备进入到界面上有相关的设备显示
|
if (deviceType == DeviceType.LightDimming.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (jsonInfo);
|
common.CurrentBrightness = 0;
|
string keyString = common.CommonLoopID;
|
var delayTime = room.DelayTime.ContainsKey (keyString + "Open");
|
if (delayTime) {
|
common.CustomDelayTimeOpen = room.DelayTime [keyString + "Open"];
|
common.CustomDelayTimeClose = room.DelayTime [keyString + "Close"];
|
} else {
|
common.CustomDelayTimeOpen = 0;
|
common.CustomDelayTimeClose = 0;
|
}
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightMixDimming.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightMixDimming> (jsonInfo);
|
common.CurrentBrightness = 0;
|
string keyString = common.CommonLoopID;
|
var delayTime = room.DelayTime.ContainsKey (keyString + "Open");
|
if (delayTime) {
|
common.CustomDelayTimeOpen = room.DelayTime [keyString + "Open"];
|
common.CustomDelayTimeClose = room.DelayTime [keyString + "Close"];
|
} else {
|
common.CustomDelayTimeOpen = 0;
|
common.CustomDelayTimeClose = 0;
|
}
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightDALI.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDALI> (jsonInfo);
|
common.CurrentBrightness = 0;
|
string keyString = common.CommonLoopID;
|
var delayTime = room.DelayTime.ContainsKey (keyString + "Open");
|
if (delayTime) {
|
common.CustomDelayTimeOpen = room.DelayTime [keyString + "Open"];
|
common.CustomDelayTimeClose = room.DelayTime [keyString + "Close"];
|
} else {
|
common.CustomDelayTimeOpen = 0;
|
common.CustomDelayTimeClose = 0;
|
}
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightRGB.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (jsonInfo);
|
common.CurrentBrightness = 0;
|
string keyString = common.SubnetID.ToString () + "_" + common.DeviceID.ToString () + "_" + common.LoopID.ToString ();
|
var delayTime = room.DelayTime.ContainsKey (keyString + "Open");
|
if (delayTime) {
|
common.CustomDelayTimeOpen = room.DelayTime [keyString + "Open"];
|
common.CustomDelayTimeClose = room.DelayTime [keyString + "Close"];
|
} else {
|
common.CustomDelayTimeOpen = 0;
|
common.CustomDelayTimeClose = 0;
|
}
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightSwitch.ToString ()){
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (jsonInfo);
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightMixSwitch.ToString () ){
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightMixSwitch> (jsonInfo);
|
room.DeviceList.Add (common);
|
} else if ( deviceType == DeviceType.LightEnergySocket.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySocket> (jsonInfo);
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.LightEnergySwitch.ToString ()) {
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySwitch> (jsonInfo);
|
room.DeviceList.Add (common);
|
} else if (deviceType == DeviceType.CurtainModel.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainModel> (jsonInfo));
|
} else if (deviceType == DeviceType.CurtainTrietex.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (jsonInfo));
|
} else if (deviceType == DeviceType.CurtainRoller.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainRoller> (jsonInfo));
|
} else if (deviceType == DeviceType.MusicModel.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<MusicModel> (jsonInfo));
|
} else if (deviceType == DeviceType.HVAC.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (jsonInfo));
|
} else if (deviceType == DeviceType.ACPanel.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<ACPanel> (jsonInfo));
|
} else if (deviceType == DeviceType.ACInfrared.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<ACInfrared> (jsonInfo));
|
} else if (deviceType == DeviceType.FoolHeat.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeat> (jsonInfo));
|
} else if (deviceType == DeviceType.FoolHeatPanel.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeatPanel> (jsonInfo));
|
} else if (deviceType == DeviceType.DryContact.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<DryContact> (jsonInfo));
|
} else if (deviceType == DeviceType.Scene.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<Scene> (jsonInfo));
|
} else if (deviceType == DeviceType.InfraredMode.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<InfraredMode> (jsonInfo));
|
} else if (deviceType == DeviceType.FanModule.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (jsonInfo));
|
} else if (deviceType == DeviceType.DoorLock.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock> (jsonInfo));//门锁111
|
} else if (deviceType == DeviceType.UniversalDevice.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (jsonInfo));
|
} else if (deviceType == DeviceType.SensorCO2.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<SensorCO2> (jsonInfo));
|
} else if (deviceType == DeviceType.SensorTVOC.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<SensorTVOC> (jsonInfo));
|
} else if (deviceType == DeviceType.SensorPM25.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<SensorPM25> (jsonInfo));
|
} else if (deviceType == DeviceType.SensorTemperature.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<SensorTemperature> (jsonInfo));
|
} else if (deviceType == DeviceType.SensorHumidity.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<SensorHumidity> (jsonInfo));
|
} else if (deviceType == DeviceType.FreshAir.ToString ()) {
|
room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject<FreshAir> (jsonInfo));
|
}
|
|
for (int i = UserConfig.Instance.HideDeviceTypes.Count - 1; i >= 0; i--) {
|
var hideType = UserConfig.Instance.HideDeviceTypes [i];
|
var devices = room.DeviceList.Find ((obj) => {
|
return obj.DeviceTextID == hideType;
|
});
|
if (devices != null) {
|
UserConfig.Instance.HideDeviceTypes.Remove (hideType);
|
UserConfig.Instance.SaveUserConfig ();
|
break;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 房间名
|
/// </summary>
|
public string Name { get; set; }
|
|
public string RoomFilePath {
|
get {
|
if (string.IsNullOrEmpty (Name)) { return "FavoriteRoom"; } else {
|
return "Room_" + Name;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 不选择到房间时候的背景图
|
/// </summary>
|
public string UnSelectedBackgroundImage {
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 选择到房间的时候背景图
|
/// </summary>
|
public string SelectedBackgroundImage {
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 不选择到房间下拉列表时候的背景图
|
/// </summary>
|
public string UnSelectedListBackgroundImage {
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 选择到房间的下拉列表时候背景图
|
/// </summary>
|
public string SelectedListBackgroundImage {
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 图标
|
/// </summary>
|
public string IconPath = "DefaultRoomIcon.png";
|
/// <summary>
|
/// 房间背景
|
/// </summary>
|
public string BackGroundImage = "Room/r1.png";
|
/// <summary>
|
/// 楼层
|
/// </summary>
|
public string Floor = "ALL ROOMS";
|
|
/// <summary>
|
/// 当前选择的房间
|
/// </summary>
|
public static Room CurrentRoom;
|
|
[Newtonsoft.Json.JsonIgnore]
|
/// <summary>
|
/// 房间里所有的视图类型
|
/// </summary>
|
public List<Common> DeviceList = new List<Common> { };
|
|
/// <summary>
|
///设备文件路径列表
|
/// </summary>
|
public readonly List<string> DeviceFilePathList = new List<string> ();
|
|
/// <summary>
|
/// 场景文件名列表
|
/// </summary>
|
public readonly List<string> SceneFilePathList = new List<string> ();
|
|
|
/// <summary>
|
/// 房间列表的文件名称
|
/// </summary>
|
static string roomListFilePath = "RoomList";
|
|
/// <summary>
|
/// 所有的房间信息
|
/// </summary>
|
public static System.Collections.Generic.List<Room> Lists = new System.Collections.Generic.List<Room> ();
|
|
/// <summary>
|
/// 删除房间
|
/// </summary>
|
public static void Remove (string roomFilePath)
|
{
|
var room = GetRoomByFilePath (roomFilePath);
|
|
if (null == room) {
|
return;
|
}
|
var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath)));
|
|
roomFilePathList.Remove (roomFilePath);
|
IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (roomFilePathList)));
|
|
IO.FileUtils.DeleteFile (roomFilePath);
|
//删除和房间相关的场景所有数据
|
foreach (var sceneFilePath in room.SceneFilePathList) {
|
IO.FileUtils.DeleteFile (sceneFilePath);
|
}
|
Room.Lists.Remove (room);
|
}
|
/// <summary>
|
/// 增加房间
|
/// </summary>
|
public void Add (string roomFilePath)
|
{
|
if (string.IsNullOrEmpty (roomFilePath) || IO.FileUtils.Exists (roomFilePath)) {
|
return;
|
}
|
var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath)));
|
if(roomFilePathList == null)
|
{
|
roomFilePathList = new List<string> ();
|
}
|
roomFilePathList.Add (roomFilePath);
|
|
//var sssddsa = IO.FileUtils.ReadFiles ().FindAll ((obj) => obj.Contains ("Room_"));
|
//foreach (var sd in sssddsa) {
|
// roomFilePathList.Add (sd);
|
//}
|
|
//保存房间列表
|
IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (roomFilePathList)));
|
Save (roomFilePath);
|
}
|
|
/// <summary>
|
/// 保存
|
/// </summary>
|
public void Save (string roomFilePath)
|
{
|
if (string.IsNullOrEmpty (roomFilePath)) {
|
return;
|
}
|
|
var list = DeviceList;
|
//不需要保存当前设备
|
DeviceList = new List<Common> ();
|
//保存房间信息
|
IO.FileUtils.WriteFileByBytes (roomFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (this)));
|
DeviceList = list;
|
|
for (int i = UserConfig.Instance.HideDeviceTypes.Count - 1; i >= 0; i--) {
|
var hideType = UserConfig.Instance.HideDeviceTypes [i];
|
var devices = DeviceList.Find ((obj) => {
|
return obj.DeviceTextID == hideType;
|
});
|
if (devices != null) {
|
UserConfig.Instance.HideDeviceTypes.Remove (hideType);
|
UserConfig.Instance.SaveUserConfig ();
|
break;
|
}
|
}
|
}
|
/// <summary>
|
/// 更新房间名
|
/// </summary>
|
/// <param name="oldRoomFilePath">Old room file path.</param>
|
/// <param name="newRoomFilePath">New room file path.</param>
|
public void ReName (string oldRoomFilePath, string newRoomFilePath)
|
{
|
if (string.IsNullOrEmpty (newRoomFilePath)) {
|
return;
|
}
|
//场景名已经更改
|
if (oldRoomFilePath != newRoomFilePath && 2 <= oldRoomFilePath.Split ('_').Length) {
|
|
var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath))); ;
|
for (int i = 0; i < roomFilePathList.Count; i++) {
|
var roomFilePath = roomFilePathList [i];
|
if (roomFilePath == oldRoomFilePath) {
|
roomFilePathList [i] = newRoomFilePath;
|
break;
|
}
|
}
|
//保存房间列表
|
IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (roomFilePathList)));
|
//这里还需要更改场景的引用
|
var oldRoomName = oldRoomFilePath.Split ('_') [1];
|
IO.FileUtils.DeleteFile (oldRoomFilePath);
|
for (int i = 0; i < SceneFilePathList.Count; i++) {
|
string oldSceneFilePath = SceneFilePathList [i];
|
string newfile = oldSceneFilePath.Replace ("RoomScene_" + oldRoomName + "_", "RoomScene_" + Name + "_");
|
SceneFilePathList [i] = newfile;
|
IO.FileUtils.ReNameFile (oldSceneFilePath, newfile);
|
}
|
}
|
|
Save (newRoomFilePath);
|
}
|
|
/// <summary>
|
/// 所有的房间列表路径
|
/// </summary>
|
/// <value>The room file path list.</value>
|
public static System.Collections.Generic.List<string> FilePathList {
|
get {
|
var roomBytes = IO.FileUtils.ReadFile (roomListFilePath);
|
string roombyteString = System.Text.Encoding.UTF8.GetString (roomBytes);
|
var ddds = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (roombyteString);
|
if (ddds == null)
|
ddds = new List<string> ();
|
if (!ddds.Contains (FavoriteRoom)) {
|
ddds.Add (FavoriteRoom);
|
if (!IO.FileUtils.Exists (FavoriteRoom)) {
|
//默认添加对应的房间
|
new Room () { Name = "" }.Save (FavoriteRoom);
|
}
|
IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (ddds)));
|
}
|
return ddds;
|
}
|
}
|
|
/// <summary>
|
/// 从文件中全部读取所有的房间数据到内存
|
/// </summary>
|
public static void InitAllRoom ()
|
{
|
Lists.Clear ();
|
if (null == Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath)))) {
|
//初始化房间列表
|
Shared.IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (new System.Collections.Generic.List<string> ())));
|
}
|
var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath)));
|
if (!roomFilePathList.Contains (FavoriteRoom)) {
|
roomFilePathList.Add (FavoriteRoom);
|
if (!IO.FileUtils.Exists (FavoriteRoom)) {
|
//默认添加对应的房间
|
new Room () { Name = "" }.Save (FavoriteRoom);
|
}
|
|
IO.FileUtils.WriteFileByBytes (roomListFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (roomFilePathList)));
|
|
}
|
|
foreach (var roomFilePath in roomFilePathList) {
|
var room = GetRoomByFilePath (roomFilePath);
|
if (null != room) {
|
Lists.Add (room);
|
}
|
}
|
|
for (int i = UserConfig.Instance.HideDeviceTypes.Count - 1; i >= 0; i--) {
|
var hideType = UserConfig.Instance.HideDeviceTypes [i];
|
foreach (var room in Room.Lists) {
|
var devices = room.DeviceList.Find ((obj) => {
|
return obj.DeviceTextID == hideType;
|
});
|
if (devices != null) {
|
UserConfig.Instance.HideDeviceTypes.Remove (hideType);
|
UserConfig.Instance.SaveUserConfig ();
|
break;
|
}
|
}
|
}
|
}
|
|
public void SaveLightScene (string roomFilePath, string SceneRemark, Scene scene)
|
{
|
IO.FileUtils.WriteFileByBytes (roomFilePath + "_" + SceneRemark, Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene)));
|
IO.FileUtils.WriteFileByBytes (roomFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (this)));
|
}
|
|
/// <summary>
|
/// 灯光场景列表
|
/// </summary>
|
public List<string> LightScenePathList = new List<string> ();
|
}
|
}
|