黄学彪
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
464
465
466
467
468
469
470
471
472
473
474
475
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));
 
            //展示模板不允许编辑
            if (Config.Instance.Home.IsShowTemplate == false)
            {
                //添加图标
                var btnAdd = new MostRightIconControl(69, 69);
                btnAdd.UnSelectedImagePath = "Item/Add.png";
                topFrameLayout.AddChidren(btnAdd);
                btnAdd.InitControl();
                btnAdd.ButtonClickEvent += (sender, e) =>
                {
                    var form = new GatewayAdd.NewGateWayMenuSelectForm();
                    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);
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                //设定中部信息
                this.SetMiddleFrameInfo();
            });
        }
 
        /// <summary>
        /// 设定中部信息
        /// </summary>
        private void SetMiddleFrameInfo()
        {
            this.dicRowContr.Clear();
            this.listview.RemoveAll();
 
            //获取本地全部网关对象
            List<ZbGateway> listway = HdlGatewayLogic.Current.GetAllLocalGateway();
            if (listway.Count == 0)
            {
                //还没有绑定网关哦
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uHadNotBindGatewayMsg));
                return;
            }
 
            for (int i = 0; i < listway.Count; i++)
            {
                //添加行
                this.AddRowLayout(listway[i].GwId, i != listway.Count - 1);
            }
 
            //调整列表控件的高度
            this.listview.AdjustRealHeight(Application.GetRealHeight(23));
 
            //如果是展示模板的话,不需要检测
            if (Common.Config.Instance.Home.IsShowTemplate == false)
            {
                //开启网关在线监测的线程
                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();
            if (addLine == true)
            {
                gatewayRow.frameTable.AddBottomLine();
            }
            //提示新版本
            var btnNew = new InformationTipView(gatewayRow.btnIcon);
            btnNew.Visible = false;
            gatewayRow.frameTable.AddChidren(btnNew, ChidrenBindMode.BindEvent);
            gatewayRow.AddTag("btnNew", btnNew);
            //单击事件
            gatewayRow.frameTable.ButtonClickEvent += (sender, e) =>
            {
                if (HdlUserCenterResourse.HideOption.GotoGatewayProductInfoForm == 1)
                {
                    //强制跳转
                    var form2 = new GatewayProductInfoForm();
                    form2.AddForm(gateway);
                    return;
                }
                var form = new GatewayInfoEditorForm();
                form.AddForm(gateway);
            };
 
            //如果是展示模板的话,不能编辑
            if (Common.Config.Instance.Home.IsShowTemplate == true)
            {
                return;
            }
 
            //非虚拟住宅,才有这个功能
            if (Common.Config.Instance.Home.IsVirtually == false)
            {
                //切换
                var btnSwitch = new NormalViewControl(Application.GetRealWidth(184), gatewayRow.Height, false);
                btnSwitch.BackgroundColor = 0xfffb744a;
                btnSwitch.TextAlignment = TextAlignment.Center;
                btnSwitch.TextColor = UserCenterColor.Current.White;
                btnSwitch.TextSize = 12;
                btnSwitch.TextID = R.MyInternationalizationString.uSwitch1;
                if (strWayId == HdlGatewayResourse.AppOldSelectGatewayId)
                {
                    btnSwitch.TextID = R.MyInternationalizationString.uRefresh;
                }
                gatewayRow.AddRightView(btnSwitch);
                btnSwitch.ButtonClickEvent += (sender, e) =>
                {
                    //是否切换到{0}网关?
                    string msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToSwitchTheGateway), "[" + gatewayRow.btnName.Text + "]");
                    if (strWayId == HdlGatewayResourse.AppOldSelectGatewayId)
                    {
                        //是否重新刷新{0}网关?
                        msg = string.Format(Language.StringByID(R.MyInternationalizationString.uConfirmWantToRefreshTheGateway), "[" + gatewayRow.btnName.Text + "]");
                    }
                    this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                    {
                        HdlThreadLogic.Current.RunThread(() =>
                        {
                            //执行切换网关操作
                            this.DoSwitchGateway(strWayId);
                        });
                    });
                };
            }
 
            //定位
            var btnPosition = gatewayRow.AddEditorControl(false);
            btnPosition.TextID = R.MyInternationalizationString.uFixedPosition;
            btnPosition.ButtonClickEvent += (sender, e) =>
            {
                //发送定位命令
                HdlGatewayLogic.Current.SetFixedPositionCommand(gateway);
            };
 
            //删除
            var btnDelete = new NormalViewControl(Application.GetRealWidth(184), gatewayRow.Height, false);
            btnDelete.BackgroundColor = 0xfff75858;
            btnDelete.TextAlignment = TextAlignment.Center;
            btnDelete.TextColor = UserCenterColor.Current.White;
            btnDelete.TextSize = 12;
            btnDelete.TextID = R.MyInternationalizationString.uDelete;
            gatewayRow.AddLeftView(btnDelete);
            btnDelete.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, () =>
                {
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        this.DeleteGateway(strWayId);
                    });
                });
            };
 
            this.dicRowContr[strWayId] = gatewayRow;
            //设置一个选择网关的默认值
            if (string.IsNullOrEmpty(HdlGatewayResourse.AppOldSelectGatewayId) == true)
            {
                HdlGatewayLogic.Current.SaveGatewayIdToLocation(strWayId);
            }
 
            //当前网关的角标
            if (strWayId == HdlGatewayResourse.AppOldSelectGatewayId)
            {
                var btnNow = new IconViewControl(52);
                btnNow.Y = Application.GetMinReal(23) + gatewayRow.frameTable.chidrenYaxis;
                btnNow.UnSelectedImagePath = "Item/NowAcctionTip.png";
                btnNow.X = gatewayRow.btnName.X + gatewayRow.btnName.GetRealWidthByText();
                gatewayRow.frameTable.AddChidren(btnNow, ChidrenBindMode.BindEvent);
            }
        }
 
        #endregion
 
        #region ■ 解绑网关___________________________
 
        /// <summary>
        /// 解绑网关
        /// </summary>
        /// <param name="strWayId"></param>
        /// <param name="row"></param>
        private void DeleteGateway(string strWayId)
        {
            //打开进度条
            this.ShowProgressBar();
            //删除云端网关
            bool result = HdlGatewayLogic.Current.DeleteGateway(strWayId);
            //关闭进度条
            this.CloseProgressBar();
            if (result == false)
            {
                return;
            }
 
            if (strWayId == HdlGatewayResourse.AppOldSelectGatewayId)
            {
                //如果解除绑定的网关是当前所选择的网关的话,则关闭设备管理界面
                this.closeDeviceManagForm = true;
                HdlGatewayLogic.Current.SaveGatewayIdToLocation(string.Empty);
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                //初始化中部控件
                this.InitMiddleFrame();
            });
        }
 
        #endregion
 
        #region ■ 网关切换___________________________
 
        /// <summary>
        /// 执行切换网关操作
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <param name="online"></param>
        private void DoSwitchGateway(string gatewayId)
        {
            var result = HdlGatewayLogic.Current.DoSwitchGateway(gatewayId);
            if (result == false)
            {
                return;
            }
            //如果选择了刷新的网关,则不关闭管理界面
            this.closeDeviceManagForm = false;
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                if (HdlFormLogic.Current.IsFormOpen("DeviceListMainForm") == false)
                {
                    //刷新主画面
                    var form = new Device.DeviceListMainForm();
                    this.AddFormAndCloseNowForm(form);
                }
                else
                {
                    //关闭界面
                    this.CloseForm();
                    //刷新主画面(不重新获取设备状态)
                    this.LoadFormMethodByName("DeviceListMainForm", "InitMiddleFrame");
                }
            });
        }
 
        #endregion
 
        #region ■ 网关在线检测_______________________
 
        /// <summary>
        /// 上一次获取网关在线的时间(不能让它每点一次都去获取网关状态)
        /// </summary>
        private DateTime oldGetOnlineTime = new DateTime(1900, 1, 1);
 
        /// <summary>
        /// 开启网关在线监测的线程
        /// </summary>
        /// <param name="listway"></param>
        private void StartGatewayOnlieCheckThread(List<ZbGateway> listway)
        {
            if (oldGetOnlineTime.Year != 1900)
            {
                var timeValue = (DateTime.Now - oldGetOnlineTime).TotalSeconds;
                if (timeValue < 10)
                {
                    //最少要间隔十秒,才去重新获取
                    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.CheckGatewayOnlineByMemory(way);
                    this.GatewayOnlinePush(way, online, true);
                }
                //网关新版本检测
                this.CheckGatewayNewVersion(listway);
            });
        }
 
        /// <summary>
        /// 网关在线状态变更
        /// </summary>
        /// <param name="gateWay">网关对象</param>
        /// <param name="online">在线状态变更后的状态</param>
        /// <param name="hadGwOnline">2020.05.25追加:此住宅是否拥有网关在线</param>
        public override void GatewayOnlinePush(ZbGateway gateWay, bool online, bool hadGwOnline)
        {
            HdlThreadLogic.Current.RunMain(() =>
            {
                if (this.Parent == null)
                {
                    return;
                }
                string gwid = gateWay.GwId;
                if (this.dicRowContr.ContainsKey(gwid) == true && this.dicRowContr[gwid] != null)
                {
                    this.dicRowContr[gwid].IsOnline = online;
                }
            }, ShowErrorMode.NO);
        }
 
        #endregion
 
        #region ■ 网关新版本检测_____________________
 
        /// <summary>
        /// 网关新版本检测
        /// </summary>
        /// <param name="listWays"></param>
        private void CheckGatewayNewVersion(List<ZbGateway> listWays)
        {
            foreach (var way in listWays)
            {
                if (this.Parent == null)
                {
                    return;
                }
                if (HdlGatewayLogic.Current.CheckGatewayOnlineByMemory(way) == false)
                {
                    //不在线的不用理它
                    continue;
                }
                //获取最新版本
                var result = HdlGatewayLogic.Current.GetGatewayAllNewVersion(way, ShowErrorMode.NO);
                if (result == null)
                {
                    continue;
                }
                if (result[0] != null || result[1] != null || result[2] != null)
                {
                    //有新版本
                    string gwid = way.GwId;
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        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 CloseFormBefore()
        {
            base.CloseFormBefore();
 
            if (this.closeDeviceManagForm == true)
            {
                //关闭界面
                HdlGatewayLogic.Current.RefreshAppOldSelectGatewayId();
                this.LoadFormMethodByName("DeviceListMainForm", "CloseForm");
            }
        }
 
        #endregion
 
        #region ■ 界面重新激活事件___________________
 
        /// <summary>
        /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
        /// </summary>
        public override int FormActionAgainEvent()
        {
            //进到这个界面,理论上前回的网关id是有的,如果为空了,应该是当前的网关被解绑了
            if (HdlGatewayResourse.AppOldSelectGatewayId == string.Empty)
            {
                //关闭设备列表
                this.closeDeviceManagForm = true;
            }
            //初始化中部控件
            this.InitMiddleFrame();
            return 1;
        }
 
        #endregion
    }
}