using System; using System.Collections.Generic; using HDL_ON.DAL.Server; using Shared; namespace HDL_ON.Entity { /// /// 读取服务器空间信息返回到数据包 /// public class SpatialApiPack { public List list = new List(); public string totalCount = "0"; public string totalPage = "0"; public string pageNo = "0"; public string pageSize = "0"; } /// /// 空间信息基础类 /// 包含楼层/房间 /// public class SpatialInfo { static SpatialInfo _currentSpatial; public static SpatialInfo CurrentSpatial { get { if (_currentSpatial == null) { _currentSpatial = new SpatialInfo(); } return _currentSpatial; } } public SpatialInfo() { } public SpatialInfo(string spatialType) { roomType = "FLOOR"; parentId = DB_ResidenceData.Instance.CurrentRegion.id; } public string homeId = ""; public string roomId = ""; public string roomName = ""; //public string roomImage = ""; public string roomImage = "Classification/Room/Roombg.png"; public string roomType = ""; public string parentId = ""; public string uid = Guid.NewGuid().ToString(); public string floorRoomName = ""; public string createTime = ""; public string modifyTime = ""; ///// ///// 删除标记 20201208:可欣:APP的在线和离线问题,刚刚确定了,离线无法编辑,只能控制 ///// 需要删除数据时,标记为:true ///// 由云端删除成功之后,返回数据再清除本地数据 ///// //public bool DeleteSign = false; /// /// 更新空间信息 /// /// 空间信息 /// 操作类型:ADD/UPDATE/DELETE public void UpdateSpatialList(List spatials) { var roomUpdateList = spatials.FindAll((obj) => obj.roomType == "ROOM"); var floorUpdateList = spatials.FindAll((obj) => obj.roomType == "FLOOR"); CurrentSpatial.DeleteAllRoom(); FloorList.Clear(); //处理房间列表 if (roomUpdateList != null && roomUpdateList.Count > 0) { foreach (var newRoom in roomUpdateList) { newRoom.SaveRoomFile(); RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject( Newtonsoft.Json.JsonConvert.SerializeObject(newRoom))); } } //楼层房间列表 if (floorUpdateList != null && floorUpdateList.Count > 0) { foreach (var updateTemp in floorUpdateList) { FloorList.Add(updateTemp); } var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } } #region 楼层列表 /// /// 楼层列表 /// List floors; /// /// 楼层列表 /// public List FloorList { get { if (MainPage.NoLoginMode) { if (floors == null) { floors = new List(); } return floors; } if (floors == null) { try { var floorsDataBytes = Common.FileUtlis.Files.ReadFile("FloorsData"); if (floorsDataBytes != null) { var floorsDataString = System.Text.Encoding.UTF8.GetString(floorsDataBytes); MainPage.Log(floorsDataString); floors = Newtonsoft.Json.JsonConvert.DeserializeObject>(floorsDataString); } if(floors == null) { floors = new List(); } } catch (Exception ex) { floors = new List(); MainPage.Log($"楼层数据初始化失败:{ex.Message}"); } } return floors; } } /// /// 数据保存路径 /// string dataSavePath { get { if(roomType == "ROOM") { return "RoomPath_" + uid; } if(roomType == "FLOOR") { return "FloorsData"; } else { return "SpatialInfo_"+uid; } } } /// /// 增加楼层 /// /// public string AddFloor(SpatialInfo floor, out SpatialInfo newFloor) { var pm = new HttpServerRequest(); var revPack = new ResponsePackNew(); revPack = pm.AddRoom(new List() { floor }); if (revPack.Code == StateCode.SUCCESS) { var revData = Newtonsoft.Json.JsonConvert.DeserializeObject>(revPack.Data.ToString()); if (revData.Count > 0) { var addFloor = revData.Find((obj) => obj.uid == floor.uid); if (addFloor != null) { CurrentSpatial.FloorList.Add(addFloor); floor = addFloor; } } var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(CurrentSpatial.FloorList)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } else { Utlis.ShowTip(Language.StringByID(StringId.AddFloorFail) + "\r\nCode:" + revPack.Code); } newFloor = floor; return revPack.Code; } /// /// 更新楼层信息 /// /// public string UpdateFloor(SpatialInfo floor) { var pm = new HttpServerRequest(); var revPack = pm.UpdateRoom(new List() { floor }); if (revPack.Code == StateCode.SUCCESS) { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } return revPack.Code; } /// /// 删除楼层 /// /// public string DelFloor(string floorId) { if (string.IsNullOrEmpty( floorId)) return ""; var pm = new HttpServerRequest(); var code = pm.DeleteRoom(new List() { floorId }).Code; if (code == StateCode.SUCCESS) { var tRoom = CurrentSpatial.FloorList.Find((obj) => obj.roomId == floorId); if (tRoom != null) { CurrentSpatial.FloorList.Remove(tRoom); } var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } else { Utlis.ShowTip(Language.StringByID(StringId.DeleteFloorFail) + "\r\nCode:" + code); } return code; } #endregion #region 房间列表 /// /// 房间列表 /// List rooms; /// /// 房间列表 /// public List RoomList { get { if(MainPage.NoLoginMode ) { if (rooms == null) { rooms = new List(); } return rooms; } if (rooms == null) { try { rooms = new List { }; var filePathList = Common.FileUtlis.Files.ReadFiles(); foreach (var filePath in filePathList) { if (filePath.StartsWith("RoomData_")) { var roomDataBytes = Common.FileUtlis.Files.ReadFile(filePath); var roomDataString = System.Text.Encoding.UTF8.GetString(roomDataBytes); var room = Newtonsoft.Json.JsonConvert.DeserializeObject(roomDataString); if (room != null) { rooms.Add(room); } } } } catch (Exception ex) { MainPage.Log($"功能数据初始化失败:{ex.Message}"); } } return rooms; } } /// /// 增加房间 /// /// public string AddRoom(Room room,out Room newRoom) { var pm = new HttpServerRequest(); var pack = pm.AddRoom(new List() { room }); if (pack.Code == StateCode.SUCCESS) { var revData = Newtonsoft.Json.JsonConvert.DeserializeObject>(pack.Data.ToString()); if (revData != null) { if (revData.Count > 0) { var tempRoom = revData.Find((obj) => obj.uid == room.uid); if (tempRoom != null) { room.roomId = tempRoom.roomId; CurrentSpatial.RoomList.Add(room); var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(room)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } } } } newRoom = room; return pack.Code; } /// /// 删除房间 /// /// public string DelRoom(Room room) { var pm = new HttpServerRequest(); var pack = pm.DeleteRoom(new List() { room.roomId }); if (pack.Code == StateCode.SUCCESS) { var tRoom = RoomList.Find((obj) => obj.roomId == room.roomId); if (tRoom != null) { CurrentSpatial.RoomList.Remove(room); } Common.FileUtlis.Files.DeleteFile(room.dataSavePath); } return pack.Code; } /// /// 删除所有房间 /// public void DeleteAllRoom() { for(int i=0;i< RoomList.Count;) { Common.FileUtlis.Files.DeleteFile(RoomList[i].dataSavePath); RoomList.RemoveAt(i); } rooms = null; } public void Clear() { if (_currentSpatial != null) { if (_currentSpatial.floors != null) { _currentSpatial.floors.Clear(); } if (_currentSpatial.rooms != null) { _currentSpatial.rooms.Clear(); } _currentSpatial = null; } } /// /// 初始化每个房间的功能数据 /// public void InitRoomListFunctions() { new System.Threading.Thread(() => { try { //初始化住宅所有房间功能数据 foreach (var r in RoomList) { InitRoomFuntion(r); } } catch (Exception ex) { MainPage.Log($"Init room function error : {ex.Message}"); } }) { IsBackground = true }.Start(); } /// /// 初始化房间功能 /// /// public void InitRoomFuntion(Room tempRoom) { tempRoom.GetRoomFunctions(true); tempRoom.GetRoomScenes(true); } /// /// 保存房间数据文件 /// public void SaveRoomFile() { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)); Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd); } /// /// 更新房间数据 /// public void UpdataRoomInfo() { new System.Threading.Thread(() => { var pm = new HttpServerRequest(); var pack = pm.UpdateRoom(new List() { this }); this.SaveRoomFile(); if (pack.Code != StateCode.SUCCESS) { IMessageCommon.Current.ShowErrorInfoAlter(pack.Code); } }) { IsBackground = true }.Start(); } #endregion } [Serializable] public class Room : SpatialInfo { public Room() { roomType = "ROOM"; roomName = "Room"; } //public string backgroundImage = "Classification/Room/Roombg.png"; public string backgroundImage { get { if (string.IsNullOrEmpty(roomImage)) { return "Classification/Room/Roombg.png"; } else { return roomImage; } } set { roomImage = value; } } /// /// 楼层 /// [Newtonsoft.Json.JsonIgnore] public string floorName { get { if (CurrentSpatial.FloorList.Count > 0) { var f = CurrentSpatial.FloorList.Find((obj) => obj.uid == parentId); if (f != null) { return f.roomName + " "; } } return ""; } } ///// ///// 房间功能列表 ///// //List functions = new List(); /// /// 获取房间功能列表 /// public List GetRoomFunctions(bool needRefresh) { if (needRefresh) { } var functions = new List(); if (functions.Count == 0) { foreach (var f in FunctionList.List.GetDeviceFunctionList()) { if (f.roomIds.Contains(roomId) && roomId != null) { functions.Add(f); } } foreach(var g in FunctionList.List.groupControls) { if (g.roomIds.Contains(roomId) && roomId != null) { functions.Add(g); } } } return functions; } ///// ///// 增加房间功能 ///// 操作的是缓存数据,不用保存 ///// //public void AddRoomFunction(Function function) //{ // functions.Add(function); //} ///// ///// 删除房间功能 ///// 操作的是缓存数据,不用保存 ///// //public void RemoveRoomFunction(Function function) //{ // functions.Remove(functions.Find((obj) => obj.sid == function.sid)); //} /// /// 房间场景列表 /// List scenes = new List(); /// /// 获取场景列表 /// /// public List GetRoomScenes(bool needRefresh) { if (needRefresh) { scenes = new List(); } if (scenes.Count == 0) { foreach (var s in FunctionList.List.scenes) { if (s.roomIds.Contains(roomId) && roomId != null) { scenes.Add(s); } } } return scenes; } /// /// 增加房间场景 /// 操作的是缓存数据,不用保存 /// /// public void AddRoomScene(Scene scene) { scenes.Add(scene); } /// /// 移除房间场景 /// 操作的是缓存数据,不用保存 /// /// public void RemoveRoomScene(Scene scene) { scenes.Remove(scenes.Find((obj) => obj.sid == scene.sid)); } } }