aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/FirmwareUpgradeViewVM.cs
blob: 1cde1fe1a59a685b39295a8bd3eebc57c6812e12 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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()
        {
            
        }
    }
}