using System; using System.Collections.Generic; using HDL_ON.DAL.Server; using Shared; namespace HDL_ON.Entity { public class SceneApiPack { static SceneApiPack _ScenePack; public static SceneApiPack ScenePack { get { if(_ScenePack == null) { _ScenePack = new SceneApiPack(); } return _ScenePack; } } } public class Scene { /// /// 云端唯一ID /// public string userSceneId = ""; /// /// 场景sid /// public string sid = ""; /// /// 场景名称 /// public string name = ""; /// /// 网关ID /// public string gatewayId = ""; /// /// 云端oss存储图片的路径 /// public string image = ""; /// /// 所属房间列表 /// public List roomIds = new List(); /// /// 延时 /// public int delay = 0; /// /// 延时显示的文本 /// [Newtonsoft.Json.JsonIgnore] public string delayText { get { string text = ""; switch (delay) { case 0: text = Language.StringByID(StringId.NoDelay); break; case 30: text = "30s"; break; case 60: text = "1min"; break; case 120: text = "2min"; break; case 300: text = "5min"; break; } return text; } } /// /// 场景分组 /// public string group = "1"; /// /// 场景类型 /// public SceneType sceneType = SceneType.OrdinaryScenes; /// /// 收藏标记 /// public bool collect = false; ///// ///// 场景背景 ///// //public string ImagePath = "Intelligence/Gallery/scenebg1.png";//"FunctionIcon/Scene/s1.png"; ///// ///// 场景背景 ///// public string ImagePath { get { if (string.IsNullOrEmpty(image)) { return "Intelligence/Gallery/scenebg1.png"; } else { return image; } } set { image = value; } } /// /// 场景功能列表 /// public List functions = new List(); /// /// 场景推送配置列表 /// public List pushConfigs = new List(); /// /// 生成场景sid /// public void NewSid() { string sSceneid = ""; try { string sOidBeginsWith = "000101";//厂商 + 通讯方式 DateTime dt = DateTime.Now; DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1)); long m = (long)((dt - startTime).TotalMilliseconds / 10); string sTimeSpan = "00000000"; byte[] arry = new byte[4]; arry[0] = (byte)(m & 0xFF); arry[1] = (byte)((m & 0xFF00) >> 8); arry[2] = (byte)((m & 0xFF0000) >> 16); arry[3] = (byte)((m >> 24) & 0xFF); sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2"); if (sTimeSpan.Length > 8) { sTimeSpan = sTimeSpan.Substring(0, 8); } else { sTimeSpan = "00000000"; } sSceneid = sOidBeginsWith + sTimeSpan; sSceneid += "0A"; sSceneid += "0A01"; //0A01 物模型为场景, 0001 表示 1 号场景功能 int iTopSceneId = 1; Random random = new Random(); iTopSceneId = random.Next(0, 255); iTopSceneId += random.Next(0, 255); sSceneid += iTopSceneId.ToString("X4");//场景号 两个byte sSceneid += "1100"; sid = sSceneid; } catch { sid = sSceneid; } } /// /// 获取设备添加到房间的房间名称 /// /// public string GetRoomListName() { string roomNameList = ""; foreach (var roomId in roomIds) { var findRoom = DB_ResidenceData.residenceData.Rooms.Find(obj => obj.uid == roomId); if (findRoom == null) { continue; } if (roomNameList != "") { roomNameList += ","; } roomNameList += findRoom.floorName + findRoom.roomName; } if (roomNameList == "" ) { roomNameList = Shared.Language.StringByID(StringId.WholeHouseScene); } return roomNameList; } /// /// 数据存储文件名 /// [Newtonsoft.Json.JsonIgnore] public string savePath { get { return "SceneData_" + sid; } } /// /// 保存功能数据 /// public void SaveSceneData() { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)); FileUtils.WriteFileByBytes(savePath, ssd); MainPage.Log($"Save Scene Data : {savePath}"); UploadScene(); } /// /// 上传数据到云端 /// void UploadScene() { var pm = new HttpServerRequest(); ResponsePackNew revPack; if (DeleteSign) { var resCode = pm.DeleteScene(userSceneId); } else { if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime)) { revPack = pm.AddScene(this); if (revPack.Code == StateCode.SUCCESS) { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)); FileUtils.WriteFileByBytes(savePath, ssd); } } else if (!string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime)) { revPack = pm.EditScene(this); if (revPack.Code == StateCode.SUCCESS) { var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject>(revPack.Data.ToString()); var tempScene = scenes.Find((obj) => obj.sid == sid); if (tempScene != null) { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene)); FileUtils.WriteFileByBytes(savePath, ssd); } } } } } /// /// 删除标记 /// 需要删除数据时,标记为:true /// 由云端删除成功之后,返回数据再清除本地数据 /// public bool DeleteSign = false; /// /// 云端数据创建的时间 /// public string createTime = ""; /// /// 云端数据修改的最后时间 /// public string modifyTime = ""; } /// /// 场景功能对象 /// public class SceneFunction { public string sid = ""; public List status = new List(); /// /// 功能延时 /// public string delay = "0"; Function _localFunction; /// /// 本地对应的功能 /// [Newtonsoft.Json.JsonIgnore] public Function localFunction { get { if(_localFunction == null) { _localFunction = ConvertFunctionObject(); } return _localFunction; } } /// /// 转换成功能对象 /// /// Function ConvertFunctionObject() { var localFunction = DB_ResidenceData.functionList.GetAllDeviceFunctionList().Find((obj) => obj.sid == sid); foreach (var s in status) { localFunction.attributes.Add(new FunctionAttributes() { key = s.key, curValue = s.value, value = new List() { s.value } }); } return localFunction; } } /// /// 场景功能属性 /// public class SceneFunctionStatus { public string key = ""; public string value = ""; } /// /// 场景推送配置 /// public class ScenePushConfig { /// /// 推送方式 /// public string pushMethod = ""; /// /// 推送内容 /// public string pushContent = ""; /// /// 推送目标集合 /// public List pushTarget = new List(); } }