黄学彪
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
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 中央空调的升级逻辑
    /// </summary>
    public class HdlACZbGatewayUpdateLogic : DeviceUpdateCommon
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// <para>更新状态变化的事件</para>
        /// <para>第一个参数为:</para>
        /// <para>-1:更新异常,后面的值为异常信息的翻译文本</para>
        /// <para> 0:更新状态正常变化,后面的值为状态变更的文本翻译</para>
        /// <para> 1:升级成功</para>
        /// <para> 2:主动终止升级(中央空调木允许终止)</para>
        /// <para> 3:从等待中取消(目前还没有用)</para>
        /// </summary>
        public Action<int, string> UpdateStatuChangedEvent = null;
        /// <summary>
        /// 进度值事件
        /// </summary>
        public Action<decimal> ProgressEvent = null;
        /// <summary>
        /// 设备新版本的固件信息(多个设备升级时,应该会需要公开这个对象)
        /// </summary>
        public FirmwareVersionInfo deviceFirmware = null;
        /// <summary>
        /// 网关
        /// </summary>
        private ZbGateway zbGateway = null;
        /// <summary>
        /// 升级的设备
        /// </summary>
        private OTADevice otaDevice = null;
        /// <summary>
        /// 设备对象
        /// </summary>
        private CommonDevice deviceAc = null;
        /// <summary>
        /// 前一次的最终状态
        /// </summary>
        private UpdateStatuMode oldUpdateStatu = UpdateStatuMode.None;
        /// <summary>
        /// 固件的byte数组
        /// </summary>
        private byte[] deviceFirmwareByte = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// <para>HDL设备升级,以下为两个重要事件</para>
        /// <para>UpdateStatuChangedEvent:更新状态变化的事件</para>
        /// <para>ProgressEvent:进度值事件</para>
        /// <para>StartUpdateReady():设备开始执行升级的函数,在调用这个之前,请先实现上面两个方法</para>
        /// </summary>
        /// <param name="i_deviceAc">设备</param>
        /// <param name="i_deviceFirmware">设备的固件信息</param>
        public HdlACZbGatewayUpdateLogic(CommonDevice i_deviceAc, FirmwareVersionInfo i_deviceFirmware)
        {
            this.ClassDiv = 2;
            this.deviceAc = i_deviceAc;
            this.otaDevice = HdlDeviceCommonLogic.Current.GetOTADevice(i_deviceAc.DeviceAddr);
            this.deviceFirmware = i_deviceFirmware;
            this.zbGateway = i_deviceAc.Gateway;
        }
 
        #endregion
 
        #region ■ 开始更新___________________________
 
        /// <summary>
        /// 进入执行更新操作准备阶段
        /// </summary>
        public void StartUpdateReady()
        {
            if (this.UpdateStatu == UpdateStatuMode.Wait)
            {
                //如果是等待模式,再次点击时,移除列表
                if (HdlFirmwareUpdateResourse.dicUpdateList.ContainsKey(otaDevice.DeviceAddr) == true)
                {
                    HdlFirmwareUpdateResourse.dicUpdateList.Remove(otaDevice.DeviceAddr);
                }
                //取消
                this.UpdateStatu = UpdateStatuMode.None;
                this.UpdateStatuChangedEvent?.Invoke(3, "");
                return;
            }
            //如果它有状态,则表示之前它被什么错误中断了
            if (this.UpdateStatu != UpdateStatuMode.None)
            {
                //保存起来,后面有用处
                this.oldUpdateStatu = this.UpdateStatu;
            }
 
            //进入等待模式
            this.UpdateStatu = UpdateStatuMode.Wait;
            HdlFirmwareUpdateResourse.dicUpdateList[otaDevice.DeviceAddr] = this;
 
            //等待中…
            this.UpdateStatuChangedEvent?.Invoke(0, Language.StringByID(R.MyInternationalizationString.uWaitting));
 
            //执行下一个可更新的固件的更新操作
            HdlFirmwareUpdateLogic.Current.DoUpdateNextFirmware();
        }
 
        /// <summary>
        /// 开始执行更新操作
        /// </summary>
        public override void DoStartUpdate()
        {
            //状态变更
            this.IsFinishUpdate = false;
            this.UpdateStatu = UpdateStatuMode.Action;
            //根据状态执行操作
            this.DoAdjustByStatuMode();
        }
 
        /// <summary>
        /// 根据状态执行操作
        /// </summary>
        private void DoAdjustByStatuMode()
        {
            //先这样写吧,有可能以后会分情况
            //首发时,从开始执行
            if (this.oldUpdateStatu == UpdateStatuMode.None)
            {
                //执行校验版本
                this.DoCheckVersion();
            }
            //设备下载失败
            else if (this.oldUpdateStatu == UpdateStatuMode.DeviceDownLoadFail)
            {
                //执行校验版本
                this.DoCheckVersion();
            }
            //设备升级失败
            else if (this.oldUpdateStatu == UpdateStatuMode.DeviceUpdateFail)
            {
                //执行校验版本
                this.DoCheckVersion();
            }
            else
            {
                //重新再来
                this.DoCheckVersion();
            }
        }
 
        #endregion
 
        #region ■ 校验版本___________________________
 
        /// <summary>
        /// 校验版本
        /// </summary>
        private async void DoCheckVersion()
        {
            this.UpdateStatu = UpdateStatuMode.DeviceUpdateReady;
            //检测设备版本
            this.UpdateStatuChangedEvent?.Invoke(0, Language.StringByID(R.MyInternationalizationString.uCheckDeviceVersion));
            //设置初始值
            this.SetProgressValue(0, 100);
 
            await System.Threading.Tasks.Task.Delay(1000);
 
            //下载固件资源
            if (this.deviceFirmware.ImagType != "-100")
            {
                this.deviceFirmwareByte = HdlFirmwareUpdateLogic.Current.DowLoadFirmware(FirmwareLevelType.A设备, this.deviceFirmware.DistributedMark);
            }
            else
            {
                //取本地模板
                this.deviceFirmwareByte = HdlFileLogic.Current.ReadFileByteContent(this.deviceFirmware.Name);
            }
            if (this.deviceFirmwareByte == null)
            {
                //设备固件资源下载失败
                this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uDeviceFirmwareDownLoadFail));
 
                //显示重新下载模式
                this.ShowReDownLoadMode();
 
                this.UpdateStatu = UpdateStatuMode.DeviceDownLoadFail;
                return;
            }
 
            //数据偏移量,每个分包偏移量+(1-43)
            string offset = string.Empty;
            //一次发送的数据长度
            int dataLength = -1;
            //接收网关的透传数据
            Action<string, object> receiveAction = (comand, pushData) =>
            {
                string receiveData = ((CommonDevice.ClientDataPassthroughResponseData)pushData).PassData;
                if (receiveData.Length < 6)
                {
                    return;
                }
                try
                {
                    var command = receiveData[4].ToString() + receiveData[5].ToString() + receiveData[2].ToString() + receiveData[3].ToString();
                    if (command == "025c")
                    {
                        offset = receiveData.Substring(10, 8);
                        dataLength = Convert.ToInt32(receiveData.Substring(18, 2), 16);
                    }
                }
                catch { }
            };
            this.zbGateway.ReportAction += receiveAction;
 
            //读取空调模块版本
            var result = await HdlDeviceAirConditionerLogic.Current.ReadACFirewareVersionAsync(deviceAc);
            if (result == null || result.readACFirewareVersionResponData == null || result.readACFirewareVersionResponData.Status != 0)
            {
                //获取空调模块版本失败
                this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uGetAirConditionerModelVersionFail));
                //显示重新安装
                this.ShowReSetupMsg();
                this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                this.zbGateway.ReportAction -= receiveAction;
                //发送失败给设备
                HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                return;
            }
            //发送升级通知
            var result2 = await HdlDeviceAirConditionerLogic.Current.UpggradeACNotificationAsync(deviceAc, result.readACFirewareVersionResponData.FirewareVersion, this.deviceFirmwareByte.Length);
            if (result2.responseData == null)
            {
                //发送升级命令失败
                this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uSendUpdateComandFail));
                //显示重新安装
                this.ShowReSetupMsg();
                this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                this.zbGateway.ReportAction -= receiveAction;
                //发送失败给设备
                HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                return;
            }
            else if (result2.responseData.status == 1)
            {
                //版本号相同,升级失败
                this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uVersionIsEqualAndUpdateFail));
                //显示重新安装
                this.ShowReSetupMsg();
                this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                this.zbGateway.ReportAction -= receiveAction;
                //发送失败给设备
                HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                return;
            }
            else if (result2.responseData.status != 0)
            {
                //校验版本号失败
                this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uCheckVersionFail));
                //显示重新安装
                this.ShowReSetupMsg();
                this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                this.zbGateway.ReportAction -= receiveAction;
                //发送失败给设备
                HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                return;
            }
 
            int timeOut = 0;
            //等待接收偏移量
            while (dataLength == -1)
            {
                timeOut++;
                if (timeOut >= 30)
                {
                    //响应超时,检测设备版本失败
                    this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndCheckDeviceVersionFail));
                    //显示重新安装
                    this.ShowReSetupMsg();
                    this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                    this.zbGateway.ReportAction -= receiveAction;
                    //发送失败给设备
                    HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                    return;
                }
                await System.Threading.Tasks.Task.Delay(1000);
                //设置进度值
                this.SetProgressValue(timeOut, 30);
            }
            this.zbGateway.ReportAction -= receiveAction;
 
            //执行设备升级操作
            this.DoSetUpdateDevice(offset, dataLength);
        }
 
        #endregion
 
        #region ■ 设备升级___________________________
 
        /// <summary>
        /// 执行设备升级操作
        /// </summary>
        private async void DoSetUpdateDevice(string i_offset, int i_dataLength)
        {
            this.UpdateStatu = UpdateStatuMode.DeviceUpdateReady;
            //设备正在升级…
            this.UpdateStatuChangedEvent?.Invoke(0, Language.StringByID(R.MyInternationalizationString.uDeviceUpdating));
            //设置初始值
            this.SetProgressValue(0, 100);
 
            await System.Threading.Tasks.Task.Delay(1000);
 
            //发送数据的索引
            int startIndex = 0;
            int allDataLength = this.deviceFirmwareByte.Length;
            int timeOut = 0;
            //是否接收到成功命令
            bool receiveSuccess = false;
 
            //接收网关的透传数据
            bool hadReceive = true;
            Action<string, object> receiveAction = (comand, pushData) =>
            {
                if (comand != "DeviceRequestAcUpdateData")
                {
                    return;
                }
                string receiveData = ((CommonDevice.ClientDataPassthroughResponseData)pushData).PassData;
                if (receiveData.Length < 6)
                {
                    return;
                }
                try
                {
                    var command = receiveData[4].ToString() + receiveData[5].ToString() + receiveData[2].ToString() + receiveData[3].ToString();
                    if (command == "025c")
                    {
                        i_offset = receiveData.Substring(10, 8);
                        i_dataLength = Convert.ToInt32(receiveData.Substring(18, 2), 16);
                        hadReceive = true;
                        timeOut = 0;
                        //设置进度值
                        this.SetProgressValue(startIndex, allDataLength);
                    }
                    else if (command == "025e")
                    {
                        //成功  设置进度值直接100%
                        this.SetProgressValue(allDataLength, allDataLength);
                        receiveSuccess = true;
                        timeOut = 0;
                    }
                }
                catch { }
            };
            this.zbGateway.ReportAction += receiveAction;
 
            //最后一次需要等待回复结果才往下走
            while (startIndex < allDataLength)
            {
                if (receiveSuccess == true)
                {
                    //已经接收到成功的命令
                    break;
                }
                if (hadReceive == false)
                {
                    timeOut++;
                    if (timeOut >= 1500)
                    {
                        //响应超时,升级失败
                        this.ShowErrorMsg(Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndUpdateFail));
                        //显示重新安装
                        this.ShowReSetupMsg();
                        this.UpdateStatu = UpdateStatuMode.DeviceUpdateFail;
                        this.zbGateway.ReportAction -= receiveAction;
                        //发送失败给设备
                        HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 1);
                        return;
                    }
                    await System.Threading.Tasks.Task.Delay(50);
                    continue;
                }
                hadReceive = false;
                //因为偏移量是高位在前,所以倒过来
                string Myoffset = string.Empty;
                for (int i = 6; i >= 0; i = i - 2)
                {
                    Myoffset += i_offset.Substring(i, 2);
                }
                startIndex = Convert.ToInt32(Myoffset, 16) - i_dataLength;
 
                //获取一次能够发送的byte
                var listData = new List<byte>();
                for (; startIndex < allDataLength; startIndex++)
                {
                    listData.Add(this.deviceFirmwareByte[startIndex]);
                    if (listData.Count == i_dataLength)
                    {
                        break;
                    }
                }
                //发送透传数据
                var sendData = new HdlDeviceAirConditionerLogic.SendUpgradeData() { dataLength = i_dataLength, offset = i_offset };
                sendData.databytes = listData.ToArray();
                HdlDeviceAirConditionerLogic.Current.UpgradeAsync(deviceAc, sendData);
            }
            this.zbGateway.ReportAction -= receiveAction;
 
            //发送结束命令
            this.DoSendFinishComand();
        }
 
        #endregion
 
        #region ■ 分包结束___________________________
 
        /// <summary>
        /// 发送结束命令
        /// </summary>
        private async void DoSendFinishComand()
        {
            //正在发送升级完成的命令
            this.UpdateStatuChangedEvent?.Invoke(0, Language.StringByID(R.MyInternationalizationString.uSendingFinishUpdateComand));
 
            //发送成功命令给设备
            HdlDeviceAirConditionerLogic.Current.SendFinishAsync(deviceAc, 0);
 
            //等个两秒钟吧
            await System.Threading.Tasks.Task.Delay(2000);
 
            //显示升级完成的信息
            this.ShowFinishMsg();
        }
 
        #endregion
 
        #region ■ 设置进度___________________________
 
        /// <summary>
        /// 设定进度值
        /// </summary>
        /// <param name="value">进度值</param>
        /// <param name="maxValue">maxValue</param>
        private void SetProgressValue(decimal value, decimal maxValue)
        {
            this.ProgressEvent?.Invoke(value / maxValue);
        }
 
        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="value"></param>
        private void ShowErrorMsg(string value)
        {
            this.UpdateStatuChangedEvent?.Invoke(-1, value);
        }
 
        #endregion
 
        #region ■ 升级完成提示_______________________
 
        /// <summary>
        /// 显示升级完成的信息
        /// </summary>
        private void ShowFinishMsg()
        {
            //状态变更
            this.IsFinishUpdate = true;
            //升级完成
            this.UpdateStatu = UpdateStatuMode.UpdateFinish;
 
            HdlFirmwareUpdateLogic.Current.DoUpdateNextFirmware();
 
            //设备升级成功!
            this.UpdateStatuChangedEvent?.Invoke(1, Language.StringByID(R.MyInternationalizationString.uDeviceUpdateSuccess));
        }
 
        #endregion
 
        #region ■ 处理结果提示_______________________
 
        /// <summary>
        /// 显示重新安装的信息
        /// </summary>
        private void ShowReSetupMsg()
        {
            //状态变更
            this.IsFinishUpdate = true;
            //执行下一个升级
            HdlFirmwareUpdateLogic.Current.DoUpdateNextFirmware();
        }
 
        /// <summary>
        /// 显示重新下载模式
        /// </summary>
        private void ShowReDownLoadMode()
        {
            //状态变更
            this.IsFinishUpdate = true;
            //执行下一个升级
            HdlFirmwareUpdateLogic.Current.DoUpdateNextFirmware();
        }
 
        #endregion
 
        #region ■ 释放缓存___________________________
 
        /// <summary>
        /// 释放缓存
        /// </summary>
        public override void Dispose()
        {
            this.ProgressEvent = null;
            this.UpdateStatuChangedEvent = null;
        }
 
        #endregion
    }
}