黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceFirmwareUpdateForm.cs
@@ -9,219 +9,199 @@
    /// <summary>
    /// 固件升级画面
    /// </summary>
    public class DeviceFirmwareUpdateForm : UserCenterCommonForm
    public class DeviceFirmwareUpdateForm : DialogCommonForm
    {
        /// <summary>
        /// 升级的设备
        /// </summary>
        private List<CommonDevice> listUpdateDevice = null;
        #region ■ 变量声明___________________________
        /// <summary>
        /// 设备新版本的固件信息
        /// </summary>
        private FirmwareVersionInfo deviceFirmware = null;
        /// <summary>
        /// 当前镜像版本
        /// 设备的Mac地址
        /// </summary>
        private int imageVersion = 0;
        private string deviceMac = string.Empty;
        /// <summary>
        /// 设备成功升级完成的回调函数
        /// </summary>
        public Action FinishUpdateEvent = null;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listdevice">设备</param>
        /// <param name="i_deviceFirmware">设备新版本</param>
        /// <param name="i_imageVersion">当前镜像版本</param>
        public void ShowForm(List<CommonDevice> i_listdevice, FirmwareVersionInfo i_deviceFirmware, int i_imageVersion)
        /// <param name="device">ota设备对象</param>
        /// <param name="deviceFirmware">固件升级信息</param>
        public void ShowForm(OTADevice device, FirmwareVersionInfo deviceFirmware)
        {
            this.listUpdateDevice = i_listdevice;
            this.imageVersion = i_imageVersion;
            this.deviceFirmware = i_deviceFirmware;
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uFirmwareUpdate));
            var btnIcon = new TopLayoutMostRightView();
            btnIcon.UnSelectedImagePath = "Item/More.png";
            btnIcon.SelectedImagePath = "Item/MoreSelected.png";
            topFrameLayout.AddChidren(btnIcon);
            btnIcon.MouseUpEventHandler += (sender, e) =>
            {
                //菜单控件
                var frameMenu = new TopRightMenuControl(this, 1);
                //历史版本
                string menu1 = Language.StringByID(R.MyInternationalizationString.uHistoryVersion);
                frameMenu.AddRowMenu(menu1, (obj) =>
                {
                    var form = new DeviceHistoryFirmwareVersionForm();
                    this.AddForm(form, listUpdateDevice);
                    form.FormCloseAction += ((firmwareInfo) =>
                    {
                        //如果当前正在升级,则历史版本无效
                        if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(this.listUpdateDevice[0].DeviceAddr) == false)
                        {
                            //重新刷新界面
                            this.ShowForm(listUpdateDevice, firmwareInfo, firmwareInfo.FirmwareVersion);
                        }
                    });
                });
            };
            //初始化中部控件
            this.InitMiddleFrame();
            this.deviceMac = device.DeviceAddr;
            //初始化中部信息
            this.InitMiddleFrame(device, deviceFirmware);
        }
        /// <summary>
        /// 初始化中部控件
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        /// <param name="device">ota设备对象</param>
        /// <param name="deviceFirmware">固件升级信息</param>
        private void InitMiddleFrame(OTADevice device, FirmwareVersionInfo deviceFirmware)
        {
            bodyFrameLayout.RemoveAll();
            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 (deviceFirmware == null)
            {
                //显示已经是新版本
                this.ShowIsMostVewVersion();
            }
            else
            {
                //显示已经是新版本
                this.ShowIsHadNewVersion();
            }
            //进度条
            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);
        }
        /// <summary>
        /// 显示已经是新版本
        /// </summary>
        private void ShowIsMostVewVersion()
        {
            //如果点的是历史版本,则同步界面
            string mainKeys = this.listUpdateDevice[0].DeviceAddr;
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(mainKeys) == true)
            {
                var btnUpdateControl = FirmwareUpdateResourse.dicDeviceUpdateList[mainKeys];
                bodyFrameLayout.AddChidren(btnUpdateControl);
                //初始化升级控件
                this.InitDeviceUpdateControl(btnUpdateControl);
                return;
            }
        #endregion
            //当前程序版本
            var btnVersionMsg = new MsgViewControl(800, true);
            btnVersionMsg.TextID = R.MyInternationalizationString.uIsMostNewVersion;
            btnVersionMsg.Y = Application.GetRealHeight(100);
            btnVersionMsg.TextAlignment = TextAlignment.Center;
            btnVersionMsg.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(btnVersionMsg);
            //版本
            var btnVersion = new MsgViewControl(800, true);
            btnVersion.Text = Common.LocalDevice.Current.AppendVersion(this.imageVersion);
            btnVersion.Y = btnVersionMsg.Bottom + Application.GetRealHeight(10);
            btnVersion.TextAlignment = TextAlignment.Center;
            btnVersion.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(btnVersion);
        }
        #region ■ 设备升级___________________________
        /// <summary>
        /// 显示存在最新版本
        /// 设备升级
        /// </summary>
        private void ShowIsHadNewVersion()
        /// <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)
        {
            //升级控件
            string mainKeys = this.listUpdateDevice[0].DeviceAddr;
            //临时控件
            var btnTemp = new ButtonCommon();
            Common.LocalDevice.Current.SetDeviceBeloneIconToControl(btnTemp, this.listUpdateDevice);
            DeviceFirmwareUpdateControl btnUpdateControl = null;
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(mainKeys) == true)
            //设备升级
            var updateLogic = new HdlDeviceUpdateLogic(device, deviceFirmware);
            //更新状态变化的事件
            updateLogic.UpdateStatuChangedEvent += (div, msg) =>
            {
                btnUpdateControl = FirmwareUpdateResourse.dicDeviceUpdateList[mainKeys];
                bodyFrameLayout.AddChidren(btnUpdateControl);
            }
            else
            {
                btnUpdateControl = new DeviceFirmwareUpdateControl(null, this.listUpdateDevice, this.deviceFirmware);
                bodyFrameLayout.AddChidren(btnUpdateControl);
                btnUpdateControl.InitChidrenControl(btnTemp.UnSelectedImagePath);
            }
            //初始化升级控件
            this.InitDeviceUpdateControl(btnUpdateControl);
        }
        /// <summary>
        /// 初始化升级控件
        /// </summary>
        /// <param name="btnUpdateControl"></param>
        private void InitDeviceUpdateControl(DeviceFirmwareUpdateControl btnUpdateControl)
        {
            //升级
            BottomClickButton btnUpdate = btnUpdateControl.btnUpdateButton;
            if (btnUpdate == null)
            {
                btnUpdate = new BottomClickButton();
                btnUpdate.UseClickStatu = false;
                btnUpdate.TextID = R.MyInternationalizationString.uLevelUp;
                btnUpdate.Gravity = Gravity.BottomCenter;
                btnUpdate.MouseUpTime = 0;
                btnUpdate.Radius = 0;
                btnUpdate.Width = Application.CurrentWidth;
                btnUpdate.MouseUpEvent += (sender, e) =>
                HdlThreadLogic.Current.RunMain(() =>
                {
                    if (btnUpdateControl.UpdateStatu == UpdateStatuMode.UpdateSuccess)
                    if (div == -1)
                    {
                        //升级成功的时候,如果单击此按钮,则关闭界面
                        //异常
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        this.CloseForm();
                        return;
                    }
                    //进入执行更新操作准备阶段
                    btnUpdateControl.StartUpdateReady();
                };
            }
            btnUpdateControl.btnUpdateButton = btnUpdate;
            bodyFrameLayout.AddChidren(btnUpdate);
            var listView = new VerticalScrolViewLayout();
            listView.Y = btnUpdateControl.Bottom;
            listView.Height = bodyFrameLayout.Height - btnUpdateControl.Height - btnUpdate.Height;
            bodyFrameLayout.AddChidren(listView);
            //做成更新内容控件
            ViewNormalControl btnTempMsg = null;
            //临时控件
            ButtonCommon btnTemp = null;
            //做成更新记录控件
            foreach (string msg in btnUpdateControl.deviceFirmware.UpdateContent)
                    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) =>
            {
                var btnmsg = new ViewNormalControl(listView.Width);
                btnmsg.X = ControlCommonResourse.XXLeft;
                if (btnTemp != null)
                Application.RunOnMainThread(() =>
                {
                    btnmsg.Y = btnTemp.Bottom + Application.GetRealHeight(5);
                }
                btnmsg.Text = msg;
                listView.AddChidren(btnmsg);
                btnTempMsg = btnmsg;
            }
                    //进度条
                    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>
        /// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
        public override void CloseForm(bool isCloseForm = true)
        public override void CloseForm()
        {
            //升级控件
            string mainKeys = this.listUpdateDevice[0].DeviceAddr;
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(mainKeys) == true
                && FirmwareUpdateResourse.dicDeviceUpdateList[mainKeys].IsFinishUpdate == true)
            //安卓可以点击系统的返回键
            UserView.HomePage.Instance.ScrollEnabled = true;
            Shared.Common.CommonPage.BackKeyCanClick = true;
            //升级对象
            if (FirmwareUpdateResourse.dicDeviceUpdateList.ContainsKey(deviceMac) == true
                && FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].IsFinishUpdate == true)
            {
                //如果设备已经升级完成,界面关闭时,则移除内存
                FirmwareUpdateResourse.dicDeviceUpdateList.Remove(mainKeys);
                FirmwareUpdateResourse.dicDeviceUpdateList[deviceMac].Dispose();
                FirmwareUpdateResourse.dicDeviceUpdateList.Remove(deviceMac);
            }
            this.FinishUpdateEvent = null;
            base.CloseForm(isCloseForm);
            base.CloseForm();
        }
        #endregion
    }
}