using Shared.Common; using System; using System.Collections.Generic; using System.Text; using ZigBee.Device; namespace Shared.Phone.UserCenter { /// /// 设备属性上报的逻辑类 /// public class DeviceAttributeLogic : ZigBee.Common.IStatus { #region ■ 变量声明___________________________ /// /// 属性上报的逻辑 /// private static DeviceAttributeLogic m_Current = null; /// /// 属性上报的逻辑 /// public static DeviceAttributeLogic Current { get { if (m_Current == null) { m_Current = new DeviceAttributeLogic(); } return m_Current; } set { m_Current = value; } } /// /// 锁 /// private object objLock = new object(); /// /// 事件集合 /// private Dictionary> dicEvent = new Dictionary>(); /// /// 命令区分 /// private Dictionary dicCommandDiv = new Dictionary(); /// /// 安防的上报(这个东西有点特殊) /// private Dictionary> dicSafetyEvent = new Dictionary>(); #endregion #region ■ 添加监听___________________________ /// /// 添加监视安防设备报警的事件(不需要再执行任何操作) /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) /// 回调函数,参数1:报警的设备。参数2:是否是安防设备报警 public void AddSafetyAlarmEvent(string mainKeys, Action action) { //以防万一,添加全局安防上报监听 this.AddSaveSafetyAlarmInfoEvent(); this.dicSafetyEvent[mainKeys] = action; } #endregion #region ■ 移除监听___________________________ /// /// 移除事件 /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) 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); } } } /// /// 移除全部的事件 /// public void RemoveAllEvent() { lock (objLock) { this.dicEvent.Clear(); this.dicCommandDiv.Clear(); } } #endregion #region ■ 获取版本___________________________ /// /// 添加读取设备固件版本的事件(之后调用SetVersionComand发送命令,最后用SetFirmwareVersion设置值) /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) /// 回调函数 public void AddFirmwareVersionEvent(string mainKeys, Action action) { lock (objLock) { if (this.dicEvent.Count == 0) { ZigBee.Device.ZbGateway.StatusList.Add(this); } this.dicEvent[mainKeys] = action; this.dicCommandDiv[mainKeys] = "DeviceStatusReport"; } } /// /// 发送获取固件版本信息的命令 /// /// 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()); } /// /// 设置设备的固件版本 /// /// 上报信息 /// 设备对象 /// 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 ■ 获取硬件信息_______________________ /// /// 添加获取硬件信息的事件(之后调用SetHardFirmwareInfoComand发送命令,最后用SetHardFirmwareInfo设置值) /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) /// 回调函数 public void AddHardFirmwareInfoEvent(string mainKeys, Action action) { lock (objLock) { if (this.dicEvent.Count == 0) { ZigBee.Device.ZbGateway.StatusList.Add(this); } this.dicEvent[mainKeys] = action; this.dicCommandDiv[mainKeys] = "DeviceStatusReport"; } } /// /// 发送获取硬件信息的命令 /// /// 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()); } /// /// 设置设备的硬件信息 /// /// 上报信息 /// 设备对象 /// 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 ■ 获取在线状态_______________________ /// /// 添加读取设备在线状态的事件(之后调用SetOnlineComand发送命令,返回即在线) /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) /// 回调函数 public void AddReadOnlineEvent(string mainKeys, Action action) { lock (objLock) { if (this.dicEvent.Count == 0) { ZigBee.Device.ZbGateway.StatusList.Add(this); } this.dicEvent[mainKeys] = action; this.dicCommandDiv[mainKeys] = "DeviceStatusReport"; } } /// /// 发送获取在线状态的命令 /// /// public void SetOnlineComand(CommonDevice device) { device.ReadAttri(Cluster_ID.Identify, AttriButeId.Switch); } /// /// 添加网关自动推送设备在线状态的事件(不需要发送命令) /// /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况) /// 回调函数 public void AddReceiveDeviceOnlinePushEvent(string mainKeys, Action action) { lock (objLock) { if (this.dicEvent.Count == 0) { ZigBee.Device.ZbGateway.StatusList.Add(this); } this.dicEvent[mainKeys] = action; this.dicCommandDiv[mainKeys] = "OnlineStatusChange"; } } #endregion #region ■ 一般方法___________________________ /// /// 是否存在指定的事件 /// /// /// public bool IsEsixt(string mainkeys) { return this.dicCommandDiv.ContainsKey(mainkeys); } #endregion #region ■ 保存安防信息_______________________ /// /// 添加保存安防设备报警的事件(不需要再执行任何操作,并且永久存在) /// 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"; } } } /// /// 保存安防信息 /// /// 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 ■ 实现接口___________________________ /// /// 设备状态通知 /// /// /// public void DeviceInfoChange(CommonDevice common, string typeTag) { lock (objLock) { if (common == null || string.IsNullOrEmpty(common.DeviceAddr) == true) { //我也不知道这有没有可能 return; } var list = new List>(); 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); } } } /// /// 不使用 /// /// public void Changed(CommonDevice common) { } /// /// 不使用 /// public void ChangedILogicStatus(ZigBee.Device.Logic logic) { } /// /// 不使用 /// public void ChangedISceneStatus(Scene scene) { } #endregion } }