2
wei
2021-03-09 d41f1d4a478c1afcc43f3a5abb0ba76e376bc7c1
HDL_ON/Entity/Room.cs
@@ -46,7 +46,7 @@
        public SpatialInfo(string spatialType)
        {
            roomType = "FLOOR";
            parentId = UserInfo.Current.CurReginID;
            parentId = DB_ResidenceData.Instance.CurrentRegion.RegionID;
        }
        public string roomId = "";
@@ -65,121 +65,33 @@
        ///// </summary>
        //public bool DeleteSign = false;
        protected ResponsePackNew SaveSpatialInfo()
        {
            var pm = new HttpServerRequest();
            var revPack = new ResponsePackNew();
            if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.AddRoom(new List<SpatialInfo>() { this });
            }
            else if (string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.DeleteRoom(new List<string>() { roomId });
            }
            else if (!string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.UpdateRoom(new List<SpatialInfo>() { this });
            }
            return revPack;
        }
        /// <summary>
        /// 下载云端房间数据
        /// </summary>
        public void DownloadRoomList()
        {
            new System.Threading.Thread(() =>
            {
                var pm = new HttpServerRequest();
                var pack = pm.GetRoomList();
                if (pack.Code == StateCode.SUCCESS)
                {
                    var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(pack.Data.ToString());
                    if (revData != null)
                    {
                        CurrentSpatial.UpdateSpatialList(revData.list, OptionType.Cover);
                    }
                }
                else
                {
                    MainPage.Log($"读取房间数据失败:\r\nCode:{pack.Code}; msg:{pack.message}");
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 更新空间信息
        /// </summary>
        /// <param name="spatials">空间信息</param>
        /// <param name="optionType">操作类型:ADD/UPDATE/DELETE</param>
        public void UpdateSpatialList(List<SpatialInfo> spatials,OptionType optionType)
        public void UpdateSpatialList(List<SpatialInfo> 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)
            {
                if (optionType == OptionType.Update)
                foreach (var newRoom in roomUpdateList)
                {
                    foreach (var updateTemp in roomUpdateList)
                    {
                        var localRoom = CurrentSpatial.RoomList.Find((obj) => obj.roomId == updateTemp.roomId);
                        if (localRoom != null)
                        {
                            if (localRoom.modifyTime != updateTemp.modifyTime)
                            {
                                localRoom.roomName = updateTemp.roomName;
                                localRoom.roomImage = updateTemp.roomImage;
                                localRoom.parentId = updateTemp.parentId;
                                localRoom.createTime = updateTemp.createTime;
                                localRoom.modifyTime = updateTemp.modifyTime;
                                localRoom.SaveRoomData(false);
                            }
                        }else
                        {
                            updateTemp.SaveRoomData(false);
                        }
                    }
                }
                else if (optionType == OptionType.Cover)
                {
                    CurrentSpatial.DeleteAllRoom();
                    foreach(var newRoom in roomUpdateList)
                    {
                        newRoom.SaveRoomData(false);
                        RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(
                            Newtonsoft.Json.JsonConvert.SerializeObject(newRoom)));
                    }
                    newRoom.SaveRoomFile();
                    RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(
                        Newtonsoft.Json.JsonConvert.SerializeObject(newRoom)));
                }
            }
            //楼层房间列表
            if (floorUpdateList != null && floorUpdateList.Count > 0)
            {
                if (optionType == OptionType.Update)
                foreach (var updateTemp in floorUpdateList)
                {
                    foreach(var updateTemp in floorUpdateList)
                    {
                        var localFloor = FloorList.Find((obj) => obj.roomId == updateTemp.roomId);
                        if (localFloor == null)
                        {
                            FloorList.Add(updateTemp);
                        }
                        else
                        {
                            localFloor = updateTemp;
                        }
                    }
                }
                if(optionType == OptionType.Cover)
                {
                    FloorList.Clear();
                    foreach (var updateTemp in floorUpdateList)
                    {
                        FloorList.Add(updateTemp);
                    }
                    FloorList.Add(updateTemp);
                }
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
@@ -247,7 +159,7 @@
        /// 增加楼层
        /// </summary>
        /// <param name="floor"></param>
        public string AddFloor(SpatialInfo floor)
        public string AddFloor(SpatialInfo floor, out SpatialInfo newFloor)
        {
            var pm = new HttpServerRequest();
            var revPack = new ResponsePackNew();
@@ -255,17 +167,23 @@
            if (revPack.Code == StateCode.SUCCESS)
            {
                var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(revPack.Data.ToString());
                if(revData.Count>0)
                if (revData.Count > 0)
                {
                    var addFloor = revData.Find((obj) => obj.uid == floor.uid);
                    if(addFloor!= null)
                    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;
        }
        /// <summary>
@@ -357,7 +275,7 @@
        /// 增加房间
        /// </summary>
        /// <param name="room"></param>
        public string AddRoom(Room room)
        public string AddRoom(Room room,out Room newRoom)
        {
            var pm = new HttpServerRequest();
            var pack = pm.AddRoom(new List<SpatialInfo>() { room });
@@ -372,7 +290,7 @@
                        var tempRoom = revData.Find((obj) => obj.uid == room.uid);
                        if (tempRoom != null)
                        {
                            room = tempRoom as Room;
                            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);
@@ -380,6 +298,7 @@
                    }
                }
            }
            newRoom = room;
            return pack.Code;
        }
        /// <summary>
@@ -406,23 +325,34 @@
        /// </summary>
        public void DeleteAllRoom()
        {
            foreach(var localRoom in RoomList)
            for(int i=0;i< RoomList.Count;)
            {
                Common.FileUtlis.Files.DeleteFile(localRoom.dataSavePath);
                Common.FileUtlis.Files.DeleteFile(RoomList[i].dataSavePath);
                RoomList.RemoveAt(i);
            }
            rooms = null;
        }
        public void ClearRooms()
        public void Clear()
        {
            floors.Clear();
            rooms.Clear();
            if (_currentSpatial != null)
            {
                if (_currentSpatial.floors != null)
                {
                    _currentSpatial.floors.Clear();
                }
                if (_currentSpatial.rooms != null)
                {
                    _currentSpatial.rooms.Clear();
                }
                _currentSpatial = null;
            }
        }
        /// <summary>
        /// 初始化每个房间的功能数据
        /// </summary>
        public void InitRoomFunction()
        public void InitRoomListFunctions()
        {
            new System.Threading.Thread(() =>
            {
@@ -431,8 +361,7 @@
                    //初始化住宅所有房间功能数据
                    foreach (var r in RoomList)
                    {
                        r.GetRoomFunctions(true);
                        r.GetRoomScenes(true);
                        InitRoomFuntion(r);
                    }
                }
                catch (Exception ex)
@@ -442,25 +371,42 @@
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 初始化房间功能
        /// </summary>
        /// <param name="tempRoom"></param>
        public void InitRoomFuntion(Room tempRoom)
        {
            tempRoom.GetRoomFunctions(true);
            tempRoom.GetRoomScenes(true);
        }
        /// <summary>
        /// 保存房间数据
        /// 保存房间数据文件
        /// </summary>
        public string SaveRoomData(bool upServer = true)
        public void SaveRoomFile()
        {
            var packCode = StateCode.SUCCESS;
            bool needSave = true;
            if (upServer)
            {
                packCode = SaveSpatialInfo().Code;
            }
            if (needSave)
            {
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
            }
            return packCode;
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
        }
        /// <summary>
        /// 更新房间数据
        /// </summary>
        public void UpdataRoomInfo()
        {
            new System.Threading.Thread(() =>
            {
                var pm = new HttpServerRequest();
                var pack = pm.UpdateRoom(new List<SpatialInfo>() { this });
                this.SaveRoomFile();
                if (pack.Code != StateCode.SUCCESS)
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            })
            { IsBackground = true }.Start();
        }
        #endregion
    }
@@ -506,7 +452,7 @@
            {
                if (CurrentSpatial.FloorList.Count > 0)
                {
                    var f = CurrentSpatial.FloorList.Find((obj) => obj.roomId == parentId);
                    var f = CurrentSpatial.FloorList.Find((obj) => obj.uid == parentId);
                    if (f != null)
                    {
                        return f.roomName + " ";
@@ -531,7 +477,7 @@
            }
            if (functions.Count == 0)
            {
                foreach (var f in DB_ResidenceData.functionList.GetAllFunction())
                foreach (var f in FunctionList.List.GetDeviceFunctionList())
                {
                    if (f.roomIds.Contains(roomId) && roomId != null)
                    {
@@ -573,7 +519,7 @@
            }
            if (scenes.Count == 0)
            {
                foreach (var s in DB_ResidenceData.functionList.scenes)
                foreach (var s in FunctionList.List.scenes)
                {
                    if (s.roomIds.Contains(roomId) && roomId != null)
                    {