黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceFirmwareUpdateForm.cs
@@ -1,26 +1,25 @@
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
    public class DeviceFirmwareUpdateForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 设备的Mac地址
        /// 设备mac地址
        /// </summary>
        private string deviceMac = string.Empty;
        private string deviceMac = null;
        /// <summary>
        /// 设备成功升级完成的回调函数
        /// 固件信息
        /// </summary>
        public Action FinishUpdateEvent = null;
        private FirmwareVersionInfo deviceFirmware = null;
        #endregion
@@ -29,74 +28,273 @@
        /// <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)
        /// <param name="i_deviceMac">设备mac地址</param>
        public void ShowForm(string i_deviceMac)
        {
            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 = 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);
            if (deviceMac != null && deviceMac != i_deviceMac)
            {
                //不是同一个东西
                return;
            }
            this.deviceMac = i_deviceMac;
            //进度条
            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);
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uFirmwareUpdate));
            //进度值文本
            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.InitMiddleFrame();
            //设备升级
            this.DeviceUpdateMethod(device, deviceFirmware, btnText, btnProgressView, frameProgress, btnProgressBar, btnProRow.Width);
            //历史版本
            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
@@ -106,18 +304,12 @@
        /// <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)
        private void DoDeviceUpdate(OTADevice oTADevice)
        {
            //打开进度条
            ProgressFormBar.Current.Start();
            //设备升级
            var updateLogic = new HdlDeviceUpdateLogic(device, deviceFirmware);
            var updateLogic = new HdlDeviceUpdateLogic(oTADevice, deviceFirmware);
            //更新状态变化的事件
            updateLogic.UpdateStatuChangedEvent += (div, msg) =>
            {
@@ -127,79 +319,57 @@
                    {
                        //异常
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        this.CloseForm();
                        ProgressFormBar.Current.Close();
                    }
                    else if (div == 0)
                    {
                        //一般信息
                        btnText.Text = msg;
                        ProgressFormBar.Current.SetMsg(msg);
                    }
                    else if (div == 1)
                    {
                        //升级完成
                        ProgressFormBar.Current.Close();
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        //调用回调函数
                        this.FinishUpdateEvent?.Invoke();
                        this.CloseForm();
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //初始化中部控件
                            this.InitMiddleFrame();
                        });
                    }
                    else if (div == 2)
                    {
                        //主动终止升级
                        this.CloseForm();
                        ProgressFormBar.Current.Close();
                    }
                });
            };
            //进度值改变事件
            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;
                });
                ProgressFormBar.Current.SetValue(value, 100);
            };
            //设备升级开始
            updateLogic.StartUpdateReady();
            //终止升级
            btnText.ButtonClickEvent += (sender, e) =>
            ProgressFormBar.Current.MsgClickEvent += () =>
            {
                //再次调用,内部条件达成时,可以选择终止升级
                updateLogic.StartUpdateReady();
            };
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseForm()
        {
            //安卓可以点击系统的返回键
            UserView.HomePage.Instance.ScrollEnabled = true;
            Shared.Common.CommonPage.BackKeyCanClick = true;
            //升级对象
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(deviceMac) == true
                && FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].IsFinishUpdate == true)
            //关闭事件
            ProgressFormBar.Current.CloseEvent += () =>
            {
                //如果设备已经升级完成,界面关闭时,则移除内存
                FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].Dispose();
                FirmwareUpdateResourse.dicDeviceUpdateList.Remove(deviceMac);
            }
            this.FinishUpdateEvent = null;
            base.CloseForm();
                //升级对象
                if (HdlFirmwareUpdateResourse.dicUpdateList.ContainsKey(deviceMac) == true
                    && HdlFirmwareUpdateResourse.dicUpdateList[deviceMac].IsFinishUpdate == true)
                {
                    //如果设备已经升级完成,界面关闭时,则移除内存
                    HdlFirmwareUpdateResourse.dicUpdateList[deviceMac].Dispose();
                    HdlFirmwareUpdateResourse.dicUpdateList.Remove(deviceMac);
                }
            };
        }
        #endregion