aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentFirmwareUpgradeViewVM.cs
blob: 54576cda7cdba4c79846cedafec7ee03f84a8042 (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
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.AzureUtils.Environment;
using Tango.AzureUtils.Firmware;
using Tango.Core.Commands;

namespace Tango.AzureUtils.UI.ViewModels
{
    public class EnvironmentFirmwareUpgradeViewVM : AzureDashboardViewModel
    {
        private FirmwareManager _firmwareManager;

        private List<IWebAppBase> _deploymentSlots;
        public List<IWebAppBase> DeploymentSlots
        {
            get { return _deploymentSlots; }
            set { _deploymentSlots = value; RaisePropertyChangedAuto(); }
        }

        private IWebAppBase _selectedDeploymentSlot;
        public IWebAppBase SelectedDeploymentSlot
        {
            get { return _selectedDeploymentSlot; }
            set { _selectedDeploymentSlot = value; RaisePropertyChangedAuto(); }
        }

        private String _filePath;
        public String FilePath
        {
            get { return _filePath; }
            set { _filePath = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _firmwareVersion;
        public String FirmwareVersion
        {
            get { return _firmwareVersion; }
            set { _firmwareVersion = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand UpgradeFirmwareCommand { get; set; }
        public RelayCommand BrowseFileCommand { get; set; }

        public EnvironmentFirmwareUpgradeViewVM()
        {
            UpgradeFirmwareCommand = new RelayCommand(UpgradeFirmware,() => FilePath != null);
            BrowseFileCommand = new RelayCommand(BrowseFile);
        }

        public override void OnAuthenticated(IAzure azure, List<IWebAppBase> apps)
        {
            DeploymentSlots = apps.Where(x => x.Name.Contains("MachineService")).ToList();
            SelectedDeploymentSlot = DeploymentSlots.FirstOrDefault(x => x.Name.EndsWith("DEV"));

            _firmwareManager = new FirmwareManager(azure);
            _firmwareManager.ConfirmationRequired += ConfirmationHandler;
            _firmwareManager.Progress += ProgressHandler;
        }

        private async void BrowseFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select Tango Firmware Package";
            dlg.Filter = "Tango Firmware Package Files|*.tfp";
            if (dlg.ShowDialog().Value)
            {
                FilePath = dlg.FileName;
                try
                {
                    FirmwareVersion = await _firmwareManager.GetFirmwareVersion(FilePath);
                }
                catch (Exception ex)
                {
                    FilePath = null;
                    StatusManager.UpdateStatus(ex);
                }
            }
        }

        private async void UpgradeFirmware()
        {
            try
            {
                if (!Validate()) return;

                IsFree = false;

                await _firmwareManager.InjectFirmwarePackage(SelectedDeploymentSlot, FilePath);

                var old = SelectedDeploymentSlot;
                SelectedDeploymentSlot = null;
                SelectedDeploymentSlot = old;
            }
            catch (Exception ex)
            {
                StatusManager.UpdateStatus(ex);
            }
            finally
            {
                IsFree = true;
            }
        }
    }
}