xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Shared.Common;
using Shared.Phone.Device.Category;
using Shared.Phone.UserCenter.Device;
using ZigBee.Device;
using static ZigBee.Device.Panel;
 
namespace Shared.Phone.UserCenter.Bind
{
    public class BindInfo
    {
        /// <summary>
        /// 当前按键配置的功能
        /// </summary>
        public static int clusterID = 0;
 
        /// <summary>
        /// 添加绑定目标
        /// </summary>
        /// <param name="key">Key.</param>
        /// <param name="currentClusterID">Current cluster identifier.</param>
        /// <param name="currentMode">Current mode.</param>
        public static void AddBindTarget(Panel key, int currentClusterID, int currentMode)
        {
            var selectDialog = new Dialog() { };
            selectDialog.Show();
 
            var itemFL = new FrameLayout()
            {
                Y = Application.GetRealHeight(1920 - 600 + 130),
                Width = Application.GetRealWidth(900),
                Height = Application.GetRealHeight(550 - 130),
                Radius = 10,
                Gravity = Gravity.CenterHorizontal
            };
            selectDialog.AddChidren(itemFL);
 
            var selectedDeviceBtn = new Button()
            {
                Height = Application.GetRealHeight(130) - 1,
                TextID = R.MyInternationalizationString.Device,
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                TextAlignment = TextAlignment.Center,
                BackgroundColor = ZigbeeColor.Current.GXCTextWhiteColor
            };
            itemFL.AddChidren(selectedDeviceBtn);
            var selectedDeviceLine = new Button()
            {
                Y = selectedDeviceBtn.Bottom,
                Height = 1,
                BackgroundColor = ZigbeeColor.Current.GXCLineColor,
            };
            itemFL.AddChidren(selectedDeviceLine);
 
            var selectedSceneBtn = new Button()
            {
                Y = selectedDeviceLine.Bottom,
                Height = Application.GetRealHeight(130) - 1,
                TextID = R.MyInternationalizationString.Scence,
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                TextAlignment = TextAlignment.Center,
                BackgroundColor = ZigbeeColor.Current.GXCTextWhiteColor
            };
            itemFL.AddChidren(selectedSceneBtn);
 
 
            var cancelBtn = new Button()
            {
                Y = selectedSceneBtn.Bottom + Application.GetRealHeight(30),
                Height = Application.GetRealHeight(150),
                TextID = R.MyInternationalizationString.Cancel,
                TextColor = ZigbeeColor.Current.GXCTextWhiteColor,
                BackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor,
                TextAlignment = TextAlignment.Center,
            };
            itemFL.AddChidren(cancelBtn);
 
            //添加设备
            selectedDeviceBtn.MouseUpEventHandler += (send, ee) =>
            {
                selectDialog.Close();
 
                var addBindTargetsPage = new Shared.Phone.UserCenter.Bind.AddBindTargetsPage();
                Shared.Phone.UserView.HomePage.Instance.AddChidren(addBindTargetsPage);
                Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                addBindTargetsPage.Show(key, currentClusterID, true);
            };
            //添加场景
            selectedSceneBtn.MouseUpEventHandler += (send, ee) =>
            {
                selectDialog.Close();
                //被绑定设备的场景图片
                bool isExistScene = false;
                bool isExistDevice = false;
                foreach (var sc in PanelBindPage.bindList)
                {
                    if (sc.BindType == 2)
                    {
                        isExistScene = true;
                    }
                    else
                    {
                        isExistDevice = true;
                    }
                }
                if (isExistScene)
                {
                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindDeviceToScene), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(selectedSceneBtn);
                }
 
                else if (isExistDevice)
                {
                    var alert = new Alert(Language.StringByID(R.MyInternationalizationString.Tip), Language.StringByID(R.MyInternationalizationString.ChangeBindDeviceToScene),
                                                   Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                    alert.Show();
                    alert.ResultEventHandler += (sender1, e) =>
                    {
                        if (e)
                        {
                            var editScenePage = new Shared.Phone.Device.Category.Category();
                            Shared.Phone.UserView.HomePage.Instance.AddChidren(editScenePage);
                            Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                            editScenePage.Show();
                        }
                    };
                }
                else
                {
                    var addBindTargetsPage = new Shared.Phone.UserCenter.Bind.AddBindTargetsPage();
                    Shared.Phone.UserView.HomePage.Instance.AddChidren(addBindTargetsPage);
                    Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                    addBindTargetsPage.Show(key, currentClusterID, false);
                }
            };
 
            cancelBtn.MouseUpEventHandler += (send, ee) =>
            {
                selectDialog.Close();
            };
        }
 
        /// <summary>
        ///  检测控制面板(按键类)所拥有的功能,现支持的有以下几种(必定存在键值,出错会返回null)
        /// </summary>
        /// <returns>The panel key function level2.</returns>
        /// <param name="key">Key.</param>
        public static async System .Threading .Tasks .Task<Dictionary<string ,bool>> CheckPanelKeyFunctionLevel3 (Panel key)
        {
            Dictionary<string, bool> dicCheck = new Dictionary<string, bool>();
            dicCheck["场景:触发"] = false;
            dicCheck["开关:开"] = false;
            dicCheck["开关:关"] = false;
            dicCheck["开关:切换"] = false;
            dicCheck["亮度:调大"] = false;
            dicCheck["亮度:调小"] = false;
            dicCheck["亮度:切换"] = false;
            dicCheck["窗帘:开"] = false;
            dicCheck["窗帘:关"] = false;
            dicCheck["窗帘:停"] = false;
            dicCheck["窗帘:上升停"] = false;
            dicCheck["窗帘:下降停"] = false;
 
            //获取第一级功能
            var result =await key. GetPanelDeviceFunctionLevel1();
            if(result ==null){
                return null;
            }
 
            //面板没有按键类
            if(result .Contains (256) == false){
                return dicCheck;
            }
 
            //获取第二级功能
            result = await key.GetPanelDeviceFunctionLevel2(256);
            if (result == null)
            {
                return null;
            }
            //特殊功能
            if(result .Contains (1)==true){
                //获取第三级功能
               var result3 = await key.GetPanelDeviceFunctionLevel3(256,1);
                if(result3!=null)
                {
                    if(result3.Contains(1)==true){
                        dicCheck["场景:触发"] = true;
                    }
                }
            }
            //按键开关类
            if(result.Contains(100) == true)
            {
                //获取第三级功能
                var result3 = await key.GetPanelDeviceFunctionLevel3(256, 100);
                if (result3 != null)
                {
                    if (result3.Contains(100) == true)
                    {
                        dicCheck["开关:开"] = true;
                    }
                    if (result3.Contains(101) == true)
                    {
                        dicCheck["开关:关"] = true;
                    }
                    if (result3.Contains(102) == true)
                    {
                        dicCheck["开关:切换"] = true;
                    }
                }
            }
            //按键调光类
            if (result.Contains(200) == true)
            {
                //获取第三级功能
                var result3 = await key.GetPanelDeviceFunctionLevel3(256, 200);
                if (result3 != null)
                {
                    if (result3.Contains(200) == true)
                    {
                        dicCheck["亮度:调大"] = true;
                    }
                    if (result3.Contains(201) == true)
                    {
                        dicCheck["亮度:调小"] = true;
                    }
                    if (result3.Contains(202) == true)
                    {
                        dicCheck["亮度:切换"] = true;
                    }
                }
            }
            //窗帘类
            if (result.Contains(300) == true)
            {
                //获取第三级功能
                var result3 = await key.GetPanelDeviceFunctionLevel3(256, 300);
                if (result3 != null)
                {
                    if (result3.Contains(300) == true)
                    {
                        dicCheck["窗帘:开"] = true;
                    }
                    if (result3.Contains(301) == true)
                    {
                        dicCheck["窗帘:关"] = true;
                    }
                    if (result3.Contains(302) == true)
                    {
                        dicCheck["窗帘:停"] = true;
                    }
                    if (result3.Contains(303) == true)
                    {
                        dicCheck["窗帘:上升停"] = true;
                    }
                    if (result3.Contains(304) == true)
                    {
                        dicCheck["窗帘:下降停"] = true;
                    }
                }
            }
 
            return dicCheck;
        }
 
        ///// <summary>
        ///// 列表控件
        ///// </summary>
        //private VerticalScrolViewLayout listView = null;
        ///// <summary>
        ///// 已经存在的绑定设备
        ///// </summary>
        //private Dictionary<string, CommonDevice> dicEsixtDevice = new Dictionary<string, CommonDevice>();
 
        ///// <summary>
        ///// 获取设备的唯一主键
        ///// </summary>
        ///// <param name="device"></param>
        ///// <returns></returns>
        //public string GetDeviceMainKeys(CommonDevice device)
        //{
        //    return device.DeviceAddr + device.DeviceEpoint;
        //}
 
        ///// <summary>
        ///// 显示错误信息窗口
        ///// </summary>
        ///// <param name="msg"></param>
        //private void ShowErrorMsg(string msg)
        //{
        //    Application.RunOnMainThread(() =>
        //    {
        //        var contr = new Phone.UserCenter.ErrorMsgControl(msg);
        //        contr.Show();
        //    });
        //}
 
        ///// <summary>
        ///// 显示Tip信息窗口
        ///// </summary>
        ///// <param name="msg"></param>
        //private void ShowTipMsg(string msg)
        //{
        //    Application.RunOnMainThread(() =>
        //    {
        //        var contr = new Phone.UserCenter.TipViewControl(msg);
        //        contr.ShowView();
        //    });
        //}
 
        ///// <summary>
        ///// 显示一个需要确认的信息框
        ///// </summary>
        ///// <param name="msg">信息</param>
        ///// <param name="methodName">方法名(请确认这是一个共有方法)</param>
        ///// <param name="obj">回调函数的启动参数</param>
        //public void ShowConfirmMsg(string msg, string methodName = null, params object[] obj)
        //{
        //    Application.RunOnMainThread(() =>
        //    {
        //        var alert = new ConfirmMsgControl(msg);
        //        alert.Show();
 
        //        if (methodName != null)
        //        {
        //            alert.ResultEventHandler += (sender, result) =>
        //            {
        //                if (result == true)
        //                {
        //                    this.LoadFormMethodByName(this, methodName, obj);
        //                }
        //            };
        //        }
        //    });
        //}
 
        ///// <summary>
        ///// 执行指定画面的方法
        ///// </summary>
        ///// <param name="form">指定画面的英文名</param>
        ///// <param name="method">指定要加载的方法名</param>
        ///// <param name="parameter">启动参数</param>
        //public object LoadFormMethodByName(BindInfo form, string method, params object[] parameter)
        //{
        //    return form.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, form, parameter);
        //}
 
        ///// <summary>
        ///// 添加画面,启动参数由指定画面的ShowForm函数所指定
        ///// </summary>
        ///// <param name="newform">对象画面</param>
        ///// <param name="parameter">启动参数:参数由指定画面的ShowForm函数所指定</param>
        //public void AddForm(UserCenterCommonForm newform, params object[] parameter)
        //{
        //    //检测能否追加画面(防止画面二重添加),当点击过快时,会有几率二重添加
        //    if (UserCenterLogic.CheckCanAddForm(newform) == false)
        //    {
        //        return;
        //    }
        //    newform.FormID = UserCenterLogic.GetFormName(newform);
 
        //    UserView.HomePage.Instance.AddChidren(newform);
        //    UserView.HomePage.Instance.PageIndex += 1;
        //    //初始化界面框架
        //    newform.InitForm(parameter);
        //    //执行ShowForm()方法
        //    newform.LoadShowFormMethod(parameter);
        //}
 
        ///// <summary>
        ///// 能绑定到按键的设备
        ///// </summary>
        ///// <param name="device"></param>
        ///// <returns></returns>
        //public  bool SupportDevice(CommonDevice device,int currentClusterID)
        //{
        //    foreach (var data in device.InClusterList)
        //    {
        //        //拥有on/off功能的,才支持测试
        //        if (data.InCluster == currentClusterID)
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}
 
        ///// <summary>
        ///// 绑定PIR传感器的目标(返回成功设置的设备,错误时,返回null)
        ///// </summary>
        ///// <param name="i_iasZone">传感器</param>
        ///// <param name="listDevice">要绑定的目标设备</param>
        ///// <returns></returns>
        //public async Task<List<CommonDevice>> BindKeyDeviceTargets(Panel key, List<CommonDevice> listDevice,int currentClusterID)
        //{
        //    if (listDevice.Count == 0)
        //    {
        //        return new List<CommonDevice>();
        //    }
 
        //    var dicDevice = new Dictionary<string, CommonDevice>();
 
        //    //组装数据
        //    var addData = new Panel.AddBindData();
        //    addData.DeviceAddr = key.DeviceAddr;
        //    addData.Epoint = key.DeviceEpoint;
        //    foreach (var device in listDevice)
        //    {
        //        var info = new Panel.AddBindListObj();
        //        info.BindCluster = currentClusterID;
        //        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 = await key.AddDeviceBindAsync(addData);
        //    if (result.addedDeviceBindResponseData == null)
        //    {
        //        //如果网关已经掉线
        //        bool flage = Common.LocalGateway.Current.CheckGatewayOnLineOnTimeout(key.CurrentGateWayId, result.errorMessageBase);
        //        if (flage == false)
        //        {
        //            return null;
        //        }
        //        //绑定目标设置失败
        //        string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail);
        //        //拼接上【网关回复超时】的Msg
        //        msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg);
 
        //        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 = Shared.Common.LocalDevice .Current.GetDeviceEpointName(dicDevice[mainkeys]) + " ";
        //                msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
        //                this.ShowTipMsg(msg);
        //            }
        //        }
        //        //2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功
        //        else if (data.Result == 2)
        //        {
        //            if (result.addBindResultResponseData == null)
        //            {
        //                //设备名称 绑定失败
        //                string msg = Shared.Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + " ";
        //                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 = Shared.Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + " ";
        //                    msg += Language.StringByID(R.MyInternationalizationString.uBindListIsFull);
        //                    this.ShowTipMsg(msg);
        //                }
        //                else
        //                {
        //                    //设备名称 绑定失败
        //                    string msg = Shared.Common.LocalDevice.Current.GetDeviceEpointName(dicDevice[mainkeys]) + " ";
        //                    msg += Language.StringByID(R.MyInternationalizationString.BindFailed);
        //                    this.ShowTipMsg(msg);
        //                }
        //            }
        //        }
        //    }
 
        //    return listSuccess;
        //}
 
        ///// <summary>
        ///// 显示设备选择的界面
        ///// </summary>
        //public  void ShowDeviceSelectForm(Panel key, int currentClusterID)
        //{
        //    var listShowDevice = new List<CommonDevice>();
        //    foreach (var device in PanelBindPage.list)
        //    {
        //        if (SupportDevice(device, currentClusterID) == false)
        //        {
        //            continue;
        //        }
 
        //        string mainKeys = this.GetDeviceMainKeys(device);
        //        if (this.dicEsixtDevice.ContainsKey(mainKeys) == true)
        //        {
        //            //如果已经添加了,则不再显示
        //            continue;
        //        }
 
        //        listShowDevice.Add(device);
        //    }
 
        //    var listSelect = new List<string>();
        //    foreach (string mainkeys in this.dicEsixtDevice.Keys)
        //    {
        //        listSelect.Add(mainkeys);
        //    }
 
        //    var form = new SelectDeviceForm();
        //    this.AddForm(form, listShowDevice, listSelect, false);
        //    //添加绑定目标
        //    form.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddBindTargets));
 
        //    //设备选择确定
        //    form.ActionSelectDevice += (async (listDevice) =>
        //    {
        //        if (listDevice.Count == 0)
        //        {
        //            return;
        //        }
 
        //        var listNewDevice = await this.BindKeyDeviceTargets(key, listDevice,currentClusterID);
 
        //        if (listNewDevice == null || listNewDevice.Count == 0)
        //        {
        //            return;
        //        }
 
        //        foreach (CommonDevice device in listNewDevice)
        //        {
        //            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
        //            this.dicEsixtDevice[mainKeys] = device;
 
        //            Application.RunOnMainThread(() =>
        //            {
        //                this.AddRowlayout(device);
        //            });
        //        }
        //    });
        //}
 
        ///// <summary>
        ///// 添加行
        ///// </summary>
        ///// <param name="device"></param>
        //private void AddRowlayout(CommonDevice device)
        //{
        //    string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
        //    this.dicEsixtDevice[mainKeys] = device;
 
        //    var row = new DeviceRoomViewRow(this.listView, device);
        //    row.ChangedChidrenBindMode(row.btnIcon, ChidrenBindMode.BindEventOnly);
        //    row.ChangedChidrenBindMode(row.btnRoom, ChidrenBindMode.BindEventOnly);
        //    row.ChangedChidrenBindMode(row.btnDevie, ChidrenBindMode.BindEventOnly);
 
        //    //删除
        //    var btnDelete = new RowDeleteButton();
        //    row.AddRightView(btnDelete);
        //    btnDelete.MouseUpEventHandler += (sender, e) =>
        //    {
        //        //确认要删除吗?
        //        string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
        //        this . ShowConfirmMsg(msg, "DeleteTargetDevice", device, row);
        //    };
        //}
 
    }
}