BruceLee
2019-09-24 a9f88790c31c8b61d9b90241c4df258a980f1c00
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 固件升级画面
    /// </summary>
    public class DeviceFirmwareUpdateForm : DialogCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备的Mac地址
        /// </summary>
        private string deviceMac = string.Empty;
        /// <summary>
        /// 设备成功升级完成的回调函数
        /// </summary>
        public Action FinishUpdateEvent = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="device">ota设备对象</param>
        /// <param name="deviceFirmware">固件升级信息</param>
        public void ShowForm(OTADevice device, FirmwareVersionInfo deviceFirmware)
        {
            this.deviceMac = device.DeviceAddr;
            //初始化中部信息
            this.InitMiddleFrame(device, deviceFirmware);
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="device">ota设备对象</param>
        /// <param name="deviceFirmware">固件升级信息</param>
        private void InitMiddleFrame(OTADevice device, FirmwareVersionInfo deviceFirmware)
        {
            var frameBack = new FrameLayout();
            frameBack.Width = Application.GetRealWidth(674);
            frameBack.Height = Application.GetRealHeight(386);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameBack.Gravity = Gravity.CenterHorizontal;
            frameBack.Y = Application.GetRealHeight(683);
            frameBack.Radius = (uint)Application.GetMinRealAverage(6);
            bodyFrameLayout.AddChidren(frameBack);
 
            //进度显示文本
            var btnText = new NormalViewControl(frameBack.Width, Application.GetRealHeight(58), false);
            btnText.Y = Application.GetRealHeight(248);
            btnText.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnText.TextAlignment = TextAlignment.Center;
            //btnText.Text = "升级中";
            frameBack.AddChidren(btnText);
 
            //进度条
            var btnProRow = new FrameLayout();
            btnProRow.Gravity = Gravity.CenterHorizontal;
            btnProRow.Y = Application.GetRealHeight(161);
            btnProRow.Width = Application.GetRealWidth(559);
            btnProRow.Height = Application.GetRealHeight(29);
            btnProRow.BackgroundColor = 0xfff5f5f5;
            btnProRow.Radius = (uint)Application.GetRealHeight(29) / 2;
            frameBack.AddChidren(btnProRow);
            var btnProgressBar = new FrameLayout();
            btnProgressBar.Width = 0;
            btnProgressBar.Height = btnProRow.Height;
            btnProgressBar.Radius = btnProRow.Radius;
            btnProgressBar.BackgroundColor = 0xfffb744a;
            btnProgressBar.Radius = (uint)Application.GetRealHeight(29) / 2;
            btnProRow.AddChidren(btnProgressBar);
 
            //进度值文本
            var frameProgress = new FrameLayout();
            frameProgress.Width = Application.GetRealWidth(84);
            frameProgress.Height = Application.GetRealHeight(60);
            frameProgress.Y = Application.GetRealHeight(86);
            frameBack.AddChidren(frameProgress);
            frameProgress.X = btnProRow.X + btnProgressBar.Right - frameProgress.Width / 2;
            var btnProgressPic = new PicViewControl(84, 60);
            btnProgressPic.UnSelectedImagePath = "Item/ProgressMsg.png";
            frameProgress.AddChidren(btnProgressPic);
            var btnProgressView = new NormalViewControl(84, 32, true);
            btnProgressView.TextSize = 10;
            btnProgressView.TextAlignment = TextAlignment.Center;
            btnProgressView.Text = "0%";
            frameProgress.AddChidren(btnProgressView);
 
            //设备升级
            this.DeviceUpdateMethod(device, deviceFirmware, btnText, btnProgressView, frameProgress, btnProgressBar, btnProRow.Width);
        }
 
        #endregion
 
        #region ■ 设备升级___________________________
 
        /// <summary>
        /// 设备升级
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="deviceFirmware">固件信息</param>
        /// <param name="btnText">标题控件</param>
        /// <param name="btnProgressView">进度值文本的显示控件</param>
        /// <param name="frameProgress">进度值能够移动的那个框控件</param>
        /// <param name="btnProgressBar">进度条</param>
        /// <param name="ProRowWidth">进度条容器的最大宽度</param>
        private void DeviceUpdateMethod(OTADevice device, FirmwareVersionInfo deviceFirmware, NormalViewControl btnText,
            NormalViewControl btnProgressView, FrameLayout frameProgress, FrameLayout btnProgressBar, int ProRowWidth)
        {
            //设备升级
            var updateLogic = new HdlDeviceUpdateLogic(device, deviceFirmware);
            //更新状态变化的事件
            updateLogic.UpdateStatuChangedEvent += (div, msg) =>
            {
                if (div == -1)
                {
                    //异常
                    this.ShowMassage(ShowMsgType.Tip, msg);
                    this.CloseForm();
                }
                else if (div == 0)
                {
                    //一般信息
                    btnText.Text = msg;
                }
                else if (div == 1)
                {
                    //升级完成
                    this.ShowMassage(ShowMsgType.Tip, msg);
                    //调用回调函数
                    this.FinishUpdateEvent?.Invoke();
                    this.CloseForm();
                }
                else if (div == 2)
                {
                    //主动终止升级
                    this.CloseForm();
                }
            };
            //进度值改变事件
            updateLogic.ProgressEvent += (value) =>
            {
                Application.RunOnMainThread(() =>
                {
                    //进度条
                    decimal result = value / 100;
                    int width = (int)(result * ProRowWidth);
                    btnProgressBar.Width = width;
 
                    //文本显示
                    btnProgressView.Text = value + "%";
                    //文本显示的那个图片框移动
                    frameProgress.X = ControlCommonResourse.XXLeft + btnProgressBar.Right - frameProgress.Width / 2;
                });
 
            };
            //设备升级开始
            updateLogic.StartUpdateReady();
            //终止升级
            btnText.ButtonClickEvent += (sender, e) =>
            {
                //再次调用,内部条件达成时,可以选择终止升级
                updateLogic.StartUpdateReady();
            };
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseForm()
        {
            //升级对象
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(deviceMac) == true
                && FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].IsFinishUpdate == true)
            {
                //如果设备已经升级完成,界面关闭时,则移除内存
                FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].Dispose();
                FirmwareUpdateResourse.dicDeviceUpdateList.Remove(deviceMac);
            }
            this.FinishUpdateEvent = null;
 
            base.CloseForm();
        }
 
        #endregion
    }
}