aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/Properties/Settings.Designer.cs
blob: d2abcf5e816c8112ba87bd030533377a77b04f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Tango.MachineStudio.HardwareDesigner.Properties
{
    
    
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
    {
        
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
        
        public static Settings Default
        {
            get
            {
                return defaultInstance;
            }
        }
    }
}
ight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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;
            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) =>
                {
                    Status = ex.FlattenMessage();
                    NavigateTo(FirmUpgradeView.FirmwareFailedView);
                };
                handler.Completed += (_, __) =>
                {
                    NavigateTo(FirmUpgradeView.FirmwareCompletedView);
                };
            }
            catch (Exception ex)
            {
                Status = ex.FlattenMessage();
                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()
        {
            
        }
    }
}