using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace ZigBee.Device
{
[System.Serializable]
public class IASZone : BindObj
{
public IASZone()
{
this.Type = DeviceType.IASZone;
}
#region IAS安防信息上报.
///
/// 上一次访问iASInfo的时间
///
[Newtonsoft.Json.JsonIgnore]
private DateTime oldiASInfoTime = DateTime.Now;
///
/// IAS安防信息上报
///
[Newtonsoft.Json.JsonIgnore]
private IASInfoData m_iASInfo = null;
///
/// IAS安防信息上报(这个东西30秒之后就清掉)
/// 当安防设备属性状态改变时候(例如一氧化碳传感器检测到一氧化碳气体),设备将上报属性状态变化数据。
///
[Newtonsoft.Json.JsonIgnore]
public IASInfoData iASInfo
{
get
{
if (m_iASInfo != null && (DateTime.Now - oldiASInfoTime).TotalMilliseconds > 30 * 1000)
{
//这个东西30秒之后就清掉
m_iASInfo = null;
return null;
}
return m_iASInfo;
}
set
{
//记录起更新时间
this.oldiASInfoTime = DateTime.Now;
m_iASInfo = value;
}
}
///
/// IAS安防信息上报
/// 当安防设备属性状态改变时候(例如一氧化碳传感器检测到一氧化碳气体),设备将上报属性状态变化数据。
///
[System.Serializable]
public class IASInfoData
{
///
///设备状态()
/// 下面的 Alarm1到BatteryDefect实则是对ZoneStatus二进制格式的解析
///
public int ZoneStatus;
///
///自定义设备状态
///
public int ExtendedStatus;
///
///区域ID
///
public int ZoneId;
///
///在设备状态改变之后延时Delay(单位:1/4秒)
///
public int Delay;
///
/// hdl
///1 - opened or alarmed
///0 - closed or not alarmed
///
public int Alarm1;
///
///1 - opened or alarmed
///0 - closed or not alarmed
///
public int Alarm2;
///
///1 - Tampered
///0 - Not tampered
///
public int Tamper;
///
///1 - Low battery
///0 - Battery OK
///
public int Battery;
///
///1 - Reports
///0 - Does not report
///
public int SupervisionReports;
///
///1 - Reports restore
///0 - Does not report restore
///
public int RestoreReports;
///
///1 - Trouble/Failure
///0 - OK
///
public int Trouble;
///
///1 - AC/Mains fault
///0 - AC/Mains OK
///
public int MaAC_Mainsins;
///
///1 - Sensor is in test mode
///0 - Sensor is operation mode
///
public int Test;
///
///1 - Sensor detects a defective battery
///0 - Sensor battery is functioning normally
///
public int BatteryDefect;
}
#endregion
#region 获取PIR光感等级(光照能力值).
///
///获取PIR光感等级(光照能力值)
/// reserve:0-ff
///
public async System.Threading.Tasks.Task GetPIRLightAbilitySizeAsync(string reserve = "01")
{
PirLightAbilitySizeInfo result = null;
if (Gateway == null)
{
result = new PirLightAbilitySizeInfo { errorMessageBase = "当前没有网关" };
return result;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
result = new PirLightAbilitySizeInfo { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
result = new PirLightAbilitySizeInfo { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "ZbDataPassthrough")
{
var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (clientDataPassthroughResponseData == null)
{
result = new PirLightAbilitySizeInfo { errorMessageBase = "网关返回的数据为空" };
}
else
{
if (clientDataPassthroughResponseData?.PassData != null)
{
var data = clientDataPassthroughResponseData.PassData;
if (data.Length == 12)
{
var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
if (command == "0304")
{
var cou = data[10].ToString() + data[11].ToString();
int countTemp = Convert.ToInt32(cou, 16);
result = new PirLightAbilitySizeInfo { LightLevelCount = countTemp };
System.Console.WriteLine($"UI收到通知后的主题_command:0303_{ topic}");
}
}
}
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("ClientDataPassthrough_Actions 启动" + System.DateTime.Now.ToString());
try
{
var passData = SendPIRLightAbilitySizeData(reserve);
var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
var data = new JObject { { "PassData", passData } };
jObject.Add("Data", data);
Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)//WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (result != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
result = new PirLightAbilitySizeInfo { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("ClientDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
return result;
});
}
///
/// 发送PIR光感等级(光照能力值)数据
/// 发配置按键指示灯颜色命令时,此时为发送到网关的透传数据
///
string SendPIRLightAbilitySizeData(string reserve)
{
string data = "";
string dataLength = "05";
string dataComand1 = "03";
string dataComand2 = "03";
string dataSerialNum = "01";
string addDataLength = "01";
string reserveData = reserve;
try
{
data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
reserveData;
}
catch { };
return data;
}
///
/// PIR配置参数回复
///
[System.Serializable]
public class PirLightAbilitySizeInfo : ErrorResponCommon
{
///
/// 光感等级个数(Lux能力)
/// 有几个等级就显示几个刻度
///
public int LightLevelCount = -1;
}
#endregion
#region PIR传感器参数配置
///
/// PIR配置参数回复
///
[System.Serializable]
public class ParamatesInfo : ErrorResponCommon
{
///
/// PIR配置参数
///
public ConfigureParamates configureParamates;
}
///
/// PIR配置参数
///
[System.Serializable]
public class ConfigureParamates
{
///
/// 亮度(光照度)使能
/// false(0):不使能
/// true(1):使能
///
public bool levelEnable;
///
/// 光照度等级
/// 值:0-10
///
public int levelSize = -1;
///
///控制设备使能位
/// false(0):不使能
/// true)1):使能
///
public bool controlDevEnable;
///
/// 触发开灯到触发关灯的时间间隔
/// 0-65535 单位秒
///
public int transitionTime;
///
/// IAS上报周期
/// 10-0xffff 秒
///
public int iasReportPeriod;
///
/// 光照度等级
/// 0:半自动模式
/// 1:自动模式
///
public int mode = 0;
///
/// 开灯类型
/// 值:0:调光
///1:开关
///
public int type = 0;
///
/// 调光模式,到达开灯亮度的时间
/// 值:0-10 秒
///
public int dimmerOnTime = 0;
///
/// 调光模式,关灯到达0%所需要的时间
/// 值:0-10 秒
///
public int dimmerOffTime = 0;
///
/// 调光模式开灯的亮度
/// 0-0xff (预留)
///
public int dimmerLevel = 0;
}
#endregion
#region 获取PIR lux值(Lux值)
///
///获取PIR lux值(Lux值)
/// reserve:0-ff
///
public async System.Threading.Tasks.Task GetPirLuxAbilitySizeAsync(string reserve = "01")
{
PirLuxAbilitySizeInfo result = null;
if (Gateway == null)
{
result = new PirLuxAbilitySizeInfo { errorMessageBase = "当前没有网关" };
return result;
}
return await System.Threading.Tasks.Task.Run(async () =>
{
Action action = (topic, message) =>
{
var gatewayID = topic.Split('/')[0];
var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
if (topic == gatewayID + "/" + "Error_Respon")
{
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (temp == null)
{
result = new PirLuxAbilitySizeInfo { errorMessageBase = "网关错误回复,且数据是空" };
}
else
{
result = new PirLuxAbilitySizeInfo { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
}
}
if (topic == gatewayID + "/" + "ZbDataPassthrough")
{
var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString());
if (clientDataPassthroughResponseData == null)
{
result = new PirLuxAbilitySizeInfo { errorMessageBase = "网关返回的数据为空" };
}
else
{
if (clientDataPassthroughResponseData?.PassData != null)
{
var data = clientDataPassthroughResponseData.PassData;
if (data.Length == 12)
{
var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
if (command == "0306")
{
var cou = data[10].ToString() + data[11].ToString();
int countTemp = Convert.ToInt32(cou, 16);
result = new PirLuxAbilitySizeInfo { pirLux = countTemp };
System.Console.WriteLine($"UI收到通知后的主题_command:0303_{ topic}");
}
}
}
}
}
};
Gateway.Actions += action;
System.Console.WriteLine("ClientDataPassthrough_Actions 启动" + System.DateTime.Now.ToString());
try
{
var passData = SendPIRLuxAbilitySizeData(reserve);
var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
var data = new JObject { { "PassData", passData } };
jObject.Add("Data", data);
Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
}
catch { }
var dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)//WaitReceiveDataTime)
{
await System.Threading.Tasks.Task.Delay(10);
if (result != null)
{
break;
}
}
if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
{
result = new PirLuxAbilitySizeInfo { errorMessageBase = " 回复超时,请重新操作" };
}
Gateway.Actions -= action;
System.Console.WriteLine("ClientDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
return result;
});
}
///
///获取PIR lux值
///
string SendPIRLuxAbilitySizeData(string reserve)
{
string data = "";
string dataLength = "05";
string dataComand1 = "05";
string dataComand2 = "03";
string dataSerialNum = "01";
string addDataLength = "01";
string reserveData = reserve;
try
{
data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
reserveData;
}
catch { };
return data;
}
///
/// PIR配置参数回复
///
[System.Serializable]
public class PirLuxAbilitySizeInfo
{
///
/// 错误信息
///
public string errorMessageBase;
///
/// 网关信息错误反馈
/// 当网关接收到客户端信息后,出现以下异常情况将反馈错误。
///
public ErrorResponData errorResponData;
///
/// 当前Lux值
/// 0-0xff Lux
///
public int pirLux = -1;
}
#endregion
}
}