黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/UserCenter/Gateway/Update/GatewayFirmwareUpdateForm.cs
@@ -6,36 +6,24 @@
namespace Shared.Phone.UserCenter.GatewayUpdate
{
    /// <summary>
    /// 固件升级画面
    /// 网关固件升级界面
    /// </summary>
    public class GatewayFirmwareUpdateForm : DialogCommonForm
    public class GatewayFirmwareUpdateForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 要升级的网关
        /// 当前选择的网关
        /// </summary>
        private ZbGateway upDatezbGateway = null;
        private ZbGateway zbGateway = null;
        /// <summary>
        /// 虚拟设备的固件信息
        /// 升级固件信息(0:Linux新版本  1:协调器新版本  2~X:都是虚拟驱动的)
        /// </summary>
        private FirmwareVersionInfo virtualFirmware = null;
        /// <summary>
        /// 协调器新版本的固件信息
        /// </summary>
        private FirmwareVersionInfo coordinatorFirmware = null;
        /// <summary>
        /// 网关新版本的固件信息
        /// </summary>
        private FirmwareVersionInfo gatewayFirmware = null;
        private List<FirmwareVersionInfo> listVersionInfo = null;
        /// <summary>
        /// 虚拟驱动号
        /// </summary>
        private List<ZbGatewayData.DriveCodeObj> listVode = null;
        /// <summary>
        /// 设备成功升级完成的回调函数
        /// </summary>
        public Action FinishUpdateEvent = null;
        #endregion
@@ -44,99 +32,338 @@
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_zbGateway">网关</param>
        /// <param name="i_virtualFirmware">虚拟设备的固件信息</param>
        /// <param name="i_coordinatorFirmware">协调器新版本的固件信息</param>
        /// <param name="i_gatewayFirmware">网关新版本的固件信息</param>
        /// <param name="i_listVode">虚拟驱动</param>
        public void ShowForm(ZbGateway i_zbGateway, FirmwareVersionInfo i_virtualFirmware, FirmwareVersionInfo i_coordinatorFirmware,
            FirmwareVersionInfo i_gatewayFirmware, List<ZbGatewayData.DriveCodeObj> i_listVode)
        /// <param name="i_zbGateway">当前选择的网关</param>
        public void ShowForm(ZbGateway i_zbGateway)
        {
            this.upDatezbGateway = i_zbGateway;
            this.virtualFirmware = i_virtualFirmware;
            this.coordinatorFirmware = i_coordinatorFirmware;
            this.gatewayFirmware = i_gatewayFirmware;
            this.listVode = i_listVode;
            this.zbGateway = i_zbGateway;
            //初始化中部信息
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uFirmwareUpdate));
            //初始化右上角菜单
            this.InitTopRightMenu();
            //初始化中部控件
            this.InitMiddleFrame();
        }
        /// <summary>
        /// 初始化中部信息
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
            HdlThreadLogic.Current.RunThread(() =>
            {
                //开启进度条
                this.ShowProgressBar();
                //获取网关新版本信息
                this.listVersionInfo = HdlGatewayLogic.Current.GetGatewayAllNewVersion(this.zbGateway);
                if (listVersionInfo == null)
                {
                    //关闭进度条
                    this.CloseProgressBar(ShowReLoadMode.YES);
                    return;
                }
                //虚拟驱动号
                this.listVode = HdlGatewayLogic.Current.GetListVDDriveCode(this.zbGateway);
                //关闭进度条
                this.CloseProgressBar();
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //初始化当前版本的控件
                    var yy = this.InitNowVersionControl();
                    if (listVersionInfo[0] != null || listVersionInfo[1] != null || listVersionInfo[2] != null)
                    {
                        //初始化新版本的控件
                        this.InitNewVersionControl(yy);
                    }
                });
            });
        }
        /// <summary>
        /// 初始化当前版本的控件
        /// </summary>
        private int InitNowVersionControl()
        {
            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 = (uint)Application.GetRealHeight(17);
            frameBack.Height = Application.GetMinReal(800);
            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 btnTitle = new NormalViewControl(700, 65, true);
            btnTitle.X = HdlControlResourse.XXLeft;
            btnTitle.Y = Application.GetRealHeight(30);
            btnTitle.TextID = R.MyInternationalizationString.uNowFirmwareVersion;
            btnTitle.TextSize = 15;
            frameBack.AddChidren(btnTitle);
            //进度条
            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.BackgroundColor = 0xfffb744a;
            btnProgressBar.Radius = (uint)Application.GetRealHeight(29) / 2;
            btnProRow.AddChidren(btnProgressBar);
            var listView = new VerticalListControl(12);
            listView.Y = Application.GetRealHeight(109);
            listView.Height = Application.GetRealHeight(800);
            frameBack.AddChidren(listView);
            //进度值文本
            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, 45, true);
            btnProgressView.TextSize = 10;
            btnProgressView.TextAlignment = TextAlignment.Center;
            btnProgressView.Text = "0%";
            frameProgress.AddChidren(btnProgressView);
            //Linux
            var rowLinux = new FrameRowControl(listView.rowSpace / 2);
            listView.AddChidren(rowLinux);
            rowLinux.AddLeftCaption("Linux", 300);
            rowLinux.AddRightArrow();
            rowLinux.ButtonClickEvent += (sender, e) =>
            {
                var form = new GatewayLinuxInfoForm();
                form.AddForm(this.zbGateway.GwId);
            };
            //设备升级
            this.GatewayUpdateMethod(btnText, btnProgressView, frameProgress, btnProgressBar, btnProRow.Width);
            //协调器
            var rowCoordinator = new FrameRowControl(listView.rowSpace / 2);
            listView.AddChidren(rowCoordinator);
            rowCoordinator.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uCoordinator), 500);
            rowCoordinator.AddRightArrow();
            rowCoordinator.ButtonClickEvent += (sender, e) =>
            {
                var form = new GatewayCoordinatorInfoForm();
                form.AddForm(this.zbGateway.GwId);
            };
            if (this.listVode != null)
            {
                //虚拟驱动
                var rowVirtual = new FrameRowControl(listView.rowSpace / 2);
                listView.AddChidren(rowVirtual);
                rowVirtual.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uVirtualDrive), 500);
                rowVirtual.AddRightArrow();
                rowVirtual.ButtonClickEvent += (sender, e) =>
                {
                    var form = new GatewayVirtualDriveInfoForm();
                    form.AddForm(listVode);
                };
            }
            //没有新版本
            if (listVersionInfo[0] == null && listVersionInfo[1] == null && listVersionInfo[2] == null)
            {
                //最新固件版本
                var rowNew = new FrameRowControl();
                rowNew.UseClickStatu = false;
                listView.AddChidren(rowNew);
                rowNew.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNewFirmwareVersion), 500);
                rowNew.AddMostRightView(Language.StringByID(R.MyInternationalizationString.uNothing), 500);
            }
            //划线
            for (int i = 0; i < listView.ChildrenCount - 1; i++)
            {
                ((FrameRowControl)listView.GetChildren(i)).AddBottomLine();
            }
            //调整真实高度
            listView.AdjustRealHeight(Application.GetRealHeight(23));
            frameBack.Height = listView.Bottom;
            return frameBack.Bottom;
        }
        /// <summary>
        /// 初始化新版本的控件
        /// </summary>
        private void InitNewVersionControl(int YY)
        {
            var frameBack = new FrameLayout();
            frameBack.Y = YY + Application.GetRealHeight(35);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameBack.Height = Application.GetMinReal(832);
            bodyFrameLayout.AddChidren(frameBack);
            //最新固件版本
            var btnTitle = new NormalViewControl(700, 65, true);
            btnTitle.X = HdlControlResourse.XXLeft;
            btnTitle.Y = Application.GetRealHeight(30);
            btnTitle.TextID = R.MyInternationalizationString.uNewFirmwareVersion;
            btnTitle.TextSize = 15;
            frameBack.AddChidren(btnTitle);
            var listView = new VerticalListControl(12);
            listView.Y = Application.GetRealHeight(109);
            listView.Height = frameBack.Height - Application.GetRealHeight(109);
            frameBack.AddChidren(listView);
            //Linux
            if (listVersionInfo[0] != null)
            {
                var rowLinux = new FrameRowControl(listView.rowSpace / 2);
                rowLinux.UseClickStatu = false;
                listView.AddChidren(rowLinux);
                rowLinux.AddLeftCaption("Linux", 300);
                rowLinux.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(listVersionInfo[0].FirmwareVersion), 300);
                rowLinux.AddBottomLine();
            }
            //协调器
            if (listVersionInfo[1] != null)
            {
                var rowCoordinator = new FrameRowControl(listView.rowSpace / 2);
                rowCoordinator.UseClickStatu = false;
                listView.AddChidren(rowCoordinator);
                rowCoordinator.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uCoordinator), 500);
                rowCoordinator.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(listVersionInfo[1].FirmwareVersion), 300);
                rowCoordinator.AddBottomLine();
            }
            //虚拟驱动
            if (listVersionInfo[2] != null)
            {
                var rowVirtual = new FrameRowControl(listView.rowSpace / 2);
                rowVirtual.UseClickStatu = false;
                listView.AddChidren(rowVirtual);
                rowVirtual.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uVirtualDrive), 500);
                rowVirtual.AddMostRightView(HdlDeviceCommonLogic.Current.AppendVersion(listVersionInfo[2].FirmwareVersion), 300);
                rowVirtual.AddBottomLine();
            }
            //添加固件介绍行
            this.AddUpdateContent(listView);
            //升级
            var btnUpdate = new BottomClickButton();
            btnUpdate.TextID = R.MyInternationalizationString.uLevelUp;
            bodyFrameLayout.AddChidren(btnUpdate);
            btnUpdate.ButtonClickEvent += (sender, e) =>
            {
                ZbGateway realway = null;
                if (HdlGatewayLogic.Current.GetRealGateway(ref realway, zbGateway) == false)
                {
                    //错误:网关对象丢失
                    string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                    this.ShowMassage(ShowMsgType.Error, msg);
                    return;
                }
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //网关升级
                    DoGatewayUpdate(realway);
                });
            };
        }
        #endregion
        #region ■ 设备升级___________________________
        #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 GatewayHistoryFirmwareVersionForm();
                form.AddForm(zbGateway, listVersionInfo[2], listVersionInfo[1], listVersionInfo[0], listVode);
                form.SelectFirmwareInfoEvent += (info1,info2,info3,listCode) =>
                {
                    listVersionInfo[2] = info1;
                    listVersionInfo[1] = info2;
                    listVersionInfo[0] = info3;
                    listVode = listCode;
                    //初始化当前版本的控件
                    var yy = this.InitNowVersionControl();
                    if (listVersionInfo[0] != null || listVersionInfo[1] != null || listVersionInfo[2] != null)
                    {
                        //初始化新版本的控件
                        this.InitNewVersionControl(yy);
                    }
                };
            });
        }
        #endregion
        #region ■ 添加固件介绍行_____________________
        /// <summary>
        /// 添加固件介绍行
        /// </summary>
        /// <param name="listContent">列表控件</param>
        private void AddUpdateContent(VerticalListControl listContent)
        {
            //修改内容
            var frameContent = new FrameLayout();
            frameContent.Height = Application.GetRealHeight(87);
            listContent.AddChidren(frameContent);
            var btnContent = new NormalViewControl(500, 87, true);
            btnContent.X = HdlControlResourse.XXLeft;
            btnContent.Y = Application.GetRealHeight(28);
            btnContent.TextID = R.MyInternationalizationString.uEditorContent;
            btnContent.TextSize = 12;
            frameContent.AddChidren(btnContent);
            foreach (var info in this.listVersionInfo)
            {
                if (info == null)
                {
                    continue;
                }
                foreach (var msg in info.UpdateContent)
                {
                    var btnRow = new FrameRowControl();
                    btnRow.UseClickStatu = false;
                    btnRow.Height = Application.GetRealHeight(40);
                    listContent.AddChidren(btnRow);
                    var btnMsg = btnRow.AddLeftCaption(msg, 965);
                    btnMsg.TextSize = 10;
                    btnMsg.TextColor = UserCenterColor.Current.TextGrayColor3;
                }
            }
            //添加底部空白间隙
            var frameSpace3 = new FrameLayout();
            frameSpace3.Height = Application.GetRealHeight(58);
            listContent.AddChidren(frameSpace3);
        }
        #endregion
        #region ■ 网关升级___________________________
        /// <summary>
        /// 网关升级
        /// </summary>
        /// <param name="btnText">标题控件</param>
        /// <param name="btnProgressView">进度值文本的显示控件</param>
        /// <param name="frameProgress">进度值能够移动的那个框控件</param>
        /// <param name="btnProgressBar">进度条</param>
        /// <param name="ProRowWidth">进度条容器的最大宽度</param>
        private void GatewayUpdateMethod(NormalViewControl btnText, NormalViewControl btnProgressView,
            FrameLayout frameProgress, FrameLayout btnProgressBar, int ProRowWidth)
        private void DoGatewayUpdate(ZbGateway realway)
        {
            //设备升级
            var updateLogic = new HdlGatewayUpdateLogic(this.upDatezbGateway, virtualFirmware, coordinatorFirmware, gatewayFirmware);
            //打开进度条
            ProgressFormBar.Current.Start();
            //网关升级
            var updateLogic = new HdlGatewayUpdateLogic(realway, listVersionInfo[2], listVersionInfo[1], listVersionInfo[0]);
            //更新状态变化的事件
            updateLogic.UpdateStatuChangedEvent += (div, msg) =>
            {
@@ -146,70 +373,47 @@
                    {
                        //异常
                        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)
                    {
                        //升级完成
                        btnText.Text = msg;
                        ProgressFormBar.Current.Close();
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        //调用回调函数
                        this.FinishUpdateEvent?.Invoke();
                        this.CloseForm();
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //初始化中部控件
                            this.InitMiddleFrame();
                        });
                    }
                });
            };
            //进度值改变事件
            updateLogic.ProgressEvent += (value) =>
            {
                Application.RunOnMainThread(() =>
                {
                    //进度条
                    int width = (int)(value * ProRowWidth);
                    btnProgressBar.Width = width;
                    //文本显示
                    int persent = (int)(value * 100);
                    btnProgressView.Text = persent + "%";
                    //文本显示的那个图片框移动
                    frameProgress.X = ControlCommonResourse.XXLeft + btnProgressBar.Right - frameProgress.Width / 2;
                });
                ProgressFormBar.Current.SetValue(value);
            };
            //网关升级开始
            updateLogic.StartUpdateReady();
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //安卓可以点击系统的返回键
            UserView.HomePage.Instance.ScrollEnabled = true;
            Shared.Common.CommonPage.BackKeyCanClick = true;
            //升级对象
            string gwId = HdlGatewayLogic.Current.GetGatewayId(upDatezbGateway);
            if (FirmwareUpdateResourse.dicGatewayUpdateList.ContainsKey(gwId) == true
                && FirmwareUpdateResourse.dicGatewayUpdateList[gwId].IsFinishUpdate == true)
            //关闭事件
            ProgressFormBar.Current.CloseEvent += () =>
            {
                //如果网关已经升级完成,界面关闭时,则移除内存
                FirmwareUpdateResourse.dicGatewayUpdateList[gwId].Dispose();
                FirmwareUpdateResourse.dicGatewayUpdateList.Remove(gwId);
            }
            this.FinishUpdateEvent = null;
            base.CloseFormBefore();
                //升级对象
                string gwId = realway.GwId;
                if (HdlFirmwareUpdateResourse.dicUpdateList.ContainsKey(gwId) == true
                    && HdlFirmwareUpdateResourse.dicUpdateList[gwId].IsFinishUpdate == true)
                {
                    //如果网关已经升级完成,界面关闭时,则移除内存
                    HdlFirmwareUpdateResourse.dicUpdateList[gwId].Dispose();
                    HdlFirmwareUpdateResourse.dicUpdateList.Remove(gwId);
                }
            };
        }
        #endregion