陈嘉乐
2021-03-30 003abaf86b766eb40ef62300ab0c707774c36ba9
HDL_ON/UI/UI0-Stan/Logic/HdlCloudReceiveLogic.cs
New file
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.Stan
{
    //应该也不多,就写在这里了
    /// <summary>
    /// 云端推送枚举
    /// </summary>
    public enum CloudPushEnum
    {
        A新设备上报 = 1,
    }
    /// <summary>
    /// 全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
    /// </summary>
    public class HdlCloudReceiveLogic
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 全局接收云端推送的的逻辑
        /// </summary>
        private static HdlCloudReceiveLogic m_Current = null;
        /// <summary>
        /// 全局接收云端推送的的逻辑
        /// </summary>
        public static HdlCloudReceiveLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlCloudReceiveLogic();
                }
                return m_Current;
            }
        }
        /// <summary>
        /// 云端接收事件集合
        /// </summary>
        private List<CloudReceiveEventClass> ListCloudEvent = new List<CloudReceiveEventClass>();
        #endregion
        #region ■ 全局接收___________________________
        /// <summary>
        /// 特殊全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作 true:执行了特殊处理 false:没有执行特殊处理)
        /// </summary>
        /// <param name="topic">整个主题</param>
        /// <param name="byteData">接收的数据</param>
        /// <param name="mqttEncryptKey">Mqtt的解密密钥</param>
        /// <param name="homeIdEncryptKey">住宅倒序的解密密钥</param>
        public bool CloudOverallMsgReceiveEx(string topic, byte[] byteData, string mqttEncryptKey, string homeIdEncryptKey)
        {
            //设备入网上报主题
            if (topic == $"/user/{Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID}/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;
        }
        /// <summary>
        /// 特殊全局接收云端推送的的逻辑(为了执行速度,尽可能的别加耗时的操作 true:执行了特殊处理 false:没有执行特殊处理)
        /// </summary>
        /// <param name="topic">整个主题</param>
        /// <param name="msgData">接收的数据</param>
        public bool CloudOverallMsgReceiveEx(string topic, string msgData)
        {
            //没有任何监听
            if (ListCloudEvent.Count == 0) { return false; }
            //设备入网上报主题(目前只有红外宝)
            if (topic == $"/user/{Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID}/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 ■ 添加云端监听_______________________
        /// <summary>
        /// 添加云端接收事件
        /// </summary>
        /// <param name="i_mainKey">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="action">(参数1:枚举 参数2:推送消息)</param>
        public void AddCloudReceiveEvent(string i_mainKey, Action<CloudPushEnum, string> action)
        {
            try
            {
                var eventClass = new CloudReceiveEventClass();
                eventClass.MainKey = i_mainKey;
                eventClass.CloudReceiveEvent = action;
                this.ListCloudEvent.Add(eventClass);
            }
            catch { }
        }
        #endregion
        #region ■ 移除云端监听_______________________
        /// <summary>
        /// 移除云端接收事件
        /// </summary>
        /// <param name="i_mainKey">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        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 ■ 结构体_____________________________
        /// <summary>
        /// 云端接收事件类
        /// </summary>
        private class CloudReceiveEventClass
        {
            /// <summary>
            /// 标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)
            /// </summary>
            public string MainKey = string.Empty;
            /// <summary>
            /// 云端接收事件(参数1:枚举 参数2:推送消息)
            /// </summary>
            public Action<CloudPushEnum, string> CloudReceiveEvent = null;
        }
        #endregion
    }
}