gxc
2019-10-29 081ea8d273048fd03756718ac6fb48a3c09218e9
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
using System.Threading.Tasks;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 设备绑定的逻辑
    /// </summary>
    public class HdlDeviceBindLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备绑定的逻辑
        /// </summary>
        private static HdlDeviceBindLogic m_Current = null;
        /// <summary>
        /// 设备绑定的逻辑
        /// </summary>
        public static HdlDeviceBindLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceBindLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 获取设备下面绑定的设备_____________
 
        /// <summary>
        /// 获取设备下面绑定的设备(错误时返回null)
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <returns></returns>
        public async Task<List<CommonDevice>> GetBindTargetDevice(CommonDevice mainDevice)
        {
            var result = (BindObj.GetDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "GetDeviceBindAsync");
            if (result == null || result.getAllBindResponseData == null)
            {
                //获取设备的绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceBindTargetFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return null;
            }
            var listDevice = new List<CommonDevice>();
            foreach (var data in result.getAllBindResponseData.BindList)
            {
                CommonDevice deviceTemp = Common.LocalDevice.Current.GetDevice(data.BindMacAddr, data.BindEpoint);
                if (deviceTemp == null)
                {
                    continue;
                }
                listDevice.Add(deviceTemp);
            }
            return listDevice;
        }
 
        #endregion
 
        #region ■ 执行绑定设备目标___________________
 
        /// <summary>
        /// 绑定设备的目标(返回成功设置的设备,错误时,返回null)
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="listDevice">要绑定的目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns></returns>
        public async Task<List<CommonDevice>> BindDeviceTarget(CommonDevice mainDevice, List<CommonDevice> listDevice, int BindCluster = 6)
        {
            if (listDevice.Count == 0)
            {
                return new List<CommonDevice>();
            }
 
            var dicDevice = new Dictionary<string, CommonDevice>();
 
            //组装数据
            var addData = new IASZone.AddBindData();
            addData.DeviceAddr = mainDevice.DeviceAddr;
            addData.Epoint = mainDevice.DeviceEpoint;
            foreach (var device in listDevice)
            {
                var info = new IASZone.AddBindListObj();
                info.BindCluster = BindCluster;
                info.BindMacAddr = device.DeviceAddr;
                info.BindEpoint = device.DeviceEpoint;
                info.BindType = 0;
 
                addData.BindList.Add(info);
 
                //返回成功设备的时候使用
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                dicDevice[mainkeys] = device;
            }
 
            var result = (BindObj.AddedDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "AddDeviceBindAsync", addData);
            if (result == null || result.addedDeviceBindResponseData == null)
            {
                //绑定目标设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return null;
            }
 
            var listSuccess = new List<CommonDevice>();
            foreach (var data in result.addedDeviceBindResponseData.BindList)
            {
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint);
                //0:添加成功  3:已经存在,也可以代表成功
                if (data.Result == 0 || data.Result == 3)
                {
                    if (dicDevice.ContainsKey(mainkeys) == true)
                    {
                        listSuccess.Add(dicDevice[mainkeys]);
                    }
                }
                //1:失败,节点设备或场景不存在
                else if (data.Result == 1)
                {
                    if (dicDevice.ContainsKey(mainkeys) == true)
                    {
                        //设备名称 绑定失败
                        string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                        msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
                        this.ShowTipMsg(msg);
                    }
                }
                //2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
                else if (data.Result == 2)
                {
                    if (result.addBindResultResponseData == null)
                    {
                        //设备名称 绑定失败
                        string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                        msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
                        this.ShowTipMsg(msg);
                    }
                    else
                    {
                        //添加成功
                        if (result.addBindResultResponseData.Result == 0)
                        {
                            if (dicDevice.ContainsKey(mainkeys) == true)
                            {
                                listSuccess.Add(dicDevice[mainkeys]);
                            }
                        }
                        //设备名称 绑定列表已满
                        else if (result.addBindResultResponseData.Result == 140)
                        {
                            string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                            msg += Language.StringByID(R.MyInternationalizationString.uBindListIsFull);
                            this.ShowTipMsg(msg);
                        }
                        else
                        {
                            //设备名称 绑定失败
                            string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                            msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
                            this.ShowTipMsg(msg);
                        }
                    }
                }
            }
 
            return listSuccess;
        }
 
        #endregion
 
        #region ■ 删除设备绑定的目标_________________
 
        /// <summary>
        /// 删除设备绑定的目标
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="deleteDevice">要删除的绑定目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns></returns>
        public async Task<bool> DeleteDeviceTarget(CommonDevice mainDevice, CommonDevice deleteDevice, int BindCluster = 6)
        {
            //组装数据
            var deleteData = new IASZone.DelDeviceBindData();
            deleteData.DeviceAddr = mainDevice.DeviceAddr;
            deleteData.Epoint = mainDevice.DeviceEpoint;
 
            var info = new IASZone.RemoveBindListObj();
            info.BindCluster = BindCluster;
            info.BindMacAddr = deleteDevice.DeviceAddr;
            info.BindEpoint = deleteDevice.DeviceEpoint;
            info.BindType = 0;
 
            deleteData.RemoveBindList.Add(info);
 
            var result = (BindObj.DelDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "DelDeviceBindAsync", deleteData);
            if (result == null || result.delDeviceBindResponseData == null)
            {
                //删除绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return false;
            }
 
            foreach (var data in result.delDeviceBindResponseData.RemoveBindList)
            {
                //0:成功  1:设备不在绑定列表中 ,也可以代表成功
                if (data.Result == 0 || data.Result == 1)
                {
                    return true;
                }
                //3:失败,在等待节点设备确认是否解除绑定成功
                else if (data.Result == 3)
                {
                    //其他绑定目标正在删除中,请稍后再试
                    string msg = Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait);
                    this.ShowErrorMsg(msg);
                    return false;
                }
                //4:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
                else if (data.Result == 4)
                {
                    if (result.removeBindResultResponseData == null)
                    {
                        //删除绑定目标失败
                        string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                        this.ShowErrorMsg(msg);
                        return false;
                    }
                    else
                    {
                        //成功
                        if (result.removeBindResultResponseData.Result == 0)
                        {
                            return true;
                        }
                        //136:控制设备本地绑定列表中无此绑定
                        else if (result.removeBindResultResponseData.Result == 136)
                        {
                            //这个可以当做成功
                            return true;
                        }
                        else
                        {
                            //删除绑定目标失败
                            string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                            this.ShowErrorMsg(msg);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
 
        /// <summary>
        /// 删除设备绑定的目标
        /// </summary>
        /// <param name="mainDevice">设备对象</param>
        /// <param name="listDeleteDevice">要删除的绑定目标设备</param>
        /// <param name="BindCluster">BindCluster</param>
        /// <returns>返回的是成功删除的设备</returns>
        public async Task<List<CommonDevice>> DeleteDeviceTarget(CommonDevice mainDevice, List<CommonDevice> listDeleteDevice, int BindCluster = 6)
        {
            //组装数据
            var deleteData = new IASZone.DelDeviceBindData();
            deleteData.DeviceAddr = mainDevice.DeviceAddr;
            deleteData.Epoint = mainDevice.DeviceEpoint;
 
            var dicDevice = new Dictionary<string, CommonDevice>();
            foreach (var device in listDeleteDevice)
            {
                var info = new IASZone.RemoveBindListObj();
                info.BindCluster = BindCluster;
                info.BindMacAddr = device.DeviceAddr;
                info.BindEpoint = device.DeviceEpoint;
                info.BindType = 0;
 
                deleteData.RemoveBindList.Add(info);
                //返回成功设备的时候使用
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                dicDevice[mainkeys] = device;
            }
            var listSuccess = new List<CommonDevice>();
 
            var result = (BindObj.DelDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "DelDeviceBindAsync", deleteData);
            if (result == null || result.delDeviceBindResponseData == null)
            {
                //删除绑定目标失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
 
                this.ShowErrorMsg(msg);
                return listSuccess;
            }
 
            foreach (var data in result.delDeviceBindResponseData.RemoveBindList)
            {
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint);
                //0:成功  1:设备不在绑定列表中 ,也可以代表成功
                if (data.Result == 0 || data.Result == 1)
                {
                    listSuccess.Add(dicDevice[mainkeys]);
                }
                //3:失败,在等待节点设备确认是否解除绑定成功
                else if (data.Result == 3)
                {
                    //设备名称 其他绑定目标正在删除中,请稍后再试
                    string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                    msg += Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait);
                    this.ShowErrorMsg(msg);
                }
                //4:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
                else if (data.Result == 4)
                {
                    if (result.removeBindResultResponseData == null)
                    {
                        //设备名称 删除绑定目标失败
                        string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                        msg += Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                        this.ShowErrorMsg(msg);
                    }
                    else
                    {
                        //成功
                        if (result.removeBindResultResponseData.Result == 0)
                        {
                            listSuccess.Add(dicDevice[mainkeys]);
                        }
                        //136:控制设备本地绑定列表中无此绑定
                        else if (result.removeBindResultResponseData.Result == 136)
                        {
                            //这个可以当做成功
                            listSuccess.Add(dicDevice[mainkeys]);
                        }
                        else
                        {
                            //设备名称 删除绑定目标失败
                            string msg = Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n";
                            msg += Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail);
                            this.ShowErrorMsg(msg);
                        }
                    }
                }
            }
            return listSuccess;
        }
 
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 执行指定设备对象类里面的方法(注意:这个是专门调用异步,并且等待异步完成的高科技函数,不调用异步的情况,别使用此函数)
        /// </summary>
        /// <param name="device">需要执行的设备的设备对象</param>
        /// <param name="method">指定要加载的方法名</param>
        /// <param name="parameter">启动参数</param>
        private async Task<object> LoadDeviceMethodByNameAsync(CommonDevice device, string method, params object[] parameter)
        {
            var task = device.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, device, parameter) as Task;
            await task;
 
            var result = task.GetType().GetProperty("Result").GetValue(task, null);
            return result;
        }
 
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowErrorMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Error, msg);
                contr.Show();
            });
        }
 
        /// <summary>
        /// 显示Tip信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTipMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new UserCenter.ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
            });
        }
 
        #endregion
    }
}