wxr
2021-07-01 43b0d5870d528f23ecd6aeceb6cfd4325188b46f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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.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;
        }
 
        /// <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.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 ■ 添加云端监听_______________________
 
        /// <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
    }
}