黄学彪
2021-03-11 84e76cf2fcbe26142750131d3c27eaac0335e40a
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
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>
        /// 超时线程是否运行
        /// </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;
 
#if __IOS__
#endif
#if __Android__
            //添加接收蓝牙反馈的事件
            HdlAndroidBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
#endif
            //添加云端反馈事件
            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()
        {
            //清空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) =>
            {
            };
        }
 
        #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.InitMiddleFrame();
            };
        }
 
        #endregion
 
        #region ■ 发送账号和密码给蓝牙_______________
 
        /// <summary>
        /// 发送账号和密码给蓝牙
        /// </summary>
        private void SendAccountAndPswToBluetooth()
        {
#if __IOS__
#endif
#if __Android__
            //获取发送到蓝牙的数据
            var sendData = this.GetSendToBluetoothData();
            //不等待
            HdlAndroidBluetoothLogic.Current.SendData(sendData);
#endif
        }
 
        #endregion
 
        #region ■ 云端反馈___________________________
 
        /// <summary>
        /// 云端反馈
        /// </summary>
        /// <param name="pushEnum">枚举</param>
        /// <param name="i_data">推送的内容</param>
        private void CloudReceiveEvent(CloudPushEnum pushEnum, string i_data)
        {
            if (pushEnum != CloudPushEnum.A新设备上报) { return; }
 
            //接收到就移除这个事件
            HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
            //停止线程
            this.timeoutThreadActivity = false;
 
            //添加设备成功之后,处理一些东西
            this.DoSomethingAfterAddDeviceSuccess();
        }
 
        /// <summary>
        /// 添加设备成功之后,处理一些东西
        /// </summary>
        private void DoSomethingAfterAddDeviceSuccess()
        {
            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();
                        });
                    }
                }
            });
        }
 
        #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(() =>
            {
                //300秒超时
                int timeout = 300;
                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()
        {
            //摧毁蓝牙
#if __IOS__
#endif
#if __Android__
            HdlAndroidBluetoothLogic.Current.Dispone();
#endif
            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
    }
}