using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter
{
///
/// 设备硬件信息的逻辑
///
public class HdlDeviceHardInfoLogic
{
#region ■ 变量声明___________________________
///
/// 设备硬件信息的逻辑
///
private static HdlDeviceHardInfoLogic m_Current = null;
///
/// 设备硬件信息的逻辑
///
public static HdlDeviceHardInfoLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlDeviceHardInfoLogic();
}
return m_Current;
}
}
///
/// 设备获取硬件信息后的回调函数
///
private Dictionary> dicDeviceHardInfoBackAction = new Dictionary>();
/// 获取硬件信息的对象设备
///
private HashSet hsGetHardInfoDevice = new HashSet();
#endregion
#region ■ 主入口函数_________________________
///
/// 读取以及设置设备硬件信息
///
/// 设备回路
/// 回调函数
public void SetAllHardFirmwareInfoToDevice(CommonDevice device, Action backAction = null)
{
if (device == null)
{
return;
}
lock (hsGetHardInfoDevice)
{
//先移除
this.RemoveDeviceHardInfoThread(device);
string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
this.hsGetHardInfoDevice.Add(mainkeys);
if (backAction != null)
{
//回调函数
this.dicDeviceHardInfoBackAction[mainkeys] = backAction;
}
//设置设备的硬件信息(需要等待推送后才会更改)
this.SetHardFirmwareInfoToDevice(device);
}
}
///
/// 设置设备的硬件信息(需要等待推送后才会更改)
///
///
private void SetHardFirmwareInfoToDevice(CommonDevice device)
{
if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceGetHardFirmwareInfo") == false)
{
//添加事件
HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceGetHardFirmwareInfo", ReceiveComandDiv.A设备属性上报, this.SetHardFirmwareInfoByInterfaceResult);
}
//发送命令
this.SetHardFirmwareInfoComand(device);
}
#endregion
#region ■ 发送命令___________________________
///
/// 发送获取硬件信息的命令
///
///
public void SetHardFirmwareInfoComand(CommonDevice device)
{
var jObject = new Newtonsoft.Json.Linq.JObject
{
{ "DeviceAddr",device.DeviceAddr },
{ "Epoint", device.DeviceEpoint },
{ "Cluster_ID", (int)Cluster_ID.Basic },
{ "Command", 108 }
};
var attriBute = new Newtonsoft.Json.Linq.JArray
{
new Newtonsoft.Json.Linq.JObject
{
{ "AttriButeId", 4}
},
new Newtonsoft.Json.Linq.JObject
{
{ "AttriButeId", 5}
},
new Newtonsoft.Json.Linq.JObject
{
{ "AttriButeId", 6}
},
new Newtonsoft.Json.Linq.JObject
{
{ "AttriButeId", 7}
},
new Newtonsoft.Json.Linq.JObject
{
{ "AttriButeId", 13}
}
};
var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
jObject.Add("Data", data);
device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
}
#endregion
#region ■ 设置属性___________________________
///
/// 设置设备的硬件信息 -1:异常 0:推送的这个东西并不是硬件信息 1:正常
///
/// 上报信息
/// 设备对象
///
public int SetHardFirmwareInfo(CommonDevice.DeviceStatusReportData report, CommonDevice device)
{
if (report == null)
{
return -1;
}
//属性是否改变
bool AttriButeChanged = false;
//是否是正确的推送数据
bool isRightData = false;
foreach (var data in report.AttriBute)
{
//生产商名字
if (data.AttributeId == 4)
{
isRightData = true;
if (data.AttriButeDataHex.Length > 2)
{
var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (device.ManufacturerName != value)
{
//属性变更了
AttriButeChanged = true;
}
device.ManufacturerName = value;
}
}
//型号码(也叫模块ID)
if (data.AttributeId == 5)
{
isRightData = true;
if (data.AttriButeDataHex.Length > 2)
{
var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (device.ModelIdentifier != value)
{
//属性变更了
AttriButeChanged = true;
}
device.ModelIdentifier = value;
}
}
//生产日期
if (data.AttributeId == 6)
{
isRightData = true;
if (data.AttriButeDataHex.Length > 2)
{
var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (device.ProductionDate != value)
{
//属性变更了
AttriButeChanged = true;
}
device.ProductionDate = value;
}
}
//电源
if (data.AttributeId == 7)
{
isRightData = true;
device.PowerSource = data.AttriButeData;
}
//序列号
if (data.AttributeId == 13)
{
isRightData = true;
if (data.AttriButeDataHex.Length > 2)
{
string value;
if (Common.LocalDevice.Current.IsHdlDevice(device) == false)
{
//第三方设备
value = data.AttriButeDataHex.Substring(2);
}
else
{
//河东设备
value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
}
if (device.SerialNumber != value)
{
//属性变更了
AttriButeChanged = true;
}
device.SerialNumber = value;
}
}
}
if (isRightData == false)
{
//这个不是硬件信息的推送
return 0;
}
//如果属性变更了
if (AttriButeChanged == true)
{
if (device.IsCustomizeImage == false)
{
//UI重新生成
device.IconPath = string.Empty;
device.ReSave();
}
}
return 1;
}
#endregion
#region ■ 接口推送处理_______________________
///
/// 根据接口推送的信息,设置设备硬件信息
///
///
private void SetHardFirmwareInfoByInterfaceResult(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.SetHardFirmwareInfo(device.DeviceStatusReport, localDevice) != 1)
{
return;
}
localDevice.ReSave();
this.hsGetHardInfoDevice.Remove(mainKeys);
if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true)
{
var action = this.dicDeviceHardInfoBackAction[mainKeys];
//调用回调函数
action?.Invoke(device, device.DeviceStatusReport);
//然后移除
this.dicDeviceHardInfoBackAction.Remove(mainKeys);
action = null;
}
}
}
#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);
}
if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true)
{
var action = this.dicDeviceHardInfoBackAction[mainKeys];
//然后移除
this.dicDeviceHardInfoBackAction.Remove(mainKeys);
action = null;
}
}
}
#endregion
}
}