using System;
|
using System.Collections.Generic;
|
using HDL_ON.DAL.Server;
|
using Shared;
|
|
namespace HDL_ON.Entity
|
{
|
/// <summary>
|
/// 读取服务器空间信息返回到数据包
|
/// </summary>
|
public class SpatialApiPack
|
{
|
public List<SpatialInfo> list = new List<SpatialInfo>();
|
|
public string totalCount = "0";
|
|
public string totalPage = "0";
|
|
public string pageNo = "0";
|
|
public string pageSize = "0";
|
|
}
|
/// <summary>
|
/// 空间信息基础类
|
/// 包含楼层/房间
|
/// </summary>
|
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 = UserInfo.Current.CurReginID;
|
}
|
|
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 createTime = "";
|
public string modifyTime = "";
|
///// <summary>
|
///// 删除标记 20201208:可欣:APP的在线和离线问题,刚刚确定了,离线无法编辑,只能控制
|
///// 需要删除数据时,标记为:true
|
///// 由云端删除成功之后,返回数据再清除本地数据
|
///// </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)
|
{
|
var roomUpdateList = spatials.FindAll((obj) => obj.roomType == "ROOM");
|
var floorUpdateList = spatials.FindAll((obj) => obj.roomType == "FLOOR");
|
//处理房间列表
|
if (roomUpdateList != null && roomUpdateList.Count > 0)
|
{
|
if (optionType == OptionType.Update)
|
{
|
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)));
|
}
|
}
|
}
|
//楼层房间列表
|
if (floorUpdateList != null && floorUpdateList.Count > 0)
|
{
|
if (optionType == OptionType.Update)
|
{
|
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);
|
}
|
}
|
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
|
Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
|
}
|
}
|
|
#region 楼层列表
|
/// <summary>
|
/// 楼层列表
|
/// </summary>
|
List<SpatialInfo> floors;
|
/// <summary>
|
/// 楼层列表
|
/// </summary>
|
public List<SpatialInfo> FloorList
|
{
|
get
|
{
|
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<List<SpatialInfo>>(floorsDataString);
|
}
|
if (floors == null)
|
{
|
floors = new List<SpatialInfo>();
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"楼层数据初始化失败:{ex.Message}");
|
}
|
}
|
return floors;
|
}
|
}
|
/// <summary>
|
/// 数据保存路径
|
/// </summary>
|
string dataSavePath
|
{
|
get
|
{
|
if(roomType == "ROOM")
|
{
|
return "RoomPath_" + uid;
|
}
|
if(roomType == "FLOOR")
|
{
|
return "FloorsData";
|
}
|
else
|
{
|
return "SpatialInfo_"+uid;
|
}
|
}
|
}
|
/// <summary>
|
/// 增加楼层
|
/// </summary>
|
/// <param name="floor"></param>
|
public string AddFloor(SpatialInfo floor)
|
{
|
var pm = new HttpServerRequest();
|
var revPack = new ResponsePackNew();
|
revPack = pm.AddRoom(new List<SpatialInfo>() { floor });
|
if (revPack.Code == StateCode.SUCCESS)
|
{
|
var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(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);
|
}
|
return revPack.Code;
|
}
|
/// <summary>
|
/// 更新楼层信息
|
/// </summary>
|
/// <param name="floor"></param>
|
public string UpdateFloor(SpatialInfo floor)
|
{
|
var pm = new HttpServerRequest();
|
var revPack = pm.UpdateRoom(new List<SpatialInfo>() { 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;
|
}
|
/// <summary>
|
/// 删除楼层
|
/// </summary>
|
/// <param name="floor"></param>
|
public string DelFloor(string floorId)
|
{
|
if (string.IsNullOrEmpty( floorId))
|
return "";
|
var pm = new HttpServerRequest();
|
var code = pm.DeleteRoom(new List<string>() { 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 房间列表
|
/// <summary>
|
/// 房间列表
|
/// </summary>
|
List<Room> rooms;
|
/// <summary>
|
/// 房间列表
|
/// </summary>
|
public List<Room> RoomList
|
{
|
get
|
{
|
if (rooms == null)
|
{
|
try
|
{
|
rooms = new List<Room> { };
|
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<Room>(roomDataString);
|
if (room != null)
|
{
|
rooms.Add(room);
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"功能数据初始化失败:{ex.Message}");
|
}
|
}
|
return rooms;
|
}
|
}
|
/// <summary>
|
/// 增加房间
|
/// </summary>
|
/// <param name="room"></param>
|
public string AddRoom(Room room)
|
{
|
var pm = new HttpServerRequest();
|
var pack = pm.AddRoom(new List<SpatialInfo>() { room });
|
|
if (pack.Code == StateCode.SUCCESS)
|
{
|
var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(pack.Data.ToString());
|
if (revData != null)
|
{
|
if (revData.Count > 0)
|
{
|
var tempRoom = revData.Find((obj) => obj.uid == room.uid);
|
if (tempRoom != null)
|
{
|
room = tempRoom as Room;
|
CurrentSpatial.RoomList.Add(room);
|
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(room));
|
Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
|
}
|
}
|
}
|
}
|
return pack.Code;
|
}
|
/// <summary>
|
/// 删除房间
|
/// </summary>
|
/// <param name="room"></param>
|
public string DelRoom(Room room)
|
{
|
var pm = new HttpServerRequest();
|
var pack = pm.DeleteRoom(new List<string>() { 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;
|
}
|
/// <summary>
|
/// 删除所有房间
|
/// </summary>
|
public void DeleteAllRoom()
|
{
|
foreach(var localRoom in RoomList)
|
{
|
Common.FileUtlis.Files.DeleteFile(localRoom.dataSavePath);
|
}
|
rooms = null;
|
}
|
|
public void ClearRooms()
|
{
|
if (floors != null)
|
{
|
floors.Clear();
|
}
|
if (rooms != null)
|
{
|
rooms.Clear();
|
}
|
}
|
|
/// <summary>
|
/// 初始化每个房间的功能数据
|
/// </summary>
|
public void InitRoomFunction()
|
{
|
new System.Threading.Thread(() =>
|
{
|
try
|
{
|
//初始化住宅所有房间功能数据
|
foreach (var r in RoomList)
|
{
|
r.GetRoomFunctions(true);
|
r.GetRoomScenes(true);
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Init room function error : {ex.Message}");
|
}
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
/// <summary>
|
/// 保存房间数据
|
/// </summary>
|
public string SaveRoomData(bool upServer = true)
|
{
|
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;
|
}
|
#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;
|
}
|
}
|
|
|
/// <summary>
|
/// 楼层
|
/// </summary>
|
[Newtonsoft.Json.JsonIgnore]
|
public string floorName
|
{
|
get
|
{
|
if (CurrentSpatial.FloorList.Count > 0)
|
{
|
var f = CurrentSpatial.FloorList.Find((obj) => obj.roomId == parentId);
|
if (f != null)
|
{
|
return f.roomName + " ";
|
}
|
}
|
return "";
|
}
|
}
|
|
/// <summary>
|
/// 房间功能列表
|
/// </summary>
|
List<Function> functions = new List<Function>();
|
/// <summary>
|
/// 获取房间功能列表
|
/// </summary>
|
public List<Function> GetRoomFunctions(bool needRefresh)
|
{
|
if (needRefresh)
|
{
|
functions = new List<Function>();
|
}
|
if (functions.Count == 0)
|
{
|
foreach (var f in FunctionList.List.GetDeviceFunctionList())
|
{
|
if (f.roomIds.Contains(roomId) && roomId != null)
|
{
|
functions.Add(f);
|
}
|
}
|
}
|
return functions;
|
}
|
/// <summary>
|
/// 增加房间功能
|
/// 操作的是缓存数据,不用保存
|
/// </summary>
|
public void AddRoomFunction(Function function)
|
{
|
functions.Add(function);
|
}
|
/// <summary>
|
/// 删除房间功能
|
/// 操作的是缓存数据,不用保存
|
/// </summary>
|
public void RemoveRoomFunction(Function function)
|
{
|
functions.Remove(functions.Find((obj) => obj.sid == function.sid));
|
}
|
/// <summary>
|
/// 房间场景列表
|
/// </summary>
|
List<Scene> scenes = new List<Scene>();
|
/// <summary>
|
/// 获取场景列表
|
/// </summary>
|
/// <returns></returns>
|
public List<Scene> GetRoomScenes(bool needRefresh)
|
{
|
if (needRefresh)
|
{
|
scenes = new List<Scene>();
|
}
|
if (scenes.Count == 0)
|
{
|
foreach (var s in FunctionList.List.scenes)
|
{
|
if (s.roomIds.Contains(roomId) && roomId != null)
|
{
|
scenes.Add(s);
|
}
|
}
|
}
|
return scenes;
|
}
|
/// <summary>
|
/// 增加房间场景
|
/// 操作的是缓存数据,不用保存
|
/// </summary>
|
/// <param name="scene"></param>
|
public void AddRoomScene(Scene scene)
|
{
|
scenes.Add(scene);
|
}
|
/// <summary>
|
/// 移除房间场景
|
/// 操作的是缓存数据,不用保存
|
/// </summary>
|
/// <param name="scene"></param>
|
public void RemoveRoomScene(Scene scene)
|
{
|
scenes.Remove(scenes.Find((obj) => obj.sid == scene.sid));
|
}
|
}
|
|
}
|