using Shared.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 设备属性上报的逻辑类
|
/// </summary>
|
public class DeviceAttributeLogic : ZigBee.Common.IStatus
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 属性上报的逻辑
|
/// </summary>
|
private static DeviceAttributeLogic m_Current = null;
|
/// <summary>
|
/// 属性上报的逻辑
|
/// </summary>
|
public static DeviceAttributeLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new DeviceAttributeLogic();
|
}
|
return m_Current;
|
}
|
set
|
{
|
m_Current = value;
|
}
|
}
|
|
/// <summary>
|
/// 锁
|
/// </summary>
|
private object objLock = new object();
|
/// <summary>
|
/// 事件集合
|
/// </summary>
|
private Dictionary<string, Action<CommonDevice>> dicEvent = new Dictionary<string, Action<CommonDevice>>();
|
/// <summary>
|
/// 命令区分
|
/// </summary>
|
private Dictionary<string, string> dicCommandDiv = new Dictionary<string, string>();
|
/// <summary>
|
/// 安防的上报(这个东西有点特殊)
|
/// </summary>
|
private Dictionary<string, Action<CommonDevice, bool>> dicSafetyEvent = new Dictionary<string, Action<CommonDevice, bool>>();
|
|
#endregion
|
|
#region ■ 添加监听___________________________
|
|
/// <summary>
|
/// 添加监视安防设备报警的事件(不需要再执行任何操作)
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
/// <param name="action">回调函数,参数1:报警的设备。参数2:是否是安防设备报警</param>
|
public void AddSafetyAlarmEvent(string mainKeys, Action<CommonDevice, bool> action)
|
{
|
//以防万一,添加全局安防上报监听
|
this.AddSaveSafetyAlarmInfoEvent();
|
this.dicSafetyEvent[mainKeys] = action;
|
}
|
|
#endregion
|
|
#region ■ 移除监听___________________________
|
|
/// <summary>
|
/// 移除事件
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
public void RemoveEvent(string mainKeys)
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.ContainsKey(mainKeys) == true)
|
{
|
this.dicEvent.Remove(mainKeys);
|
this.dicCommandDiv.Remove(mainKeys);
|
}
|
if (this.dicSafetyEvent.ContainsKey(mainKeys) == true)
|
{
|
this.dicSafetyEvent.Remove(mainKeys);
|
}
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Remove(this);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 移除全部的事件
|
/// </summary>
|
public void RemoveAllEvent()
|
{
|
lock (objLock)
|
{
|
this.dicEvent.Clear();
|
this.dicCommandDiv.Clear();
|
}
|
}
|
|
#endregion
|
|
#region ■ 获取版本___________________________
|
|
/// <summary>
|
/// 添加读取设备固件版本的事件(之后调用SetVersionComand发送命令,最后用SetFirmwareVersion设置值)
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
/// <param name="action">回调函数</param>
|
public void AddFirmwareVersionEvent(string mainKeys, Action<CommonDevice> action)
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Add(this);
|
}
|
this.dicEvent[mainKeys] = action;
|
this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
|
}
|
}
|
|
/// <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());
|
}
|
|
/// <summary>
|
/// 设置设备的固件版本
|
/// </summary>
|
/// <param name="report">上报信息</param>
|
/// <param name="device">设备对象</param>
|
/// <returns></returns>
|
public bool SetFirmwareVersion(CommonDevice.DeviceStatusReportData report, CommonDevice device)
|
{
|
if (report == null)
|
{
|
return false;
|
}
|
foreach (var data in report.AttriBute)
|
{
|
//镜像版本
|
if (data.AttributeId == (int)AttriButeId.ImgVersion)
|
{
|
device.ImgTypeId = data.AttriButeData;
|
}
|
//硬件版本
|
if (data.AttributeId == (int)AttriButeId.mgHWversion)
|
{
|
device.HwVersion = data.AttriButeData;
|
}
|
//镜像版本
|
if (data.AttributeId == (int)AttriButeId.ImgTypeId)
|
{
|
device.ImgTypeId = data.AttriButeData;
|
}
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 获取硬件信息_______________________
|
|
/// <summary>
|
/// 添加获取硬件信息的事件(之后调用SetHardFirmwareInfoComand发送命令,最后用SetHardFirmwareInfo设置值)
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
/// <param name="action">回调函数</param>
|
public void AddHardFirmwareInfoEvent(string mainKeys, Action<CommonDevice> action)
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Add(this);
|
}
|
this.dicEvent[mainKeys] = action;
|
this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
|
}
|
}
|
|
/// <summary>
|
/// 发送获取硬件信息的命令
|
/// </summary>
|
/// <param name="device"></param>
|
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());
|
}
|
|
/// <summary>
|
/// 设置设备的硬件信息
|
/// </summary>
|
/// <param name="report">上报信息</param>
|
/// <param name="device">设备对象</param>
|
/// <returns></returns>
|
public bool SetHardFirmwareInfo(CommonDevice.DeviceStatusReportData report, CommonDevice device)
|
{
|
if (report == null)
|
{
|
return false;
|
}
|
foreach (var data in report.AttriBute)
|
{
|
//生产商名字
|
if (data.AttributeId == 4)
|
{
|
if (data.AttriButeDataHex.Length > 2)
|
{
|
device.ManufacturerName = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
|
}
|
}
|
//型号码(也叫模块ID)
|
if (data.AttributeId == 5)
|
{
|
if (data.AttriButeDataHex.Length > 2)
|
{
|
device.ModelIdentifier = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
|
}
|
}
|
//生产日期
|
if (data.AttributeId == 6)
|
{
|
if (data.AttriButeDataHex.Length > 2)
|
{
|
device.ProductionDate = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
|
}
|
}
|
//电源
|
if (data.AttributeId == 7)
|
{
|
device.PowerSource = data.AttriButeData;
|
}
|
//序列号
|
if (data.AttributeId == 13)
|
{
|
if (data.AttriButeDataHex.Length > 2)
|
{
|
device.SerialNumber = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
|
}
|
}
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 获取在线状态_______________________
|
|
/// <summary>
|
/// 添加读取设备在线状态的事件(之后调用SetOnlineComand发送命令,返回即在线)
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
/// <param name="action">回调函数</param>
|
public void AddReadOnlineEvent(string mainKeys, Action<CommonDevice> action)
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Add(this);
|
}
|
this.dicEvent[mainKeys] = action;
|
this.dicCommandDiv[mainKeys] = "DeviceStatusReport";
|
}
|
}
|
|
/// <summary>
|
/// 发送获取在线状态的命令
|
/// </summary>
|
/// <param name="device"></param>
|
public void SetOnlineComand(CommonDevice device)
|
{
|
device.ReadAttri(Cluster_ID.Identify, AttriButeId.Switch);
|
}
|
|
/// <summary>
|
/// 添加网关自动推送设备在线状态的事件(不需要发送命令)
|
/// </summary>
|
/// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
|
/// <param name="action">回调函数</param>
|
public void AddReceiveDeviceOnlinePushEvent(string mainKeys, Action<CommonDevice> action)
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Add(this);
|
}
|
this.dicEvent[mainKeys] = action;
|
this.dicCommandDiv[mainKeys] = "OnlineStatusChange";
|
}
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 是否存在指定的事件
|
/// </summary>
|
/// <param name="mainkeys"></param>
|
/// <returns></returns>
|
public bool IsEsixt(string mainkeys)
|
{
|
return this.dicCommandDiv.ContainsKey(mainkeys);
|
}
|
|
#endregion
|
|
#region ■ 保存安防信息_______________________
|
|
/// <summary>
|
/// 添加保存安防设备报警的事件(不需要再执行任何操作,并且永久存在)
|
/// </summary>
|
public void AddSaveSafetyAlarmInfoEvent()
|
{
|
lock (objLock)
|
{
|
if (this.dicEvent.Count == 0)
|
{
|
ZigBee.Device.ZbGateway.StatusList.Add(this);
|
}
|
|
if (this.dicEvent.ContainsKey("SaveSafetyAlarmInfo") == false)
|
{
|
this.dicEvent["SaveSafetyAlarmInfo"] = this.SaveSafetyAlarmInfo;
|
this.dicCommandDiv["SaveSafetyAlarmInfo"] = "IASInfoReport";
|
}
|
}
|
}
|
|
/// <summary>
|
/// 保存安防信息
|
/// </summary>
|
/// <param name="device"></param>
|
private void SaveSafetyAlarmInfo(CommonDevice device)
|
{
|
if (Common.LocalGateway.Current.IsGatewayExist(device.CurrentGateWayId) == true)
|
{
|
//保存安防报警信息到本地
|
bool result = Common.LocalSafeguard.Current.SaveSafeguardAlarmInfo(device);
|
foreach (var action in this.dicSafetyEvent.Values)
|
{
|
action(device, result);
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 实现接口___________________________
|
|
/// <summary>
|
/// 设备状态通知
|
/// </summary>
|
/// <param name="common"></param>
|
/// <param name="typeTag"></param>
|
public void DeviceInfoChange(CommonDevice common, string typeTag)
|
{
|
lock (objLock)
|
{
|
if (common == null || string.IsNullOrEmpty(common.DeviceAddr) == true)
|
{
|
//我也不知道这有没有可能
|
return;
|
}
|
var list = new List<Action<CommonDevice>>();
|
foreach (string keys in this.dicEvent.Keys)
|
{
|
if (this.dicCommandDiv[keys] != typeTag)
|
{
|
//命令区分不一致,则不调用回调函数
|
continue;
|
}
|
//命令区分一致时,则调用回调函数
|
list.Add(this.dicEvent[keys]);
|
}
|
//有可能在回调函数中移除了事件,导致报错,所以先收集,再调用
|
foreach (var action in list)
|
{
|
action(common);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 不使用
|
/// </summary>
|
/// <param name="common"></param>
|
public void Changed(CommonDevice common)
|
{
|
}
|
|
/// <summary>
|
/// 不使用
|
/// </summary>
|
public void ChangedILogicStatus(ZigBee.Device.Logic logic)
|
{
|
}
|
|
/// <summary>
|
/// 不使用
|
/// </summary>
|
public void ChangedISceneStatus(Scene scene)
|
{
|
}
|
#endregion
|
}
|
}
|