aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Upgrade/ViewModels/ApplicationUpgradeViewVM.cs
blob: c120b3f747d62b99d20af463192175846def33a3 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.RemoteUpgrade;

namespace Tango.FSE.Upgrade.ViewModels
{
    public class ApplicationUpgradeViewVM : FSEViewModel, INavigationObjectReceiver<ApplicationUpgradeViewVM.NavigationObject>
    {
        public enum ApplicationUpgradeMode
        {
            ConnectedMachine,
            OtherMachine
        }

        public class NavigationObject
        {
            public ApplicationUpgradeMode ApplicationUpgradeMode { get; set; }
            public Machine SelectedMachine { get; set; }
        }

        private List<TangoVersion> _tangoVersions;
        public List<TangoVersion> TangoVersions
        {
            get { return _tangoVersions; }
            set { _tangoVersions = value; RaisePropertyChangedAuto(); }
        }

        private TangoVersion _selectedVersion;
        public TangoVersion SelectedVersion
        {
            get { return _selectedVersion; }
            set { _selectedVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _tupFileLocation;
        public String TupFileLocation
        {
            get { return _tupFileLocation; }
            set { _tupFileLocation = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private Machine _selectedMachine;
        public Machine SelectedMachine
        {
            get { return _selectedMachine; }
            set { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private RemoteUpgradeHandler _handler;
        public RemoteUpgradeHandler Handler
        {
            get { return _handler; }
            set { _handler = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand SelectTupFileLocationCommand { get; set; }

        public RelayCommand GeneratePackageCommand { get; set; }

        public ApplicationUpgradeViewVM()
        {
            SelectTupFileLocationCommand = new RelayCommand(SelectTupFileLocation, () => SelectedVersion != null && SelectedMachine != null);
            GeneratePackageCommand = new RelayCommand(GeneratePackage, () => SelectedVersion != null && SelectedMachine != null && TupFileLocation != null);
            Handler = new RemoteUpgradeHandler(null) { Message = "Ready" };
        }

        private async void SelectTupFileLocation()
        {
            String fileName = $"{SelectedMachine.SerialNumber}_Update_{DateTime.Now.Date.ToFileName()}_v{SelectedVersion.Version}.tup";
            var result = await StorageProvider.SaveFile("Select update package location", "Tango Update Package|*.tup", fileName, ".tup");
            if (result)
            {
                TupFileLocation = result.SelectedItem;
            }
        }

        private async void GeneratePackage()
        {
            try
            {
                IsFree = false;
                Handler = await RemoteUpgradeManager.CreateTupFile(SelectedVersion, SelectedMachine.SerialNumber, TupFileLocation);
                await Handler.WaitForCompletion();
            }
            catch (OperationCanceledException)
            {
                //Aborted...
            }
            catch (Exception ex)
            {
                LogManager.Log("Error generating remote upgrade package.");
                await NotificationProvider.ShowError($"Error occurred while trying to generate the update package.\n{ex.FlattenMessage()}");
            }
            finally
            {
                IsFree = true;
            }
        }

        public async override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            if (TangoVersions == null)
            {
                try
                {
                    TangoVersions = (await Services.TangoVersionsService.GetAllTangoVersions()).Take(10).ToList();
                    SelectedVersion = TangoVersions.FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error retrieving tango versions.");
                    await NotificationProvider.ShowError($"Error retrieving Tango versions.\n{ex.FlattenMessage()}");
                }
            }
        }

        public void OnNavigatedToWithObject(NavigationObject obj)
        {
            SelectedMachine = obj.SelectedMachine;
        }
    }
}