黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.GatewayAdd
{
    /// <summary>
    /// 网关搜索中
    /// </summary>
    public class WiredGatewaySearchForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 网关的主键(value:剩余检测次数,用于对应异常的网关)
        /// </summary>
        private Dictionary<string, int> dicIdCheckCount = new Dictionary<string, int>();
        /// <summary>
        /// 搜索到的网关列表
        /// </summary>
        private List<ZbGateway> listZbGateway = new List<ZbGateway>();
        /// <summary>
        /// 搜索到的网关类别  0:第一次绑定 1:已经绑定 2:需要换绑
        /// </summary>
        private Dictionary<string, int> dicZbGatewayDiv = new Dictionary<string, int>();
        /// <summary>
        /// 获取得到了新网关
        /// </summary>
        private bool newGatewayGetting = false;
        /// <summary>
        /// 网关搜索中
        /// </summary>
        private bool isGatewaySearching = true;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddWiredGateway));
 
            //初始化初始中部控件
            this.InitDefultMiddleFrame();
        }
 
        /// <summary>
        /// 初始化初始中部控件
        /// </summary>
        private void InitDefultMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //图标
            var picGwImage = new FrameLayout();
            picGwImage.Width = this.GetPictrueRealSize(861);
            picGwImage.Height = this.GetPictrueRealSize(478);
            picGwImage.Y = Application.GetRealHeight(251);
            picGwImage.Gravity = Gravity.CenterHorizontal;
            picGwImage.BackgroundImagePath = "Gateway/WiredGatewaySearching.png";
            bodyFrameLayout.AddChidren(picGwImage);
 
            //正在搜索网关…
            var btnMsg = new NormalViewControl(bodyFrameLayout.Width, false);
            btnMsg.TextID = R.MyInternationalizationString.uGatewaySearching;
            btnMsg.Y = Application.GetRealHeight(1008);
            btnMsg.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnMsg.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnMsg);
 
            //初始化连接的假想动画效果
            this.InitConcetionAnimete(picGwImage, btnMsg);
            //开启网关检测的线程
            this.StartCheckGatewayThread();
        }
 
        /// <summary>
        /// 初始化失败模式的中部控件
        /// </summary>
        private void InitFailMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //图标
            var picGwImage = new FrameLayout();
            picGwImage.Width = this.GetPictrueRealSize(861);
            picGwImage.Height = this.GetPictrueRealSize(478);
            picGwImage.Y = Application.GetRealWidth(158);
            picGwImage.Gravity = Gravity.CenterHorizontal;
            picGwImage.BackgroundImagePath = "Gateway/WiredGatewaySearching.png";
            bodyFrameLayout.AddChidren(picGwImage);
            var btnFailIcon = new PicViewControl(66, 66);
            btnFailIcon.UnSelectedImagePath = "Item/FailIcon.png";
            btnFailIcon.Gravity = Gravity.CenterHorizontal;
            btnFailIcon.Y = this.GetPictrueRealSize(181);
            picGwImage.AddChidren(btnFailIcon);
            //搜索失败
            var btnFailView = new NormalViewControl(300, 60, true);
            btnFailView.TextAlignment = TextAlignment.Center;
            btnFailView.Y = this.GetPictrueRealSize(305);
            btnFailView.Gravity = Gravity.CenterHorizontal;
            btnFailView.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnFailView.TextID = R.MyInternationalizationString.uSearchFail;
            picGwImage.AddChidren(btnFailView);
 
            //解决方法,可选择执行以下任意一种方式:
            var btnMsg1 = new NormalViewControl(930, 60, true);
            btnMsg1.X = Application.GetRealWidth(75);
            btnMsg1.Y = picGwImage.Bottom + Application.GetRealHeight(112);
            btnMsg1.TextID = R.MyInternationalizationString.uSearchGatewayFailMsg1;
            bodyFrameLayout.AddChidren(btnMsg1);
            //1.短按HID/WCFG按键,系统指示灯快闪3秒,网关进入换绑模式,强制网关绑定到当前APP
            var btnMsg2 = new NormalViewControl(930, 100, true);
            btnMsg2.X = Application.GetRealWidth(75);
            btnMsg2.Y = btnMsg1.Bottom + Application.GetRealHeight(35);
            btnMsg2.Text = Language.StringByID(R.MyInternationalizationString.uSearchGatewayFailMsg2).Replace("{0}", "\r\n");
            btnMsg2.TextSize = 12;
            btnMsg2.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnMsg2.IsMoreLines = true;
            bodyFrameLayout.AddChidren(btnMsg2);
            //2.长按网关ZB/RST按键20秒以上,直到所有指示灯同时灭,即恢复出厂设置并重启
            var btnMsg3 = new NormalViewControl(930, 100, true);
            btnMsg3.X = Application.GetRealWidth(75);
            btnMsg3.Y = btnMsg2.Bottom + Application.GetRealHeight(12);
            btnMsg3.Text = Language.StringByID(R.MyInternationalizationString.uSearchGatewayFailMsg3).Replace("{0}", "\r\n");
            btnMsg3.TextSize = 12;
            btnMsg3.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnMsg3.IsMoreLines = true;
            bodyFrameLayout.AddChidren(btnMsg3);
            //3.请重试,重新添加网关。
            var btnMsg4 = new NormalViewControl(930, 50, true);
            btnMsg4.X = Application.GetRealWidth(75);
            btnMsg4.Y = btnMsg3.Bottom + Application.GetRealHeight(12);
            btnMsg4.Text = Language.StringByID(R.MyInternationalizationString.uSearchGatewayFailMsg4);
            btnMsg4.TextSize = 12;
            btnMsg4.TextColor = UserCenterColor.Current.TextGrayColor1;
            bodyFrameLayout.AddChidren(btnMsg4);
            //4.返回网关管理。
            var btnMsg5 = new NormalViewControl(930, 50, true);
            btnMsg5.X = Application.GetRealWidth(75);
            btnMsg5.Y = btnMsg4.Bottom + Application.GetRealHeight(12);
            btnMsg5.Text = Language.StringByID(R.MyInternationalizationString.uSearchGatewayFailMsg5);
            btnMsg5.TextSize = 12;
            btnMsg5.TextColor = UserCenterColor.Current.TextGrayColor1;
            bodyFrameLayout.AddChidren(btnMsg5);
 
            //重试
            var frameback = new FrameLayoutStatuControl();
            frameback.UseClickStatu = false;
            frameback.Y = Application.GetRealHeight(1259);
            frameback.Gravity = Gravity.CenterHorizontal;
            frameback.Width = Application.GetRealWidth(746);
            frameback.Height = Application.GetRealHeight(184);
            frameback.BackgroundImagePath = "Item/BottomButtonGround.png";
            bodyFrameLayout.AddChidren(frameback);
            var btnReDo = new NormalViewControl(300, 65, true);
            btnReDo.IsBold = true;
            btnReDo.TextColor = UserCenterColor.Current.White;
            btnReDo.TextSize = 16;
            btnReDo.Y = Application.GetRealHeight(49);
            btnReDo.Gravity = Gravity.CenterHorizontal;
            btnReDo.TextAlignment = TextAlignment.Center;
            btnReDo.TextID = R.MyInternationalizationString.uReDoAgain;
            frameback.AddChidren(btnReDo, ChidrenBindMode.BindEvent);
            frameback.ButtonClickEvent += (sender, e) =>
            {
                //初始化初始中部控件
                this.InitDefultMiddleFrame();
            };
 
            //返回网关管理
            var btnGoBack = new BottomClickButton(688);
            btnGoBack.Y = Application.GetRealHeight(1495);
            btnGoBack.TextID = R.MyInternationalizationString.uGobackGatewayManage;
            if (HdlFormLogic.Current.IsFormOpen("GatewayListForm") == false)
            {
                //返回主页
                btnGoBack.TextID = R.MyInternationalizationString.uGobackToHomePage;
            }
            bodyFrameLayout.AddChidren(btnGoBack);
            btnGoBack.ButtonClickEvent += (sender, e) =>
            {
                this.CloseForm();
                //关闭指定界面
                HdlFormLogic.Current.CloseFormByFormName("NewGateWayMenuSelectForm");
            };
        }
 
        #endregion
 
        #region ■ 假想动画(网关检测)_________________
 
        /// <summary>
        /// 初始化连接的假想动画效果
        /// </summary>
        private void InitConcetionAnimete(FrameLayout picGwImage, NormalViewControl btnMsg)
        {
            //首格的X轴
            int firstPoint = this.GetPictrueRealSize(288);
            //一格的宽度
            int width = this.GetPictrueRealSize(22);
            //间距大小
            int space = this.GetPictrueRealSize(12);
 
            //生成格子坐标
            var listPoint = new List<int>();
            for (int i = 0; i < 9; i++)
            {
                listPoint.Add(firstPoint + (width + space) * i);
            }
 
            //开启连接的假想动画效果线程
            var btnRound = new PicViewControl(width, width, false);
            btnRound.Radius = (uint)width / 2;
            btnRound.BackgroundColor = UserCenterColor.Current.ConcetionRoundColor;
            btnRound.Y = this.GetPictrueRealSize(202);
            btnRound.X = listPoint[0];
            picGwImage.AddChidren(btnRound);
 
            string strMsg = btnMsg.Text;
            HdlThreadLogic.Current.RunThread(() =>
            {
                int index = 1;
                int maxCount = 120 * 2;
                int timeCount = 120;
                while (picGwImage.Parent != null)
                {
                    System.Threading.Thread.Sleep(500);
                    maxCount--;
                    if (maxCount % 2 == 0)
                    {
                        timeCount--;
                    }
                    if (maxCount <= 0 || this.newGatewayGetting == true)
                    {
                        //获取得到新网关,则中断线程
                        break;
                    }
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        btnRound.X = listPoint[index];
                        index++;
                        if (index == listPoint.Count)
                        {
                            index = 0;
                        }
                        //正在搜索网关…XXs
                        btnMsg.Text = strMsg + timeCount + "s";
                    }, ShowErrorMode.NO);
                }
                //搜索结束
                this.isGatewaySearching = false;
 
                //2020.07.14追加:倒计时结束时,哪怕是已经绑定了的网关,也显示出来
                if (maxCount <= 0 && listZbGateway.Count == 0)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //初始化失败模式的中部控件
                        this.InitFailMiddleFrame();
                    });
                }
                else if (newGatewayGetting == true || listZbGateway.Count > 0)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        if (this.Parent != null)
                        {
                            var form = new GatewayManage.GatewaySearchListForm();
                            this.AddFormAndCloseNowForm(form, listZbGateway, dicZbGatewayDiv);
                        }
                    });
                }
            });
        }
 
        #endregion
 
        #region ■ 网关检测___________________________
 
        /// <summary>
        /// 开启网关检测的线程
        /// </summary>
        private void StartCheckGatewayThread()
        {
            this.isGatewaySearching = true;
            this.dicZbGatewayDiv = new Dictionary<string, int>();
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //清空全部列表
                HdlGatewayLogic.Current.ClearAllRealGatewayConection(true);
                ZigBee.Common.Application.IsSearchingGateway = true;
 
                int count = 0;
                while (this.Parent != null && this.isGatewaySearching == true)
                {
                    //每2秒检测搜索到的网关
                    System.Threading.Thread.Sleep(500);
                    count++;
                    if (count < 4)
                    {
                        //主要为了快一点能够中断这个线程
                        continue;
                    }
                    count = 0;
 
                    this.CheckZbGatewayAndSetRow();
 
                    if (this.newGatewayGetting == true)
                    {
                        //获取得到新网关,则中断线程
                        break;
                    }
                }
            });
        }
 
        /// <summary>
        /// 检测搜索到的网关,然后添加到画面的行里面
        /// </summary>
        private bool CheckZbGatewayAndSetRow()
        {
            List<string> listId = new List<string>();
            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
            {
                if (ZbGateway.GateWayList[i].GwIP == string.Empty)
                {
                    //IP没有的网关,我也不知道它是干嘛的
                    continue;
                }
                string gwId = ZbGateway.GateWayList[i].GwId;
                //重复添加检测
                if (this.dicIdCheckCount.ContainsKey(gwId) == false)
                {
                    //允许五次检测
                    this.dicIdCheckCount[gwId] = 5;
                    listId.Add(gwId);
                }
                else if (this.dicIdCheckCount[gwId] > 0)
                {
                    //如果前一次检测失败了的话,它的次数-1
                    this.dicIdCheckCount[gwId] = this.dicIdCheckCount[gwId] - 1;
                    listId.Add(gwId);
                }
            }
            if (listId.Count == 0)
            {
                //没有新的网关,或者次数已经用完
                return true;
            }
            for (int i = 0; i < listId.Count; i++)
            {
                ZbGateway way = ZbGateway.GateWayList.Find((obj) => obj.GwId == listId[i]);
                if (way == null)
                {
                    continue;
                }
                //2020.09.18 本地连接没有连接上,则不处理
                if (way.LocalIsConnected == false)
                {
                    string strMsg = "搜索到了网关,但是本地链接不上  ID:" + way.GwId + " IP:" + way.GwIP + " 名称:" + way.GwName;
                    HdlLogLogic.Current.WriteLog(-1, strMsg);
                    continue;
                }
 
                //网关绑定模式
                var mode = GatewayBindMode.BindAgain;
                //住宅ID为空 第一次绑定
                if (HdlGatewayLogic.Current.HomeIdIsEmpty(way) == true)
                {
                    mode = GatewayBindMode.First;
                }
                //住宅ID不相等 需要重新绑定
                else if (way.HomeId != Common.Config.Instance.HomeId)
                {
                    mode = GatewayBindMode.BindAgain;
                }
                //如果本地不存在的话,当做第一次绑定处理
                else if (HdlGatewayLogic.Current.IsGatewayExist(way) == false)
                {
                    mode = GatewayBindMode.First;
                }
                //住宅ID一样,则是已经绑定过了
                else
                {
                    mode = GatewayBindMode.Binded;
                }
                //这两种情况都是需要重新添加(已经绑定过了的,有可能住宅ID没写进去,它有可能是人为的清空住宅ID)
                if (mode == GatewayBindMode.First || mode == GatewayBindMode.Binded)
                {
                    //添加搜索到的网关到缓存(执行网关保存操作)
                    var result = HdlGatewayLogic.Current.AddNewGateway(way, ShowErrorMode.NO, WriteLogMode.YES);
                    if (result == false)
                    {
                        continue;
                    }
                }
                //收集网关对象
                this.listZbGateway.Add(way);
 
                //如果它已经完全成功了的话,则将它的剩余次数置零
                this.dicIdCheckCount[listId[i]] = 0;
                //全部处理OK后,才能设置flage
                if (mode == GatewayBindMode.First)
                {
                    //第一次绑定
                    this.dicZbGatewayDiv[listId[i]] = 0;
                    //获取到了新网关
                    this.newGatewayGetting = true;
                }
                else if (mode == GatewayBindMode.Binded)
                {
                    //已经绑定过
                    this.dicZbGatewayDiv[listId[i]] = 1;
                }
                else
                {
                    //需要重新绑定
                    this.dicZbGatewayDiv[listId[i]] = 2;
                    //获取到了新网关
                    this.newGatewayGetting = true;
                }
 
                if (mode == GatewayBindMode.First || mode == GatewayBindMode.Binded)
                {
                    //我觉得这里需要获取一下新网关的设备列表
                    HdlDeviceCommonLogic.Current.SetDeviceToMemmoryByGateway(way.GwId, true);
                    if (way.IsMainGateWay == true)
                    {
                        //如果它是主网关,则刷新场景列表
                        HdlSceneLogic.Current.RefreshSceneUIList(true);
                    }
                }
            }
            return true;
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            ZigBee.Common.Application.IsSearchingGateway = false;
          
            base.CloseFormBefore();
        }
 
        #endregion
    }
}