JLChen
2021-06-08 dc20ddb39af74fc2a0080d61a4da811206b1a0d9
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
using System;
using HDL_ON.DAL.Server;
using Shared;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace HDL_ON.UI.UI2.PersonalCenter.PirDevice
{
    public class PirMethod
    {
 
        /// <summary>
        /// 红外宝程序主入口
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="function"></param>
        public void MainView(FrameLayout frame, Entity.Function function, Action action)
        {
            Pir pirDevice = new Pir();
            if (function != null)
            {//数据转换
                pirDevice.name = function.name;
                pirDevice.deviceId = function.deviceId;
                pirDevice.sid = function.sid;
                pirDevice.online = function.online;
                pirDevice.versions = function.versions;
            }
            Pir.currPir = pirDevice;
            GetControlList(frame, () =>
            {
                Application.RunOnMainThread(() =>
                {
                    var page = new PirMain();
                    MainPage.BasePageView.AddChidren(page);
                    page.Show();
                    MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                    PirMain.BackAction += () =>
                    {
                        action?.Invoke();
                        PirMain.BackAction = null;
                    };
                });
            }, Pir.currPir);
        }
 
        /// <summary>
        /// 管理位置
        /// </summary>
        /// <param name="control">当前设备</param>
        /// <param name="action">回调函数</param>
        public void ManagementPosition(Entity.Function control, Action action)
        {
            var view = new ChooseRoomPage(control, action);
            MainPage.BasePageView.AddChidren(view);
            view.LoadPage();
            MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
        }
        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="tipText">提示标题文本</param>
        /// <param name="list">当前存在名称列表</param>
        /// <param name="currName">当前名称</param>
        /// <param name="action">回调函数</param>
        public void EditControlName(int tipText, List<string> list, string currName, Action<string, Dialog> action, Action actionCancel, bool tag = false)
        {
            new View.TipView().InputBox(tipText, currName, StringId.nameNull, StringId.NameAlreadyExists, list, (text, view
                ) =>
            {
                action(text, view);
            }, () => { actionCancel(); }, tag);
        }
        /// <summary>
        /// 添加遥控器的方法
        /// </summary>
        /// <param name="frameLayout">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public void AddControl(FrameLayout frameLayout, Action<Control> action)
        {
            View.TipView tipView = new View.TipView();
            tipView.InputBox(frameLayout, "", (name, frame) =>
            {
                if (Pir.currPir != null && Pir.currPir.FunctioList.Count < 10)
                {
                    Control control = new Control();
                    control.name = name;
                    control.type = "learn";
                    control.spk = "ir.learn";
                    control.deviceId = Pir.currPir.deviceId;
                    ThreadAddControl(control, frame, action);
                }
                else
                {
                    View.TipView tt = new View.TipView();
                    tt.TipBox(StringId.tip, StringId.bunengchaoguo10);
                }
            }, false);
 
        }
        /// <summary>
        /// 发送遥控器命令方法
        /// </summary>
        /// <param name="control">发送参数对象</param>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public void ThreadAddControl(Control control, FrameLayout frame, Action<Control> action)
        {
            Cloud mqttdate = null;
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    //发送添加命令
                    var responsePackNew = PirSend.Add(control);
                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                    {
                        string sid = responsePackNew.Data.ToString();
                        controldata = "";
                        mqttdate = MqttDate("遥控器", sid);
                        if (mqttdate != null)
                        {
                            control.sid = sid;
                        }
                    }
 
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        ///这里:监听MTTP推送下来主题;
                        if (mqttdate != null)
                        {
                            //休眠1000毫秒,为等待云端创建deviceid;
                            System.Threading.Thread.Sleep(1000);
                            //读取添加遥控器deviceID,才知道是否添加成功;
                            GetControl(frame, control, (device) =>
                            {
                                if (device != null)
                                {
 
                                    control.deviceId = device.deviceId;
                                    //遥控器添加到列表;
                                    if (null == Pir.currPir.FunctioList.Find((c) => c.deviceId == device.deviceId))
                                    {
                                        Pir.currPir.FunctioList.Add(device);
                                    }
                                    frame.RemoveFromParent();//添加成功关闭弹窗
                                    action(control);
                                }
                                else
                                {
                                    //监听Mqtt推送下来状态码做提示
                                    View.FailView failView = new View.FailView();
                                    failView.ShouError((view) =>
                                    {
                                        view.Close();
                                        ThreadAddControl(control, frame, action);
                                    });
 
                                }
 
                            });
                        }
                        else
                        {
                            //监听Mqtt推送下来状态码做提示
                            View.FailView failView = new View.FailView();
                            failView.ShouError((view) =>
                            {
                                view.Close();
                                ThreadAddControl(control, frame, action);
                            });
                        }
 
 
                    });
 
                }
            })
            { IsBackground = true }.Start();
 
        }
        /// <summary>
        /// 获取红外设备列表
        /// </summary>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public static void GetPirDeviceList(FrameLayout frame, Action action)
        {
            //清除之前列表;
            Pir.pirDeviceList.Clear();
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    var responsePackNew = PirSend.GetDeviceList("ir.module");
                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                    {
                        var jobject = Newtonsoft.Json.Linq.JObject.Parse(responsePackNew.Data.ToString());
                        string list = jobject["list"].ToString();
 
                        var jArray = Newtonsoft.Json.Linq.JArray.Parse(list);
                        for (int a = 0; a < jArray.Count; a++)
                        {
                            var jay = jArray[a];
                            string spk = jay["spk"].ToString();
                            if (spk == "ir.module")
                            {
                                //数据返序列化为Logic对象
                                var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Pir>(str);
                                if (pirJosn != null)
                                {
 
                                    if (null == PirDevice.Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
                                    {
                                        PirDevice.Pir.pirDeviceList.Add(pirJosn);
                                    }
                                }
                            }
 
                        }
 
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        try
                        {
                            if (Pir.pirDeviceList.Count != 0)
                            {
                                GetControlList(() =>
                                {
                                    loading.Hide();
                                    action();
 
                                });
                            }
                            else
                            {
                                loading.Hide();
                                PirMethod method = new PirMethod();
                                method.ErrorShow(null, "读取红外宝列表失败");
                            }
                        }
                        catch { }
 
                    });
                }
 
            })
            { IsBackground = true }.Start();
 
        }
        /// <summary>
        /// 获取所有红外宝遥控器列表
        /// </summary>
        /// <param name="action">回调函数</param>
        public static void GetControlList(Action action)
        {
 
            new System.Threading.Thread(() =>
            {
                try
                {
                    for (int i = 0; i < Pir.pirDeviceList.Count; i++)
                    {
                        var pirDevice = Pir.pirDeviceList[i];
                        try
                        {
                            var responsePackNew = PirSend.ControlList(pirDevice.deviceId);
                            if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                            {
                                var jArray = JArray.Parse(responsePackNew.Data.ToString());
                                for (int a = 0; a < jArray.Count; a++)
                                {
                                    var jay = jArray[a];
                                    //数据返序列化为Logic对象
                                    var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                    var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                                    if (pirJosn != null)
                                    {
                                        if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid))
                                        {
                                            pirDevice.FunctioList.Add(pirJosn);
                                        }
                                    }
 
                                }
                            }
 
                        }
                        catch { }
 
                    }
                }
                catch { }
                finally
                {
 
                    Application.RunOnMainThread(() =>
                    {
                        action();
                    });
                }
 
            })
            { IsBackground = true }.Start();
 
        }
 
        /// <summary>
        /// 获取单个红外宝遥控器列表
        /// </summary>
        /// <param name="action">回调函数</param>
        public static void GetControlList(FrameLayout frame, Action action, Pir pirDevice)
        {
            ThreadSend(new Control { deviceId = pirDevice.deviceId }, (responsePackNew) =>
            {
                var jArray = JArray.Parse(responsePackNew.Data.ToString());
                for (int a = 0; a < jArray.Count; a++)
                {
                    var jay = jArray[a];
                    //数据返序列化为Logic对象
                    var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                    var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                    if (pirJosn != null)
                    {
                        if (null == pirDevice.FunctioList.Find((c) => c.deviceId == pirJosn.deviceId))
                        {
                            pirDevice.FunctioList.Add(pirJosn);
                        }
                    }
                }
                action();
            }, "获取遥控器列表", "frame", frame, null);
 
        }
        /// <summary>
        /// 获取设备详情通过(spk,sid)
        /// </summary>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="control">发送参数对象</param>
        /// <param name="action">回调函数</param>
        public void GetControl(FrameLayout frame, Control control, Action<Entity.Function> action)
        {
            ThreadSend(control, (responsePackNew) =>
            {
                var function = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(responsePackNew.Data.ToString());
                action(function);
            }, "获取设备详情", "frame", frame, null);
        }
        /// <summary>
        /// 发送命令线程
        /// </summary>
        /// <param name="control">发送数据对象</param>
        /// <param name="action">回调函数</param>
        /// <param name="str">判断字符</param>
        /// <param name="view">判断log父控件</param>
        /// <param name="frame">log父控件</param>
        /// <param name="dialog">log父控件</param>
        /// <param name="attributesStatus">学习按键</param>
        public static void ThreadSend(Control control, Action<ResponsePackNew> action, string str, string view, FrameLayout frame, Dialog dialog, Entity.AttributesStatus attributesStatus = null)
        {
 
            //加载log
            Loading loading = new Loading();
            if (view == "dialog")
            {
                dialog.AddChidren(loading);
            }
            else
            {
                frame.AddChidren(loading);
            }
            loading.Start();
            ResponsePackNew responsePackNew = null;
            new System.Threading.Thread(() =>
            {
                try
                {
                    if (str == "删除")
                    {
                        responsePackNew = PirSend.DeleteDevice(control.deviceId);
                    }
                    else if (str == "修改名称")
                    {
 
                        responsePackNew = PirSend.DeviceRename(control.deviceId, control.name);
                    }
                    else if (str == "删除按键")
                    {
                        if (attributesStatus != null)
                        {
                            responsePackNew = PirSend.CodeRemove(attributesStatus, control.deviceId);
                        }
                    }
                    else if (str == "获取设备详情")
                    {
                        // 获取设备详情通过(spk,sid)
                        responsePackNew = PirSend.GetinfoBySid(control);
                    }
                    else if (str == "获取遥控器列表")
                    {
                        responsePackNew = PirSend.ControlList(control.deviceId);
                    }
                    else if (str == "库码测试")
                    {
                        responsePackNew = PirSend.CodeTest(control);
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                        {
                            action(responsePackNew);
                        }
                        else
                        {
                            PirMethod method = new PirMethod();
                            //自定义错误提示文本
                            string eorroText = "";
                            if (str == "删除")
                            {
                            }
                            else if (str == "修改名称")
                            {
 
                            }
                            else if (str == "删除按键")
                            {
                            }
                            else if (str == "获取设备详情")
                            {
                            }
                            else if (str == "获取遥控器列表")
                            {
                            }
                            else if (str == "库码测试")
                            {
                            }
                            method.ErrorShow(responsePackNew, eorroText);
                        }
 
                    });
                }
 
            })
            { IsBackground = true }.Start();
 
 
        }
        /// <summary>
        /// MQTT主题推送下来的遥控器数据
        /// </summary>
        public static string controldata = "";
        /// <summary>
        /// MQTT主题推送下来按键的数据
        /// </summary>
        public static string buttondata = "";
        /// <summary>
        /// 判断这个主题是否是添加遥控器主题
        /// </summary>
        /// <param name="text">表示不同主题数据</param>
        /// <param name="sid">唯一标识</param>
        /// <param name="timeValue">等待时间值</param>
        /// <returns></returns>
        public Cloud MqttDate(string text, string sid, int timeValue = 10)
        {
            Cloud cloud = null;
            var dateTime = DateTime.Now;
            while ((DateTime.Now - dateTime).TotalMilliseconds < timeValue * 1000)
            {
                string str = "";
                if (text == "遥控器")
                {
                    str = controldata;
                }
                else if (text == "按键")
                {
                    str = buttondata;
                }
 
                if (!string.IsNullOrEmpty(str))
                {
                    try
                    {
                        var cloudjson = Newtonsoft.Json.JsonConvert.DeserializeObject<Cloud>(str);
                        for (int i = 0; i < cloudjson.objects.Count; i++)
                        {
                            var objects = cloudjson.objects[i];
                            if (sid == objects.sid)
                            {
                                cloud = cloudjson;
                                break;
                            }
                        }
                        if (cloud != null)
                        {
                            break;
                        }
                    }
                    catch { }
                }
 
            }
            return cloud;
        }
        /// <summary>
        /// 错误码提示
        /// </summary>
        /// <param name="responsePackNew"></param>
        /// <param name="text">自定义错误文本</param>
        /// <param name="popValue">弹框类型(1=闪烁弹框)</param>
        public void ErrorShow(ResponsePackNew responsePackNew, string text, int popValue = 1)
        {
            string str = "";
            if (text == "删除遥控器")
            {
                str = Language.StringByID(StringId.delFail);
            }
            else if (text == "读取红外宝列表失败")
            {
                str = Language.StringByID(StringId.huoqushujushibao);
            }
            else if (text == "添加失败")
            {
                str = Language.StringByID(StringId.tianjiashibai);
            }
            else
            {
                if (responsePackNew != null)
                {
 
                    switch (responsePackNew.Code)
                    {
 
                        case "14005":
                            {
                                str = Language.StringByID(StringId.gatewayNotOnline);
                            }
                            break;
                        case "10807":
                            {
                                str = Language.StringByID(StringId.bunengchaoguo10);
                            }
                            break;
                        case "14002":
                            {
                                str = Language.StringByID(StringId.bucunzaichanpin);
                            }
                            break;
                        case "10805":
                            {
                                str = Language.StringByID(StringId.shebeibucunzai);
                            }
                            break;
                        case "14006":
                            {
                                str = Language.StringByID(StringId.shebeibuzaixian);
                            }
                            break;
                        case "9":
                            {
                                str = Language.StringByID(StringId.wangguanshebeibuzaixian);
                            }
                            break;
                        case "2":
                            {
                                str = Language.StringByID(StringId.xitongweihuzhong);
                            }
                            break;
                        default:
                            {
                                str = Language.StringByID(StringId.huoqushujushibao);
                            }
                            break;
 
                    }
                }
            }
            switch (popValue)
            {
                case 1:
                    {
                        new Intelligence.Automation.LogicView.TipPopView().FlashingBox(str);
                    }
                    break;
                case 2: { } break;
                case 3: { } break;
            }
        }
        /// <summary>
        ///指定刷新界面
        /// </summary>
        /// <param name="strView">判断字符</param>
        public static void RefreshView(string strView)
        {
            //标记是不是已经刷新完成
            bool if_bool = false;
            for (int i = MainPage.BasePageView.ChildrenCount - 1; 0 <= i; i--)
            {
                var view = MainPage.BasePageView.GetChildren(i);
                if (strView == "PirMain")
                {
                    if (view.GetType() == typeof(PirMain))
                    {
                        //强制转换对象
                        var f = (PirMain)view;
                        //移除所有子控件
                        f.RemoveAll();
                        //重新加载UI
                        f.Show();
                        //退出for循环
                        //break;
                        if_bool = true;
                    }
                }
                if (if_bool)
                {
                    //退出for循环
                    break;
                }
            }
        }
        /// <summary>
        /// 指定删除界面
        /// </summary>
        /// <param name="strView">判断字符</param>
        public static void RemoveView(string strView)
        {
 
            for (int i = MainPage.BasePageView.ChildrenCount - 1; 0 <= i; i--)
            {
                var view = MainPage.BasePageView.GetChildren(i);
                if (strView == "PirMain")
                {
                    if (view.GetType() == typeof(PirMain))
                    {
                        //移除界面
                        view.RemoveFromParent();
                    }
 
                }
                else if (strView == "AddControl")
                {
 
                    if (view.GetType() == typeof(AddControl))
                    {
                        //找到移除
                        view.RemoveFromParent();
                    }
                }
                else if (strView == "AddControlComplete")
                {
 
                    if (view.GetType() == typeof(AddControlComplete))
                    {
                        //找到移除
                        view.RemoveFromParent();
                    }
                }
 
            }
        }
 
    }
    [Serializable]
    public class Cloud
    {
        public string id = "";
        public List<Objects> objects = new List<Objects>();
        public string time_stamp = string.Empty;
    }
    [Serializable]
    public class Objects
    {
        public string sid = string.Empty;
        public string spk = string.Empty;
        public List<Attributes> attributes = new List<Attributes>();
    }
    [Serializable]
    public class Attributes
    {
        public string key = "";
        public string data_type = "";
        public List<string> value = new List<string>();
 
    }
}