using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.FSE.BL; using Tango.FSE.Common; using Tango.FSE.Common.RemoteUpgrade; namespace Tango.FSE.Upgrade.ViewModels { public class FirmwareUpgradeViewVM : RemoteUpgradeViewModel { private List _tangoVersions; public List TangoVersions { get { return _tangoVersions; } set { _tangoVersions = value; RaisePropertyChangedAuto(); } } private TangoVersion _selectedVersion; public TangoVersion SelectedVersion { get { return _selectedVersion; } set { _selectedVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private bool _useExistingTfpFile; public bool UseExistingTfpFile { get { return _useExistingTfpFile; } set { _useExistingTfpFile = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _existingTfpFileLocation; public String ExistingTfpFileLocation { get { return _existingTfpFileLocation; } set { _existingTfpFileLocation = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _existingTfpFileVersion; public String ExistingTfpFileVersion { get { return _existingTfpFileVersion; } set { _existingTfpFileVersion = value; RaisePropertyChangedAuto(); } } private RemoteUpgradeHandler _handler; public RemoteUpgradeHandler Handler { get { return _handler; } set { _handler = value; RaisePropertyChangedAuto(); } } public RelayCommand SelectExistingTfpFileLocationCommand { get; set; } public RelayCommand GeneratePackageCommand { get; set; } public RelayCommand ContinueCommand { get; set; } public FirmwareUpgradeViewVM() { SelectExistingTfpFileLocationCommand = new RelayCommand(SelectExistingTfpFileLocation); GeneratePackageCommand = new RelayCommand(GeneratePackage, () => SelectedVersion != null); ContinueCommand = new RelayCommand(ContinueWithExistingTfpFile, () => ExistingTfpFileLocation != null); } public override void OnBeforeNavigatedTo() { base.OnBeforeNavigatedTo(); if (UseExistingTfpFile && !MachineProvider.IsConnected) { UseExistingTfpFile = false; } Handler = new RemoteUpgradeHandler("Ready"); } private async void SelectExistingTfpFileLocation() { var result = await StorageProvider.OpenFile("Select existing firmware package file", "Tango Firmware Package|*.tfp"); if (result) { try { LogManager.Log("Validating firmware package file..."); using (FileStream s = File.OpenRead(result.SelectedItem)) { var package = MachineProvider.MachineOperator.GetFirmwarePackageInfo(s).Result; package.Validate(); ExistingTfpFileVersion = package.GetMcuVersion().ToString(); } } catch (Exception ex) { await NotificationProvider.ShowError($"Error loading the selected tfp file.\n{ex.FlattenMessage()}"); return; } ExistingTfpFileLocation = result.SelectedItem; } } public async override void OnNavigatedTo() { base.OnNavigatedTo(); if (TangoVersions == null) { try { TangoVersions = (await Services.TangoVersionsService.GetAllTangoVersions()).DistinctBy(x => x.FirmwareVersion).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()}"); } } } private async void GeneratePackage() { try { IsFree = false; var tempTfpFile = TemporaryManager.CreateImaginaryFile(".tfp"); Handler = await RemoteUpgradeManager.CreateTfpFile(SelectedVersion, tempTfpFile); await Handler.WaitForCompletion(); await ModularNavigationManager.NavigateTo(Navigation.RemoteUpgradeView.FirmwareUpgradeGeneratedView, new FirmwareUpgradeGeneratedViewVM.NavigationObject() { IsExistingTfpFile = false, TfpFileLocation = tempTfpFile, SelectedVersion = SelectedVersion, }, false); } catch (OperationCanceledException) { //Aborted... } catch (AuthorizationException ex) { await NotificationProvider.ShowError(ex.Message); } catch (Exception ex) { LogManager.Log(ex, "Error generating remote firmware upgrade package."); } finally { IsFree = true; } } private async void ContinueWithExistingTfpFile() { await ModularNavigationManager.NavigateTo(Navigation.RemoteUpgradeView.FirmwareUpgradeGeneratedView, new FirmwareUpgradeGeneratedViewVM.NavigationObject() { IsExistingTfpFile = true, TfpFileLocation = ExistingTfpFileLocation, }, false); } } }