using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 设备固定信息的逻辑
|
/// </summary>
|
public class HdlDeviceFixedAttributeLogic
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 设备固定信息的逻辑
|
/// </summary>
|
private static HdlDeviceFixedAttributeLogic m_Current = null;
|
/// <summary>
|
/// 设备固定信息的逻辑
|
/// </summary>
|
public static HdlDeviceFixedAttributeLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new HdlDeviceFixedAttributeLogic();
|
}
|
return m_Current;
|
}
|
}
|
|
/// 需要获取固定属性的对象设备
|
/// </summary>
|
private HashSet<string> hsGetHardInfoDevice = new HashSet<string>();
|
|
#endregion
|
|
#region ■ 主入口函数_________________________
|
|
/// <summary>
|
/// 读取以及设置设备固定属性
|
/// </summary>
|
/// <param name="device">设备回路</param>
|
public void SetAllFixedAttributeToDevice(CommonDevice device)
|
{
|
if (device == null)
|
{
|
return;
|
}
|
lock (this.hsGetHardInfoDevice)
|
{
|
//先移除
|
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
|
if (this.hsGetHardInfoDevice.Contains(mainKeys) == true)
|
{
|
this.hsGetHardInfoDevice.Remove(mainKeys);
|
}
|
if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceGetFixedAttribute") == false)
|
{
|
//添加事件
|
HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceGetFixedAttribute", ReceiveComandDiv.A设备属性上报, this.SetFixedAttributeByInterfaceResult);
|
}
|
//发送命令
|
this.SeFixedAttributeComand(device);
|
}
|
}
|
|
#endregion
|
|
#region ■ 发送命令___________________________
|
|
/// <summary>
|
/// 发送获取固定属性的命令
|
/// </summary>
|
/// <param name="device"></param>
|
private void SeFixedAttributeComand(CommonDevice device)
|
{
|
Newtonsoft.Json.Linq.JObject jObject = null;
|
Newtonsoft.Json.Linq.JArray attriBute = null;
|
//窗帘
|
if (device.Type == DeviceType.WindowCoveringDevice)
|
{
|
this.GetCurtainComand(device, ref jObject, ref attriBute);
|
}
|
if (jObject == null)
|
{
|
//不需要发送
|
return;
|
}
|
string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
|
this.hsGetHardInfoDevice.Add(mainkeys);
|
//发送
|
var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
|
jObject.Add("Data", data);
|
device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
|
}
|
|
#endregion
|
|
#region ■ 获取窗帘命令_______________________
|
|
/// <summary>
|
/// 获取窗帘命令
|
/// </summary>
|
/// <param name="device">设备对象</param>
|
/// <param name="jObject">标题数据</param>
|
/// <param name="attriBute">属性数据</param>
|
private void GetCurtainComand(CommonDevice device, ref Newtonsoft.Json.Linq.JObject jObject, ref Newtonsoft.Json.Linq.JArray attriBute)
|
{
|
jObject = new Newtonsoft.Json.Linq.JObject
|
{
|
{ "DeviceAddr",device.DeviceAddr },
|
{ "Epoint", device.DeviceEpoint },
|
{ "Cluster_ID", (int)Cluster_ID.WindowCovering },
|
{ "Command", 108 }
|
};
|
attriBute = new Newtonsoft.Json.Linq.JArray
|
{
|
new Newtonsoft.Json.Linq.JObject
|
{
|
{ "AttriButeId", (int)AttriButeId.WindowCoveringType }
|
}
|
};
|
}
|
|
#endregion
|
|
#region ■ 设置属性(主函数)___________________
|
|
/// <summary>
|
/// 设置设备的固定属性 -1:异常 0:推送的这个东西并不是需要的 1:正常
|
/// </summary>
|
/// <param name="report">上报信息</param>
|
/// <param name="device">设备对象</param>
|
/// <returns></returns>
|
private int SetFixedAttribute(CommonDevice.DeviceStatusReportData report, CommonDevice device)
|
{
|
if (report == null)
|
{
|
return -1;
|
}
|
//窗帘
|
if (device.Type == DeviceType.WindowCoveringDevice)
|
{
|
return this.SetCurtainAttribute(report, (Rollershade)device);
|
}
|
return 0;
|
}
|
|
#endregion
|
|
#region ■ 设置窗帘属性_______________________
|
|
/// <summary>
|
/// 设置窗帘属性 -1:异常 0:推送的这个东西并不是需要的 1:正常
|
/// </summary>
|
/// <param name="report">上报信息</param>
|
/// <param name="device">设备对象</param>
|
/// <returns></returns>
|
private int SetCurtainAttribute(CommonDevice.DeviceStatusReportData report, Rollershade device)
|
{
|
if (report.CluterID != (int)Cluster_ID.WindowCovering || device == null)
|
{
|
return -1;
|
}
|
//属性是否改变
|
bool AttriButeChanged = false;
|
int attriButeId = (int)AttriButeId.WindowCoveringType;
|
foreach (var data in report.AttriBute)
|
{
|
if (data.AttributeId == attriButeId)
|
{
|
AttriButeChanged = true;
|
device.WcdType = data.AttriButeData;
|
}
|
}
|
if (AttriButeChanged == true)
|
{
|
//属性已经改变,则保存
|
return 1;
|
}
|
return 0;
|
}
|
|
#endregion
|
|
#region ■ 接口推送处理_______________________
|
|
/// <summary>
|
/// 根据接口推送的信息,设置设备硬件信息
|
/// </summary>
|
/// <param name="device"></param>
|
private void SetFixedAttributeByInterfaceResult(CommonDevice device)
|
{
|
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
|
if (this.hsGetHardInfoDevice.Contains(mainKeys) == false)
|
{
|
//推送的这个东西并不是指定的设备
|
return;
|
}
|
//设置设备硬件信息
|
var localDevice = Common.LocalDevice.Current.GetDevice(mainKeys);
|
if (localDevice == null)
|
{
|
return;
|
}
|
lock (hsGetHardInfoDevice)
|
{
|
//-1:异常 0:推送的这个东西并不是需要的 1:正常
|
if (this.SetFixedAttribute(device.DeviceStatusReport, localDevice) != 1)
|
{
|
return;
|
}
|
//保存
|
localDevice.ReSave();
|
//移除
|
this.hsGetHardInfoDevice.Remove(mainKeys);
|
}
|
}
|
|
#endregion
|
|
#region ■ 移除监听线程_______________________
|
|
/// <summary>
|
/// 移除获取设备硬件信息的监听线程
|
/// </summary>
|
/// <param name="device"></param>
|
public void RemoveDeviceHardInfoThread(CommonDevice device)
|
{
|
lock (hsGetHardInfoDevice)
|
{
|
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
|
if (this.hsGetHardInfoDevice.Contains(mainKeys) == true)
|
{
|
this.hsGetHardInfoDevice.Remove(mainKeys);
|
}
|
}
|
}
|
|
#endregion
|
}
|
}
|