using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.Stan
{
//应该也不多,就写在这里了
///
/// 云端推送枚举
///
public enum CloudPushEnum
{
A新设备上报 = 1,
}
///
/// 全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
///
public class HdlCloudReceiveLogic
{
#region ■ 变量声明___________________________
///
/// 全局接收云端推送的的逻辑
///
private static HdlCloudReceiveLogic m_Current = null;
///
/// 全局接收云端推送的的逻辑
///
public static HdlCloudReceiveLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlCloudReceiveLogic();
}
return m_Current;
}
}
///
/// 云端接收事件集合
///
private List ListCloudEvent = new List();
#endregion
#region ■ 全局接收___________________________
///
/// 特殊全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作 true:执行了特殊处理 false:没有执行特殊处理)
///
/// 整个主题
/// 接收的数据
/// Mqtt的解密密钥
/// 住宅倒序的解密密钥
public bool CloudOverallMsgReceiveEx(string topic, byte[] byteData, string mqttEncryptKey, string homeIdEncryptKey)
{
//设备入网上报主题
if (topic == $"/user/{Entity.DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/topo/found")
{
if (string.IsNullOrEmpty(homeIdEncryptKey) == false)
{
//解密
byteData = Securitys.EncryptionService.AesDecryptPayload(byteData, homeIdEncryptKey);
}
string msgData = Encoding.UTF8.GetString(byteData);
//这里特殊,别急着转字符串,先判断主题再转
return this.CloudOverallMsgReceiveEx(topic, msgData);
}
return false;
}
///
/// 特殊全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作 true:执行了特殊处理 false:没有执行特殊处理)
///
/// 整个主题
/// 接收的数据
public bool CloudOverallMsgReceiveEx(string topic, string msgData)
{
//没有任何监听
if (ListCloudEvent.Count == 0) { return false; }
//设备入网上报主题(目前只有红外宝)
if (topic == $"/user/{Entity.DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/topo/found")
{
for (int i = 0; i < this.ListCloudEvent.Count; i++)
{
//回调事件
this.ListCloudEvent[i].CloudReceiveEvent(CloudPushEnum.A新设备上报, msgData);
}
return true;
}
return false;
}
#endregion
#region ■ 添加云端监听_______________________
///
/// 添加云端接收事件
///
/// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
/// (参数1:枚举 参数2:推送消息)
public void AddCloudReceiveEvent(string i_mainKey, Action action)
{
try
{
var eventClass = new CloudReceiveEventClass();
eventClass.MainKey = i_mainKey;
eventClass.CloudReceiveEvent = action;
this.ListCloudEvent.Add(eventClass);
}
catch { }
}
#endregion
#region ■ 移除云端监听_______________________
///
/// 移除云端接收事件
///
/// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
public void RemoveCloudReceiveEvent(string i_mainKey)
{
try
{
for (int i = 0; i < this.ListCloudEvent.Count; i++)
{
if (this.ListCloudEvent[i].MainKey == i_mainKey)
{
this.ListCloudEvent.RemoveAt(i);
i--;
}
}
}
catch { }
}
#endregion
#region ■ 结构体_____________________________
///
/// 云端接收事件类
///
private class CloudReceiveEventClass
{
///
/// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
///
public string MainKey = string.Empty;
///
/// 云端接收事件(参数1:枚举 参数2:推送消息)
///
public Action CloudReceiveEvent = null;
}
#endregion
}
}