using Shared.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Threading.Tasks;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 场景的逻辑
|
/// </summary>
|
public class HdlSceneLogic
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 场景的逻辑
|
/// </summary>
|
private static HdlSceneLogic m_Current = null;
|
/// <summary>
|
/// 场景的逻辑
|
/// </summary>
|
public static HdlSceneLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new HdlSceneLogic();
|
}
|
return m_Current;
|
}
|
}
|
|
/// <summary>
|
/// 场景对象
|
/// </summary>
|
private Dictionary<int, SceneUI> dicScenes = new Dictionary<int, SceneUI>();
|
|
#endregion
|
|
#region ■ 刷新场景___________________________
|
|
/// <summary>
|
/// 从本地刷新场景
|
/// </summary>
|
public void ReFreshByLocal()
|
{
|
//清空场景
|
this.dicScenes.Clear();
|
//获取全部文件
|
var listFile = this.GetAllSceneFile();
|
foreach (string fileName in listFile)
|
{
|
try
|
{
|
var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(fileName));
|
var tempScene = Newtonsoft.Json.JsonConvert.DeserializeObject<SceneUI>(jsonInfo);
|
if (tempScene == null)
|
{
|
continue;
|
}
|
this.dicScenes[tempScene.Id] = tempScene;
|
}
|
catch(Exception ex)
|
{
|
HdlLogLogic.Current.WriteLog(ex);
|
try
|
{
|
//删掉错误文件
|
Global.DeleteFilebyHomeId(fileName);
|
}
|
catch { }
|
}
|
}
|
}
|
|
/// <summary>
|
/// 刷新本地的全部场景
|
/// </summary>
|
public bool RefreshSceneUIList()
|
{
|
//获取网关存在的场景
|
var sceneList = this.RefreshSceneListFromGateway();
|
if (sceneList == null)
|
{
|
return false;
|
}
|
var listEsxit = new HashSet<int>();
|
foreach (var scene in sceneList)
|
{
|
if (scene == null)
|
{
|
continue;
|
}
|
listEsxit.Add(scene.ScenesId);
|
//刷新scene的信息
|
this.RefreshScene(scene);
|
}
|
|
var listDelete = new List<SceneUI>();
|
foreach (var scId in this.dicScenes.Keys)
|
{
|
if (listEsxit.Contains(scId) == false)
|
{
|
//删除的对象
|
listDelete.Add(this.dicScenes[scId]);
|
}
|
}
|
foreach (var sceneui in listDelete)
|
{
|
//执行删除
|
this.DeleteLocalScene(sceneui);
|
}
|
|
return true;
|
}
|
|
/// <summary>
|
/// 刷新scene的信息
|
/// </summary>
|
/// <param name="sceneInfo"></param>
|
private void RefreshScene(Scene.GetSceneAllInfo sceneInfo)
|
{
|
if (this.dicScenes.ContainsKey(sceneInfo.ScenesId) == true)
|
{
|
var localScene = this.dicScenes[sceneInfo.ScenesId];
|
//只刷新名字和绑定表
|
localScene.Name = sceneInfo.ScenesName;
|
localScene.AdjustTargetList.Clear();
|
localScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
|
localScene.Save();
|
}
|
else
|
{
|
var newScene = new SceneUI();
|
newScene.Name = sceneInfo.ScenesName;
|
newScene.IconPath = "SceneIcon/0.png";
|
newScene.Id = sceneInfo.ScenesId;
|
newScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
|
//添加缓存
|
newScene.Save();
|
//加入缓存
|
this.dicScenes[newScene.Id] = newScene;
|
//备份
|
HdlAutoBackupLogic.AddOrEditorFile(newScene.FileName);
|
}
|
|
}
|
|
/// <summary>
|
/// 从网关重新刷新场景列表
|
/// </summary>
|
/// <returns></returns>
|
private List<Scene.GetSceneAllInfo> RefreshSceneListFromGateway()
|
{
|
var mainGateway = ZbGateway.MainGateWay;
|
if (mainGateway == null)
|
{
|
//获取网关对象失败
|
this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail));
|
return null;
|
}
|
|
//超时时间
|
int TimeOut = 0;
|
bool receiptAll = false;
|
|
var listScene = new List<Scene.GetSceneAllInfo>();
|
Action<string, string> action = (topic, message) =>
|
{
|
var gatewayID = topic.Split('/')[0];
|
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
|
|
if (topic == gatewayID + "/" + "Scene/GetAllInfo_Respon")
|
{
|
var sceneGetAllInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<Scene.GetSceneAllInfo>(jobject["Data"].ToString());
|
listScene.Add(sceneGetAllInfo);
|
|
if (sceneGetAllInfo.ScenesNum == sceneGetAllInfo.ScenesSum)
|
{
|
//接收完成
|
receiptAll = true;
|
TimeOut = 0;
|
}
|
}
|
};
|
mainGateway.Actions += action;
|
|
var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 807 } };
|
mainGateway.Send(("Scene/GetAllInfo"), System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
|
|
int waitTime = 20 * 6;
|
while (receiptAll == false && TimeOut < waitTime)
|
{
|
//全部接收才退出
|
System.Threading.Thread.Sleep(50);
|
TimeOut++;
|
}
|
mainGateway.Actions -= action;
|
action = null;
|
if (receiptAll == false)
|
{
|
//网络不稳定,场景列表信息缺损
|
this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uNetworkUnStableAndSceneInfoIsNotFull));
|
return null;
|
}
|
|
return listScene;
|
}
|
|
#endregion
|
|
#region ■ 添加场景___________________________
|
|
/// <summary>
|
/// 添加新场景到网关(返回null代表出错)
|
/// </summary>
|
/// <param name="sceneName">场景名称</param>
|
/// <param name="listAdjustTarget">执行目标</param>
|
/// <returns></returns>
|
public async Task<SceneUI> AddNewSceneToGateway(string sceneName, List<Scene.DeviceListData> listAdjustTarget)
|
{
|
int NewScenesId = -1;
|
//如果当前住宅不是虚拟住宅
|
if (Common.Config.Instance.Home.IsVirtually == false)
|
{
|
var result1 = await Scene.GetSceneNewIdAsync(sceneName);
|
//共通错误检测
|
string error = HdlCheckLogic.Current.CheckCommonErrorCode(result1);
|
if (error != null)
|
{
|
this.ShowErrorMsg(error);
|
return null;
|
}
|
if (result1 == null || result1.getSceneIdData == null)
|
{
|
//添加场景失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.AddSceneFail);
|
//拼接上【网关回复超时】的Msg
|
msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result1);
|
this.ShowTipMsg(msg1);
|
return null;
|
}
|
NewScenesId = result1.getSceneIdData.NewScenesId;
|
}
|
else
|
{
|
//虚拟场景ID
|
NewScenesId = Convert.ToInt32(DateTime.Now.ToString("HHmmss"));
|
}
|
//添加执行目标
|
var listSuccess = await this.AddTargetToScene(NewScenesId, listAdjustTarget);
|
if (listSuccess == null)
|
{
|
return null;
|
}
|
|
//创建场景对象
|
var newScene = new SceneUI();
|
newScene.Name = sceneName;
|
newScene.Id = NewScenesId;
|
newScene.AdjustTargetList.AddRange(listSuccess);
|
//添加缓存
|
newScene.Save();
|
//加入缓存
|
this.dicScenes[newScene.Id] = newScene;
|
|
return newScene;
|
}
|
|
/// <summary>
|
/// 添加执行目标到指定的场景(返回成功添加的执行目标)
|
/// </summary>
|
/// <param name="sceneId">场景Id</param>
|
/// <param name="listAdjustTarget">执行目标</param>
|
/// <returns></returns>
|
private async Task<List<Scene.DeviceListData>> AddTargetToScene(int sceneId, List<Scene.DeviceListData> listAdjustTarget)
|
{
|
//如果当前住宅是虚拟住宅的话
|
if (Common.Config.Instance.Home.IsVirtually == true)
|
{
|
//不需要更新网关
|
return listAdjustTarget;
|
}
|
var listSuccess = new List<Scene.DeviceListData>();
|
for (int i = 0; i < listAdjustTarget.Count; i++)
|
{
|
var data = listAdjustTarget[i];
|
var addData = new Scene.AddSceneMemberData();
|
if (data.Type == 0)
|
{
|
//设备
|
addData.Type = 0;
|
addData.ScenesId = sceneId;
|
addData.DeviceAddr = data.DeviceAddr;
|
addData.Epoint = data.Epoint;
|
addData.TaskList = data.TaskList;
|
addData.DelayTime = 0;
|
addData.MemberNumber = i + 1;
|
}
|
else if (data.Type == 1)
|
{
|
//场景
|
addData.Type = 1;
|
addData.ScenesId = sceneId;
|
addData.ElseScenesId = data.ElseScenesId;
|
addData.DelayTime = 0;
|
addData.MemberNumber = i + 1;
|
}
|
else
|
{
|
//延时
|
addData.Type = 2;
|
addData.ScenesId = sceneId;
|
addData.DelayTime = data.DelayTime;
|
addData.MemberNumber = i + 1;
|
}
|
|
//添加新成员 返回结果
|
var result2 = await Scene.AddSceneMemberAsync(addData);
|
if (result2 == null || result2.addSceneMemberResponseData == null
|
|| result2.addSceneMemberResponseData.Result != 1)
|
{
|
continue;
|
}
|
//拥有成功的
|
listSuccess.Add(data);
|
}
|
if (listSuccess.Count == 0)
|
{
|
//添加执行目标失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uAddAdjustTargetFail);
|
this.ShowTipMsg(msg1);
|
return null;
|
}
|
//如果不是全部成功的话,提示
|
if (listSuccess.Count != listAdjustTarget.Count)
|
{
|
//个别执行目标添加失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uSomeAdjustTargetAddFail);
|
this.ShowTipMsg(msg1);
|
}
|
return listSuccess;
|
}
|
|
/// <summary>
|
/// 添加场景(这个单纯只改房间)
|
/// </summary>
|
/// <param name="scene">Scene.</param>
|
public void AddSceneToRoom(Room i_room, SceneUI scene)
|
{
|
//添加缓存
|
if (i_room.ListSceneId.Contains(scene.Id) == false)
|
{
|
i_room.ListSceneId.Add(scene.Id);
|
i_room.Save();
|
}
|
}
|
|
/// <summary>
|
/// 改变场景房间
|
/// </summary>
|
/// <param name="scene">场景对象</param>
|
/// <param name="newRoomId">新的房间的ID</param>
|
public void ChangedSceneRoom(SceneUI scene, string newRoomId)
|
{
|
var room = HdlRoomLogic.Current.GetRoomBySceneId(scene.Id);
|
if (room != null)
|
{
|
//房间没有改变
|
if (room.Id == newRoomId)
|
{
|
return;
|
}
|
//从房间中移除缓存
|
this.DeleteSceneFromRoom(room, scene);
|
}
|
//添加进新房间
|
var newRoom = HdlRoomLogic.Current.GetRoomById(newRoomId);
|
this.AddSceneToRoom(newRoom, scene);
|
}
|
|
/// <summary>
|
/// 添加搜藏场景
|
/// </summary>
|
/// <param name="scene">Scene.</param>
|
public void AddLoveScene(SceneUI scene)
|
{
|
var nowRoom = HdlRoomLogic.Current.GetLoveRoom();
|
//添加缓存
|
if (nowRoom.ListSceneId.Contains(scene.Id) == false)
|
{
|
nowRoom.ListSceneId.Add(scene.Id);
|
nowRoom.Save();
|
}
|
}
|
|
/// <summary>
|
/// 添加虚拟场景
|
/// </summary>
|
/// <param name="sceneId">指定场景id,如果为-1,则会内部自动编号(可能会重复)</param>
|
/// <param name="sceneName">场景名字</param>
|
/// <param name="listAdjustTarget">绑定目标</param>
|
/// <returns></returns>
|
public SceneUI AddVirtualScene(int sceneId, string sceneName, List<Scene.DeviceListData> listAdjustTarget)
|
{
|
if (sceneId == -1)
|
{
|
sceneId = Convert.ToInt32(DateTime.Now.ToString("HHmmss"));
|
}
|
//创建场景对象
|
var newScene = new SceneUI();
|
newScene.Name = sceneName;
|
newScene.IconPath = "SceneIcon/0.png";
|
//虚拟场景ID
|
newScene.Id = sceneId;
|
if (listAdjustTarget != null)
|
{
|
newScene.AdjustTargetList.AddRange(listAdjustTarget);
|
}
|
//添加缓存
|
newScene.Save();
|
//加入缓存
|
this.dicScenes[newScene.Id] = newScene;
|
|
return newScene;
|
}
|
|
#endregion
|
|
#region ■ 修改场景___________________________
|
|
/// <summary>
|
/// 修改网关的场景
|
/// </summary>
|
/// <param name="sceneUI">场景对象</param>
|
/// <param name="listAdjustTarget">全部的执行目标</param>
|
/// <returns></returns>
|
public async Task<bool> EditorSceneFromGateway(SceneUI sceneUI, List<Scene.DeviceListData> listAdjustTarget)
|
{
|
//如果当前住宅是虚拟住宅的话
|
if (Common.Config.Instance.Home.IsVirtually == true)
|
{
|
//清空场景的执行目标列表的缓存
|
sceneUI.AdjustTargetList.Clear();
|
sceneUI.AdjustTargetList.AddRange(listAdjustTarget);
|
sceneUI.Save();
|
return true;
|
}
|
|
//先清空全部的执行目标
|
var deleteData = new Scene.SceneRemoveMemberData();
|
var deleteTargetData = new List<Scene.RemoveSceneDeviceListInfo>();
|
for (int i = 0; i < sceneUI.AdjustTargetList.Count; i++)
|
{
|
var data = sceneUI.AdjustTargetList[i];
|
var removeData = new Scene.RemoveSceneDeviceListInfo();
|
if (data.Type == 0)
|
{
|
//设备
|
removeData.Type = 0;
|
removeData.DeviceAddr = data.DeviceAddr;
|
removeData.Epoint = data.Epoint;
|
removeData.MemberNumber = data.MemberNumber;
|
}
|
else if (data.Type == 1)
|
{
|
//场景
|
removeData.Type = 1;
|
removeData.ElseScenesId = data.ElseScenesId;
|
removeData.MemberNumber = data.MemberNumber;
|
}
|
else
|
{
|
//延时
|
removeData.Type = 2;
|
removeData.DelayTime = data.DelayTime;
|
removeData.MemberNumber = data.MemberNumber;
|
}
|
//拥有成功的
|
deleteTargetData.Add(removeData);
|
}
|
deleteData.DeviceList = deleteTargetData;
|
deleteData.ScenesId = sceneUI.Id;
|
//执行清空
|
var result = await Scene.RemoveSceneMemberAsync(deleteData);
|
if (result == null || result.removeSceneMemberResponseData == null)
|
{
|
//初始化执行目标失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uInitAdjustTargetFail);
|
//拼接上【网关回复超时】的Msg
|
msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result);
|
this.ShowTipMsg(msg1);
|
return false;
|
}
|
if (result.removeSceneMemberResponseData.Result != 0)
|
{
|
//初始化执行目标失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uInitAdjustTargetFail);
|
this.ShowTipMsg(msg1);
|
return false;
|
}
|
//清空场景的执行目标列表的缓存
|
sceneUI.AdjustTargetList.Clear();
|
|
//然后重新添加
|
var listSuccess = await this.AddTargetToScene(sceneUI.Id, listAdjustTarget);
|
if (listSuccess == null)
|
{
|
return false;
|
}
|
sceneUI.AdjustTargetList.AddRange(listSuccess);
|
sceneUI.Save();
|
|
return true;
|
}
|
|
/// <summary>
|
/// 修改网关的场景名称
|
/// </summary>
|
/// <param name="scene">场景对象</param>
|
/// <param name="newName">新的名字</param>
|
/// <returns></returns>
|
public async Task<bool> EditorSceneNameFromGateway(SceneUI scene, string newName)
|
{
|
//如果当前住宅是虚拟住宅的话
|
if (Common.Config.Instance.Home.IsVirtually == true)
|
{
|
//不需要更新网关
|
return true;
|
}
|
var result1 = await Scene.RenameSceneAsync(scene.Id, newName);
|
//共通错误检测
|
string error = HdlCheckLogic.Current.CheckCommonErrorCode(result1);
|
if (error != null)
|
{
|
this.ShowErrorMsg(error);
|
return false;
|
}
|
if (result1 == null || result1.sceneRenameResponseData == null)
|
{
|
//修改场景名称失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uEditorSceneNameFail);
|
//拼接上【网关回复超时】的Msg
|
msg1 = UserCenterLogic.CombineGatewayTimeOutMsg(msg1, result1);
|
this.ShowTipMsg(msg1);
|
return false;
|
}
|
if (result1.sceneRenameResponseData.Result == 2)
|
{
|
//目标场景不存在
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uTargetSceneIsNotExsit);
|
this.ShowTipMsg(msg1);
|
return false;
|
}
|
if (result1.sceneRenameResponseData.Result != 1)
|
{
|
//修改场景名称失败
|
string msg1 = Language.StringByID(R.MyInternationalizationString.uEditorSceneNameFail);
|
this.ShowTipMsg(msg1);
|
return false;
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 删除场景___________________________
|
|
/// <summary>
|
/// 删除本地场景数据
|
/// </summary>
|
/// <param name="sceneUI">Scene user interface.</param>
|
public void DeleteLocalScene(SceneUI sceneUI)
|
{
|
//移除缓存
|
this.dicScenes.Remove(sceneUI.Id);
|
var room = HdlRoomLogic.Current.GetRoomBySceneId(sceneUI.Id);
|
if (room != null)
|
{
|
room.ListSceneId.Remove(sceneUI.Id);
|
room.Save();
|
}
|
|
//移除喜爱
|
var roomLove = HdlRoomLogic.Current.GetLoveRoom();
|
if (roomLove.ListSceneId.Contains(sceneUI.Id) == true)
|
{
|
roomLove.ListSceneId.Remove(sceneUI.Id);
|
roomLove.Save();
|
}
|
|
//删除文件
|
if (Global.IsExistsByHomeId(sceneUI.FileName) == true)
|
{
|
Global.DeleteFilebyHomeId(sceneUI.FileName);
|
HdlAutoBackupLogic.DeleteFile(sceneUI.FileName);
|
}
|
//删除图片
|
if (sceneUI.IconPathType == 1 || sceneUI.IconPathType == 2)
|
{
|
if (Global.IsExistsByHomeId(sceneUI.IconPath))
|
{
|
Global.DeleteFilebyHomeId(sceneUI.IconPath);
|
HdlAutoBackupLogic.DeleteFile(sceneUI.IconPath);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 删除场景(这个只移除内存)
|
/// </summary>
|
/// <param name="scene"></param>
|
public void DeleteSceneFromRoom(Room i_room, SceneUI scene)
|
{
|
//移除缓存
|
if (i_room.ListSceneId.Contains(scene.Id) == true)
|
{
|
i_room.ListSceneId.Remove(scene.Id);
|
i_room.Save();
|
}
|
}
|
|
/// <summary>
|
/// 删除搜藏场景
|
/// </summary>
|
/// <param name="scene"></param>
|
public void DeleteLoveScene(SceneUI scene)
|
{
|
var nowRoom = HdlRoomLogic.Current.GetLoveRoom();
|
//移除缓存
|
if (nowRoom.ListSceneId.Contains(scene.Id) == true)
|
{
|
nowRoom.ListSceneId.Remove(scene.Id);
|
nowRoom.Save();
|
}
|
}
|
|
#endregion
|
|
#region ■ 获取场景___________________________
|
|
/// <summary>
|
/// 通过场景id获取场景
|
/// </summary>
|
/// <returns>The scene UIB y scene identifier.</returns>
|
/// <param name="sceneId">Scene identifier.</param>
|
public SceneUI GetSceneUIBySceneId(int sceneId)
|
{
|
if (this.dicScenes.ContainsKey(sceneId) == true)
|
{
|
return this.dicScenes[sceneId];
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取该楼层所有场景
|
/// </summary>
|
/// <param name="floorId"></param>
|
/// <returns></returns>
|
public List<SceneUI> GetSameFloorScenes(string floorId)
|
{
|
List<SceneUI> sceneUIs = new List<SceneUI>();
|
var rooms = HdlRoomLogic.Current.GetRoomsByFloorId(floorId);
|
foreach (var r in rooms)
|
{
|
foreach (int sceneId in r.ListSceneId)
|
{
|
if (this.dicScenes.ContainsKey(sceneId) == true)
|
{
|
sceneUIs.Add(this.dicScenes[sceneId]);
|
}
|
}
|
}
|
return sceneUIs;
|
}
|
|
/// <summary>
|
/// 根据场景Id,获取楼层和房间名字(楼层,房间名字)
|
/// </summary>
|
/// <returns></returns>
|
public string GetZoneById(int sceneId)
|
{
|
var room = HdlRoomLogic.Current.GetRoomBySceneId(sceneId);
|
if (room == null)
|
{
|
return null;
|
}
|
var floorName = HdlResidenceLogic.Current.GetFloorNameById(room.FloorId);
|
//更改代码:如果floorName=“”也要判断,否则有“,”
|
if (string.IsNullOrEmpty(floorName))
|
{
|
return room.Name;
|
}
|
return $"{floorName},{room.Name}";
|
}
|
|
/// <summary>
|
/// 获取未分配区域场景
|
/// </summary>
|
/// <returns></returns>
|
public List<SceneUI> GetUnalloctedScenes()
|
{
|
List<SceneUI> sceneUIs = new List<SceneUI>();
|
//已经存在的场景
|
var listEsxit = new HashSet<int>();
|
var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
|
foreach (var room in listAllRoom)
|
{
|
if (room.IsLove == true)
|
{
|
//不包含收藏房间
|
continue;
|
}
|
foreach (int sceneId in room.ListSceneId)
|
{
|
if (listEsxit.Contains(sceneId) == false)
|
{
|
listEsxit.Add(sceneId);
|
}
|
}
|
}
|
foreach (var myScene in this.dicScenes.Values)
|
{
|
if (listEsxit.Contains(myScene.Id) == false)
|
{
|
sceneUIs.Add(myScene);
|
}
|
}
|
return sceneUIs;
|
}
|
|
/// <summary>
|
/// 获取全部房间的全部场景
|
/// </summary>
|
/// <returns></returns>
|
public List<SceneUI> GetAllRoomSceneList()
|
{
|
List<SceneUI> sceneUIs = new List<SceneUI>();
|
var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
|
foreach (var room in listAllRoom)
|
{
|
if (room.IsLove == true)
|
{
|
//不包含收藏房间
|
continue;
|
}
|
foreach (int sceneId in room.ListSceneId)
|
{
|
if (this.dicScenes.ContainsKey(sceneId) == true)
|
{
|
sceneUIs.Add(this.dicScenes[sceneId]);
|
}
|
}
|
}
|
|
return sceneUIs;
|
}
|
|
/// <summary>
|
/// 获取指定房间的全部场景
|
/// </summary>
|
/// <returns></returns>
|
public List<SceneUI> GetRoomSceneList(Room i_room)
|
{
|
List<SceneUI> sceneUIs = new List<SceneUI>();
|
foreach (int sceneId in i_room.ListSceneId)
|
{
|
if (this.dicScenes.ContainsKey(sceneId) == true)
|
{
|
sceneUIs.Add(this.dicScenes[sceneId]);
|
}
|
}
|
|
return sceneUIs;
|
}
|
|
#endregion
|
|
#region ■ 调用场景___________________________
|
|
/// <summary>
|
/// 调用场景
|
/// </summary>
|
/// <param name="scene"></param>
|
/// <returns></returns>
|
public async Task<bool> ControlScene(SceneUI scene)
|
{
|
if (scene.RemainTime > 0)
|
{
|
HdlThreadLogic.Current.RunMain(() =>
|
{
|
//该场景正在延时,请稍后
|
var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
|
msgContr.Show();
|
});
|
return false;
|
}
|
//如果是虚拟住宅
|
if (Common.Config.Instance.Home.IsVirtually == true)
|
{
|
//不调用网关
|
return true;
|
}
|
|
//执行调用场景
|
var result = await Scene.ControlSceneAsync(scene.Id, scene.SceneDelayTime);
|
if (result == null || result.sceneOpenData == null)
|
{
|
//控制场景失败
|
string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
|
HdlThreadLogic.Current.RunMain(() =>
|
{
|
//该场景正在延时,请稍后
|
var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
|
msgContr.Show();
|
});
|
return false;
|
}
|
if (result.sceneOpenData.Result == 0)
|
{
|
//控制场景失败
|
string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
|
HdlThreadLogic.Current.RunMain(() =>
|
{
|
//该场景正在延时,请稍后
|
var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
|
msgContr.Show();
|
});
|
return false;
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 获取场景的执行目标列表_____________
|
|
/// <summary>
|
/// 获取场景的执行目标列表
|
/// </summary>
|
/// <param name="i_scene">场景对象</param>
|
/// <returns></returns>
|
public async Task<List<Scene.DeviceListData>> GetAdjustTargetList(SceneUI i_scene)
|
{
|
//如果住宅为虚拟住宅
|
if (Common.Config.Instance.Home.IsVirtually == true)
|
{
|
var listAdjustTarget = new List<Scene.DeviceListData>();
|
listAdjustTarget.AddRange(i_scene.AdjustTargetList);
|
return listAdjustTarget;
|
}
|
|
var result = await Scene.GetSceneDeviceListAsync(i_scene.Id);
|
//共通错误检测
|
string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
|
if (error != null)
|
{
|
this.ShowErrorMsg(error);
|
return null;
|
}
|
if (result == null || result.getSceneDeviceListInfo == null)
|
{
|
//获取执行目标失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetAdjustTargetFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
|
|
this.ShowTipMsg(msg);
|
return null;
|
}
|
i_scene.AdjustTargetList = result.getSceneDeviceListInfo.DeviceList;
|
//保存缓存
|
i_scene.Save(false);
|
|
var listData = new List<Scene.DeviceListData>();
|
listData.AddRange(result.getSceneDeviceListInfo.DeviceList);
|
|
return listData;
|
}
|
#endregion
|
|
#region ■ 延时时间线程_______________________
|
|
/// <summary>
|
/// 开启延时时间线程(旨在全部地方的同一场景时间同步)
|
/// </summary>
|
public void StartDelayTimeThread(SceneUI i_scene)
|
{
|
if (i_scene.RemainThreadAction == true)
|
{
|
//线程在运行中
|
return;
|
}
|
i_scene.RemainThreadAction = true;
|
|
HdlThreadLogic.Current.RunThread(() =>
|
{
|
while (i_scene.RemainTime > 0)
|
{
|
System.Threading.Thread.Sleep(1000);
|
i_scene.RemainTime--;
|
}
|
i_scene.RemainThreadAction = false;
|
});
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 获取本地全部的场景文件
|
/// </summary>
|
/// <returns></returns>
|
public List<string> GetAllSceneFile()
|
{
|
List<string> listSceneFile = new List<string>();
|
List<string> listAllFile = Global.FileListByHomeId();
|
|
foreach (string file in listAllFile)
|
{
|
if (file.StartsWith("Scene_") == false)
|
{
|
//如果不是场景文件
|
continue;
|
}
|
listSceneFile.Add(file);
|
}
|
return listSceneFile;
|
}
|
|
/// <summary>
|
/// 显示错误信息窗口
|
/// </summary>
|
/// <param name="msg"></param>
|
private void ShowErrorMsg(string msg)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
var contr = new ShowMsgControl(ShowMsgType.Error, msg);
|
contr.Show();
|
});
|
}
|
|
/// <summary>
|
/// 显示Tip信息窗口
|
/// </summary>
|
/// <param name="msg"></param>
|
private void ShowTipMsg(string msg)
|
{
|
Application.RunOnMainThread(() =>
|
{
|
var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
|
contr.Show();
|
});
|
}
|
|
#endregion
|
}
|
}
|