黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 网关备份业务的逻辑
    /// </summary>
    public class HdlGatewayBackupLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 备份业务的逻辑
        /// </summary>
        private static HdlGatewayBackupLogic m_Current = null;
        /// <summary>
        /// 备份业务的逻辑
        /// </summary>
        public static HdlGatewayBackupLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlGatewayBackupLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 网关备份___________________________
 
        /// <summary>
        /// 网关执行备份
        /// </summary>
        /// <param name="gatewayId">网关ID</param>
        /// <param name="backupName">需要创建的备份的名字</param>
        public void DoBackupGateway(string gatewayId, string backupName)
        {
            //展开进度条
            ProgressFormBar.Current.Start();
            //创建备份文件
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uCreatBackupFile));
            System.Threading.Thread.Sleep(1000);
 
            //创建一个备份
            string backupClassId = this.CreatGatewayBackup(gatewayId, backupName);
            if (backupClassId == null)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return;
            }
 
            //保存协调器的nv分区组网信息
            //bool result = this.SaveNVFile(realGateway);
            //if (result == false)
            //{
            //    //如果上传失败的话,就把它删除
            //    await HdlBackupLogic.Current.DeleteDbBackupData(backupClassId);
            //    //关闭进度条
            //    ProgressFormBar.Current.Close();
            //    return;
            //}
 
            //打开协调器串口发送功能
            //result = this.OpenZbMsComSend(realGateway);
            //if (result == false)
            //{
            //    //如果上传失败的话,就把它删除
            //    await HdlBackupLogic.Current.DeleteDbBackupData(backupClassId);
            //    //关闭进度条
            //    ProgressFormBar.Current.Close();
            //    return;
            //}
 
            //开始上传数据
            bool result = this.StartUpLoadData(gatewayId, backupClassId);
            if (result == false)
            {
                //如果上传失败的话,就把它删除
                this.DeleteGatewayBackup(gatewayId, backupClassId);
                //关闭进度条
                ProgressFormBar.Current.Close();
                return;
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
        }
 
        /// <summary>
        /// 删除网关备份
        /// </summary>
        /// <param name="i_gwId">网关id</param>
        /// <param name="i_backupId">备份的主键</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public bool DeleteGatewayBackup(string i_gwId, string i_backupId, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { gatewayId = i_gwId, folderId = i_backupId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/folder/delete", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 创建网关备份(返回备份主键,返回null代表失败)
        /// </summary>
        /// <param name="gwId">网关id</param>
        /// <param name="i_backName">备份名字</param>
        /// <returns></returns>
        private string CreatGatewayBackup(string gwId, string i_backName)
        {
            //创建一个备份名字
            var pra = new { backupClassify = "USER_DEFINED_BACKUP", backupDataType = "ZIGBEE", folderName = i_backName, gatewayId = gwId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/folder/add", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, ShowNetCodeMode.YES) == false)
            {
                return null;
            }
            var info = Newtonsoft.Json.JsonConvert.DeserializeObject<IdInfoClass>(result.Data.ToString());
            return info.Id;
        }
 
        #endregion
 
        #region ■ 开始网关数据备份___________________
 
        /// <summary>
        /// 开始上传数据
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <param name="backupClassId"></param>
        private bool StartUpLoadData(string gatewayId, string backupClassId)
        {
            //开始备份网关数据
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uStartBackupgatewayData));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            bool threadAction = true;
            bool success = false;
            //等待时间
            int waitime = 60;
            //计时时间
            int TimeOut = 0;
 
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/BackupGwDataFile_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    var statu = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (statu == 1)
                    {
                        threadAction = false;
                        //网关数据备份失败
                        HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uBackupgatewayDataFail));
                        return;
                    }
                    if (statu == 2)
                    {
                        threadAction = false;
                        success = true;
                        //上传数据成功
                        string msg2 = Language.StringByID(R.MyInternationalizationString.uUploadDataSuccess);
                        HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg2);
                    }
                    //将时间刷新,然后等待时间变更为30秒
                    TimeOut = 0;
                    waitime = 300;
                }
                else if (topic == gatewayId + "/BackupGwDataFileProgress_Respon")
                {
                    //刷新等待时间
                    TimeOut = 0;
                    //上传进度百分比
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    var statu = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (statu != 0)
                    {
                        threadAction = false;
                        //网关数据备份失败
                        HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uBackupgatewayDataFail));
                        return;
                    }
                    //设置进度百分比
                    var persent = Convert.ToInt32(jobject["Data"]["BackupPercent"].ToString());
                    ProgressFormBar.Current.SetValue(persent, 100);
                }
            });
            //发送数据
            var fullUrl = HdlHttpLogic.Current.RequestHttpsHost + @"home-wisdom/gateway/file/create";
            string authorization = Common.Config.Instance.HeaderPrefix + HdlHttpLogic.Current.GetHttpToken(CheckMode.A账号权限);
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6200 } };
            var data = new Newtonsoft.Json.Linq.JObject {
                { "url", fullUrl },{ "authorization", authorization },{ "folderId", backupClassId }};
            jObject.Add("Data", data);
 
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "BackupGwDataFile", jObject.ToString());
          
            while (threadAction == true && TimeOut < waitime)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            //清除事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
 
            if (TimeOut >= waitime)
            {
                //响应超时,网关数据备份失败
                string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndBackupgatewayDataFail);
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
            System.Threading.Thread.Sleep(1000);
 
            return success;
        }
 
        #endregion
 
        #region ■ 从云端获取备份文件列表_____________
 
        /// <summary>
        /// 从云端获取网关的备份列表
        /// </summary>
        /// <param name="gatewayId">网关di</param>
        /// <param name="backMode">获取的备份类型</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        public List<BackupListNameInfo> GetGatewayBackupListFromDb(string gatewayId, BackUpMode backMode, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { mac = gatewayId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/folder/findAll", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return null;
            }
            var listTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BackupListNameInfo>>(result.Data.ToString());
            var strDiv = HdlBackupLogic.Current.GetBackUpModeText(backMode);
 
            var listData = new List<BackupListNameInfo>();
            foreach (var info in listTemp)
            {
                if (info.BackupClassify != strDiv || info.BackupDataType != "ZIGBEE")
                {
                    //不是指定的备份
                    continue;
                }
                listData.Add(info);
            }
            return listData;
        }
 
        /// <summary>
        /// 发送从云端获取备份文件列表的命令给网关
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <param name="backupClassId"></param>
        /// <returns></returns>
        private bool SendGetGatewayFileListComandToGateway(string gatewayId, string backupClassId)
        {
            //正在获取备份文件列表
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            int result = -1;
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/GetDataFileList_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (result != 0)
                    {
                        //获取备份文件列表失败
                        HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uGetBackupFileListFail));
                        return;
                    }
                }
            });
            //发送数据
            var fullUrl = HdlHttpLogic.Current.RequestHttpsHost + @"home-wisdom/backup/file/findAll";
            string authorization = Common.Config.Instance.HeaderPrefix + HdlHttpLogic.Current.GetHttpToken(CheckMode.A账号权限);
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6202 } };
            var data = new Newtonsoft.Json.Linq.JObject {
                { "url", fullUrl },{ "authorization", authorization },{ "folderId", backupClassId }};
            jObject.Add("Data", data);
 
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "GetDataFileList", jObject.ToString());
 
            int timeOut = 0;
            while (result == -1 && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            //移除事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
 
            if (result != 0)
            {
                string msg = Language.StringByID(R.MyInternationalizationString.uGetBackupFileListFail);
                if (result == -1)
                {
                    //响应超时,获取备份文件列表失败
                    msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndGetBackupFileListFail);
                }
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 从云端下载备份文件_________________
 
        /// <summary>
        /// 发送从云端下载备份文件的命令到网关
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <param name="backupClassId"></param>
        /// <returns></returns>
        private bool SendDownloadGatewayFileComandToGateway(string gatewayId, string backupClassId)
        {
            //正在下载备份文件
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileDownloading));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            bool threadAction = true;
            bool success = false;
            int timeOut = 0;
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/RestoreDataFileProgress_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    var result = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                    if (result == 1 || result == 3)
                    {
                        //下载备份文件失败
                        HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uDownloadBackupFileFail));
                        threadAction = false;
                        return;
                    }
                    else if (result == 2)
                    {
                        //下载成功
                        success = true;
                        threadAction = false;
                        return;
                    }
                    else if (result == 0)
                    {
                        //刷新等待时间
                        timeOut = 0;
                        //设置进度百分比
                        var persent = Convert.ToInt32(jobject["Data"]["RestoreDataPercent"].ToString());
                        //网关下载完成之后,它自己会卡一会,才会发送2过来
                        ProgressFormBar.Current.SetValue(persent - 1, 100);
                    }
                }
            });
 
            //发送数据
            var fullUrl = HdlHttpLogic.Current.RequestHttpsHost + @"home-wisdom/gateway/file/downOne";
            string authorization = Common.Config.Instance.HeaderPrefix + HdlHttpLogic.Current.GetHttpToken(CheckMode.A账号权限);
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6203 } };
            var data = new Newtonsoft.Json.Linq.JObject {
                { "url", fullUrl },{ "authorization", authorization },{ "folderId", backupClassId }};
            jObject.Add("Data", data);
 
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "GetDataFileList", jObject.ToString());
 
            while (threadAction == true && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            //移除事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
 
            if (timeOut >= 30)
            {
                //响应超时,下载备份文件失败
                string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndDownloadBackupFileFail);
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
            return success;
        }
 
        #endregion
 
        #region ■ 编辑网关备份名称___________________
 
        /// <summary>
        /// 编辑网关备份名称
        /// </summary>
        /// <param name="i_backId">备份的主键</param>
        /// <param name="i_newName">备份的新名称</param>
        /// <param name="mode">失败时是否显示tip消息</param>
        /// <returns></returns>
        public bool EditorGatewayBackupName(string i_backId, string i_newName, ShowNetCodeMode mode = ShowNetCodeMode.YES)
        {
            var pra = new { gatewayFolderId = i_backId, folderName = i_newName, homeId = Common.Config.Instance.Home.Id };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("home-wisdom/app/gateway/folder/update", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            //检测状态码
            if (HdlCheckLogic.Current.CheckNetCode(result, mode) == false)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 网关还原___________________________
 
        /// <summary>
        /// 执行网关还原操作
        /// </summary>
        /// <param name="gatewayId">网关ID</param>
        /// <param name="backupId">备份主键</param>
        public bool RecoverGateway(string gatewayId, string backupId)
        {
            //展开进度条
            ProgressFormBar.Current.Start();
            //发送从云端获取备份文件列表的命令给网关
            var result = this.SendGetGatewayFileListComandToGateway(gatewayId, backupId);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
 
            //发送从云端下载备份文件的命令到网关
            result = this.SendDownloadGatewayFileComandToGateway(gatewayId, backupId);
            if (result == false)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
 
            ////恢复协调器nv分区的组网信息
            //result = this.RestoreGatewayNV(realGateway);
            //if (result == false)
            //{
            //    //关闭进度条
            //    ProgressFormBar.Current.Close();
            //    return false;
            //}
 
            //打开协调器串口发送功能
            //result = this.OpenZbMsComSend(realGateway);
            //if (result == false)
            //{
            //    //关闭进度条
            //    ProgressFormBar.Current.Close();
            //    return false;
            //}
 
            //重启网关系统
            result = this.GatewayReboot(gatewayId);
            //关闭进度条
            ProgressFormBar.Current.Close();
            return result;
        }
 
        #endregion
 
        #region ■ 重启网关系统_______________________
 
        /// <summary>
        /// 重启网关系统
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <returns></returns>
        private bool GatewayReboot(string gatewayId)
        {
            //正在重启网关系统
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayRerooting));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            int result = -1;
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/" + "GwReboot_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Result"].ToString());
                }
            });
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 83 } };
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "GwReboot", jObject.ToString());
 
            int timeOut = 0;
            while (result == -1 && timeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                timeOut++;
            }
            //移除事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
 
            if (result != 0)
            {
                //重启网关系统失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGatewayRerootFail);
                if (result == -1)
                {
                    msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                }
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
 
            //网关已经接收到重启命令,接下来去判断网关是否重启完成
            bool gatewatAction = false;
            bool threadAction = true;
            HdlThreadLogic.Current.RunThread(() =>
            {
                timeOut = 0;
                while (gatewatAction == false)
                {
                    //大概网关重启要60秒
                    if (timeOut >= 65 && timeOut % 5 == 0)
                    {
                        //5秒一次,去获取版本
                        var data = HdlGatewayLogic.Current.RefreshGatewayInfo(gatewayId, false, ShowErrorMode.NO);
                        if (data != null)
                        {
                            gatewatAction = true;
                            threadAction = false;
                            break;
                        }
                    }
                    System.Threading.Thread.Sleep(1000);
                    timeOut++;
                    //设置进度值
                    ProgressFormBar.Current.SetValue(timeOut, 180);
                    if (timeOut >= 180)
                    {
                        //180秒还等不到的话,网关应该出问题了
                        threadAction = false;
                        break;
                    }
                }
            });
            while (threadAction == true)
            {
                System.Threading.Thread.Sleep(4000);
            }
            if (gatewatAction == true)
            {
                //设置进度值:100%
                ProgressFormBar.Current.SetValue(1, 1);
                System.Threading.Thread.Sleep(1000);
                //网关数据恢复成功!
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreGatewayDataSuccess);
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
                return true;
            }
            else
            {
                //响应超时,重启网关系统失败
                string msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndGatewayRerootFail);
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
        }
 
        #endregion
 
        #region ■ 保存协调器的nv分区组网信息_________
 
        /// <summary>
        /// 保存协调器的nv分区组网信息
        /// </summary>
        /// <param name="realGateway"></param>
        /// <returns></returns>
        private bool SaveNVFile(ZbGateway realGateway)
        {
            //正在保存协调器nv分区信息
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayNVFileSaving));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGwOperation/SaveNVFile_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Flag"].ToString());
                }
            };
            realGateway.Actions += action;
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 500 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "ImageName", "NVImage.bin" }, { "ImagePath", "/etc/hdlDat/" } };
            jObject.Add("Data", data);
            realGateway.Send("ZbGwOperation/SaveNVFile", jObject.ToString());
 
            int TimeOut = 0;
            while (result == -1 && TimeOut < 60)
            {
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
                ProgressFormBar.Current.SetValue(TimeOut, 60);
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                //保存协调器的nv分区组网信息失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSaveGatewayNVFileFail);
                if (result == -1)
                {
                    msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                }
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
 
            return true;
        }
 
        #endregion
 
        #region ■ 打开协调器串口发送功能_____________
 
        /// <summary>
        /// 打开协调器串口发送功能
        /// </summary>
        /// <param name="realGateway"></param>
        /// <returns></returns>
        private bool OpenZbMsComSend(ZbGateway realGateway)
        {
            //正在打开协调器串口发送功能
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uZbMsComSendOpening));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "OpenZbMsComSend_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Status"].ToString());
                }
            };
            realGateway.Actions += action;
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 6119 } };
            realGateway.Send("OpenZbMsComSend", jObject.ToString());
 
            int TimeOut = 0;
            while (result == -1 && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                //打开协调器串口发送功能失败
                string msg = Language.StringByID(R.MyInternationalizationString.uOpenZbMsComSendFail);
                if (result == -1)
                {
                    msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                }
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
 
            return true;
        }
 
        #endregion
 
        #region ■ 恢复协调器nv分区的组网信息_________
 
        /// <summary>
        /// 恢复协调器nv分区的组网信息
        /// </summary>
        /// <param name="realGateway"></param>
        /// <returns></returns>
        private bool RestoreGatewayNV(ZbGateway realGateway)
        {
            //正在恢复协调器nv分区
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uGatewayNVRestoring));
            ProgressFormBar.Current.SetValue(0);
            System.Threading.Thread.Sleep(1000);
 
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGwOperation/RestoreNV_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Flag"].ToString());
                }
            };
            realGateway.Actions += action;
 
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 501 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "ImageName", "NVImage.bin" }, { "ImagePath", "/etc/hdlDat/" } };
            jObject.Add("Data", data);
            realGateway.Send("ZbGwOperation/RestoreNV", jObject.ToString());
 
            int TimeOut = 0;
            while (result == -1 && TimeOut < 60)
            {
                System.Threading.Thread.Sleep(1000);
                TimeOut++;
                ProgressFormBar.Current.SetValue(TimeOut, 60);
            }
            realGateway.Actions -= action;
            if (result != 0)
            {
                //恢复协调器nv分区失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreGatewayNVFail);
                if (result == -1)
                {
                    //响应超时,恢复协调器nv分区失败
                    msg = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndRestoreGatewayNVFail);
                }
                HdlMessageLogic.Current.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
 
            return true;
        }
 
        #endregion
    }
}