HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2019-10-10 2ed75b8b337048e5d75e6d9ec8307633134f02fd
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
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.GatewayManage
{
    /// <summary>
    /// 网关管理的界面
    /// </summary>
    public class GatewayListForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listview = null;
        /// <summary>
        /// 生成的网关状态控件暂时存入内存中
        /// </summary>
        private Dictionary<string, GatewayRowControl> dicRowContr = new Dictionary<string, GatewayRowControl>();
        /// <summary>
        /// 关闭设备管理界面(如果解除绑定的网关是当前所选择的网关的话,则关闭设备管理界面)
        /// </summary>
        private bool closeDeviceManagForm = false;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uGatewayManagement));
 
            //添加图标
            var btnAdd = new MostRightIconControl(69, 69);
            btnAdd.UnSelectedImagePath = "Item/Add.png";
            topFrameLayout.AddChidren(btnAdd);
            btnAdd.InitControl();
            btnAdd.ButtonClickEvent += (sender, e) =>
            {
                var form = new GatewayAdd.NewGateWaySelectForm();
                form.AddForm();
            };
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            listview = new VerticalListControl(29);
            listview.Height = bodyFrameLayout.Height + Application.GetRealHeight(6);
            listview.Y = Application.GetRealHeight(-6);
            listview.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(listview);
 
            //设定中部信息
            this.SetMiddleFrameInfo();
        }
 
        /// <summary>
        /// 设定中部信息
        /// </summary>
        private void SetMiddleFrameInfo()
        {
            this.dicRowContr.Clear();
            this.listview.RemoveAll();
 
            //获取本地全部网关对象
            List<ZbGateway> listway = HdlGatewayLogic.Current.GetAllLocalGateway();
            if (listway.Count == 0)
            {
                return;
            }
 
            int count = listway.Count - 1;
            for (int i = 0; i < listway.Count; i++)
            {
                //添加行
                var gwId = HdlGatewayLogic.Current.GetGatewayId(listway[i]);
                this.AddRowLayout(gwId, i != count);
            }
            //调整列表控件的高度
            var realHeight = listview.ChildrenCount * listview.GetChildren(listview.ChildrenCount - 1).Height;
            realHeight += Application.GetRealHeight(23);
            if (realHeight < listview.Height)
            {
                //缩小控件高度
                listview.Height = realHeight;
            }
 
            //设置接受在线状态推送
            this.AddGatewayOnlinePush();
            //开启网关在线监测的线程
            this.StartGatewayOnlieCheckThread(listway);
        }
 
        #endregion
 
        #region ■ 添加网关行_________________________
 
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="strWayId">String way.</param>
        /// <param name="addLine">是否添加底线</param>
        private void AddRowLayout(string strWayId, bool addLine)
        {
            //网关控件
            var Gateway = HdlGatewayLogic.Current.GetLocalGateway(strWayId);
            var gatewayRow = new GatewayRowControl(Gateway, listview.rowSpace / 2);
            listview.AddChidren(gatewayRow);
            gatewayRow.InitControl(81);
            //向右图标
            gatewayRow.frameTable.AddRightArrow();
            //添加在线状态
            gatewayRow.AddOnLineControl();
            if (addLine == true)
            {
                gatewayRow.frameTable.AddBottomLine();
            }
 
            //提示有新版本
            var btnNew = new InformationTipView(gatewayRow.btnIcon);
            btnNew.Visible = false;
            gatewayRow.frameTable.AddChidren(btnNew, ChidrenBindMode.BindEventOnly);
            gatewayRow.AddTag("btnNew", btnNew);
 
            this.dicRowContr[strWayId] = gatewayRow;
 
            //设置一个选择网关的默认值
            if (string.IsNullOrEmpty(GatewayResourse.AppOldSelectGatewayId) == true && gatewayRow.IsOnline == true)
            {
                this.SaveGatewayIdToLocation(strWayId);
            }
 
            //编辑按钮
            var btnEditor = new NormalViewControl(Application.GetRealWidth(177), gatewayRow.Height, false);
            btnEditor.BackgroundColor = UserCenterColor.Current.RowEditorButtonColor;
            btnEditor.TextAlignment = TextAlignment.Center;
            btnEditor.TextColor = UserCenterColor.Current.White;
            btnEditor.TextID = R.MyInternationalizationString.uEditor;
            gatewayRow.AddRightView(btnEditor);
            btnEditor.ButtonClickEvent += (sender, e) =>
            {
                //如果点击的是不在线的网关,则当什么事都没有发生
                if (gatewayRow.IsOnline == false)
                {
                    //指定的网关不在线
                    this.ShowMassage(ShowMsgType.Normal, Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline));
                    return;
                }
                //点击编辑的话,直接隐藏
                btnNew.Visible = false;
 
                var form = new GatewayInfoEditorForm();
                form.AddForm(Gateway);
            };
 
            //解除绑定
            var btnUnBind = new NormalViewControl(Application.GetRealWidth(177), gatewayRow.Height, false);
            btnUnBind.BackgroundColor = UserCenterColor.Current.RowDeleteButtonColor;
            btnUnBind.TextAlignment = TextAlignment.Center;
            btnUnBind.TextColor = UserCenterColor.Current.White;
            btnUnBind.TextID = R.MyInternationalizationString.uUnBinded;
            gatewayRow.AddRightView(btnUnBind);
            btnUnBind.ButtonClickEvent += (sender, e) =>
            {
                //如移除网关,该网关绑定的设备列表{0}将清空,确认继续执行该操作?
                string msg = Language.StringByID(R.MyInternationalizationString.uUnBindedMsg);
                if (msg.Contains("{0}") == true)
                {
                    msg = string.Format(msg, "\r\n");
                }
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                {
                    this.DeleteGateway(strWayId, gatewayRow);
                });
            };
 
            //单击事件
            gatewayRow.frameTable.ButtonClickEvent += (sender, e) =>
            {
                //如果点击的是不在线的网关,则当什么事都没有发生
                if (gatewayRow.IsOnline == false)
                {
                    //指定的网关不在线
                    this.ShowMassage(ShowMsgType.Normal, Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline));
                    return;
                }
                //如果存在新版本,并且单击他的话
                if (btnNew.Visible == true)
                {
                    if (sender is InformationTipView || sender is IconViewControl)
                    {
                        //打开编辑界面
                        btnNew.Visible = false;
                        var form = new GatewayInfoEditorForm();
                        form.AddForm(Gateway);
                        return;
                    }
                }
 
                //是否切换到{0}网关?
                string msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToSwitchTheGateway), "[" + gatewayRow.btnName.Text + "]");
                if (strWayId == GatewayResourse.AppOldSelectGatewayId)
                {
                    //是否重新刷新{0}网关?
                    msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToRefreshTheGateway), "[" + gatewayRow.btnName.Text + "]");
                }
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                {
                    //执行切换网关操作
                    this.DoSwitchGateway(strWayId);
                });
            };
        }
 
        #endregion
 
        #region ■ 解绑网关___________________________
 
        /// <summary>
        /// 解绑网关
        /// </summary>
        /// <param name="strWayId"></param>
        /// <param name="row"></param>
        private async void DeleteGateway(string strWayId, GatewayRowControl row)
        {
            //删除云端网关
            bool result = await HdlGatewayLogic.Current.DeleteGateway(strWayId);
            if (result == false)
            {
                return;
            }
 
            if (strWayId == GatewayResourse.AppOldSelectGatewayId)
            {
                //如果解除绑定的网关是当前所选择的网关的话,则关闭设备管理界面
                this.closeDeviceManagForm = true;
                this.SaveGatewayIdToLocation(string.Empty);
            }
 
            //移除
            if (this.dicRowContr.ContainsKey(strWayId) == true)
            {
                this.dicRowContr.Remove(strWayId);
            }
            row?.RemoveFromParent();
        }
 
        #endregion
 
        #region ■ 网关切换___________________________
 
        /// <summary>
        /// 执行切换网关操作
        /// </summary>
        /// <param name="gatewayId"></param>
        private async void DoSwitchGateway(string gatewayId)
        {
            //显示进度条
            this.ShowProgressBar();
            //获取网关
            var gateway = HdlGatewayLogic.Current.GetLocalGateway(gatewayId);
 
            //检测广播到的这个网关是否拥有住宅ID
            ZbGateway realWay = null;
            bool getGatewayInfo = true;
            if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, gatewayId) == true)
            {
                //重新设置住宅ID(这个应该是不经过APP,直接把网关恢复了出厂设置)
                if (HdlGatewayLogic.Current.HomeIdIsEmpty(realWay.getGatewayBaseInfo.HomeId) == true)
                {
                    int result2 = await HdlGatewayLogic.Current.ReBindNewGateway(realWay);
                    if (result2 == 0)
                    {
                        //出现未知错误,请稍后再试
                        this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uUnKnowErrorAndResetAgain));
                        //关闭进度条
                        this.CloseProgressBar();
                    }
                    else if (result2 == -1)
                    {
                        //关闭进度条
                        this.CloseProgressBar();
                        return;
                    }
                    //重新绑定网关里面已经重新获取了网关信息
                    getGatewayInfo = false;
                }
            }
 
            if (getGatewayInfo == true)
            {
                //获取网关信息
                var info = await HdlGatewayLogic.Current.GetGatewayNewInfoAsync(gateway);
                if (info == null)
                {
                    //关闭进度条
                    this.CloseProgressBar();
                    return;
                }
            }
 
            //获取全部设备
            bool result = await Common.LocalDevice.Current.SetDeviceToMemmoryByGateway(gateway);
            //关闭进度条
            this.CloseProgressBar();
            if (result == false)
            {
                return;
            }
 
            //切换网关,保存缓存
            this.SaveGatewayIdToLocation(gatewayId);
            //如果选择了刷新的网关,则不关闭管理界面
            this.closeDeviceManagForm = false;
 
            if (UserCenterResourse.DicActionForm.ContainsKey("DeviceListMainForm") == false)
            {
                //刷新主画面
                var form = new Device.DeviceListMainForm();
                this.AddFromAndRemoveNowForm(form);
            }
            else
            {
                //关闭界面
                this.CloseForm();
                //刷新主画面(不重新获取设备状态)
                this.LoadFormMethodByName("DeviceListMainForm", "InitMiddleFrame", false);
            }
        }
 
        /// <summary>
        /// 保存选择的网关ID到本地
        /// </summary>
        /// <param name="gatewayId"></param>
        private void SaveGatewayIdToLocation(string gatewayId)
        {
            GatewayResourse.AppOldSelectGatewayId = gatewayId;
            byte[] data = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(GatewayResourse.AppOldSelectGatewayId));
            Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AppOldSelectGatewayFile, data);
        }
 
        #endregion
 
        #region ■ 网关在线检测_______________________
 
        /// <summary>
        /// 上一次获取网关在线的时间(不能让它每点一次都去获取网关状态)
        /// </summary>
        private DateTime oldGetOnlineTime = new DateTime(1900, 1, 1);
 
        /// <summary>
        /// 开启网关在线监测的线程
        /// </summary>
        /// <param name="listway"></param>
        private void StartGatewayOnlieCheckThread(List<ZbGateway> listway)
        {
            var timeValue = (DateTime.Now - oldGetOnlineTime).Milliseconds;
            if (timeValue <= 10000)
            {
                //最少要间隔十秒,才去重新获取
                return;
            }
            oldGetOnlineTime = DateTime.Now;
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(300);
                //刷新网关在线状态
                HdlGatewayLogic.Current.RefreshGatewayOnlineStatu(listway);
                foreach (var way in listway)
                {
                    if (this.Parent == null)
                    {
                        return;
                    }
                    bool online = HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(way);
                    this.GatewayOnlinePush(way, online);
                }
                //网关新版本检测
                this.CheckGatewayNewVersion(listway);
            });
        }
 
        /// <summary>
        /// 网关在线状态变更
        /// </summary>
        /// <param name="gateWay">网关对象</param>
        /// <param name="online">在线状态变更后的状态</param>
        public override void GatewayOnlinePush(ZbGateway gateWay, bool online)
        {
            Application.RunOnMainThread(() =>
            {
                if (this.Parent == null)
                {
                    return;
                }
                string gwid = HdlGatewayLogic.Current.GetGatewayId(gateWay);
                if (this.dicRowContr.ContainsKey(gwid) == true && this.dicRowContr[gwid] != null)
                {
                    this.dicRowContr[gwid].RefreshControl(gateWay);
                }
            });
        }
 
        #endregion
 
        #region ■ 网关新版本检测_____________________
 
        /// <summary>
        /// 网关新版本检测
        /// </summary>
        /// <param name="listWays"></param>
        private async void CheckGatewayNewVersion(List<ZbGateway> listWays)
        {
            foreach (var way in listWays)
            {
                if (this.Parent == null)
                {
                    return;
                }
                if (HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(way) == false)
                {
                    //不在线的不用理它
                    continue;
                }
                //获取最新版本
                var result = await HdlGatewayLogic.Current.GetGatewayAllNewVersion(way, ShowErrorMode.NO);
                if (result == null)
                {
                    continue;
                }
                if (result[0] != null || result[1] != null || result[2] != null)
                {
                    //有新版本
                    string gwid = HdlGatewayLogic.Current.GetGatewayId(way);
                    Application.RunOnMainThread(() =>
                    {
                        if (this.dicRowContr.ContainsKey(gwid) == true && this.dicRowContr[gwid] != null)
                        {
                            var btnNew = (InformationTipView)this.dicRowContr[gwid].GetTagByKey("btnNew");
                            if (btnNew != null)
                            {
                                btnNew.Visible = true;
                            }
                        }
                    });
                }
            }
        }
 
        #endregion
 
        #region ■ 画面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseForm()
        {
            base.CloseForm();
 
            if (string.IsNullOrEmpty(GatewayResourse.AppOldSelectGatewayId) == true && this.closeDeviceManagForm == true)
            {
                //关闭界面
                HdlGatewayLogic.Current.RefreshAppOldSelectGatewayId();
                this.LoadFormMethodByName("DeviceListMainForm", "CloseForm");
            }
        }
 
        #endregion
 
        #region ■ 界面重新激活事件___________________
 
        /// <summary>
        /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
        /// </summary>
        public override int FormActionAgainEvent()
        {
            //初始化中部控件
            this.InitMiddleFrame();
            return 1;
        }
 
        #endregion
    }
}