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
{
public Scene()
{
sid = NewSid();
if (!DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
{
gatewayId = DB_ResidenceData.Instance.HomeGateway.gatewayId;
}
}
///
/// 云端唯一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 List uids = new List();
///
/// 延时
///
public string 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 string NewSid()
{
string sceneId = "";
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";
}
sceneId = sOidBeginsWith + sTimeSpan;
sceneId += "0A";
sceneId += "0A01";
int maxId = 1;
for (int i = 0; i < FunctionList.List.scenes.Count; i++)
{
string s = FunctionList.List.scenes[i].sid.Substring(20, 4);
int iThisSceneId = Convert.ToInt16(s, 16);
if (iThisSceneId > maxId)
maxId = iThisSceneId;
}
sceneId += (maxId + 1).ToString("X4");
sceneId += "0000";
}
catch
{
return sceneId;
}
return sceneId;
}
///
/// 获取设备添加到房间的房间名称
///
///
public string GetRoomListName()
{
string roomNameList = "";
foreach (var roomId in roomIds)
{
var findRoom = SpatialInfo.CurrentSpatial.RoomList.Find(obj => obj.roomId == 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(bool upServer)
{
if (upServer)
{
UploadScene();
}
else
{
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
MainPage.Log($"Save Scene Data : {savePath}");
}
}
///
/// 收藏场景
///
///
public string CollectScene()
{
var pm = new HttpServerRequest();
if (this.collect)
{
var revPack = pm.CollectScene(this.userSceneId);
return revPack;
}
else
{
var revPack = pm.CancelCollectScene(this.userSceneId);
return revPack;
}
}
///
/// 编辑场景
///
public string EditScene()
{
var pm = new HttpServerRequest();
var 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));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
}
}
return revPack.Code;
}
///
/// 修改场景
///
public string UpdateScene()
{
var pm = new HttpServerRequest();
var 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));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
}
}
return revPack.Code;
}
///
/// 上传数据到云端
///
void UploadScene()
{
var pm = new HttpServerRequest();
ResponsePackNew revPack;
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));
Common.FileUtlis.Files.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));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
}
}
}
}
///
/// 云端数据创建的时间
///
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 = FunctionList.List.GetDeviceFunctionList().Find((obj) => obj.sid == sid);
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();
}
}