using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace ZigBee.Device
{
[System.Serializable]
public class CommonDevice
{
[Newtonsoft.Json.JsonIgnore]
public DateTime LastDateTime = DateTime.MinValue;
///
/// 调试时打开打印信息,true:打印,false:不打印
///
/// Message.
/// If set to true flage.
public static void DebugPrintLog(string msg, bool flage = true)
{
#if DEBUG
if (flage == true)
{
//if (msg.Contains("DeviceStatusReport") == false)
//{
System.Console.WriteLine(msg + " " + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond);
//}
}
#endif
}
///
/// 等待从网关接收数据的时间
///
/// The wait receive data time.
public virtual int WaitReceiveDataTime
{
get
{
if (Device.ZbGateway.RemoteMqttClient != null && Device.ZbGateway.RemoteMqttClient.IsConnected)
{
return 10000;
}
else
{
return 3000;
}
}
}
[Newtonsoft.Json.JsonIgnore]
public bool IsValid
{
get
{
return (DateTime.Now - LastDateTime).TotalSeconds < 10;
}
}
///
/// 通过设备调用网关
///
[Newtonsoft.Json.JsonIgnore]
public ZbGateway Gateway
{
get
{
if (string.IsNullOrEmpty(CurrentGateWayId))
{
return null;
}
var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.getGatewayBaseInfo != null) && (obj.getGatewayBaseInfo.gwID == CurrentGateWayId));
if (gateWay == null)
{
gateWay = new ZbGateway { IsVirtual = true, };
gateWay.getGatewayBaseInfo.gwID = CurrentGateWayId;
gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
ZbGateway.GateWayList.Add(gateWay);
}
return gateWay;
}
}
///
/// 保存设备数据的文件名
///
[Newtonsoft.Json.JsonIgnore]
public virtual string FilePath
{
get
{
var deviceType = Type.ToString();
var fileName = "Device_" + deviceType + "_" + DeviceAddr;
fileName += "_" + (DeviceEpoint.ToString().Length < 2 ? "0" + DeviceEpoint.ToString() : DeviceEpoint.ToString());
return fileName;
}
}
///
/// 由设备路径恢复设备对象
///
/// The device by file path.
/// Device file path.
public static CommonDevice CommonDeviceByFilePath(string deviceFilePath)
{
var v = deviceFilePath.Split('_');
if (v.Length < 3)
{
return null;
}
return CommonDeviceByByteString(v[1], System.Text.Encoding.UTF8.GetString(Shared.Common.Global.ReadFileByHomeId(deviceFilePath)));
}
///
/// 由设备字符串比特恢复设备对象
///
/// 设备DeviceType的字符串类型
/// 设备Json文件转为比特后再转为的字符串
///
public static CommonDevice CommonDeviceByByteString(string strDeviceType, string strDeviceByte)
{
if (strDeviceType == ZigBee.Device.DeviceType.DimmableLight.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.OnOffOutput.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.WindowCoveringDevice.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.OnOffSwitch.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.IASZone.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.OtaDevice.ToString() || strDeviceType == ZigBee.Device.DeviceType.OtaPanelDevice.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.AirSwitch.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.Repeater.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.Thermostat.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.Transverter.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.DoorLock.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
else if (strDeviceType == ZigBee.Device.DeviceType.TemperatureSensor.ToString())
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(strDeviceByte);
}
return null;
}
///
/// 保存设备
///
public void Save()
{
if (Shared.Common.Global.IsExistsByHomeId(FilePath))
{
return;
}
ReSave();
}
///
/// 重新保存设备
///
public void ReSave()
{
if (IconPath == string.Empty)
{
//保存设备图标(这里会保存一次,下面就不用保存了)
this.SaveDeviceIcon();
return;
}
Shared.Common.Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
}
///
/// 保存设备图标
///
private void SaveDeviceIcon()
{
if (IconPath == string.Empty)
{
//干接点
if (this.Type == DeviceType.OnOffSwitch)
{
IconPath = "Device/DryContact.png";
}
else if (this.Type == DeviceType.ColorDimmableLight)
{
//彩灯
IconPath = "Device/ColorLight.png";
}
else if (this.Type == DeviceType.DimmableLight)
{
//调光器
IconPath = "Device/Light.png";
}
else if (this.Type == DeviceType.OnOffOutput)
{
//继电器
IconPath = "Device/RelayEpoint.png";
}
else if (this.Type == DeviceType.Thermostat)
{
//空调
IconPath = "Device/AirConditionerEpoint.png";
}
else if (this.Type == DeviceType.TemperatureSensor)
{
if (((TemperatureSensor)this).SensorDiv == 1)
{
//温度传感器
IconPath = "Device/SensorTemperature.png";
}
else if (((TemperatureSensor)this).SensorDiv == 2)
{
//湿度传感器
IconPath = "Device/SensorHumidity.png";
}
}
else if (this.Type != DeviceType.UnKown)
{
//其他的图标有点特殊
string unSelectPic = string.Empty;
string selectPic = string.Empty;
Shared.Common.LocalDevice.Current.GetDeviceObjectIcon(new List { this }, ref unSelectPic, ref selectPic);
IconPath = unSelectPic;
}
Shared.Common.Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
}
}
///
/// 是否是自定义图片
///
public bool IsCustomizeImage = false;
///
/// 设备图片
///
public string IconPath = string.Empty;
///
/// 设备图片--在线或者选中状态
///
/// The online icon path.
[Newtonsoft.Json.JsonIgnore]
public string OnlineIconPath
{
get
{
if (string.IsNullOrEmpty(IconPath))
{
return string.Empty;
}
var pathArr = IconPath.Split('.');
if (pathArr == null || string.IsNullOrEmpty(pathArr[0]))
{
return string.Empty;
}
return $"{pathArr[0]}Selected.png";
}
}
///
/// 当前网关的ID
///
public string CurrentGateWayId;
///
/// 当前设备类型
///
public DeviceType Type = DeviceType.UnKown;
///
/// 设备的功能类型(此类型目前只针对继电器回路有效,默认未指定)
///
public DeviceFunctionType DfunctionType = DeviceFunctionType.A未定义;
///
/// MAC地址
///
public string DeviceAddr;
///
/// 设备端口号
///
public int DeviceEpoint;
///
/// 设备命令格式:Mac+端口
///
/// The common device address epoint.
public string CommonDeviceAddrEpoint
{
get
{
return DeviceAddr + "_" + DeviceEpoint.ToString();
}
}
///
/// 网关反馈的时间戳
///
public int Time;
///
/// 网关回复的数据ID
///
public int DataID;
///
/// 设备id
/// 258:color dimmable light,调关灯
/// 10:Door lock,门锁
/// 514:Window covering device,窗帘
/// 515:Window covering controller,窗帘控制器
/// 769:Thermostat,恒温面板/空调
/// 770:Temperature Sensor,温度传感器
/// 775:Temperature Sensor,湿度传感器
/// 262:Light sensor,光照传感器
/// 1026:sensor,传感器,具体类型的传感器DeviceType来区分
///
public int DeviceID;
///
/// 该字段主要针对IAS安防设备设立。所有IAS安防设备共用一个DeviceID为1026。所以要区分子设备类型,需要该字段。
/// 瞬间数据上报的传感器 (MomentStatus=1 TriggerZoneStatus>=1<报警>)
/// 13:Motion sensor (运动传感器)
/// 40 Fire sensor 烟雾传感器
/// 42 Water sensor 水侵传感器
/// 43 Carbon Monoxide (CO) 燃气传感器
/// 44 Personal emergency device 紧急按钮
/// 277 Key fob 钥匙扣
/// 持续数据上报(MomentStatus=0 TriggerZoneStatus>=1<报警> TriggerZoneStatus=0<取消报警>)
/// 21: Door/Window 门窗传感器(有21和22,这里没有写错)
/// 22:Door/Window 门窗传感器(有21和22,这里没有写错
///
public int IasDeviceType;
///
/// 设备名称,以设备的MAC命名
///
public string DeviceName = "UnKown";
///
/// 设备端点名称,以设备端点名称命名
///
public string DeviceEpointName = "UnKown";
///
/// 用于判断设备的zigbee协议版本。
///49246:ZLL1.0标准设备。
///260: ZHA1.2标准设备、 Z3.0标准设备。
///41440:ZGP3.0标准设备。
///265:ZSE1.4标准设备。
///
public int Profile;
///
/// 0:设备不在线
/// 1:设备在线
///
public int IsOnline;
///
/// 当前运行程序版本信息。 最大64字节
///
public int ImgVersion;
///
/// 硬件版本
///
public int HwVersion;
///
/// 当前镜像类型id
///
public int ImgTypeId;
///
/// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
///
public int DriveCode;
///
/// 生产商名字
///
public string ManufacturerName = string.Empty;
///
/// 模块ID(这个东西也叫【型号码】)
///
public string ModelIdentifier = string.Empty;
///
/// 生产日期
///
public string ProductionDate = string.Empty;
///
/// 电源
///
public int PowerSource = -1;
///
/// 序列号
///
public string SerialNumber = string.Empty;
///
/// 所有指定cluster是否都已经成功绑定协调器
///0:未完全绑定
///1:已经绑定
///
public int ClusterBindZbSuccess = -1;
///
/// 是否获取所有默认绑定信息
///0:否
///1:是
///
public int IsGetAllDefaultBind = -1;
///
/// 输入簇列表
///
public List InClusterList = new List();
///
/// 输出簇列表
///
public List OutClusterList = new List();
///
/// 用于记录设备最新上报的属性状态信息。最大支持记录16个属性状态,且只记录属性值长度不大于4字节的数据。
///
public List AttributeStatus = new List();
///
/// 设备最新上报的属性状态信息
///
[System.Serializable]
public class AttributeStatusObj
{
///
/// 属性簇id
///
public int ClusterId;
///
/// 属性id
///
public int AttributeId;
///
/// 属性值,最大占用4个字节
///
public int AttributeData;
///
/// 属性状态最后更新的utc时间戳
///
public int ReportTime;
}
///
/// 输入簇
///
[System.Serializable]
public class InClusterObj
{
///
/// 设备支持的输入功能
/// 0:Basic,设备支持“基础属性”
/// 3:Identify,设备支持“识别功能”
/// 4:Groups,设备支持“组功能”
/// 5:Scenes,设备支持“场景功能”
/// 6:on/off,设备支持“开关功能”
/// 开关功能的设备如:调关灯/继电器/窗帘等。。。
/// 8:Level Control,设备支持“亮度调节功能”
/// 亮度调节功能的设备如:调关灯。。。
/// 257:Door Lock,设备支持“门锁功能”
/// 门锁功能的设备如:门锁。。。
/// 258:Window Covering,设备支持“窗帘控制功能”
/// 窗帘控制功能的设备如:窗帘/开合帘/卷帘。。。
/// 513:Thermostat,设备支持“恒温器功能”
/// 恒温器功能的设备如:空调。。。
/// 768:Color Control,设备支持“颜色调节功能”
/// 颜色调节功能的设备如:调光灯。。。
/// 1026:Temperature Measurement,设备支持“温度测量功能”
/// 温度测量功能的设备如:温度传感器。。。
/// 1029:Relative Humidity Measurement,设备支持“湿度测量功能”
/// 湿度测量功能的设备如:湿度传感器。。。
///
public int InCluster;
}
///
/// 输出簇
///
[System.Serializable]
public class OutClusterObj
{
///
/// 设备支持的输出功能
/// 0:Basic,设备支持“基础属性”
/// 3:Identify,设备支持“识别功能”
/// 4:Groups,设备支持“组功能”
/// 5:Scenes,设备支持“场景功能”
/// 6:on/off,设备支持“开关功能”
/// 开关功能的设备如:调关灯/继电器/窗帘等。。。
/// 8:Level Control,设备支持“亮度调节功能”
/// 亮度调节功能的设备如:调关灯。。。
/// 257:Door Lock,设备支持“门锁功能”
/// 门锁功能的设备如:门锁。。。
/// 258:Window Covering,设备支持“窗帘控制功能”
/// 窗帘控制功能的设备如:窗帘/开合帘/卷帘。。。
/// 513:Thermostat,设备支持“恒温器功能”
/// 恒温器功能的设备如:空调。。。
/// 768:Color Control,设备支持“颜色调节功能”
/// 颜色调节功能的设备如:调光灯。。。
/// 1026:Temperature Measurement,设备支持“温度测量功能”
/// 温度测量功能的设备如:温度传感器。。。
/// 1029:Relative Humidity Measurement,设备支持“湿度测量功能”
/// 湿度测量功能的设备如:湿度传感器。。。
///
public int OutCluster;
}
///
/// 支持的属性数据
///
[System.Serializable]
public class AttributeObj
{
///
/// 响应的属性ID
///
public int AttributeId;
///
/// 属性值的数据类型
///
public int AttributeType;
}
///
/// 设备上报的属性数据
///
[System.Serializable]
public class AttributeDataObj
{
///
/// 属性id
///
public int AttributeId;
///
/// 要报告属性的数据类型
///
public int AttriButeDataType;
///
/// AttriButeData占用的字节数
///
public int AttriButeDataLength;
///
/// 属性值
///
public int AttriButeData = 999;
///
/// 属性数据16进制转字符
///
public string AttriButeDataHex;
}
///
/// Command数组
///
[System.Serializable]
public class CommandObj
{
///
/// 支持的命令id
///
public int commandId;
}
#region 二、获取已入网设备信息
///
/// 网关中的设备信息
///
public DeviceInfoData DeviceInfo = new DeviceInfoData();
///
/// 网关中的设备信息
///
[System.Serializable]
public class DeviceInfoData
{
///
/// 入网设备总数。等于0时,表示没有设备信息,下面字段将不存在。
///
public int TotalNum;
///
/// 标识当前设备是发送的是第几个设备。DeviceNum从1开始每发送一个设备信息,下一个设备信息的DeviceNum将加1。直到DeviceNum等于TotalNum说明所有设备信息发送完毕。
///
public int DeviceNum;
///
/// 入网的utc时间戳
///
public int JoinTime;
///
/// 1:路由器设备
/// 2:终端设备
///
public int ZigbeeType;
///
/// 设备网络地址
///
public int NwkAddr;
///
/// 该字段主要针对IAS安防设备设立。所有IAS安防设备共用一个DeviceID为1026。所以要区分子设备类型,需要该字段。
/// 13:Motion sensor (运动传感器)
/// 43:Carbon Monoxide sensor (一氧化碳传感器)
/// 44:Personal emergency device (紧急按钮)
///
public int DeviceType;
///
/// 用于判断设备的zigbee协议版本。
///49246:ZLL1.0标准设备。
///260: ZHA1.2标准设备、 Z3.0标准设备。
///41440:ZGP3.0标准设备。
///265:ZSE1.4标准设备。
///
public int Profile;
///
/// 设备mac名称
///
public string MacName;
///
/// 设备名(Mac+断点命名的)
///
public string DeviceName;
///
/// 0:设备不在线
/// 1:设备在线
///
public int IsOnline;
///
/// 当前运行程序版本信息。 最大64字节
///
public int ImgVersion;
///
/// 硬件版本
///
public int HwVersion;
///
/// 当前镜像类型id
///
public int ImgTypeId;
///
/// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
///
public int DriveCode;
///
/// 厂商名称
///
public string ManufacturerName = string.Empty;
///
/// 模块ID
///
public string ModelIdentifier = string.Empty;
///
/// 好像是序列号
///
public string ProductCode = string.Empty;
///
/// 输入簇列表
///
public List InClusterList = new List();
///
/// 输出簇列表
///
public List OutClusterList = new List();
///
/// 用于记录设备最新上报的属性状态信息。最大支持记录16个属性状态,且只记录属性值长度不大于4字节的数据。
///
public List AttributeStatus = new List();
}
///
/// 获取所有网关的节点设备信息(用于主网关)
///
public AllGatewayDeviceInfo getAllGatewayDeviceInfo;
///
/// 获取所有网关的节点设备信息(用于主网关)
///
[System.Serializable]
public class AllGatewayDeviceInfo
{
///
/// 入网设备总数。等于0时,表示没有设备信息,下面字段将不存在。
///
public int TotalNum;
///
/// 标识当前设备是发送的是第几个设备。DeviceNum从1开始每发送一个设备信息,下一个设备信息的DeviceNum将加1。直到DeviceNum等于TotalNum说明所有设备信息发送完毕。
///
public int DeviceNum;
///
/// 设备所在网关的网关id
///
public string GwId;
///
/// 入网的utc时间戳
///
public int JoinTime;
///
/// 1:路由器设备
/// 2:终端设备
///
public int ZigbeeType;
///
/// 设备网络地址
///
public int NwkAddr;
///
/// 该字段主要针对IAS安防设备设立。所有IAS安防设备共用一个DeviceID为1026。所以要区分子设备类型,需要该字段。
/// 13:Motion sensor (运动传感器)
/// 43:Carbon Monoxide sensor (一氧化碳传感器)
/// 44:Personal emergency device (紧急按钮)
///
public int DeviceType;
///
/// 用于判断设备的zigbee协议版本。
/// 49246:ZLL1.0标准设备。
/// 260: ZHA1.2标准设备、 Z3.0标准设备。
/// 41440:ZGP3.0标准设备。
/// 265:ZSE1.4标准设备。
///
public int Profile;
///
/// 设备端点名
///
public string DeviceName;
///
/// 设备名
///
public string MacName;
///
/// 0:设备不在线
/// 1:设备在线
///
public int IsOnline;
///
/// 当前运行程序版本信息。 最大64字节
///
public int ImgVersion;
///
/// 硬件版本
///
public int HwVersion;
///
/// 当前镜像类型id
///
public int ImgTypeId;
///
/// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
///
public int DriveCode;
///
/// 输入簇列表
///
public List InClusterList = new List();
///
/// 输出簇列表
///
public List OutClusterList = new List();
///
/// 用于记录设备最新上报的属性状态信息。最大支持记录16个属性状态,且只记录属性值长度不大于4字节的数据。
///
public List AttributeStatus = new List();
}
///
/// 获取网关记录的设备属性状态
///
public GetStatusRecordAllInfo getStatusRecordAllInfo;
///
/// 获取网关记录的设备属性状态
///
[System.Serializable]
public class GetStatusRecordAllInfo
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 设备名称修改
///
public GetStatusRecordInfo getStatusRecordInfo;
}
///
/// 获取所有网关的节点设备信息(用于主网关)
///
public GetStatusRecordInfo getStatusRecordInfo;
///
/// 获取所有网关的节点设备信息(用于主网关)
///
[System.Serializable]
public class GetStatusRecordInfo
{
///
/// 设备所在网关的网关id
///
public string GwId;
///
/// 入网的utc时间戳
///
public int JoinTime;
///
/// 1:路由器设备
/// 2:终端设备
///
public int ZigbeeType;
///
/// 设备网络地址
///
public int NwkAddr;
///
/// 该字段主要针对IAS安防设备设立。所有IAS安防设备共用一个DeviceID为1026。所以要区分子设备类型,需要该字段。
/// 13:Motion sensor (运动传感器)
/// 43:Carbon Monoxide sensor (一氧化碳传感器)
/// 44:Personal emergency device (紧急按钮)
///
public int DeviceType;
///
/// 用于判断设备的zigbee协议版本。
/// 49246:ZLL1.0标准设备。
/// 260: ZHA1.2标准设备、 Z3.0标准设备。
/// 41440:ZGP3.0标准设备。
/// 265:ZSE1.4标准设备。
///
public int Profile;
///
/// 设备端点名
///
public string DeviceName;
///
/// 设备名
///
public string MacName;
///
/// 0:设备不在线
/// 1:设备在线
///
public int IsOnline;
///
/// 当前运行程序版本信息。 最大64字节
///
public int ImgVersion;
///
/// 硬件版本
///
public int HwVersion;
///
/// 当前镜像类型id
///
public int ImgTypeId;
///
/// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
///
public int DriveCode;
///
/// 输入簇列表
///
public List InClusterList = new List();
///
/// 输出簇列表
///
public List OutClusterList = new List();
///
/// 用于记录设备最新上报的属性状态信息。最大支持记录16个属性状态,且只记录属性值长度不大于4字节的数据。
///
public List AttributeStatus = new List();
}
#endregion
///
/// 有新设备加入zigbee网络的信息
///
public DeviceDeviceJoinZbNetResponData deviceDeviceJoinZbNetResponData;
///
/// 有新设备加入zigbee网络的信息
///
[System.Serializable]
public class DeviceDeviceJoinZbNetResponData
{
///
/// 设备网络地址
///
public int NwkAddr;
///
/// 1:路由器设备
/// 2:终端设备
///
public int ZigbeeType;
}
///
/// 获取新设备所有端点信息是否成功信息
///
public DeviceIsGetEpointInfoResponData deviceIsGetEpointInfoResponData;
///
/// 获取新设备所有端点信息是否成功信息
///
[System.Serializable]
public class DeviceIsGetEpointInfoResponData
{
///
/// 0:成功获取所有端点信息
///1:获取失败
///
public int Result;
}
///
/// 网关中新搜索出的设备信息
///
public NewDeviceInfoData getNewDeviceInfo;
///
/// 网关中新搜索出的设备信息
///
[System.Serializable]
public class NewDeviceInfoData
{
///
/// 入网的utc时间戳
///
public int JoinTime;
///
/// 1:路由器设备
/// 2:终端设备
///
public int ZigbeeType;
///
/// 设备网络地址
///
public int NwkAddr;
///
/// 该字段主要针对IAS安防设备设立。所有IAS安防设备共用一个DeviceID为1026。所以要区分子设备类型,需要该字段。
/// 13:Motion sensor (运动传感器)
/// 43:Carbon Monoxide sensor (一氧化碳传感器)
/// 44:Personal emergency device (紧急按钮)
///
public int DeviceType;
///
/// 用于判断设备的zigbee协议版本。区分3.0设备和ZHA设备
/// 49246:ZLL1.0标准设备。
/// 260: ZHA1.2标准设备、 Z3.0标准设备。
/// 41440:ZGP3.0标准设备。
/// 265:ZSE1.4标准设备。
///
public int Profile;
///
/// 是否是新入网设备。
/// 如果网关储存的设备列表中原来是没有该设备则为新入网设备。
/// 如果网关储存的设备列表中有该设备则为旧设备。
/// 如果重入网后设备信息已经改变(如设备的网络地址,设备ID,cluster列表)则也视为新设备入网。
/// 该字段用来判别用户可能通过节点的实体按键将设备恢复出厂设备后节点设备重新入网的情况或节点设备重启主动发送入网信息的情况。
/// 0:旧设备入网
/// 1:新设备入网
/// 2:设备为新设备,并在上报该信息前已经退网,即设备入网后网关还来不及上报该设备信息设备便已经退网。(设备入网,到网关上报设备信息有一段延时,如果在此期间如果设备已经退网,将反馈该值。该值为异常情况,当收到该值时候说明设备并没入网,可丢弃这个入网信息)
///
public int IsNewDev;
///
/// 当前运行程序版本信息。 最大64字节
///
public int ImgVersion;
///
/// 硬件版本
///
public int HwVersion;
///
/// 当前镜像类型id
///
public int ImgTypeId;
///
/// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
///
public int DriveCode;
///
/// 设备所在网关的网关id
///
public string GwId;
///
/// 设备名
///
public string MacName;
///
/// 设备端点名
///
public string DeviceName;
///
/// 0:设备不在线
/// 1:设备在线
///
public int IsOnline;
///
/// 所有指定cluster是否都已经成功绑定协调器
///0:未完全绑定
///1:已经绑定
///
public int ClusterBindZbSuccess = -1;
///
/// 是否获取所有默认绑定信息
///0:否
///1:是
///
public int IsGetAllDefaultBind = -1;
///
/// 生产商名字
///
public string ManufacturerName = string.Empty;
///
/// 模块ID(这个东西也叫【型号码】)
///
public string ModelIdentifier = string.Empty;
///
/// 生产日期
///
public string ProductionDate = string.Empty;
///
/// 电源
///
public int PowerSource = -1;
///
/// 序列号
///
public string SerialNumber = string.Empty;
///
/// 输入簇列表
///
public List InClusterList = new List();
///
/// 输出簇列表
///
public List OutClusterList = new List();
///
/// 用于记录设备最新上报的属性状态信息。最大支持记录16个属性状态,且只记录属性值长度不大于4字节的数据。
///
public List AttributeStatus = new List();
}
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
[System.Serializable]
public class ErrorResponData
{
///
/// Error参数含义
///1:网关无法解析命令数据。
///2:协调器正在升级或备份/恢复数据
///3:操作设备/组/场景不存在
///4:其他错误
///5:数据传输错误(在某次客户端向网关发送数据的过程中,网关在合理时间范围内接收客户端数据不完整导致该错误发生。如客户端向网关一次发送100个字节的数据,但网关等待接收了一秒只接收了80个字节。发生该错误,网关将主动关闭客户端连接)
///
public int Error;
}
///
/// 网关错误信息具体内容
///
public static string ErrorMess(int err)
{
string message = "";
switch (err)
{
case 1:
message = " 网关无法解析命令数据。";
break;
case 2:
message = " 协调器正在升级或备份 / 恢复数据。";
break;
case 3:
message = "操作设备 / 组 / 场景不存在";
break;
case 4:
message = " 其他错误";
break;
case 5:
message = " 数据传输错误(在某次客户端向网关发送数据的过程中,网关在合理时间范围内接收客户端数据不完整导致该错误发生。如客户端向网关一次发送100个字节的数据,但网关等待接收了一秒只接收了 个字节。发生该错误,网关将主动关闭客户端连接)";
break;
default:
break;
}
return message;
}
#region 修改设备端口名称
///
/// 修改设备端口(按键)名称
/// Gateway:设备所属网关(调用方法:device.Gateway)
/// deviceName:按键名称
///
public async System.Threading.Tasks.Task RenameDeviceNameAsync(string deviceAddr, int deviceEpoint, string deviceName)
{
if (Gateway == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
DeviceRenameAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new DeviceRenameAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "DeviceRenameRespon")
{
var deviceID = jobject.Value("Device_ID");
switch ((DeviceType)(deviceID))
{
case DeviceType.OnOffOutput:
var toggleLight = new ToggleLight() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
toggleLight.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (toggleLight.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = toggleLight.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var infoToggleLight = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == toggleLight.DeviceID && obj.DeviceAddr == toggleLight.DeviceAddr && obj.DeviceEpoint == toggleLight.DeviceEpoint);
if (infoToggleLight != null)
{
infoToggleLight.DeviceEpointName = toggleLight.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.AirSwitch:
var airSwitch = new AirSwitch() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
airSwitch.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (airSwitch.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = airSwitch.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var infoAirSwitch = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == airSwitch.DeviceID && obj.DeviceAddr == airSwitch.DeviceAddr && obj.DeviceEpoint == airSwitch.DeviceEpoint);
if (infoAirSwitch != null)
{
infoAirSwitch.DeviceEpointName = airSwitch.renameDeviceData.DeviceName;
//infoAirSwitch.ReSave();
}
}
break;
case DeviceType.OnOffSwitch:
var panelObj = new Panel() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
panelObj.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (panelObj.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = panelObj.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var infoPanel = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == panelObj.DeviceID && obj.DeviceAddr == panelObj.DeviceAddr && obj.DeviceEpoint == panelObj.DeviceEpoint);
if (infoPanel != null)
{
infoPanel.DeviceEpointName = panelObj.renameDeviceData.DeviceName;
//infoPanel.ReSave();
}
}
break;
case DeviceType.WindowCoveringDevice:
var windowCovering = new Rollershade() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
windowCovering.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (windowCovering.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = windowCovering.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{topic}");
var wc = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == windowCovering.DeviceID && obj.DeviceAddr == windowCovering.DeviceAddr && obj.DeviceEpoint == windowCovering.DeviceEpoint);
if (wc != null)
{
wc.DeviceEpointName = windowCovering.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.IASZone:
var ias = new IASZone() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
ias.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ias.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = ias.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{topic}");
var zone = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ias.DeviceID && obj.DeviceAddr == ias.DeviceAddr && obj.DeviceEpoint == ias.DeviceEpoint);
if (zone != null)
{
zone.DeviceEpointName = ias.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.DimmableLight:
var dimmableLight = new DimmableLight() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
dimmableLight.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (dimmableLight.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = dimmableLight.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == dimmableLight.DeviceID && obj.DeviceAddr == dimmableLight.DeviceAddr && obj.DeviceEpoint == dimmableLight.DeviceEpoint);
if (info != null)
{
info.DeviceEpointName = dimmableLight.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.Repeater:
var repeater = new Repeater() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
repeater.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (repeater.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = repeater.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == repeater.DeviceID && obj.DeviceAddr == repeater.DeviceAddr && obj.DeviceEpoint == repeater.DeviceEpoint);
if (info != null)
{
info.DeviceEpointName = repeater.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.Thermostat:
var ac = new AC() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
ac.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ac.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = ac.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ac.DeviceID && obj.DeviceAddr == ac.DeviceAddr && obj.DeviceEpoint == ac.DeviceEpoint);
if (info != null)
{
info.DeviceEpointName = ac.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.Transverter:
var transverter = new Transverter() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
transverter.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (transverter.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = transverter.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == transverter.DeviceID && obj.DeviceAddr == transverter.DeviceAddr && obj.DeviceEpoint == transverter.DeviceEpoint);
if (info != null)
{
info.DeviceEpointName = transverter.renameDeviceData.DeviceName;
}
}
break;
case DeviceType.DoorLock:
var doorLock = new DoorLock() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
doorLock.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (doorLock.renameDeviceData == null)
{
d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new DeviceRenameAllData { deviceRenameData = doorLock.renameDeviceData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
if (info != null)
{
info.DeviceEpointName = doorLock.renameDeviceData.DeviceName;
}
}
break;
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("DeviceRename_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var bytes = new byte[64];
var reamarkGwBytes = Encoding.UTF8.GetBytes(deviceName);
System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
deviceName = Encoding.UTF8.GetString(bytes);
var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 96 } };
var data = new JObject { { "DeviceName", deviceName } };
jObject.Add("Data", data);
Gateway?.Send(("DeviceRename"), jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (d != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
d = new DeviceRenameAllData { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("DeviceRename_Actions退出" + System.DateTime.Now.ToString());
return d;
});
}
///
/// 重命名设备,网关反馈具体信息
///
public DeviceRenameAllData renameDeviceAllData;
///
/// 重命名设备,网关反馈具体信息
///
[System.Serializable]
public class DeviceRenameAllData
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 设备名称修改
///
public DeviceRenameResponseData deviceRenameData;
}
///
/// 设备名称修改
///
public DeviceRenameResponseData renameDeviceData;
///
/// 设备名称修改
///
[System.Serializable]
public class DeviceRenameResponseData
{
///
/// 0:修改成功
/// 1:修改失败
///
public int Result;
///
/// 修改后的设备名称
///
public string DeviceName;
}
#endregion
#region 修改设备mac名称
///
/// 修改设备mac名称
/// macName:设备名称
///
public async System.Threading.Tasks.Task RenameDeviceMacNameAsync(string deviceAddr, int deviceEpoint, string macName)
{
if (Gateway == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
RenameDeviceMacNameAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new RenameDeviceMacNameAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "MacRename_Respon")
{
var deviceID = jobject.Value("Device_ID");
switch ((DeviceType)(deviceID))
{
case DeviceType.OnOffOutput:
var toggleLight = new ToggleLight() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
toggleLight.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (toggleLight.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = toggleLight.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var infoToggleLight = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == toggleLight.DeviceID && obj.DeviceAddr == toggleLight.DeviceAddr && obj.DeviceEpoint == toggleLight.DeviceEpoint);
if (infoToggleLight != null)
{
infoToggleLight.DeviceName = toggleLight.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.AirSwitch:
var airSwitch = new AirSwitch() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
airSwitch.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (airSwitch.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = airSwitch.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
var infoAirSwitch = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == airSwitch.DeviceID && obj.DeviceAddr == airSwitch.DeviceAddr && obj.DeviceEpoint == airSwitch.DeviceEpoint);
if (infoAirSwitch != null)
{
infoAirSwitch.DeviceName = airSwitch.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.OnOffSwitch:
var panelObj = new Panel() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
panelObj.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (panelObj.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = panelObj.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{topic}");
var infoPanel = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == panelObj.DeviceID && obj.DeviceAddr == panelObj.DeviceAddr && obj.DeviceEpoint == panelObj.DeviceEpoint);
if (infoPanel != null)
{
infoPanel.DeviceName = panelObj.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.WindowCoveringDevice:
var windowCovering = new Rollershade() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
windowCovering.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (windowCovering.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = windowCovering.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var wc = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == windowCovering.DeviceID && obj.DeviceAddr == windowCovering.DeviceAddr && obj.DeviceEpoint == windowCovering.DeviceEpoint);
if (wc != null)
{
wc.DeviceName = windowCovering.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.IASZone:
var ias = new IASZone() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
ias.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ias.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = ias.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ias.DeviceID && obj.DeviceAddr == ias.DeviceAddr && obj.DeviceEpoint == ias.DeviceEpoint);
if (info != null)
{
info.DeviceName = ias.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.DimmableLight:
var dimmableLight = new DimmableLight() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
dimmableLight.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (dimmableLight.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = dimmableLight.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == dimmableLight.DeviceID && obj.DeviceAddr == dimmableLight.DeviceAddr && obj.DeviceEpoint == dimmableLight.DeviceEpoint);
if (info != null)
{
info.DeviceName = dimmableLight.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.Repeater:
var repeater = new DimmableLight() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
repeater.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (repeater.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = repeater.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == repeater.DeviceID && obj.DeviceAddr == repeater.DeviceAddr && obj.DeviceEpoint == repeater.DeviceEpoint);
if (info != null)
{
info.DeviceName = repeater.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.Thermostat:
var ac = new AC() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
ac.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ac.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = ac.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ac.DeviceID && obj.DeviceAddr == ac.DeviceAddr && obj.DeviceEpoint == ac.DeviceEpoint);
if (info != null)
{
info.DeviceName = ac.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.Transverter:
var transverter = new Transverter() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
transverter.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (transverter.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = transverter.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == transverter.DeviceID && obj.DeviceAddr == transverter.DeviceAddr && obj.DeviceEpoint == transverter.DeviceEpoint);
if (info != null)
{
info.DeviceName = transverter.renameDeviceMacNameData.MacName;
}
}
break;
case DeviceType.DoorLock:
var doorLock = new DoorLock() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
doorLock.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (doorLock.renameDeviceMacNameData == null)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = doorLock.renameDeviceMacNameData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
if (info != null)
{
info.DeviceName = doorLock.renameDeviceMacNameData.MacName;
}
}
break;
//case DeviceType.TemperatureSensor:
// var temperatureSensor = new TemperatureSensor() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
// temperatureSensor.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// if (temperatureSensor.renameDeviceData == null)
// {
// d.errorMessageBase = "网关返回的数据为空";
// }
// else
// {
// d.deviceRenameData = temperatureSensor.renameDeviceData;
// }
// var ts = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == temperatureSensor.DeviceID && obj.DeviceEpoint == temperatureSensor.DeviceEpoint && obj.DeviceEpoint == temperatureSensor.DeviceEpoint);
// if (ts == null)
// {
// ts.DeviceName = temperatureSensor.renameDeviceData.DeviceName;
// IO.LocalFileUtils.SaveDeviceInfo(ts, ts.DeviceEpoint.ToString());
// }
// break;
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("MacRename_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var bytes = new byte[64];
var reamarkGwBytes = Encoding.UTF8.GetBytes(macName);
System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
macName = Encoding.UTF8.GetString(bytes);
var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 100 } };
var data = new JObject { { "MacName", macName } };
jObject.Add("Data", data);
Gateway?.Send(("MacRename"), jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (d != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
d = new RenameDeviceMacNameAllData { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("MacRename_Action退出" + System.DateTime.Now.ToString());
return d;
});
}
///
/// 修改设备mac名称数据,网关反馈具体信息
///
public RenameDeviceMacNameAllData renameDeviceMacNameAllData;
///
/// 修改设备mac名称数据,网关反馈具体信息
///
[System.Serializable]
public class RenameDeviceMacNameAllData
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 修改设备mac名称数据
///
public RenameDeviceMacNameData renameDeviceMacNameData;
}
///
/// 修改设备mac名称数据
///
public RenameDeviceMacNameData renameDeviceMacNameData;
///
/// 修改设备mac名称数据
///
[System.Serializable]
public class RenameDeviceMacNameData
{
///
/// 0:修改成功
/// 1:修改失败,mac不存在
///
public int Result;
///
/// 修改后的设备名称
///
public string MacName;
}
#endregion
#region 设备恢复出厂设置与出网
///
/// 使设备恢复出厂设置
/// 仅恢复出厂设置,不离网。但有些不标准的3.0设备,恢复出厂设置就会离网。 客户端或云端到网关
///
public async void ResetDeviceFactoryAsync(string deviceAddr, int deviceEpoint)
{
if (Gateway == null)
{
return;
}
//Action action = (topic, message) => { };
//Gateway.Actions += action;
System.Console.WriteLine("FactoryResete_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 97 } };
Gateway.Send("FactoryReset", jObject.ToString());
}
catch { }
//Gateway.Actions -= action;
System.Console.WriteLine("FactoryReset_Action退出" + System.DateTime.Now.ToString());
}
#endregion
#region 删除设备(使设备离网)
///
/// 删除设备(使设备离网)
///
/// The device async.
/// Remove data.
public async System.Threading.Tasks.Task DeleteDeviceAsync(ZigBee.Device.CommonDevice.RemoveDeviceData removeData)
{
if (Gateway == null || removeData == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
RemoveDeviceResponseAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new RemoveDeviceResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new RemoveDeviceResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "RemoveDeviceRespon")
{
var gatewayTemp = new ZbGateway() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
gatewayTemp.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (gatewayTemp.removeDeviceResponseData == null)
{
d = new RemoveDeviceResponseAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RemoveDeviceResponseAllData { removeDeviceResponseData = gatewayTemp.removeDeviceResponseData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
try
{
if (gatewayTemp.removeDeviceResponseData.Result == 0)
{
foreach (var delD in gatewayTemp.removeDeviceResponseData.DeviceList)
{
var deviceID = delD.Device_ID;
switch ((DeviceType)(deviceID))
{
case DeviceType.OnOffOutput:
var toggleLight = new ToggleLight() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
toggleLight.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (toggleLight.removeDeviceResponseData == null)
{
return;
}
else
{
if (toggleLight.removeDeviceResponseData.Result == 0)
{
var infoToggleLight = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == toggleLight.DeviceID && obj.DeviceAddr == toggleLight.DeviceAddr && obj.DeviceEpoint == toggleLight.DeviceEpoint);
if (infoToggleLight != null)
{
Gateway.DeviceList.Remove(infoToggleLight);
}
}
}
break;
case DeviceType.AirSwitch:
var airSwitch = new AirSwitch() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
airSwitch.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (airSwitch.removeDeviceResponseData == null)
{
return;
}
else
{
if (airSwitch.removeDeviceResponseData.Result == 0)
{
var infoAirSwitch = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == airSwitch.DeviceID && obj.DeviceAddr == airSwitch.DeviceAddr && obj.DeviceEpoint == airSwitch.DeviceEpoint);
if (infoAirSwitch != null)
{
Gateway.DeviceList.Remove(infoAirSwitch);
}
}
}
break;
case DeviceType.OnOffSwitch:
var panelObj = new Panel() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
panelObj.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (panelObj.removeDeviceResponseData == null)
{
return;
}
else
{
if (panelObj.removeDeviceResponseData.Result == 0)
{
var infoPanel = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == panelObj.DeviceID && obj.DeviceAddr == panelObj.DeviceAddr && obj.DeviceEpoint == panelObj.DeviceEpoint);
if (infoPanel != null)
{
Gateway.DeviceList.Remove(infoPanel);
}
}
}
break;
case DeviceType.WindowCoveringDevice:
var rollershade = new Rollershade() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
rollershade.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (rollershade.removeDeviceResponseData == null)
{
return;
}
else
{
if (rollershade.removeDeviceResponseData.Result == 0)
{
var infoRoller = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == rollershade.DeviceID && obj.DeviceAddr == rollershade.DeviceAddr && obj.DeviceEpoint == rollershade.DeviceEpoint);
if (infoRoller != null)
{
Gateway.DeviceList.Remove(infoRoller);
}
}
}
break;
case DeviceType.IASZone:
var ias = new IASZone() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
ias.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ias.removeDeviceResponseData == null)
{
return;
}
else
{
if (ias.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ias.DeviceID && obj.DeviceAddr == ias.DeviceAddr && obj.DeviceEpoint == ias.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
case DeviceType.DimmableLight:
var dimmableLight = new DimmableLight() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
dimmableLight.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (dimmableLight.removeDeviceResponseData == null)
{
return;
}
else
{
if (dimmableLight.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == dimmableLight.DeviceID && obj.DeviceAddr == dimmableLight.DeviceAddr && obj.DeviceEpoint == dimmableLight.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
case DeviceType.Repeater:
var repeater = new Repeater() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
repeater.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (repeater.removeDeviceResponseData == null)
{
return;
}
else
{
if (repeater.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == repeater.DeviceID && obj.DeviceAddr == repeater.DeviceAddr && obj.DeviceEpoint == repeater.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
case DeviceType.Thermostat:
var ac = new AC() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
ac.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (ac.removeDeviceResponseData == null)
{
return;
}
else
{
if (ac.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == ac.DeviceID && obj.DeviceAddr == ac.DeviceAddr && obj.DeviceEpoint == ac.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
case DeviceType.Transverter:
var transverter = new Transverter() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
transverter.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (transverter.removeDeviceResponseData == null)
{
return;
}
else
{
if (transverter.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == transverter.DeviceID && obj.DeviceAddr == transverter.DeviceAddr && obj.DeviceEpoint == transverter.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
case DeviceType.DoorLock:
var doorLock = new DoorLock() { DeviceAddr = jobject.Value("DeviceAddr"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
doorLock.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (doorLock.removeDeviceResponseData == null)
{
return;
}
else
{
if (doorLock.removeDeviceResponseData.Result == 0)
{
var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
if (info != null)
{
Gateway.DeviceList.Remove(info);
}
}
}
break;
}
}
}
}
catch { }
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("RemoveDevice_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
if (removeData != null)
{
var jObject = new JObject { { "Cluster_ID", 0 }, { "Command", 99 } };
var deviceAddrList = new JArray { };
foreach (var a in removeData.DeviceAddrList)
{
var dd = new JObject { { "DeviceAddr", a.DeviceAddr } };
deviceAddrList.Add(dd);
}
var data = new JObject {
{ "CompelClear", removeData.CompelClear },
{ "DeviceAddrList", deviceAddrList }
};
jObject.Add("Data", data);
Gateway.Send(("RemoveDevice"), jObject.ToString());
}
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (d != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
d = new RemoveDeviceResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("RemoveDevice_Actions 退出" + System.DateTime.Now.ToString());
return d;
});
}
///
/// 需要删除设备的数据
///
public RemoveDeviceData removeDeviceData;
///
/// 需要删除设备的数据
///
[System.Serializable]
public class RemoveDeviceData
{
///
/// 0:不强制清除。需要节点设备反馈离网确认信息后才能删除设备信息。
///1:强制清除。不需要节点设备反馈离网确认信息,直接删除设备信息。
///说明:正常情况下让节点设备离网,需要节点设备在线,节点设备反馈离网确认信息后网关方可删除该设备的设备信息。但如果设备已经损坏,或已经通过外部功能离网,此时节点设备已经无法反馈离网确认信息,面对这种情况,要删除保存在网关上的该节点设备的设备信息,需要将该字段赋值为1,网关强制删除该设备信息,不需要节点设备确认。
///
public int CompelClear = 1;
///
/// 出网设备列表
///
public List DeviceAddrList = new List();
}
///
/// 场景信息
///
public class RemoveDeviceListInfo
{
///
/// 设备mac地址
///
public string DeviceAddr;
}
///
/// 移除设备,网关反馈具体信息
///
public RemoveDeviceResponseAllData removeDeviceResponseAllData;
///
/// 移除设备,网关反馈具体信息
///
[System.Serializable]
public class RemoveDeviceResponseAllData
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 设备名称修改
///
public RemoveDeviceResponseData removeDeviceResponseData;
}
///
/// 删除设备回复数据
///
public RemoveDeviceResponseData removeDeviceResponseData;
///
/// 删除设备回复数据
/// 返回结果Resul=,删除成功
///
[System.Serializable]
public class RemoveDeviceResponseData
{
///
/// 0:删除成功
/// 删除失败
///
public int Result = 2;
///
/// 出网设备列表
///
public List DeviceList = new List();
}
///
/// 场景信息
///
[System.Serializable]
public class DeviceListInfo
{
///
/// 设备ID
///
public int Device_ID;
///
/// 设备mac地址
///
public string MacAddr;
///
/// 设备端口号
///
public int Epoint;
}
#endregion
#region 从总设备列表中移除一个网关的所有节点设备(用于主网关)
///
/// 从总设备列表中移除一个网关的所有节点设备(用于主网关)
/// GwId:要移除节点设备的网关id
///
public async System.Threading.Tasks.Task RemoveGatewayDeviceListAsync(string gwId)
{
if (Gateway == null || gwId == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
RemoveGatewayDeviceListAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new RemoveGatewayDeviceListAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new RemoveGatewayDeviceListAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "RemoveEqOfGw_Respon")
{
var gatewayTemp = new ZbGateway() { DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
gatewayTemp.removeGatewayDeviceListData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (gatewayTemp.removeGatewayDeviceListData == null)
{
d = new RemoveGatewayDeviceListAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new RemoveGatewayDeviceListAllData { removeGatewayDeviceListData = gatewayTemp.removeGatewayDeviceListData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
//for (int listCount = 0; listCount < Gateway.AllGatewayDeviceList.Count; listCount++)
//{
// var dev = Gateway.AllGatewayDeviceList[listCount];
// if (gatewayTemp.removeGatewayDeviceListData.GwId == dev.Gateway.CurrentGateWayId)
// {
// ZigBee.Device.ZbGateway.LogicList.RemoveAt(listCount);
// listCount--;
// }
//}
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("RemoveEqOfGw_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var jObject = new JObject { { "Cluster_ID", 0 }, { "Command", 81 } };
var data = new JObject { { "GwId", gwId } };
jObject.Add("Data", data);
Gateway.Send(("RemoveEqOfGw"), jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (d != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
d = new RemoveGatewayDeviceListAllData { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("RemoveEqOfGw_Actions 退出" + System.DateTime.Now.ToString());
return d;
});
}
///
/// 移除设备,网关反馈具体信息
///
public RemoveGatewayDeviceListAllData removeGatewayDeviceListAllData;
///
/// 移除设备,网关反馈具体信息
///
[System.Serializable]
public class RemoveGatewayDeviceListAllData
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 设备名称修改
///
public RemoveGatewayDeviceListData removeGatewayDeviceListData;
}
///
/// 从总设备列表中移除一个网关的所有节点设备(用于主网关)
///
public RemoveGatewayDeviceListData removeGatewayDeviceListData;
///
/// 从总设备列表中移除一个网关的所有节点设备(用于主网关)
///
[System.Serializable]
public class RemoveGatewayDeviceListData
{
///
/// 要移除节点设备的网关id
///
public string GwId;
///
/// 被删除设备的数量
///
public int RemoveNum;
}
#endregion
#region 识别设备
///
/// 识别设备
/// cluster=3,具有Identify(识别)功能,属于ZCL库
/// time:设置设备闪烁时间(秒)范围:0-65535
///
public void IdentifyControl(string deviceAddr, int deviceEpoint, int time)
{
if (Gateway == null)
{
return;
}
Action action = (topic, message) => { };
Gateway.Actions += action;
System.Console.WriteLine("Identify_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 3 }, { "Command", 0 }, { "SendMode", 2 } };
var data = new JObject { { "Time", time } };
jObject.Add("Data", data);
Gateway.Send(("Identify"), jObject.ToString());
}
catch { }
Gateway.Actions -= action;
System.Console.WriteLine("Identify_Actions 退出" + System.DateTime.Now.ToString());
}
#endregion
#region 设备上报返回数据,网关回复信息
/////
///// 设备上报返回数据,网关回复信息
/////
//public DeviceReportResponAllData deviceReportResponAllData;
/////
///// 网关恢复出厂设置返回数据
/////
//[System.Serializable]
//public class DeviceReportResponAllData
//{
// ///
// /// 错误信息
// ///
// public string errorMessageBase;
// ///
// /// 网关信息错误反馈
// /// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
// ///
// public ErrorResponData errorResponData;
// ///
// ///设备数据
// ///
// public DeviceStatusReportData deviceStatusReportData;
//}
/////
///// 读取报告属性配置,异步获取数据
/////
///// Cluster identifier.
///// Attri bute identifier.
//public async System.Threading.Tasks.Task ReadAttriAsync(Cluster_ID clusterID, AttriButeId attriButeId)
//{
//if (Gateway == null)
//{
// return null;
//}
////string result = null;
//return await System.Threading.Tasks.Task.Run(async () =>
//{
//var d = new DeviceReportResponAllData();
//Action action = (topic, message) =>
//{
//var gatewayID = topic.Split('/')[0];
//var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
//if (topic == gatewayID + "/" + "Error_Respon")
//{
// var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
// var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// if (temp == null)
// {
// d.errorMessageBase = "网关错误回复,且数据是空";
// }
// else
// {
// d.errorResponData = temp;
// d.errorMessageBase = ErrorMess(temp.Error);
// }
//}
//if (topic == gatewayID + "/" + "DeviceStatusReport" + "/" + DeviceAddr + "/" + DeviceEpoint + "/" + (int)clusterID + "/" + (int)attriButeId)
//{
//var deviceID = jobject.Value("Device_ID");
//var deviceAddr = jobject.Value("DeviceAddr");
//var ep = jobject.Value("Epoint");
//var device = Gateway.DeviceList.Find((obj) => obj.DeviceID == deviceID && obj.DeviceAddr == deviceAddr && obj.DeviceEpoint == ep);
//if (device == null)
//{
// return;
//}
//switch ((DeviceType)(deviceID))
//{
// case DeviceType.ColorDimmableLight:
// device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// if (device.DeviceStatusReport == null)
// {
// d.errorMessageBase = "网关错误回复,且数据是空";
// }
// else
// {
// d.deviceStatusReportData = device.DeviceStatusReport;
// System.Console.WriteLine($"收到通知后的主题_{ topic}");
// var light = device as ColorDimmableLight;
// if (device.DeviceStatusReport.CluterID == 8)
// {
// var attriButeList = device.DeviceStatusReport.AttriBute;
// foreach (var attriBute1 in attriButeList)
// {
// light.Level = attriBute1.AttriButeData;
// }
// light.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(light);
// }
// else if (device.DeviceStatusReport.CluterID == 6)
// {
// var attriButeList = device.DeviceStatusReport.AttriBute;
// foreach (var attriBute1 in attriButeList)
// {
// light.OnOffStatus = attriBute1.AttriButeData;
// var key = light.DeviceAddr + "_" + light.DeviceEpoint;
// Gateway.sceneTaskInfoList.Remove(key);
// var st = new Scene.TaskListInfo()
// {
// TaskType = 1,
// Data1 = attriBute1.AttriButeData,
// Data2 = 0,
// };
// Gateway.sceneTaskInfoList.Add(key, st);
// }
// light.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(light);
// }
// else if (device.DeviceStatusReport.CluterID == 768)
// {
// var attriButeList = device.DeviceStatusReport.AttriBute;
// //foreach (var attriBute1 in attriButeList)
// //{
// // if (attriBute1.AttriButeId == 0)
// // {
// // light.Hue = attriBute1.AttriButeData.ToString();
// // }
// // else if ((attriBute1.AttriButeId == 1))
// // {
// // light.Saturation = attriBute1.AttriButeData.ToString();
// // }
// // else if ((attriBute1.AttriButeId == 16394))
// // {
// // light.ColorCapabilities = attriBute1.AttriButeData.ToString();
// // }
// // else if ((attriBute1.AttriButeId == 16384))
// // {
// // light.EnhancedCurrentHue = attriBute1.AttriButeData.ToString();
// // }
// //}
// light.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(light);
// }
// }
// break;
// case DeviceType.OnOffSwitch:
// device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// if (device.DeviceStatusReport == null)
// {
// d.errorMessageBase = "网关错误回复,且数据是空";
// }
// else
// {
// d.deviceStatusReportData = device.DeviceStatusReport;
// System.Console.WriteLine($"收到通知后的主题_{ topic}");
// var panelObj = device as Panel;
// foreach (var common in Gateway.DeviceList)
// {
// if (common.DeviceAddr != panelObj.DeviceAddr || common.DeviceEpoint != panelObj.DeviceEpoint)
// {
// continue;
// }
// if (common.Type == DeviceType.OnOffSwitch)
// {
// if (device.DeviceStatusReport.CluterID == 6)
// {
// var attriButeList = device.DeviceStatusReport.AttriBute;
// foreach (var attriBute1 in attriButeList)
// {
// panelObj.panelMode = attriBute1.AttriButeData;
// }
// }
// }
// panelObj.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(panelObj);
// }
// }
// break;
//case DeviceType.OnOffOutput:
// device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// //if (device.DeviceStatusReport != null)
// //{
// // result = "成功";
// //}
// if (device.DeviceStatusReport == null)
// {
// d.errorMessageBase = "网关错误回复,且数据是空";
// }
// else
// {
// d.deviceStatusReportData = device.DeviceStatusReport;
// System.Console.WriteLine($"收到通知后的主题_{ topic}");
// var lighttoggle = device as ToggleLight;
// foreach (var common in Gateway.DeviceList)
// {
// if (common.DeviceAddr != lighttoggle.DeviceAddr || common.DeviceEpoint != lighttoggle.DeviceEpoint)
// {
// continue;
// }
// if (common.Type == DeviceType.OnOffOutput)
// {
// if (device.DeviceStatusReport.CluterID == 6)
// {
// var attriButeList = device.DeviceStatusReport.AttriBute;
// foreach (var attriBute1 in attriButeList)
// {
// lighttoggle.OnOffStatus = attriBute1.AttriButeData;
// System.Console.WriteLine("当前开关状态" + "_" + attriBute1.AttriButeData.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// }
// }
// }
// lighttoggle.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(lighttoggle);
// }
// }
// break;
//case DeviceType.WindowCoveringDevice:
// device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
// if (device.DeviceStatusReport == null)
// {
// d.errorMessageBase = "网关错误回复,且数据是空";
// }
// else
// {
// d.deviceStatusReportData = device.DeviceStatusReport;
// System.Console.WriteLine($"收到通知后的主题_{ topic}");
// var curtain = device as Rollershade;
// foreach (var common in Gateway.DeviceList)
// {
// if (common.DeviceAddr != curtain.DeviceAddr || common.DeviceEpoint != curtain.DeviceEpoint)
// {
// continue;
// }
// if (common.Type == DeviceType.WindowCoveringDevice)
// {
// if (device.DeviceStatusReport.CluterID == 258)
// {
// foreach (var attriBute1 in curtain.DeviceStatusReport.AttriBute)
// {
// switch (attriBute1.AttributeId)
// {
// case 0:
// curtain.WcdType = attriBute1.AttriButeData;
// System.Console.WriteLine("当前窗帘的类型" + "_" + curtain.WcdType.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 3:
// curtain.WcdCurrentPositionLift = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘当前高度" + "_" + curtain.WcdCurrentPositionLift.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 8:
// curtain.WcdCurrentPositionLiftPercentage = attriBute1.AttriButeData;
// System.Console.WriteLine("当前窗帘所在的进度(百分)位置" + "_" + curtain.WcdCurrentPositionLiftPercentage.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 16:
// curtain.WcdInstalledOpenLimitLift = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘全开所在的位置" + "_" + curtain.WcdInstalledOpenLimitLift.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 17:
// curtain.WcdInstalledClosedLimitLift = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘全关所在的位置" + "_" + curtain.WcdCurrentPositionLift.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 18:
// curtain.WcdInstalledOpenLimitTilt = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘全开所在的角度" + "_" + curtain.WcdCurrentPositionLift.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 19:
// curtain.WcdInstalledClosedLimitTilt = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘全关所在的角度" + "_" + curtain.WcdCurrentPositionLift.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// case 23:
// curtain.WcdCurrentMode = attriBute1.AttriButeData;
// System.Console.WriteLine("窗帘当前模式" + "_" + curtain.WcdCurrentMode.ToString() + "_" + Gateway.getGatewayBaseInfo.gwID + "_" + System.DateTime.Now.ToString());
// break;
// default:
// break;
// }
// }
// curtain.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(curtain);
// }
// }
// }
// }
// break;
//case DeviceType.IASZone:
//device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
//var ias = device as IASZone;
//foreach (var common in Gateway.DeviceList)
//{
// if (common.DeviceAddr != ias.DeviceAddr || common.DeviceEpoint != ias.DeviceEpoint)
// {
// continue;
// }
// if (common.Type == DeviceType.IASZone)
// {
// ias.ReSave();
// ZigBee.Device.ZbGateway.UpdateDeviceStatus(ias);
// }
//}
//ias.ReSave();
//break;
//case DeviceType.Thermostat:
//device.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
//var ther = device as ThermostatObj;
//if (ther.DeviceStatusReport.CluterID == 514)
//{
// foreach (var attriBute1 in ther.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// ther.CurentFanControlMode = attriBute1.AttriButeData;
// }
// }
//}
//if (ther.DeviceStatusReport.CluterID == 513)
//{
// foreach (var attriBute1 in ther.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// ther.LocalThermostat = attriBute1.AttriButeData / 100;
// }
// else if (attriBute1.AttributeId == 17)
// {
// ther.CurentCoolingSetpoint = attriBute1.AttriButeData / 100;
// }
// else if (attriBute1.AttributeId == 18)
// {
// ther.CurentHeatingSetpoint = attriBute1.AttriButeData / 100;
// }
// else if (attriBute1.AttributeId == 28)
// {
// ther.CurentSystemMode = attriBute1.AttriButeData;
// }
// }
//}
//ZigBee.Device.ZbGateway.UpdateDeviceStatus(ther);
//break;
//case DeviceType.TemperatureSensor:
//var sensor = new TemperatureSensor() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
//sensor.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
//ZigBee.Device.ZbGateway.UpdateDeviceStatus(sensor);
//if (sensor.DeviceStatusReport.CluterID == 1026)
//{
// foreach (var attriBute1 in sensor.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// sensor.CurentTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 1)
// {
// sensor.MaxTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 2)
// {
// sensor.MinTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 3)
// {
// sensor.TorleranceTemperature = attriBute1.AttriButeData;
// }
// IO.LocalFileUtils.SaveDeviceInfo(sensor, sensor.DeviceEpoint.ToString());
// }
//}
//else if (sensor.DeviceStatusReport.CluterID == 1029)
//{
// foreach (var attriBute1 in sensor.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// sensor.CurentHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 1)
// {
// sensor.MaxHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 2)
// {
// sensor.MinHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 3)
// {
// sensor.ToleranceHumidity = attriBute1.AttriButeData;
// }
// IO.LocalFileUtils.SaveDeviceInfo(sensor, sensor.DeviceEpoint.ToString());
// }
//}
//var sen = Gateway.SensorInfoList.Find(obj => obj.DeviceAddr == sensor.DeviceAddr && obj.DeviceEpoint == sensor.DeviceEpoint);
//if (sen == null)
//{
// Gateway.SensorInfoList.Add(sensor);
//}
//else
//{
// if (sensor.DeviceStatusReport.CluterID == 1026)
// {
// foreach (var attriBute1 in sensor.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// sen.CurentTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 1)
// {
// sen.MaxTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 2)
// {
// sen.MinTemperature = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 3)
// {
// sen.TorleranceTemperature = attriBute1.AttriButeData;
// }
// IO.LocalFileUtils.SaveDeviceInfo(sensor, sensor.DeviceEpoint.ToString());
// }
// }
// else if (sensor.DeviceStatusReport.CluterID == 1029)
// {
// foreach (var attriBute1 in sensor.DeviceStatusReport.AttriBute)
// {
// if (attriBute1.AttributeId == 0)
// {
// sen.CurentHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 1)
// {
// sen.MaxHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 2)
// {
// sen.MinHumidity = attriBute1.AttriButeData;
// }
// else if (attriBute1.AttributeId == 3)
// {
// sen.ToleranceHumidity = attriBute1.AttriButeData;
// }
// IO.LocalFileUtils.SaveDeviceInfo(sensor, sensor.DeviceEpoint.ToString());
// }
// }
//}
//break;
// }
// }
// };
// Gateway.Actions += action;
// var JObject = new JObject {
// { "DeviceAddr",DeviceAddr },
// { "Epoint", DeviceEpoint },
// { "Cluster_ID", (int)clusterID },
// { "Command", 108 }
// };
// var attriBute = new JArray{
// new JObject {
// { "AttriButeId", (int)attriButeId}
// }
// };
// var data = new JObject { { "AttriBute", attriBute } };
// JObject.Add("Data", data);
// Gateway?.Send(("GetDeviceStatus"), Common.SecuritySet.Encryption((JObject.ToString())));
// var dateTime = DateTime.Now;
// while ((DateTime.Now - dateTime).TotalMilliseconds < 5000)
// {
// await System.Threading.Tasks.Task.Delay(10);
// if (d.deviceStatusReportData != null)
// {
// break;
// }
// }
// if ((DateTime.Now - dateTime).TotalMilliseconds > 10000)
// {
// d.errorMessageBase = " 回复超时,请重新操作";
// }
// Gateway.Actions -= action;
// return d;
// });
//}
#endregion
#region 设备属性状态上报
///
/// 获取设备当前属性状态
///
/// Cluster identifier.
/// Attri bute identifier.
public async void ReadAttri(Cluster_ID clusterID, AttriButeId attriButeId)
{
if (Gateway == null)
{
return;
}
await System.Threading.Tasks.Task.Run(async () =>
{
//Action action = (topic, message) => { };
// Gateway.Actions += action;
System.Console.WriteLine("GetDeviceStatus_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var JObject = new JObject {
{ "DeviceAddr",DeviceAddr },
{ "Epoint", DeviceEpoint },
{ "Cluster_ID", (int)clusterID },
{ "Command", 108 }
};
var attriBute = new JArray{
new JObject {
{ "AttriButeId", (int)attriButeId}
}
};
var data = new JObject { { "AttriBute", attriBute } };
JObject.Add("Data", data);
Gateway?.Send(("GetDeviceStatus"), JObject.ToString());
}
catch { }
// Gateway.Actions -= action;
System.Console.WriteLine("GetDeviceStatus_Actions 退出" + System.DateTime.Now.ToString());
});
}
///
/// 设备属性状态上报
///
public DeviceStatusReportData DeviceStatusReport = new DeviceStatusReportData { };
///
/// 设备属性状态上报
///
[System.Serializable]
public class DeviceStatusReportData
{
///
/// 属性所在CluterID
///
/// 设备支持的输入功能
/// 0:Basic,设备支持“基础属性”
/// 3:Identify,设备支持“识别功能”
/// 4:Groups,设备支持“组功能”
/// 5:Scenes,设备支持“场景功能”
/// 6:on/off,设备支持“开关功能”
/// 开关功能的设备如:调关灯/继电器/窗帘等。。。
/// 8:Level Control,设备支持“亮度调节功能”
/// 亮度调节功能的设备如:调关灯。。。
/// 257:Door Lock,设备支持“门锁功能”
/// 门锁功能的设备如:门锁。。。
/// 258:Window Covering,设备支持“窗帘控制功能”
/// 窗帘控制功能的设备如:窗帘/开合帘/卷帘。。。
/// 513:Thermostat,设备支持“恒温器功能”
/// 恒温器功能的设备如:空调。。。
/// 768:Color Control,设备支持“颜色调节功能”
/// 颜色调节功能的设备如:调光灯。。。
/// 1026:Temperature Measurement,设备支持“温度测量功能”
/// 温度测量功能的设备如:温度传感器。。。
/// 1029:Relative Humidity Measurement,设备支持“湿度测量功能”
/// 湿度测量功能的设备如:湿度传感器。。。
///
///
public int CluterID;
///
/// 属性列表
///
public List AttriBute = new List();
}
#endregion
#region 设置可写属性的值
///
/// 设置可写属性的值
///
/// The writable value async.
/// Gateway.
/// 要配置的属性所在的cluster.
/// 设置可写属性的数据
public async System.Threading.Tasks.Task SetWritableValueAsync(ZigBee.Device.ZbGateway gateway, int clusterID, SetWritableValueData setWritableValue)
{
if (gateway == null || setWritableValue == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
SetWritableValueResponAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "SetWritableValue_Respon")
{
var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value("Device_ID"), DeviceAddr = jobject.Value("DeviceAddr"), DeviceEpoint = jobject.Value("Epoint"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = gateway.getGatewayBaseInfo.gwID };
var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (tempData == null)
{
d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
}
else
{
d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
}
}
};
gateway.Actions += action;
System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
try
{
var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", clusterID }, { "Command", 120 } };
var data = new JObject { { "Undivided", setWritableValue.Undivided }, { "AttributeId", setWritableValue.AttributeId }, { "AttributeDataType", setWritableValue.AttributeDataType }, { "AttributeData", setWritableValue.AttributeData } };
jObject.Add("Data", data);
gateway.Send("SetWritableValue", jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (d != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
}
gateway.Actions -= action;
System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
return d;
});
}
///
/// 网关版本信息,网关反馈信息
///
public SetWritableValueResponAllData setWritableValueResponAllData;
///
/// 网关版本信息,网关反馈信息
///
[System.Serializable]
public class SetWritableValueResponAllData
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 网关版本信息
///
public SetWritableValueResponData setWritableValueResponData;
}
///
/// 设置可写属性的值的数据
///
[System.Serializable]
public class SetWritableValueResponData
{
///
/// 配置属性所在的cluster
///
public int Cluster;
///
/// 0:配置成功(若配置成功,下面的AttributeId字段不存在)
///134:不支持该属性
///135:无效的属性值
///141:无效的数据类型
///
public int Status;
///
/// 配置的属性ID(当Status=0 时,该字段将不存在 ,也就是只有失败的结果才会返回该字段)
///
public int AttributeId;
}
///
/// 设置可写属性的值的数据
///
public SetWritableValueData setWritableValueData;
///
/// 设置可写属性的值的数据
///
[System.Serializable]
public class SetWritableValueData
{
///
/// 是否强制写入属性
///0:否
///1:强制写属性值
///可缺省,默认为0。
///
public int Undivided;
///
/// 属性id
///
public int AttributeId;
///
/// 属性数据类型
///
public int AttributeDataType;
///
/// 写入数值
///
public int AttributeData;
}
#endregion
#region 开关
///
///开关控制(仅用于cluster=6的设备)
/// 设备支持cluster=6的设备才能调用该接口
/// command的值
///0 : 关闭
///1: 打开
///2:取反
///
public void SwitchControl(int command)
{
try
{
var jobject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 6 }, { "Command", command }, { "SendMode", 2 } };
Gateway?.Send(("DeviceControl"), jobject.ToString());
System.Console.WriteLine("SwitchControl_发送数据" + "_" + jobject.ToString() + "_" + System.DateTime.Now.ToString());
}
catch { }
}
#endregion
#region ZCL库-发现属性
///
///ZCL库-发现属性(如果返回为空,可能是设备不是标准设备,不支持这条命令)
/// 获取设备的某个Cluster所支持的Attributes
/// gateway:当前网关
/// clusterID:需要查询的cluster
/// 读Panel的属性:clusterID=6
/// 读亮度的属性:clusterID=8
///
public async System.Threading.Tasks.Task ClusterOwnAttributesAsync(ZigBee.Device.ZbGateway gateway, int clusterID)
{
if (gateway == null)
{
return null;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
ClusterOwnAttributesResponAllData d = null;
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var gatewayTemp = new ZbGateway() { Time = jobject.Value("Time"), DataID = jobject.Value("Data_ID"), CurrentGateWayId = gateway.getGatewayBaseInfo.gwID };
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
d = new ClusterOwnAttributesResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
d = new ClusterOwnAttributesResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "Cluster/OwnAttributes_Respon")
{
var gatewayTemp = new ZbGateway() { DataID = jobject.Value("Data_ID"), CurrentGateWayId = gateway.getGatewayBaseInfo.gwID };
gatewayTemp.clusterOwnAttributesResponData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
var tempAttributes = Newtonsoft.Json.Linq.JArray.Parse(jobject["Data"]["Attribute"].ToString());
for (int m = 0; tempAttributes != null && m < tempAttributes.Count; m++)
{
var tempAttribute = tempAttributes[m];
gatewayTemp.clusterOwnAttributesResponData.AttributeList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject