wei
2021-03-23 fe2b566c8d9c097d2568ad211088fa5bc6f8c229
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
using HDL_ON.Stan;
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HDL_ON.UI
{
    /// <summary>
    /// 添加Evoyo的Mini智能遥控器步骤4界面
    /// </summary>
    public class AddMiniRemoteControlDirection4Page : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// wifi名字
        /// </summary>
        private string wifiName = string.Empty;
        /// <summary>
        /// wifi密码
        /// </summary>
        private string wifiPsw = string.Empty;
        /// <summary>
        /// 新设备的sid(用来匹配哪一个设备是新的)
        /// </summary>
        private string newDeviceSid = string.Empty;
        /// <summary>
        /// 超时线程是否运行
        /// </summary>
        private bool timeoutThreadActivity = false;
        /// <summary>
        /// 上传到云的图标
        /// </summary>
        private IconViewControl btnClound = null;
        /// <summary>
        /// 上传到云
        /// </summary>
        private NormalViewControl btnCloundView = null;
        /// <summary>
        /// 连接成功的图标
        /// </summary>
        private IconViewControl btnConnect = null;
        /// <summary>
        /// 连接成功
        /// </summary>
        private NormalViewControl btnConnetView = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_wifiName">wifi名字</param>
        /// <param name="i_wifiPsw">wifi密码</param>
        public void ShowForm(string i_wifiName, string i_wifiPsw)
        {
            this.wifiName = i_wifiName;
            this.wifiPsw = i_wifiPsw;
 
            //设置头部信息
            base.SetTitleText(Language.StringByID(StringId.AddInfraredRemoteControl));
            //这个界面的背景需要白色
            bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
 
            //添加接收蓝牙反馈的事件
            HdlBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
 
            //添加云端反馈事件
            HdlCloudReceiveLogic.Current.AddCloudReceiveEvent("AddMiniRemoteControlDirection4Page", this.CloudReceiveEvent);
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空body
            this.ClearBodyFrame();
            //图片
            var btnPic = new PicViewControl(282, 121);
            btnPic.Y = Application.GetRealHeight(122);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "PersonalCenter/AddDevice/WifiAndPhoneConnect.png";
            bodyFrameLayout.AddChidren(btnPic);
            //正在连接中...
            var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(20), false);
            btnSearch.Y = btnPic.Bottom + Application.GetRealHeight(40);
            btnSearch.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnSearch.TextID = StringId.NowConnectting;
            btnSearch.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnSearch);
            //请让红外遥控尽量接近WIFI路由器
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg5);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.PromptingColor1, Application.GetRealHeight(20), btnSearch.Bottom + Application.GetRealHeight(4));
 
            //网络连接中
            var btnNetIcon = new IconViewControl(20);
            btnNetIcon.X = Application.GetRealWidth(60);
            btnNetIcon.Y = btnPic.Bottom + Application.GetRealHeight(265);
            btnNetIcon.UnSelectedImagePath = "PersonalCenter/AddDevice/NetConnectSelect.png";
            bodyFrameLayout.AddChidren(btnNetIcon);
            var btnNetView = new NormalViewControl(140, 18, true);//左右间距40再加文本60
            btnNetView.Y = btnNetIcon.Bottom + Application.GetRealHeight(16);
            btnNetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnNetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnNetView.TextAlignment = TextAlignment.Center;
            btnNetView.TextID = StringId.InTheNetworkLink;
            bodyFrameLayout.AddChidren(btnNetView);
 
            //一条横线
            var btnLine1 = new NormalViewControl(Application.GetRealWidth(92), 2, false);
            btnLine1.X = btnNetIcon.Right + Application.GetRealWidth(3);
            btnLine1.Y = btnNetIcon.Y + (btnNetIcon.Height + 2) / 2;
            btnLine1.BackgroundColor = CSS.CSS_Color.PromptingColor1;
            bodyFrameLayout.AddChidren(btnLine1);
 
            //上传到云
            this.btnClound = new IconViewControl(20);
            btnClound.Gravity = Gravity.CenterHorizontal;
            btnClound.Y = btnNetIcon.Y;
            btnClound.UnSelectedImagePath = "PersonalCenter/AddDevice/UploadToClound.png";
            btnClound.SelectedImagePath = "PersonalCenter/AddDevice/UploadToCloundSelect.png";
            bodyFrameLayout.AddChidren(btnClound);
            this.btnCloundView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
            btnCloundView.Y = btnNetView.Y;
            btnCloundView.Gravity = Gravity.CenterHorizontal;
            btnCloundView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnCloundView.TextColor = CSS.CSS_Color.PromptingColor1;
            btnCloundView.TextAlignment = TextAlignment.Center;
            btnCloundView.TextID = StringId.UploadToCloud;
            bodyFrameLayout.AddChidren(btnCloundView);
 
            //一条横线
            var btnLine2 = new NormalViewControl(btnLine1.Width, btnLine1.Height, false);
            btnLine2.X = btnClound.Right + Application.GetRealWidth(3);
            btnLine2.Y = btnLine1.Y;
            btnLine2.BackgroundColor = CSS.CSS_Color.PromptingColor1;
            bodyFrameLayout.AddChidren(btnLine2);
 
            //连接成功
            this.btnConnect = new IconViewControl(20);
            btnConnect.X = bodyFrameLayout.Width - btnNetIcon.X - btnConnect.IconSize;
            btnConnect.Y = btnNetIcon.Y;
            btnConnect.UnSelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccess.png";
            btnConnect.SelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccessSelect.png";
            bodyFrameLayout.AddChidren(btnConnect);
            this.btnConnetView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
            btnConnetView.X = bodyFrameLayout.Width - btnNetView.Width - btnNetView.X;
            btnConnetView.Y = btnNetView.Y;
            btnConnetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnConnetView.TextColor = CSS.CSS_Color.PromptingColor1;
            btnConnetView.TextAlignment = TextAlignment.Center;
            btnConnetView.TextID = StringId.ConnectSuccess;
            bodyFrameLayout.AddChidren(btnConnetView);
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(2000);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //发送账号和密码给蓝牙
                    this.SendAccountAndPswToBluetooth();
                });
                //启动超时线程
                this.StartTimeoutThread();
            });
        }
 
        #endregion
 
        #region ■ 显示成功界面_______________________
 
        /// <summary>
        /// 显示成功界面
        /// </summary>
        private void ShowSuccessView(Entity.Function newDevice)
        {
            //清空body
            this.ClearBodyFrame();
 
            //图片
            var btnPic = new PicViewControl(180, 180);
            btnPic.Y = Application.GetRealHeight(48);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "Public/TipIcon_Successfully.png";
            bodyFrameLayout.AddChidren(btnPic);
 
            //添加成功
            var btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnFail.TextColor = CSS.CSS_Color.MainColor;
            btnFail.TextAlignment = TextAlignment.Center;
            btnFail.TextID = StringId.AddSuccess;
            bodyFrameLayout.AddChidren(btnFail);
 
            //可以开始使用红外遥控功能!
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg7);
            int yy = this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
                Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8));
 
            //开始使用
            var btnUse = new BottomClickButton(220);
            btnUse.Y = yy + Application.GetRealHeight(60);
            btnUse.TextID = StringId.StartUse;
            bodyFrameLayout.AddChidren(btnUse);
            btnUse.ButtonClickEvent += (sender, e) =>
            {
                if (newDevice == null)
                {
                    //关闭掉全部的界面,应该不会发生
                    HdlFormLogic.Current.CloseAllOpenForm();
                    return;
                }
                var form = HdlFormLogic.Current.GetFormByName("AddMiniRemoteControlDirection1Page") as AddMiniRemoteControlDirection1Page;
                //if (form.AddDeviceEvent != null)
                {
                    //代表这个是由温总那边调用的,直接回调函数
                    form.AddDeviceEvent?.Invoke(newDevice);
                    //关闭掉这个界面
                    this.CloseForm();
                    //再把AddMiniRemoteControlDirection1Page界面关了
                    HdlFormLogic.Current.CloseFormByFormName("AddMiniRemoteControlDirection1Page");
                }
                //else
                //{
                //    //代表这并不是由温总的界面调用的,则关闭掉全部的界面
                //    HdlFormLogic.Current.CloseAllOpenForm();
                //    //然后再把温总的界面new起来
                //    new UI2.PersonalCenter.PirDevice.Method().MainView(this, newDevice,()=> { });
                //}
            };
        }
 
        #endregion
 
        #region ■ 显示失败界面_______________________
 
        /// <summary>
        /// 显示失败界面
        /// </summary>
        private void ShowFailView()
        {
            //清空body
            this.ClearBodyFrame();
 
            //图片
            var btnPic = new PicViewControl(180, 180);
            btnPic.Y = Application.GetRealHeight(48);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "Public/TipIcon_Failed.png";
            bodyFrameLayout.AddChidren(btnPic);
 
            //添加失败
            var btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnFail.TextColor = CSS.CSS_Color.AuxiliaryColor2;
            btnFail.TextAlignment = TextAlignment.Center;
            btnFail.TextID = StringId.AddFail;
            bodyFrameLayout.AddChidren(btnFail);
            //1、请检查设备是否正常通电
            //2、请开启蓝牙功能
            //3、并长按按钮10s,指示灯常亮
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg6);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
                Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8),
                 TextAlignment.Center, true);
 
            //重试
            var btnReDo = this.AddBottomClickButton(Language.StringByID(StringId.Retry));
            btnReDo.ButtonClickEvent += (sender, e) =>
            {
                //重新初始化中部信息
                this.CloseForm();
                var form = new AddMiniRemoteControlDirection2Page();
                form.AddForm(this.wifiName, this.wifiPsw);
            };
        }
 
        #endregion
 
        #region ■ 发送账号和密码给蓝牙_______________
 
        /// <summary>
        /// 发送账号和密码给蓝牙
        /// </summary>
        private void SendAccountAndPswToBluetooth()
        {
            //获取发送到蓝牙的数据
            var sendData = this.GetSendToBluetoothData();
            //不等待
            HdlBluetoothLogic.Current.SendData(sendData);
        }
 
        #endregion
 
        #region ■ 红外宝相关方法_____________________
 
        /// <summary>
        /// 检测是否是新的红外宝
        /// </summary>
        /// <param name="pushEnum">推送枚举</param>
        /// <param name="i_data">推送的数据</param>
        /// <returns></returns>
        private bool CheckIsNewMiniRemoteControl(CloudPushEnum pushEnum, string i_data)
        {
            if (pushEnum != CloudPushEnum.A新设备上报) { return false; }
 
            try
            {
                var strSpk = string.Empty;
                //转为json
                var json = Newtonsoft.Json.Linq.JObject.Parse(i_data);
                var linqArry = json["objects"] as Newtonsoft.Json.Linq.JArray;
                if (linqArry != null)
                {
                    //数组类型
                    if (linqArry.Count == 0) { return false; }
                    strSpk = linqArry[0]["spk"].ToString();
                    this.newDeviceSid = linqArry[0]["sid"].ToString();
                }
                else
                {
                    //非数组类型
                    strSpk = json["objects"]["spk"].ToString();
                    this.newDeviceSid = json["objects"]["sid"].ToString();
                }
                if (strSpk != Entity.SPK.IrModule)
                {
                    //如果不是红外宝的话
                    return false;
                }
                return true;
            }
            catch { return false; }
        }
 
        /// <summary>
        /// 刷新红外宝信息
        /// </summary>
        /// <param name="successEvent">成功的回调事件</param>
        private void RefreshMiniRemoteControlInfo(Action<Entity.Function> successEvent)
        {
            var pra = new Dictionary<string, object>();
            pra.Add("homeId", Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID);
            pra.Add("spk", Entity.SPK.IrModule);
 
            var requestJson = DAL.Server.HttpUtil.GetSignRequestJson(pra);
            HdlThreadLogic.Current.RunThread(() =>
            {
                int count = 0;
                while (this.Parent != null)
                {
                    try
                    {
                        //超过5次都不成功,真不处理了
                        count++;
                        if (count > 5) { return; }
 
                        //获取全部红外宝列表
                        var result = DAL.Server.HttpUtil.RequestHttpsPostFroHome(DAL.Server.NewAPI.Api_Post_GetDevcieList, requestJson);
                        if (result == null || result.Code != DAL.Server.StateCode.SUCCESS)
                        {
                            System.Threading.Thread.Sleep(3000);
                            continue;
                        }
                        var listDevice = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.DevcieApiPack>(result.Data.ToString());
                        Entity.Function newDevice = null;
                        foreach (var newFunction in listDevice.list)
                        {
                            //添加到缓存当中
                            newFunction.SaveFunctionFile();
                            Entity.FunctionList.List.IniFunctionList(newFunction.savePath, true);
                            if (newFunction.sid == this.newDeviceSid)
                            {
                                //这个设备是新追加的
                                newDevice = newFunction;
                            }
                        }
                        //调用回调函数
                        successEvent?.Invoke(newDevice);
                        return;
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(3000);
                    }
                }
            });
        }
 
        #endregion
 
        #region ■ 云端反馈___________________________
 
        /// <summary>
        /// 云端反馈
        /// </summary>
        /// <param name="pushEnum">枚举</param>
        /// <param name="i_data">推送的内容</param>
        private void CloudReceiveEvent(CloudPushEnum pushEnum, string i_data)
        {
            //检测是否是新的红外宝
            if (this.CheckIsNewMiniRemoteControl(pushEnum, i_data) == false)
            {
                return;
            }
            //刷新红外宝信息
            this.RefreshMiniRemoteControlInfo((Entity.Function newDevice) =>
            {
                //移除这个事件
                HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
                //停止线程
                this.timeoutThreadActivity = false;
 
                //添加设备成功之后,处理一些东西
                this.DoSomethingAfterAddDeviceSuccess(newDevice);
            });
        }
 
        /// <summary>
        /// 添加设备成功之后,处理一些东西
        /// </summary>
        private void DoSomethingAfterAddDeviceSuccess(Entity.Function newDevice)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //整点特效而已
                for (int i = 1; i <= 6; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    if (i == 1)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //整点特效而已
                            this.btnClound.IsSelected = true;
                            this.btnCloundView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
                        });
                    }
                    else if (i == 4)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //整点特效而已
                            this.btnConnect.IsSelected = true;
                            this.btnConnetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
                        });
                    }
                    else if (i == 6)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //显示成功界面
                            this.ShowSuccessView(newDevice);
                        });
                    }
                }
            });
        }
 
        #endregion
 
        #region ■ 蓝牙反馈___________________________
 
        /// <summary>
        /// 蓝牙反馈
        /// </summary>
        /// <param name="i_receviceData">蓝牙反馈的数据</param>
        private void BluetoothReceiveEvent(string i_receviceData)
        {
#if DEBUG
            System.Console.WriteLine("蓝牙返回:" + i_receviceData);
#endif
        }
 
        #endregion
 
        #region ■ 超时线程___________________________
 
        /// <summary>
        /// 启动超时线程
        /// </summary>
        private void StartTimeoutThread()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //360秒超时
                int timeout = 360;
                while (this.timeoutThreadActivity == false && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    timeout--;
                    if (timeout == 0)
                    {
                        break;
                    }
                }
                if (timeout <= 0)
                {
                    //显示失败界面
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        this.ShowFailView();
                    });
                }
            });
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //摧毁蓝牙
            HdlBluetoothLogic.Current.Dispone();
            HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
 
            base.CloseFormBefore();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取发送到蓝牙的数据
        /// </summary>
        /// <returns></returns>
        private string GetSendToBluetoothData()
        {
            var pra = new { id = "id010203", ssid = this.wifiName, password = this.wifiPsw };
            var praData = Newtonsoft.Json.JsonConvert.SerializeObject(pra);
 
            var sendData = "Topic:/user/id/custom/wifi/set\r\n";
            sendData += "Length:" + praData.Length + "\r\n\r\n";
            sendData += praData;
            return sendData;
        }
 
        #endregion
    }
}