using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
using System.Threading.Tasks;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 设备绑定的逻辑
|
/// </summary>
|
public class HdlDeviceBindLogic
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 设备绑定的逻辑
|
/// </summary>
|
private static HdlDeviceBindLogic m_Current = null;
|
/// <summary>
|
/// 设备绑定的逻辑
|
/// </summary>
|
public static HdlDeviceBindLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new HdlDeviceBindLogic();
|
}
|
return m_Current;
|
}
|
}
|
|
#endregion
|
|
#region ■ 获取设备下面绑定的设备_____________
|
|
/// <summary>
|
/// 获取设备下面绑定的设备(错误时返回null)
|
/// </summary>
|
/// <param name="mainDevice">设备对象</param>
|
/// <returns></returns>
|
public async Task<List<CommonDevice>> GetBindTargetDevice(CommonDevice mainDevice)
|
{
|
var result = (BindObj.GetDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "GetDeviceBindAsync");
|
if (result == null || result.getAllBindResponseData == null)
|
{
|
//获取设备的绑定目标失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceBindTargetFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
|
|
this.ShowErrorMsg(msg);
|
return null;
|
}
|
var listDevice = new List<CommonDevice>();
|
foreach (var data in result.getAllBindResponseData.BindList)
|
{
|
CommonDevice deviceTemp = Common.LocalDevice.Current.GetDevice(data.BindMacAddr, data.BindEpoint);
|
if (deviceTemp == null)
|
{
|
continue;
|
}
|
listDevice.Add(deviceTemp);
|
}
|
return listDevice;
|
}
|
|
#endregion
|
|
#region ■ 执行绑定设备目标___________________
|
|
/// <summary>
|
/// 绑定设备的目标(返回成功设置的设备,错误时,返回null)
|
/// </summary>
|
/// <param name="mainDevice">设备对象</param>
|
/// <param name="listDevice">要绑定的目标设备</param>
|
/// <param name="BindCluster">要绑定的目标设备</param>
|
/// <returns></returns>
|
public async Task<List<CommonDevice>> BindDeviceTarget(CommonDevice mainDevice, List<CommonDevice> listDevice, int BindCluster = 6)
|
{
|
if (listDevice.Count == 0)
|
{
|
return new List<CommonDevice>();
|
}
|
|
var dicDevice = new Dictionary<string, CommonDevice>();
|
|
//组装数据
|
var addData = new IASZone.AddBindData();
|
addData.DeviceAddr = mainDevice.DeviceAddr;
|
addData.Epoint = mainDevice.DeviceEpoint;
|
foreach (var device in listDevice)
|
{
|
var info = new IASZone.AddBindListObj();
|
info.BindCluster = BindCluster;
|
info.BindMacAddr = device.DeviceAddr;
|
info.BindEpoint = device.DeviceEpoint;
|
info.BindType = 0;
|
|
addData.BindList.Add(info);
|
|
//返回成功设备的时候使用
|
string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
|
dicDevice[mainkeys] = device;
|
}
|
|
var result = (BindObj.AddedDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "AddDeviceBindAsync", addData);
|
if (result == null || result.addedDeviceBindResponseData == null)
|
{
|
//绑定目标设置失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
|
|
this.ShowErrorMsg(msg);
|
return null;
|
}
|
|
var listSuccess = new List<CommonDevice>();
|
foreach (var data in result.addedDeviceBindResponseData.BindList)
|
{
|
string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint);
|
//0:添加成功 3:已经存在,也可以代表成功
|
if (data.Result == 0 || data.Result == 3)
|
{
|
if (dicDevice.ContainsKey(mainkeys) == true)
|
{
|
listSuccess.Add(dicDevice[mainkeys]);
|
}
|
}
|
//1:失败,节点设备或场景不存在
|
else if (data.Result == 1)
|
{
|
if (dicDevice.ContainsKey(mainkeys) == true)
|
{
|
//设备名称 绑定失败
|
string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
|
msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
|
this.ShowTipMsg(msg);
|
}
|
}
|
//2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
|
else if (data.Result == 2)
|
{
|
if (result.addBindResultResponseData == null)
|
{
|
//设备名称 绑定失败
|
string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
|
msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
|
this.ShowTipMsg(msg);
|
}
|
else
|
{
|
//添加成功
|
if (result.addBindResultResponseData.Result == 0)
|
{
|
if (dicDevice.ContainsKey(mainkeys) == true)
|
{
|
listSuccess.Add(dicDevice[mainkeys]);
|
}
|
}
|
//设备名称 绑定列表已满
|
else if (result.addBindResultResponseData.Result == 140)
|
{
|
string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
|
msg += Language.StringByID(R.MyInternationalizationString.uBindListIsFull);
|
this.ShowTipMsg(msg);
|
}
|
else
|
{
|
//设备名称 绑定失败
|
string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
|
msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
|
this.ShowTipMsg(msg);
|
}
|
}
|
}
|
}
|
|
return listSuccess;
|
}
|
|
#endregion
|
|
#region ■ 删除设备绑定的目标_________________
|
|
/// <summary>
|
/// 删除设备绑定的目标
|
/// </summary>
|
/// <param name="mainDevice">设备对象</param>
|
/// <param name="deleteDevice">要删除的绑定目标设备</param>
|
/// <param name="BindCluster">要绑定的目标设备</param>
|
/// <returns></returns>
|
public async Task<bool> DeleteDeviceTarget(CommonDevice mainDevice, CommonDevice deleteDevice, int BindCluster = 6)
|
{
|
//组装数据
|
var deleteData = new IASZone.DelDeviceBindData();
|
deleteData.DeviceAddr = mainDevice.DeviceAddr;
|
deleteData.Epoint = mainDevice.DeviceEpoint;
|
|
var info = new IASZone.RemoveBindListObj();
|
info.BindCluster = 6;
|
info.BindMacAddr = deleteDevice.DeviceAddr;
|
info.BindEpoint = deleteDevice.DeviceEpoint;
|
info.BindType = 0;
|
|
deleteData.RemoveBindList.Add(info);
|
|
var result = (BindObj.DelDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "DelDeviceBindAsync", deleteData);
|
if (result == null || result.delDeviceBindResponseData == null)
|
{
|
//删除绑定目标失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
|
//拼接上【网关回复超时】的Msg
|
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
|
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
|
foreach (var data in result.delDeviceBindResponseData.RemoveBindList)
|
{
|
//0:成功 1:设备不在绑定列表中 ,也可以代表成功
|
if (data.Result == 0 || data.Result == 1)
|
{
|
return true;
|
}
|
//3:失败,在等待节点设备确认是否解除绑定成功
|
else if (data.Result == 3)
|
{
|
//其他绑定目标正在删除中,请稍后再试
|
string msg = Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait);
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
//4:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
|
else if (data.Result == 4)
|
{
|
if (result.removeBindResultResponseData == null)
|
{
|
//删除绑定目标失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
else
|
{
|
//成功
|
if (result.removeBindResultResponseData.Result == 0)
|
{
|
return true;
|
}
|
//136:控制设备本地绑定列表中无此绑定
|
else if (result.removeBindResultResponseData.Result == 136)
|
{
|
//这个可以当做成功
|
return true;
|
}
|
else
|
{
|
//删除绑定目标失败
|
string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
|
this.ShowErrorMsg(msg);
|
return false;
|
}
|
}
|
}
|
}
|
return false;
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 执行指定设备对象类里面的方法(注意:这个是专门调用异步,并且等待异步完成的高科技函数,不调用异步的情况,别使用此函数)
|
/// </summary>
|
/// <param name="device">需要执行的设备的设备对象</param>
|
/// <param name="method">指定要加载的方法名</param>
|
/// <param name="parameter">启动参数</param>
|
private async Task<object> LoadDeviceMethodByNameAsync(CommonDevice device, string method, params object[] parameter)
|
{
|
var task = device.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, device, parameter) as Task;
|
await task;
|
|
var result = task.GetType().GetProperty("Result").GetValue(task, null);
|
return result;
|
}
|
|
/// <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 UserCenter.ShowMsgControl(ShowMsgType.Tip, msg);
|
contr.Show();
|
});
|
}
|
|
#endregion
|
}
|
}
|