using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 设备镜像的逻辑
|
/// </summary>
|
public class HdlDeviceImageInfoLogic
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 设备镜像的逻辑
|
/// </summary>
|
private static HdlDeviceImageInfoLogic m_Current = null;
|
/// <summary>
|
/// 设备镜像的逻辑
|
/// </summary>
|
public static HdlDeviceImageInfoLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new HdlDeviceImageInfoLogic();
|
}
|
return m_Current;
|
}
|
}
|
|
/// <summary>
|
/// OTA设备获取镜像后的回调函数
|
/// </summary>
|
private Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>> dicOtaBackAction = new Dictionary<string, Action<CommonDevice, CommonDevice.DeviceStatusReportData>>();
|
|
#endregion
|
|
#region ■ 主入口函数_________________________
|
|
/// <summary>
|
/// 设置设备全部的镜像信息
|
/// </summary>
|
/// <param name="tADevice"></param>
|
/// <param name="backAction"></param>
|
public void SetAllImageInfoToOtaDevice(OTADevice tADevice, Action<CommonDevice, CommonDevice.DeviceStatusReportData> backAction = null)
|
{
|
if (tADevice == null)
|
{
|
return;
|
}
|
lock (dicOtaBackAction)
|
{
|
//先移除
|
this.RemoveDeviceFirmwareVersionThread(tADevice);
|
|
if (backAction != null)
|
{
|
this.dicOtaBackAction[tADevice.DeviceAddr] = backAction;
|
}
|
//设置设备的固件版本号(需要等待推送后才会更改)
|
this.SetFirmwareVersionToOtaDevice(tADevice);
|
}
|
}
|
|
/// <summary>
|
/// 设置设备的固件版本号(需要等待推送后才会更改)
|
/// </summary>
|
/// <param name="tADevice"></param>
|
private void SetFirmwareVersionToOtaDevice(OTADevice tADevice)
|
{
|
if (HdlGatewayReceiveLogic.Current.IsEsixt("DeviceAutoGetFirmwareVersion") == false)
|
{
|
//添加事件
|
HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceAutoGetFirmwareVersion", ReceiveComandDiv.A设备属性上报, this.SetFirmwareVersionByInterfaceResult);
|
}
|
//发送命令
|
this.SetFirmwareVersionComand(tADevice);
|
}
|
|
#endregion
|
|
#region ■ 发送命令___________________________
|
|
/// <summary>
|
/// 发送获取固件版本信息的命令
|
/// </summary>
|
/// <param name="device"></param>
|
public void SetFirmwareVersionComand(OTADevice device)
|
{
|
var jObject = new Newtonsoft.Json.Linq.JObject
|
{
|
{ "DeviceAddr",device.DeviceAddr },
|
{ "Epoint", device.DeviceEpoint },
|
{ "Cluster_ID", (int)Cluster_ID.Ota },
|
{ "Command", 108 }
|
};
|
var attriBute = new Newtonsoft.Json.Linq.JArray
|
{
|
new Newtonsoft.Json.Linq.JObject
|
{
|
{ "AttriButeId", (int)AttriButeId.ImgVersion}
|
},
|
new Newtonsoft.Json.Linq.JObject
|
{
|
{ "AttriButeId", (int)AttriButeId.mgHWversion}
|
},
|
new Newtonsoft.Json.Linq.JObject
|
{
|
{ "AttriButeId", (int)AttriButeId.ImgTypeId}
|
}
|
};
|
var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
|
jObject.Add("Data", data);
|
device.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
|
}
|
|
#endregion
|
|
#region ■ 设置属性___________________________
|
|
/// <summary>
|
/// 设置设备的固件版本 -1:异常 0:推送的这个东西并不是固件信息 1:正常
|
/// </summary>
|
/// <param name="report">上报信息</param>
|
/// <param name="device">需要修改的设备对象</param>
|
/// <returns></returns>
|
public int SetFirmwareVersion(CommonDevice.DeviceStatusReportData report, CommonDevice device)
|
{
|
if (report == null)
|
{
|
return -1;
|
}
|
|
//是否是正确的推送数据
|
bool isRightData = false;
|
foreach (var data in report.AttriBute)
|
{
|
//镜像版本
|
if (data.AttributeId == (int)AttriButeId.ImgVersion)
|
{
|
isRightData = true;
|
device.ImgVersion = data.AttriButeData;
|
}
|
//硬件版本
|
if (data.AttributeId == (int)AttriButeId.mgHWversion)
|
{
|
isRightData = true;
|
device.HwVersion = data.AttriButeData;
|
}
|
//镜像ID
|
if (data.AttributeId == (int)AttriButeId.ImgTypeId)
|
{
|
isRightData = true;
|
device.ImgTypeId = data.AttriButeData;
|
}
|
}
|
if (isRightData == false)
|
{
|
//这个不是固件信息的推送
|
return 0;
|
}
|
return 1;
|
}
|
|
#endregion
|
|
#region ■ 接口推送处理_______________________
|
|
/// <summary>
|
/// 根据接口推送的信息,设置固件版本号
|
/// </summary>
|
/// <param name="device"></param>
|
private void SetFirmwareVersionByInterfaceResult(CommonDevice device)
|
{
|
var otaDevice = Common.LocalDevice.Current.GetOTADevice(device.DeviceAddr, device.DeviceEpoint);
|
if (otaDevice != null)
|
{
|
//设置固件版本信息
|
this.SetFirmwareVersion(device.DeviceStatusReport, otaDevice);
|
otaDevice.ReSave();
|
}
|
lock (dicOtaBackAction)
|
{
|
if (this.dicOtaBackAction.ContainsKey(device.DeviceAddr) == true)
|
{
|
var action = this.dicOtaBackAction[device.DeviceAddr];
|
//调用回调函数
|
action?.Invoke(device, device.DeviceStatusReport);
|
//然后移除
|
this.dicOtaBackAction.Remove(device.DeviceAddr);
|
action = null;
|
}
|
}
|
}
|
#endregion
|
|
#region ■ 移除监听线程_______________________
|
|
/// <summary>
|
/// 移除获取设备固件信息的监听线程
|
/// </summary>
|
/// <param name="device"></param>
|
public void RemoveDeviceFirmwareVersionThread(CommonDevice device)
|
{
|
lock (dicOtaBackAction)
|
{
|
if (this.dicOtaBackAction.ContainsKey(device.DeviceAddr) == true)
|
{
|
var action = this.dicOtaBackAction[device.DeviceAddr];
|
//然后移除
|
this.dicOtaBackAction.Remove(device.DeviceAddr);
|
action = null;
|
}
|
}
|
}
|
|
#endregion
|
}
|
}
|