using System; using System.Collections.Generic; using Shared; namespace Shared.SimpleControl.Phone { public class ManualUpgradeDialog : FrameLayout { static ManualUpgradeDialog bodyView; int result = -99; byte subnetId = 0; public ManualUpgradeDialog () { bodyView = this; } public void ShowDialog () { #region 弹窗 Dialog dialog = new Dialog (); FrameLayout dialogBodyView = new FrameLayout () { Gravity = Gravity.Center, Width = Application.GetRealWidth (500), Height = Application.GetRealHeight (500), BackgroundColor = SkinStyle.Current.DialogColor, Radius = 5, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 0, }; dialog.AddChidren (dialogBodyView); Button btnTitle = new Button () { Height = Application.GetRealHeight (80), BackgroundColor = SkinStyle.Current.DialogTitle, TextAlignment = TextAlignment.Center, TextID = R.MyInternationalizationString.Tip, TextColor = SkinStyle.Current.DialogTextColor }; dialogBodyView.AddChidren (btnTitle); var contentView = new FrameLayout () { Y = Application.GetRealHeight (80), Height = Application.GetRealHeight (340), BackgroundColor = SkinStyle.Current.DialogColor, }; dialogBodyView.AddChidren (contentView); var btnTipMsg = new Button () { Gravity = Gravity.CenterHorizontal, Y = Application.GetRealHeight (20), Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (80), Text = "Waiting for gateway upgrade", TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.TextColor, }; contentView.AddChidren (btnTipMsg); //var btnTipMsg = new Button () { // Gravity = Gravity.CenterHorizontal, // Y = btnTipTitle.Bottom, // Width = Application.GetRealWidth (400), // Height = Application.GetRealHeight (80), // TextAlignment = TextAlignment.CenterLeft, // TextColor = SkinStyle.Current.DelColor, //}; //contentView.AddChidren (btnTipMsg); FrameLayout bottomView = new FrameLayout () { Y = Application.GetRealHeight (420), Height = Application.GetRealHeight (85), BackgroundColor = SkinStyle.Current.DialogTitle }; dialogBodyView.AddChidren (bottomView); Button btnClose = new Button () { TextID = R.MyInternationalizationString.Close, TextAlignment = TextAlignment.Center }; bottomView.AddChidren (btnClose); btnClose.MouseUpEventHandler += (send2er, e2) => { dialog.Close (); }; Button btnBottomLine = new Button () { X = btnClose.Right, Width = 1, BackgroundColor = SkinStyle.Current.Black50Transparent, }; bottomView.AddChidren (btnBottomLine); dialog.Show (); new System.Threading.Thread (() => { List> upgradeData = ReadUpgradeData (); byte [] arrayTemp = new byte [4]; arrayTemp [0] = Convert.ToByte ((upgradeData.Count & 0xFF000000) >> 24); arrayTemp [1] = Convert.ToByte ((upgradeData.Count & 0xFF0000) >> 16); arrayTemp [2] = Convert.ToByte ((upgradeData.Count & 0xFF00) >> 8); arrayTemp [3] = Convert.ToByte (upgradeData.Count & 0xFF); while (true) { var ub = MainPage.GatewayStatus.Split ("_"); if (ub.Length > 1) { result = Convert.ToInt32 (ub [1]); { Application.RunOnMainThread (() => { btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count; }); } } if (ub.Length > 3) { byte.TryParse (ub [2], out subnetId); } if (MainPage.GatewayStatus.Contains ("upgrading") && result == 0) { SendUpgradeData (subnetId, 0, arrayTemp); Application.RunOnMainThread (() => { btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count; }); } else if (result == -99) { System.Threading.Thread.Sleep (100); continue; } else if (result == 100) { Application.RunOnMainThread (() => { btnTipMsg.Text = "Gateway upgrade succeeded. Initializing gateway."; btnTipMsg.TextColor = SkinStyle.Current.TextColor; MainPage.GatewayStatus = ""; }); break; } else { if (upgradeData.Count >= result) { var listPack = upgradeData [result - 1];// byte [] packData = new byte [2 + listPack.Count]; packData [0] = Convert.ToByte (result / 256); packData [1] = Convert.ToByte (result % 256); Array.Copy (listPack.ToArray (), 0, packData, 2, listPack.Count); Console.WriteLine ("packId" + result); SendUpgradeData (subnetId, 0, packData); } } } }) { IsBackground = true }.Start (); #endregion } /// /// 读取固件数据 /// /// private static List> ReadUpgradeData () { byte [] buffer = new byte [1024]; List> upgradeData = new List> (); System.IO.Stream stream = Application.Activity.Assets.Open ("ind_C03.02U_2022-06-22.bin"); int length = 0; try { while ((length = stream.Read (buffer, 0, buffer.Length)) != 0) { List bbb = new List (); for (int i = 0; i < length; i++) { bbb.Add (buffer [i]); } upgradeData.Add (bbb); } } catch { } finally { stream.Close (); } return upgradeData; } /// /// 发送网关固件数据 /// /// /// /// /// public int SendUpgradeData (byte subnetId, byte deviceId, byte [] sendByets) { var resutl = Control.ControlBytesSendHasReturn (Command.enjoyUpgrade2, subnetId, deviceId, sendByets); if (resutl == null) { return -2; } if (resutl.Length == 0) { return -1; } if (resutl [0] == 0xF8) { return 999999; } else { if (resutl.Length > 1) { var packId = resutl [0] * 256 + resutl [1]; Console.WriteLine ("packId:" + packId); return packId; } return 1; } } } }