using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter
{
///
/// 设备固定信息的逻辑
///
public class HdlDeviceFixedAttributeLogic
{
#region ■ 变量声明___________________________
///
/// 设备固定信息的逻辑
///
private static HdlDeviceFixedAttributeLogic m_Current = null;
///
/// 设备固定信息的逻辑
///
public static HdlDeviceFixedAttributeLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlDeviceFixedAttributeLogic();
}
return m_Current;
}
}
/// 需要获取固定属性的对象设备
///
private HashSet hsGetHardInfoDevice = new HashSet();
#endregion
#region ■ 主入口函数_________________________
///
/// 读取以及设置设备固定属性
///
/// 设备回路
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 ■ 发送命令___________________________
///
/// 发送获取固定属性的命令
///
///
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 ■ 获取窗帘命令_______________________
///
/// 获取窗帘命令
///
/// 设备对象
/// 标题数据
/// 属性数据
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 ■ 设置属性(主函数)___________________
///
/// 设置设备的固定属性 -1:异常 0:推送的这个东西并不是需要的 1:正常
///
/// 上报信息
/// 设备对象
///
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 ■ 设置窗帘属性_______________________
///
/// 设置窗帘属性 -1:异常 0:推送的这个东西并不是需要的 1:正常
///
/// 上报信息
/// 设备对象
///
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 ■ 接口推送处理_______________________
///
/// 根据接口推送的信息,设置设备硬件信息
///
///
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 ■ 移除监听线程_______________________
///
/// 移除获取设备硬件信息的监听线程
///
///
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
}
}