diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs new file mode 100644 index 000000000..b7c36f263 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core.Commands; +using Tango.PPC.Common; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.FirmwareUpgrade; +using Tango.PPC.Common.Navigation; +using Tango.PPC.UI.ViewsContracts; + +namespace Tango.PPC.UI.ViewModels +{ + public class FirmwareUpgradeViewVM : PPCViewModel<IFirmwareUpgradeView> + { + public enum FirmUpgradeView + { + FirmwareProgressView, + FirmwareCompletedView, + FirmwareFailedView, + } + + + private IFirmwareUpgrader _firmwareUpgrader; + + private String _status; + /// <summary> + /// Gets or sets the firmware upgrade status. + /// </summary> + public String Status + { + get { return _status; } + set { _status = value; RaisePropertyChangedAuto(); } + } + + private long _maxProgress; + /// <summary> + /// Gets or sets the firmware upgrade maximum progress. + /// </summary> + public long MaxProgress + { + get { return _maxProgress; } + set { _maxProgress = value; RaisePropertyChangedAuto(); } + } + + private long _progress; + /// <summary> + /// Gets or sets the firmware upgrade progress. + /// </summary> + public long Progress + { + get { return _progress; } + set { _progress = value; RaisePropertyChangedAuto(); } + } + + private bool _isIntermediate; + /// <summary> + /// Gets or sets a value indicating whether firmware upgrade progress is intermediate. + /// </summary> + public bool IsIntermediate + { + get { return _isIntermediate; } + set { _isIntermediate = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Gets or sets the complete command. + /// </summary> + public RelayCommand CompleteCommand { get; set; } + + public RelayCommand TryAgainCommand { get; set; } + + /// <summary> + /// Initializes a new instance of the <see cref="FirmwareUpgradeViewVM"/> class. + /// </summary> + /// <param name="applicationManager">The application manager.</param> + /// <param name="firmwareUpgrader">The firmware upgrader.</param> + public FirmwareUpgradeViewVM(IPPCApplicationManager applicationManager, IFirmwareUpgrader firmwareUpgrader) + { + _firmwareUpgrader = firmwareUpgrader; + applicationManager.FirmwareUpgradeRequired += ApplicationManager_FirmwareUpgradeRequired; + CompleteCommand = new RelayCommand(Complete); + TryAgainCommand = new RelayCommand(TryAgain); + } + + private async void Upgrade() + { + IsIntermediate = true; + Progress = 0; + Status = "Connecting to the embedded firmware device..."; + await Task.Delay(2000); + await NavigateTo(FirmUpgradeView.FirmwareProgressView); + try + { + var handler = await _firmwareUpgrader.PerformUpgrade(); + IsIntermediate = false; + + handler.Progress += (_, e) => + { + MaxProgress = e.Total; + Progress = e.Current; + Status = e.Message; + + if (e.Status != Integration.Upgrade.FirmwareUpgradeStatus.Uploading) + { + IsIntermediate = true; + } + }; + handler.Canceled += (_, __) => + { + NavigateTo(FirmUpgradeView.FirmwareFailedView); + }; + handler.Failed += (_, ex) => + { + NavigateTo(FirmUpgradeView.FirmwareFailedView); + }; + handler.Completed += (_, __) => + { + NavigateTo(FirmUpgradeView.FirmwareCompletedView); + }; + } + catch (Exception ex) + { + LogManager.Log(ex); + await NavigateTo(FirmUpgradeView.FirmwareFailedView); + } + } + + private void Complete() + { + Restart(); + } + + private async void TryAgain() + { + await NavigateTo(FirmUpgradeView.FirmwareProgressView); + Upgrade(); + } + + private async void ApplicationManager_FirmwareUpgradeRequired(object sender, EventArgs e) + { + LogManager.Log("SetupRequired event received. Navigating to FirmwareUpgradeView..."); + await NavigationManager.NavigateTo(NavigationView.FirmwareUpgradeView); + Upgrade(); + } + + private void Restart() + { + Settings.ApplicationState = ApplicationStates.Ready; + Settings.Save(); + ApplicationManager.Restart(); + } + + /// <summary> + /// Navigates to the specified view. + /// </summary> + /// <param name="view">The view.</param> + private Task NavigateTo(FirmUpgradeView view) + { + return View.NavigateTo(view); + } + + public override void OnApplicationStarted() + { + + } + } +} |
