using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using Shared.SimpleControl; namespace Shared { [System.Serializable] /// /// 房间对象 /// public class Room { public static string FavoriteRoom = "FavoriteRoom"; /// /// 房间里面灯光设备的延迟时间 /// public Dictionary DelayTime = new Dictionary (); public static void UpdateMemorry (string roomFilePath) { GetRoomByFilePath (roomFilePath); } // 房间命名规则 typeof (Room).Name + "_" + etNameBox.Text.Trim (); /// /// 根据房间路径恢复房间对象 /// /// The by file path. /// Room file path. public static Room GetRoomByFilePath (string roomFilePath) { try { var sdf = IO.FileUtils.ReadFile (roomFilePath); var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject (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 tempCommomList = new List (); 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 (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 (jsonInfo); if (null == jsonInfoCommon) { //当前对象数据无效 return; } var deviceType = deviceFilePath.Split ('_') [1]; //判断设备,使其在用户界面数量和从设备进入到界面上有相关的设备显示 if (deviceType == DeviceType.LightDimming.ToString ()) { var common = Newtonsoft.Json.JsonConvert.DeserializeObject (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 (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 (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 (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 (jsonInfo); room.DeviceList.Add (common); } else if (deviceType == DeviceType.LightMixSwitch.ToString () ){ var common = Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo); room.DeviceList.Add (common); } else if ( deviceType == DeviceType.LightEnergySocket.ToString ()) { var common = Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo); room.DeviceList.Add (common); } else if (deviceType == DeviceType.LightEnergySwitch.ToString ()) { var common = Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo); room.DeviceList.Add (common); } else if (deviceType == DeviceType.CurtainModel.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.CurtainTrietex.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.CurtainRoller.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.MusicModel.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.HVAC.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.ACPanel.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.ACInfrared.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.FoolHeat.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.FoolHeatPanel.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.DryContact.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.Scene.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.InfraredMode.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.FanModule.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.DoorLock.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo));//门锁111 } else if (deviceType == DeviceType.UniversalDevice.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.SensorCO2.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.SensorTVOC.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.SensorPM25.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.SensorTemperature.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.SensorHumidity.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (jsonInfo)); } else if (deviceType == DeviceType.FreshAir.ToString ()) { room.DeviceList.Add (Newtonsoft.Json.JsonConvert.DeserializeObject (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; } } } /// /// 房间名 /// public string Name { get; set; } public string RoomFilePath { get { if (string.IsNullOrEmpty (Name)) { return "FavoriteRoom"; } else { return "Room_" + Name; } } } /// /// 不选择到房间时候的背景图 /// public string UnSelectedBackgroundImage { get; set; } /// /// 选择到房间的时候背景图 /// public string SelectedBackgroundImage { get; set; } /// /// 不选择到房间下拉列表时候的背景图 /// public string UnSelectedListBackgroundImage { get; set; } /// /// 选择到房间的下拉列表时候背景图 /// public string SelectedListBackgroundImage { get; set; } /// /// 图标 /// public string IconPath = "DefaultRoomIcon.png"; /// /// 房间背景 /// public string BackGroundImage = "Room/r1.png"; /// /// 楼层 /// public string Floor = "ALL ROOMS"; /// /// 当前选择的房间 /// public static Room CurrentRoom; [Newtonsoft.Json.JsonIgnore] /// /// 房间里所有的视图类型 /// public List DeviceList = new List { }; /// ///设备文件路径列表 /// public readonly List DeviceFilePathList = new List (); /// /// 场景文件名列表 /// public readonly List SceneFilePathList = new List (); /// /// 房间列表的文件名称 /// static string roomListFilePath = "RoomList"; /// /// 所有的房间信息 /// public static System.Collections.Generic.List Lists = new System.Collections.Generic.List (); /// /// 删除房间 /// public static void Remove (string roomFilePath) { var room = GetRoomByFilePath (roomFilePath); if (null == room) { return; } var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject> (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); } /// /// 增加房间 /// public void Add (string roomFilePath) { if (string.IsNullOrEmpty (roomFilePath) || IO.FileUtils.Exists (roomFilePath)) { return; } var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (roomListFilePath))); if(roomFilePathList == null) { roomFilePathList = new List (); } 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); } /// /// 保存 /// public void Save (string roomFilePath) { if (string.IsNullOrEmpty (roomFilePath)) { return; } var list = DeviceList; //不需要保存当前设备 DeviceList = new List (); //保存房间信息 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; } } } /// /// 更新房间名 /// /// Old room file path. /// New room file path. 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.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); } /// /// 所有的房间列表路径 /// /// The room file path list. public static System.Collections.Generic.List FilePathList { get { var roomBytes = IO.FileUtils.ReadFile (roomListFilePath); string roombyteString = System.Text.Encoding.UTF8.GetString (roomBytes); var ddds = Newtonsoft.Json.JsonConvert.DeserializeObject> (roombyteString); if (ddds == null) ddds = new List (); 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; } } /// /// 从文件中全部读取所有的房间数据到内存 /// public static void InitAllRoom () { Lists.Clear (); if (null == Newtonsoft.Json.JsonConvert.DeserializeObject> (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 ()))); } var roomFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject> (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))); } /// /// 灯光场景列表 /// public List LightScenePathList = new List (); } }