黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 设备固件升级界面
    /// </summary>
    public class DeviceFirmwareUpdateForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备mac地址
        /// </summary>
        private string deviceMac = null;
        /// <summary>
        /// 固件信息
        /// </summary>
        private FirmwareVersionInfo deviceFirmware = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_deviceMac">设备mac地址</param>
        public void ShowForm(string i_deviceMac)
        {
            if (deviceMac != null && deviceMac != i_deviceMac)
            {
                //不是同一个东西
                return;
            }
            this.deviceMac = i_deviceMac;
 
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uFirmwareUpdate));
 
            //初始化中部控件
            this.InitMiddleFrame();
 
            //历史版本
            this.InitTopRightMenu();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            var oTADevice = HdlDeviceCommonLogic.Current.GetOTADevice(deviceMac);
            //获取设备最新版本
            this.deviceFirmware = HdlFirmwareUpdateLogic.Current.GetFirmwareMostVersionInfo(FirmwareLevelType.A设备,
                oTADevice.HwVersion.ToString(),
                oTADevice.ImgTypeId.ToString(),
                oTADevice.ImgVersion);
 
            //如果当前住宅是虚拟住宅,或者是展示模板,则没有新版本的说法
            if (Common.Config.Instance.Home.IsVirtually == true
                || Common.Config.Instance.Home.IsShowTemplate == true)
            {
                this.deviceFirmware = null;
            }
 
            if (deviceFirmware != null && deviceFirmware.FirmwareVersion > oTADevice.ImgVersion)
            {
                //拥有新版本
                this.InitControlByNewVersion();
            }
            else
            {
                //没有新版本
                this.InitControlByNotNewVersion();
            }
        }
 
        #endregion
 
        #region ■ 右上角菜单_________________________
 
        /// <summary>
        /// 初始化右上角菜单
        /// </summary>
        private void InitTopRightMenu()
        {
            if (HdlUserCenterResourse.HideOption.DeviceHistory != 1)
            {
                return;
            }
            var btnIcon = new MostRightIconControl(69, 69);
            btnIcon.UnSelectedImagePath = "Item/More.png";
            topFrameLayout.AddChidren(btnIcon);
            btnIcon.InitControl();
            btnIcon.ButtonClickEvent += ((sender, e) =>
            {
                //显示右上角菜单界面
                this.ShowTopRightMenu();
            });
        }
 
        /// <summary>
        /// 显示右上角菜单界面
        /// </summary>
        private void ShowTopRightMenu()
        {
            var frame = new TopRightMenuControl(1, 2);
            //历史版本
            var deviceMenu = Language.StringByID(R.MyInternationalizationString.uHistoryVersion);
            frame.AddRowMenu(deviceMenu, "", "", () =>
            {
                var form = new DeviceHistoryFirmwareVersionForm();
                form.AddForm(deviceMac);
                form.SelectFirmwareInfoEvent += (info) =>
                {
                    this.deviceFirmware = info;
                    //拥有新版本
                    this.InitControlByNewVersion();
                };
            });
        }
 
        #endregion
 
        #region ■ 拥有新版本_________________________
 
        /// <summary>
        /// 拥有新版本
        /// </summary>
        private void InitControlByNewVersion()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            var oTADevice = HdlDeviceCommonLogic.Current.GetOTADevice(deviceMac);
 
            //升级
            var btnUpdate = new BottomClickButton();
            btnUpdate.TextID = R.MyInternationalizationString.uLevelUp;
            bodyFrameLayout.AddChidren(btnUpdate);
            btnUpdate.ButtonClickEvent += (sender, e) =>
            {
                //设备升级
                HdlThreadLogic.Current.RunThread(() =>
                {
                    this.DoDeviceUpdate(oTADevice);
                });
            };
 
            var frameWhiteBack = new FrameLayout();
            frameWhiteBack.Height = Application.GetRealHeight(286);
            frameWhiteBack.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(frameWhiteBack);
 
            //当前固件版本
            var frameNow = new FrameRowControl();
            frameNow.UseClickStatu = false;
            frameNow.Y = Application.GetRealHeight(20);
            frameWhiteBack.AddChidren(frameNow);
            frameNow.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowFirmwareVersion), 500);
            frameNow.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(oTADevice.ImgVersion), 500);
            frameNow.AddBottomLine();
 
            //最新固件版本
            var frameNew = new FrameRowControl();
            frameNew.UseClickStatu = false;
            frameNew.Y = frameNow.Bottom + Application.GetRealHeight(12);
            frameWhiteBack.AddChidren(frameNew);
            frameNew.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNewFirmwareVersion), 500);
            frameNew.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(deviceFirmware.FirmwareVersion), 500);
            frameNew.AddBottomLine();
 
            //添加固件介绍行
            int maxHeight = btnUpdate.Y - HdlControlResourse.BottomButtonAndListViewSpace - frameNew.Bottom;
            this.AddUpdateContent(deviceFirmware, maxHeight, frameNew.Bottom);
        }
 
        #endregion
 
        #region ■ 没有新版本_________________________
 
        /// <summary>
        /// 没有新版本
        /// </summary>
        private void InitControlByNotNewVersion()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            var oTADevice = HdlDeviceCommonLogic.Current.GetOTADevice(deviceMac);
 
            var frameWhiteBack = new FrameLayout();
            frameWhiteBack.Height = Application.GetRealHeight(308);
            frameWhiteBack.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(frameWhiteBack);
 
            //当前固件版本
            var frameNow = new FrameRowControl();
            frameNow.UseClickStatu = false;
            frameNow.Y = Application.GetRealHeight(20);
            frameWhiteBack.AddChidren(frameNow);
            frameNow.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowFirmwareVersion), 500);
            frameNow.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(oTADevice.ImgVersion), 500);
            frameNow.AddBottomLine();
 
            //最新固件版本
            var frameNew = new FrameRowControl();
            frameNew.UseClickStatu = false;
            frameNew.Y = frameNow.Bottom + Application.GetRealHeight(12);
            frameWhiteBack.AddChidren(frameNew);
            frameNew.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNewFirmwareVersion), 500);
            frameNew.AddMostRightView(Language.StringByID(R.MyInternationalizationString.uNothing), 500);
        }
 
        #endregion
 
        #region ■ 添加固件介绍行_____________________
 
        /// <summary>
        /// 添加固件介绍行
        /// </summary>
        /// <param name="versionInfo">固件对象</param>
        /// <param name="Maxheight">最大高度</param>
        /// <param name="YY"></param>
        private void AddUpdateContent(FirmwareVersionInfo versionInfo, int Maxheight,int YY)
        {
            FrameListControl listFrame = null;
            VerticalListControl listView = null;
 
            var realHeight = Application.GetRealHeight(40 + 12) * versionInfo.UpdateContent.Count;
            realHeight += Application.GetRealHeight(17 + 12 + 49 + 8 + 63);
 
            //行间距
            int rowSpace = 0;
            if (realHeight > Maxheight)
            {
                realHeight = Maxheight;
                listView = new VerticalListControl(12);
                listView.Height = realHeight;
                listView.Y = YY;
                listView.BackgroundColor = UserCenterColor.Current.White;
                bodyFrameLayout.AddChidren(listView);
                rowSpace = listView.rowSpace / 2;
            }
            else
            {
                listFrame = new FrameListControl(12);
                listFrame.Height = realHeight;
                listFrame.Y = YY;
                listFrame.BackgroundColor = UserCenterColor.Current.White;
                bodyFrameLayout.AddChidren(listFrame);
                rowSpace = listFrame.rowSpace / 2;
            }
 
            //添加头部空白间隙
            var frameSpace1 = new FrameLayout();
            frameSpace1.Height = Application.GetRealHeight(17);
            listView?.AddChidren(frameSpace1);
            listFrame?.AddChidren(frameSpace1);
 
            //修改内容
            var btnContentRow = new FrameRowControl(rowSpace);
            btnContentRow.UseClickStatu = false;
            btnContentRow.Height = Application.GetRealHeight(49);
            listView?.AddChidren(btnContentRow);
            listFrame?.AddChidren(btnContentRow);
            var btnContent = btnContentRow.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uEditorContent), 500);
            btnContent.TextSize = 12;
 
            //稍微再添加空白间隙
            var frameSpace2 = new FrameLayout();
            frameSpace2.Height = Application.GetRealHeight(8);
            listView?.AddChidren(frameSpace2);
            listFrame?.AddChidren(frameSpace2);
 
            foreach (var msg in versionInfo.UpdateContent)
            {
                var btnRow = new FrameRowControl(rowSpace);
                btnRow.UseClickStatu = false;
                btnRow.Height = Application.GetRealHeight(40);
                listView?.AddChidren(btnRow);
                listFrame?.AddChidren(btnRow);
 
                var btnMsg = btnRow.AddLeftCaption(msg, 965);
                btnMsg.TextSize = 10;
                btnMsg.TextColor = UserCenterColor.Current.TextGrayColor3;
            }
 
            //添加底部空白间隙
            var frameSpace3 = new FrameLayout();
            frameSpace3.Height = Application.GetRealHeight(63);
            listView?.AddChidren(frameSpace3);
            listFrame?.AddChidren(frameSpace3);
        }
 
        #endregion
 
        #region ■ 设备升级___________________________
 
        /// <summary>
        /// 设备升级
        /// </summary>
        private void DoDeviceUpdate(OTADevice oTADevice)
        {
            //打开进度条
            ProgressFormBar.Current.Start();
            //设备升级
            var updateLogic = new HdlDeviceUpdateLogic(oTADevice, deviceFirmware);
            //更新状态变化的事件
            updateLogic.UpdateStatuChangedEvent += (div, msg) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    if (div == -1)
                    {
                        //异常
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        ProgressFormBar.Current.Close();
                    }
                    else if (div == 0)
                    {
                        //一般信息
                        ProgressFormBar.Current.SetMsg(msg);
                    }
                    else if (div == 1)
                    {
                        //升级完成
                        ProgressFormBar.Current.Close();
                        this.ShowMassage(ShowMsgType.Tip, msg);
 
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //初始化中部控件
                            this.InitMiddleFrame();
                        });
                    }
                    else if (div == 2)
                    {
                        //主动终止升级
                        ProgressFormBar.Current.Close();
                    }
                });
            };
            //进度值改变事件
            updateLogic.ProgressEvent += (value) =>
            {
                ProgressFormBar.Current.SetValue(value, 100);
            };
            //设备升级开始
            updateLogic.StartUpdateReady();
            //终止升级
            ProgressFormBar.Current.MsgClickEvent += () =>
            {
                //再次调用,内部条件达成时,可以选择终止升级
                updateLogic.StartUpdateReady();
            };
            //关闭事件
            ProgressFormBar.Current.CloseEvent += () =>
            {
                //升级对象
                if (HdlFirmwareUpdateResourse.dicUpdateList.ContainsKey(deviceMac) == true
                    && HdlFirmwareUpdateResourse.dicUpdateList[deviceMac].IsFinishUpdate == true)
                {
                    //如果设备已经升级完成,界面关闭时,则移除内存
                    HdlFirmwareUpdateResourse.dicUpdateList[deviceMac].Dispose();
                    HdlFirmwareUpdateResourse.dicUpdateList.Remove(deviceMac);
                }
            };
        }
 
        #endregion
    }
}