using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using ZigBee.Device;
namespace Shared.Phone
{
///
/// 全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
///
public class HdlGatewayReceiveLogic
{
#region ■ 变量声明___________________________
///
/// 全局接收网关推送的的逻辑
///
private static HdlGatewayReceiveLogic m_Current = null;
///
/// 全局接收网关推送的的逻辑
///
public static HdlGatewayReceiveLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlGatewayReceiveLogic();
}
return m_Current;
}
}
///
/// 网关接收事件(参数1:主题 参数2:推送消息)
///
private Action GatewayReceiveEvent = null;
///
/// 接收网关的id
///
private string GatewayReceiveId = null;
///
/// 设备推送事件集合
///
private Dictionary> dicDeviceEvent = new Dictionary>();
///
/// 命令区分
///
private Dictionary dicCommandDiv = new Dictionary();
#endregion
#region ■ 全局接收___________________________
///
/// 全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
///
/// 网关ID
/// 整个主题
/// 上报数据的主题
/// 接收的数据
public void GatewayOverallMsgReceive(string gatewayId, string topic, string reportTopic, string msgData)
{
//如果它在登陆界面,则不做任何处理
if (Common.Config.Instance.HomeId == string.Empty)
{
return;
}
if (topic == "AppNoLogin")
{
HdlThreadLogic.Current.RunMain(() =>
{
//登录密匙已经过期,请重新登录
string msg = Language.StringByID(R.MyInternationalizationString.uTokenIsOldAndLoginAgain);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.ConnEmqClientId + "/Push/NotifySqueeze")
{
HdlThreadLogic.Current.RunMain(() =>
{
//此帐号已在别处登录,您被迫下线
string msg = Language.StringByID(R.MyInternationalizationString.uHadBeenLoginAndOffLine);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
else if (topic == "YouIpAndPortNoRecord")
{
HdlThreadLogic.Current.RunMain(() =>
{
//您当前的IP及端口在云端不存在,请重新登录!
string msg = Language.StringByID(R.MyInternationalizationString.uYouIpAndPortNoRecord);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Home.Id + "_" + Common.Config.Instance.Guid + "/PrimaryUserDelYou")
{
HdlThreadLogic.Current.RunMain(() =>
{
//分享住宅已更改,请联系管理员!
string msg = Language.StringByID(R.MyInternationalizationString.uShardResidenceHadDeletePleaseTakeToAdmin);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/Update")
{
HdlThreadLogic.Current.RunMain(() =>
{
//您的权限已经变更,请重新登陆
string msg = Language.StringByID(R.MyInternationalizationString.uYouAccessHadChangedPleaseTakeToAdmin);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
else if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 3)
{
if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/Deleted"
|| topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/DeletedShareData")
{
HdlThreadLogic.Current.RunMain(() =>
{
//分享数据已经变更,请重新登陆
string msg = Language.StringByID(R.MyInternationalizationString.uShardDataIsChangedPleaseLoginAgain);
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
HdlAccountLogic.Current.ReLoginAgain(HdlUserCenterResourse.UserInfo.Account, false);
});
return;
}
}
try
{
if (HdlGatewayLogic.Current.IsGatewayExist(gatewayId) == false)
{
//不是自己绑定的网关,则不处理,但是下面这个东西有点特殊
if (gatewayId == this.GatewayReceiveId)
{
this.GatewayReceiveEvent?.Invoke(topic, msgData);
}
return;
}
//设备属性上报
if (reportTopic == "DeviceStatusReport")
{
//设备属性上报
this.DeviceAttributeReportPush(JObject.Parse(msgData));
}
//传感器上报
else if (reportTopic == "IASInfoReport")
{
this.SensorDeviceReportPush(JObject.Parse(msgData));
}
//门锁上报
else if (topic == gatewayId + "/Alarms/SendAlarmInform")
{
this.DoorLockDeviceReportPush(JObject.Parse(msgData));
}
//通过外部方式布防撤防成功时报告
else if (topic == gatewayId + "/Security/EnOrWithdrawSucceedReport")
{
this.SecurityEnOrWithdrawSucceedReport(JObject.Parse(msgData));
}
//设备在线状态更新反馈
else if (reportTopic == "OnlineStatusChange_Respon")
{
this.DeviceOnlineChangePush(JObject.Parse(msgData));
}
//设备控制状态反馈
else if (reportTopic == "DeviceDefaultAck")
{
this.DeviceControlResponePush(JObject.Parse(msgData));
}
//撤防
else if (topic == gatewayId + "/Security/WithdrawMode_Respon")
{
this.RemoveSafetyGarrisonPush(JObject.Parse(msgData));
}
//布防
else if (topic == gatewayId + "/Security/EnableMode_Respon")
{
this.SetSafetyGarrisonPush(JObject.Parse(msgData));
}
//逻辑触发上报
else if (topic == gatewayId + "/Logic/Execute_Respon")
{
this.LogicExecutePush(JObject.Parse(msgData));
}
//场景触发上报
else if (topic == gatewayId + "/Scene/Exec_Respon")
{
this.SceneExecPush(null);
}
//网关接收事件
else if (gatewayId == this.GatewayReceiveId)
{
this.GatewayReceiveEvent?.Invoke(topic, msgData);
}
}
catch (Exception ex)
{
//Log出力
HdlLogLogic.Current.WriteLog(ex);
}
}
#endregion
#region ■ 设备属性上报_______________________
///
/// 设备属性上报
///
///
private void DeviceAttributeReportPush(JObject receiveData)
{
if (this.dicDeviceEvent.Count == 0)
{
//没有添加监听
return;
}
var deviceAddr = receiveData.Value("DeviceAddr");
var deviceEpoint = receiveData.Value("Epoint");
var report = new CommonDevice { DeviceAddr = deviceAddr, DeviceEpoint = deviceEpoint };
report.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (report.DeviceStatusReport.AttriBute.Count == 0)
{
//网关有些奇葩,没有属性它也会发过来
return;
}
//处理网关上报的数据,然后变更本地缓存
var locadevice = HdlDeviceCommonLogic.Current.GetDevice(deviceAddr, deviceEpoint);
if (locadevice != null)
{
//有反馈,这个设备肯定是在线的
locadevice.IsOnline = 1;
locadevice.LastDateTime = DateTime.Now;
#region ■ 开关功能
//开关功能
if (report.DeviceStatusReport.CluterID == 6)
{
if (locadevice is LightBase)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((LightBase)locadevice).OnOffStatus = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
#endregion
#region ■ 电量推送
//电量推送
if (report.DeviceStatusReport.CluterID == 1)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//电量
if (attData.AttributeId == 33)
{
string receiptData = string.Empty;
//两个两个位置替换
for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
{
receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
}
int batteryValue = Convert.ToInt32(receiptData, 16);
//低于20%,则代表电量低
locadevice.IsBatteryDown = batteryValue < 20;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
#endregion
#region ■ 蜂鸣器推送
//蜂鸣器数据
else if (report.DeviceStatusReport.CluterID == 1282)
{
//mini夜灯
if (HdlDeviceCommonLogic.Current.IsMiniLight(locadevice) == true)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
if (attData.AttributeId == 0)
{
//这个是报警持续时间(大于2秒时,标记为还在响着)
((ColorTemperatureLight)locadevice).IsBuzzerRing = attData.AttriButeData > 2 ? true : false;
}
}
}
}
#endregion
#region ■ 窗帘数据
//窗帘数据
else if (report.DeviceStatusReport.CluterID == 258)
{
//窗帘类型
if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((Rollershade)locadevice).WcdType = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//这个东西要保存
locadevice.ReSave();
}
//窗帘百分比
else if (report.DeviceStatusReport.AttriBute[0].AttributeId == 8)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((Rollershade)locadevice).WcdCurrentPositionLiftPercentage = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
#endregion
#region ■ 空调和新风数据
//空调数据
else if (report.DeviceStatusReport.CluterID == 513)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
var curTemp = attData.AttriButeData / 100;
if (attData.AttributeId == 0)
{
//此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
((AC)locadevice).currentLocalTemperature = curTemp;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 17)
{
//此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
((AC)locadevice).currentCoolingSetpoint = curTemp;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 18)
{
//此属性表明此设备当前的制热温度,实际温度为“HeatingSetpoint / 100”,单位:℃。
((AC)locadevice).currentHeatingSetpoint = curTemp;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 28)
{
//此属性描述恒温设备正处于哪种模式
((AC)locadevice).currentSystemMode = attData.AttriButeData;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 4096)
{
//此属性表明此设备当前的自动温度,实际温度为“AutoSetpoint / 100”,单位:℃。
((AC)locadevice).currentAutoSetpoint = curTemp;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 4097)
{
//过虑网清洗标志:42
((AC)locadevice).CleanStatu = attData.AttriButeData == 42;
}
else if (attData.AttributeId == 4099)
{
//转换为二进制
var value = Convert.ToString(attData.AttriButeData, 2).PadLeft(16, '0');
//这五个设置是放在后面的
value = value.Substring(value.Length - 5, 5);
for (int i = 0; i < value.Length; i++)
{
//自定义的空调模式
((AC)locadevice).listSupportMode[i] = Convert.ToInt32(value[i].ToString());
}
locadevice.ReSave();
}
}
}
//空调数据
else if (report.DeviceStatusReport.CluterID == 514)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//区分是空调还是新风
var device = HdlDeviceCommonLogic.Current.GetDevice(report.DeviceAddr, report.DeviceEpoint);
if (device.Type == DeviceType.FreshAir)
{
if (attData.AttributeId == 0)
{
switch (attData.AttriButeData)
{
case 0:
case 4:
((FreshAir)locadevice).currentFanStatus = attData.AttriButeData;
break;
case 1:
case 2:
case 3:
((FreshAir)locadevice).currentFanSpeed = attData.AttriButeData;
break;
case 5:
case 15:
((FreshAir)locadevice).currentFanMode = attData.AttriButeData;
break;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
else
{
if (attData.AttributeId == 0)
{
//风扇模式
((AC)locadevice).currentFanMode = attData.AttriButeData;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 4096)
{
//风扇扫风
((AC)locadevice).currentFanSwingMode = attData.AttriButeData;
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
else if (attData.AttributeId == 4097)
{
//转换为二进制
var value = Convert.ToString(attData.AttriButeData, 2).PadLeft(16, '0');
//这个设置是放在后面的
value = value.Substring(value.Length - 1, 1);
//启用摆风功能
((AC)locadevice).UseSwingFunction = value == "1";
locadevice.ReSave();
}
}
}
}
#endregion
#region ■ 亮度数据
//亮度数据
else if (report.DeviceStatusReport.CluterID == 8)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
if (locadevice.Type == DeviceType.DimmableLight)
{
//此属性表明当前亮度程度
((DimmableLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
}
else if (locadevice.Type == DeviceType.ColorTemperatureLight)
{
//此属性表明当前亮度程度
((ColorTemperatureLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
//if (locadevice is LightBase)
//{
// //当接收到亮度值时,默认打开
// ((LightBase)locadevice).OnOffStatus = 1;
//}
}
}
#endregion
#region ■ 色温数据
//色温数据
else if (report.DeviceStatusReport.CluterID == 768)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
if (locadevice.Type == DeviceType.ColorTemperatureLight)
{
//此属性表明当前色温
int value = report.DeviceStatusReport.AttriBute[0].AttriButeData != 0 ? 1000000 / report.DeviceStatusReport.AttriBute[0].AttriButeData : 0;
((ColorTemperatureLight)locadevice).ColorTemperature = value;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
#endregion
#region ■ 温度数据
//温度数据
else if (report.DeviceStatusReport.CluterID == 1026)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//温度
if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
{
decimal temperatrue = 0;
string receiptData = string.Empty;
//两个两个位置替换
for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
{
receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
}
//有符号(会出现负数)
if (attData.AttriButeDataType == 40 || attData.AttriButeDataType == 41)
{
//小数点需要一位
string strValue = Convert.ToInt16(receiptData, 16).ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
//无符号(不会出现负数)
else if (attData.AttriButeDataType == 32 || attData.AttriButeDataType == 33)
{
ushort shortData = Convert.ToUInt16(receiptData, 16);
if (shortData > 32767)
{
//负数(特殊处理)
string strValue = (shortData - 65536).ToString();
//小数点需要一位
strValue = strValue.Substring(0, strValue.Length - 1);
temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
else
{
//小数点需要一位
string strValue = shortData.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
}
//温度传感器
if (locadevice is TemperatureSensor)
{
((TemperatureSensor)locadevice).currentTemperature = temperatrue;
}
//PM2.5传感器
else if (locadevice is PMSensor)
{
((PMSensor)locadevice).currentTemperature = (int)temperatrue;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
//温度值需要保存
locadevice.ReSave();
}
}
}
#endregion
#region ■ 湿度数据
//湿度数据
else if (report.DeviceStatusReport.CluterID == 1029)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//湿度
if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
{
decimal humidity = 0;
string receiptData = string.Empty;
//两个两个位置替换
for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
{
receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
}
//有符号(会出现负数)
if (attData.AttriButeDataType == 40 || attData.AttriButeDataType == 41)
{
//小数点需要一位
string strValue = Convert.ToInt16(receiptData, 16).ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
//无符号(不会出现负数)
else if (attData.AttriButeDataType == 32 || attData.AttriButeDataType == 33)
{
//小数点需要一位 湿度不会出现负数
string strValue = Convert.ToUInt16(receiptData, 16).ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
//湿度传感器
if (locadevice is TemperatureSensor)
{
((TemperatureSensor)locadevice).currentHumidity = humidity;
}
//新风的湿度传感器
else if (locadevice is HumiditySensor)
{
((HumiditySensor)locadevice).currentHumidity = humidity;
}
//PM2.5传感器
else if (locadevice is PMSensor)
{
((PMSensor)locadevice).currentHumidity = (int)humidity;
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
//湿度值需要保存
locadevice.ReSave();
}
}
}
#endregion
#region ■ PM2.5数据
//PM2.5数据
else if (report.DeviceStatusReport.CluterID == 1066)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//PM2.5
if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
{
if (attData.AttriButeDataType == 57)
{
((PMSensor)locadevice).currentPmData = attData.AttriButeData;
}
}
//已经接收到状态
locadevice.HadReadDeviceStatu = true;
}
}
#endregion
#region ■ 设备基础信息
else if (report.DeviceStatusReport.CluterID == 0)
{
var listLocalDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(deviceAddr, false);
//属性都是一样的
foreach (var myDevice in listLocalDevice)
{
//属性是否改变
bool attriButeChanged = false;
foreach (var data in report.DeviceStatusReport.AttriBute)
{
//生产商名字
if (data.AttributeId == 4 && data.AttriButeDataHex.Length > 2)
{
if (data.AttriButeDataHex.Length > 2)
{
var value = HdlCommonLogic.Current.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (myDevice.ManufacturerName != value)
{
//属性变更了
attriButeChanged = true;
}
myDevice.ManufacturerName = value;
}
}
//型号码(也叫模块ID)
if (data.AttributeId == 5)
{
if (data.AttriButeDataHex.Length > 2)
{
var value = HdlCommonLogic.Current.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (myDevice.ModelIdentifier != value)
{
//属性变更了
attriButeChanged = true;
}
myDevice.ModelIdentifier = value;
}
}
//生产日期
if (data.AttributeId == 6)
{
if (data.AttriButeDataHex.Length > 2)
{
var value = HdlCommonLogic.Current.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
if (myDevice.ProductionDate != value)
{
//属性变更了
attriButeChanged = true;
}
myDevice.ProductionDate = value;
}
}
//电源
if (data.AttributeId == 7)
{
myDevice.PowerSource = data.AttriButeData;
}
//序列号
if (data.AttributeId == 13)
{
if (data.AttriButeDataHex.Length > 2)
{
string value;
if (HdlDeviceCommonLogic.Current.IsHdlDevice(myDevice) == false)
{
//第三方设备
value = data.AttriButeDataHex.Substring(2);
}
else
{
//河东设备
value = HdlCommonLogic.Current.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
}
if (myDevice.SerialNumber != value)
{
//属性变更了
attriButeChanged = true;
}
myDevice.SerialNumber = value;
}
}
}
//如果属性变更了
if (attriButeChanged == true && myDevice.IsCustomizeImage == false)
{
//UI重新生成
myDevice.IconPath = string.Empty;
myDevice.ReSave();
}
}
}
#endregion
}
else if (deviceEpoint == 200)
{
var localOta = HdlDeviceCommonLogic.Current.GetOTADevice(deviceAddr);
if (localOta != null)
{
#region ■ 固件版本
//固件版本
if (report.DeviceStatusReport.CluterID == (int)Cluster_ID.Ota)
{
foreach (var data in report.DeviceStatusReport.AttriBute)
{
//镜像版本
if (data.AttributeId == (int)AttriButeId.ImgVersion)
{
localOta.ImgVersion = data.AttriButeData;
}
//硬件版本
if (data.AttributeId == (int)AttriButeId.mgHWversion)
{
localOta.HwVersion = data.AttriButeData;
}
//镜像ID
if (data.AttributeId == (int)AttriButeId.ImgTypeId)
{
localOta.ImgTypeId = data.AttriButeData;
}
}
localOta.ReSave();
}
#endregion
}
}
this.DeviceReportPush(report, ReceiveComandDiv.A设备属性上报);
}
#endregion
#region ■ 传感器上报_________________________
///
/// 传感器设备上报
///
///
private void SensorDeviceReportPush(JObject receiveData)
{
var ias = new IASZone() { DeviceAddr = receiveData.Value("DeviceAddr"), DeviceEpoint = receiveData.Value("Epoint") };
ias.iASInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
//如果没有添加入安防
if (HdlSafeguardLogic.Current.GetZoneIdByIASZone(ias) != -1)
{
//保存安防报警信息到本地
HdlAlarmsLogic.Current.SaveSafeguardAlarmInfo(ias);
}
//处理网关上报的数据,然后变更本地缓存
var locadevice = HdlDeviceCommonLogic.Current.GetDevice(ias.DeviceAddr, ias.DeviceEpoint);
if (locadevice != null)
{
((IASZone)locadevice).iASInfo = ias.iASInfo;
//如果接收到上报,即说明这个传感器是在线的
locadevice.IsOnline = 1;
//记录回复时间
locadevice.LastDateTime = DateTime.Now;
}
this.DeviceReportPush(ias, ReceiveComandDiv.A传感器上报);
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
#endregion
#region ■ 门锁上报___________________________
///
/// 门锁上报
///
///
private void DoorLockDeviceReportPush(JObject receiveData)
{
if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 1)
{
var device = HdlDeviceCommonLogic.Current.GetDevice(receiveData.Value("DeviceAddr"), receiveData.Value("Epoint"));
if (device.Type != DeviceType.DoorLock)
{
//它不是门锁
return;
}
var info = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (info.Clusterid == 257)
{
//216:锁上设置的常开
if (info.AlarmCode == 216)
{
HdlThreadLogic.Current.RunMain(() =>
{
//更新门锁涉及的常开/常关的界面
if (UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction != null)
{
UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction(device.DeviceAddr, true);
}
});
}
}
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
}
#endregion
#region ■ 设备在线状态更新反馈_______________
///
/// 设备在线状态更新反馈
///
///
private void DeviceOnlineChangePush(JObject receiveData)
{
if (this.dicDeviceEvent.Count == 0)
{
//没有添加监听
return;
}
var tempDevice = new CommonDevice() { DeviceAddr = receiveData.Value("DeviceAddr"), DeviceEpoint = receiveData.Value("Epoint") };
tempDevice.IsOnline = Convert.ToInt32(receiveData["Data"]["IsOnline"].ToString());
//处理网关上报的数据,然后变更本地缓存
var locadevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(tempDevice.DeviceAddr);
for (int i = 0; i < locadevice.Count; i++)
{
bool onlineChanged = locadevice[i].IsOnline != tempDevice.IsOnline;
locadevice[i].IsOnline = tempDevice.IsOnline;
//记录回复时间
locadevice[i].LastDateTime = DateTime.Now;
if (onlineChanged == true)
{
//在线状态变更了,才保存
locadevice[i].ReSave();
}
}
this.DeviceReportPush(tempDevice, ReceiveComandDiv.A设备在线上报);
}
#endregion
#region ■ 设备控制状态反馈___________________
///
/// 设备控制状态反馈
///
///
private void DeviceControlResponePush(JObject receiveData)
{
if (this.dicDeviceEvent.Count == 0)
{
//没有添加监听
return;
}
var tempDevice = new CommonDevice() { DeviceAddr = receiveData.Value("DeviceAddr"), DeviceEpoint = receiveData.Value("Epoint") };
this.DeviceReportPush(tempDevice, ReceiveComandDiv.A节点控制反馈);
}
#endregion
#region ■ 布防_______________________________
///
/// 布防推送
///
///
private void SetSafetyGarrisonPush(JObject receiveData)
{
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (data.Result == 0)
{
var garrison = GarrisonMode.None;
//在家布防
if (data.ModeId == 1) { garrison = GarrisonMode.AtHome; }
//离家布防
else if (data.ModeId == 2) { garrison = GarrisonMode.RemoveHome; }
else { return; }
//保存报警信息然后推送到界面上
HdlAlarmsLogic.Current.SaveSafeguardAlarmInfo(garrison);
//推送
var form = HdlFormLogic.Current.GetFormByName("SafetyManagementMainForm") as UserCenter.Safety.SafetyManagementMainForm;
form?.GarrisonModePush(garrison);
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
}
#endregion
#region ■ 撤防_______________________________
///
/// 撤防推送
///
///
private void RemoveSafetyGarrisonPush(JObject receiveData)
{
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (data.Result == 0)
{
//保存报警信息然后推送到界面上
HdlAlarmsLogic.Current.SaveSafeguardAlarmInfo(GarrisonMode.RemoveGarrison);
//推送
var form = HdlFormLogic.Current.GetFormByName("SafetyManagementMainForm") as UserCenter.Safety.SafetyManagementMainForm;
form?.GarrisonModePush(GarrisonMode.RemoveGarrison);
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
}
#endregion
#region ■ 通过外部方式布防撤防_______________
///
/// 通过外部方式布防撤防
///
/// 接收的数据
private void SecurityEnOrWithdrawSucceedReport(JObject receiveData)
{
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (data.EnOrWithdraw == -1 || data.ModeId == -1 || data.OperationWay == -1)
{
return;
}
var garrison = GarrisonMode.None;
if (data.EnOrWithdraw == 0)
{
//在家布防
if (data.ModeId == 1) { garrison = GarrisonMode.AtHome; }
//离家布防
else if (data.ModeId == 2) { garrison = GarrisonMode.RemoveHome; }
}
else if (data.EnOrWithdraw == 1)
{
//撤防
garrison = GarrisonMode.RemoveGarrison;
}
string appendText = string.Empty;
//自动化
if (data.OperationWay == 0) { appendText = "(" + Language.StringByID(R.MyInternationalizationString.uLogicOperation) + ")"; }
//按键操作
else if (data.OperationWay == 1) { appendText = "(" + Language.StringByID(R.MyInternationalizationString.uPanelOperation) + ")"; }
//保存报警信息然后推送到界面上
HdlAlarmsLogic.Current.SaveSafeguardAlarmInfo(garrison, appendText);
//推送
var form = HdlFormLogic.Current.GetFormByName("SafetyManagementMainForm") as UserCenter.Safety.SafetyManagementMainForm;
form?.GarrisonModePush(garrison);
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
///
/// 通过外部方式布防撤防的接收结果
///
private class SecurityEnOrWithdrawResult
{
///
/// 0:布防成功 1:撤防成功
///
public int EnOrWithdraw = -1;
///
/// 安防模式id
///
public int ModeId = -1;
///
/// 外部布撤防方式-> 0:执行逻辑动作 1:按键操作
///
public int OperationWay = -1;
}
#endregion
#region ■ 逻辑触发上报_______________________
///
/// 逻辑触发上报
///
///
private void LogicExecutePush(JObject receiveData)
{
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
// 逻辑执行常开模式失效的情况
if (HdlUserCenterResourse.ResidenceOption.AuthorityNo == 1)
{
//自动化执行 常开关闭
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(receiveData["Data"].ToString());
if (data != null && data.ActionData != null)
{
if (data.ActionData.Actiontype == 8 && data.ActionData.PassDataString == "055704010113")
{
var deviceAddr = data.ActionData.MacStr;
var device = HdlDeviceCommonLogic.Current.GetDevicesByMac(deviceAddr, false);
if (device.Count > 0 && device[0].Type != DeviceType.DoorLock)
{
return;
}
HdlThreadLogic.Current.RunThread(async () =>
{
HdlThreadLogic.Current.RunMain(() =>
{
//提示门锁已经失效
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.NormallyClosed), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(Common.CommonPage.Instance);
//更新门锁涉及的常开/常关的界面
if (UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction != null)
{
UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction(deviceAddr, false);
}
});
});
}
}
}
}
#endregion
#region ■ 场景触发上报_______________________
///
/// 场景触发上报
///
///
private void SceneExecPush(JObject receiveData)
{
//目前不处理场景上报内容
//显示有新消息的特效
this.ShowHadNewMessageAppeal();
}
#endregion
#region ■ 添加设备事件_______________________
///
/// 添加获取设备属性的事件(属性上报的对象:device.DeviceStatusReport)
///
/// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
/// 命令区分
///
/// 当接收到网关回复之后的回调函数
/// 设备属性上报的对象:device.DeviceStatusReport
/// 传感器上报的对象:ias.iASInfo
/// 设备在线上报的对象:device.IsOnline
public void AddAttributeEvent(string mainKeys, ReceiveComandDiv comand, Action action)
{
lock (this.dicDeviceEvent)
{
if (this.dicDeviceEvent.ContainsKey(mainKeys) == true)
{
this.RemoveEvent(mainKeys);
}
this.dicDeviceEvent[mainKeys] = action;
this.dicCommandDiv[mainKeys] = comand;
}
}
///
/// 添加网关接收事件(action只能存在一个,与AddAttributeEvent不共存,AddAttributeEvent优先)
///
/// 网关id
/// 只能存在一个action (参数1:主题 参数2:推送消息)
public void AddGatewayReceiveEvent(string i_gatewayId, Action action)
{
//添加事件
this.GatewayReceiveEvent = action;
this.GatewayReceiveId = i_gatewayId;
}
#endregion
#region ■ 移除设备监听_______________________
///
/// 移除事件
///
/// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
public void RemoveEvent(string mainKeys)
{
lock (this.dicDeviceEvent)
{
if (this.dicDeviceEvent.ContainsKey(mainKeys) == true)
{
var action = this.dicDeviceEvent[mainKeys];
this.dicDeviceEvent.Remove(mainKeys);
this.dicCommandDiv.Remove(mainKeys);
action = null;
}
}
}
///
/// 移除全部的事件
///
public void RemoveAllEvent()
{
lock (this.dicDeviceEvent)
{
var list = new HashSet();
foreach (var keys in this.dicDeviceEvent.Keys)
{
list.Add(keys);
}
foreach (var keys in list)
{
//需要慢慢一个一个的释放Action,听说
this.RemoveEvent(keys);
}
}
}
///
/// 移除网关接收事件(只能存在一个事件)
///
public void RemoveGatewayReceiveEvent()
{
this.GatewayReceiveEvent = null;
this.GatewayReceiveId = null;
}
#endregion
#region ■ 一般方法___________________________
///
/// 设备上报推送(调用此方法,他会推送到各自的界面)
///
///
///
public void DeviceReportPush(CommonDevice common, ReceiveComandDiv comand)
{
if (this.dicDeviceEvent.Count == 0)
{
//没有添加监听
return;
}
//lock (this.dicDeviceEvent)
{
var list = new List>();
try
{
foreach (string keys in this.dicDeviceEvent.Keys)
{
if (this.dicCommandDiv[keys] != comand)
{
//命令区分不一致,则不调用回调函数
continue;
}
//命令区分一致时,则调用回调函数
list.Add(this.dicDeviceEvent[keys]);
}
}
catch { return; }
//有可能在回调函数中移除了事件,导致报错,所以先收集,再调用
foreach (var action in list)
{
try
{
action?.Invoke(common);
}
catch (Exception ex)
{
//Log出力
string msg = "推送错误! 当前激活的界面[" + HdlFormLogic.Current.NowActionFormID + "]";
HdlLogLogic.Current.WriteLog(ex, msg);
}
}
}
}
///
/// 是否存在指定的事件
///
///
///
public bool IsEsixt(string mainkeys)
{
return this.dicCommandDiv.ContainsKey(mainkeys);
}
///
/// 显示有新消息的特效
///
private void ShowHadNewMessageAppeal()
{
//有新消息(特效还在时,不需要再处理)
if (HdlControlResourse.HadNewMessage == false)
{
HdlThreadLogic.Current.RunMain(() =>
{
for (int i = 0; i < HdlControlResourse.listMessageManaContr.Count; i++)
{
//显示角标特效
HdlControlResourse.listMessageManaContr[i].IsSelected = true;
}
HdlControlResourse.HadNewMessage = true;
});
}
}
#endregion
}
///
/// 接收命令区分
///
public enum ReceiveComandDiv
{
///
/// 设备属性上报
///
A设备属性上报 = 1,
///
/// 传感器上报
///
A传感器上报 = 2,
///
/// 设备在线上报
///
A设备在线上报 = 3,
///
/// 当客户端发送控制设备指令,如打开或关闭设备、调节亮度、颜色。如果被控制的节点设备在线,节点设备将反馈
///
A节点控制反馈 = 4
}
}