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.BL.Enumerations; using Tango.Core.Commands; namespace Tango.AzureUtils.UI.ViewModels { public class EnvironmentFirmwareUpgradeViewVM : AzureDashboardViewModel { private FirmwareManager _firmwareManager; private List _deploymentSlots; public List DeploymentSlots { get { return _deploymentSlots; } set { _deploymentSlots = value; RaisePropertyChangedAuto(); } } private IWebAppBase _selectedDeploymentSlot; public IWebAppBase SelectedDeploymentSlot { get { return _selectedDeploymentSlot; } set { _selectedDeploymentSlot = value; RaisePropertyChangedAuto(); } } private List _machineTypes; public List MachineTypes { get { return _machineTypes; } set { _machineTypes = value; RaisePropertyChangedAuto(); } } private MachineTypes _selectedMachineType; public MachineTypes SelectedMachineType { get { return _selectedMachineType; } set { _selectedMachineType = 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); MachineTypes = Enum.GetValues(typeof(MachineTypes)).Cast().ToList(); } public override void OnAuthenticated(IAzure azure, List 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 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, SelectedMachineType, FilePath, null); } catch (Exception ex) { StatusManager.UpdateStatus(ex); } finally { RequireRefresh(); IsFree = true; } } protected override void OnRefreshRequired() { base.OnRefreshRequired(); var old = SelectedDeploymentSlot; SelectedDeploymentSlot = null; SelectedDeploymentSlot = old; } } }