using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Core.Helpers; using Tango.PPC.Common; using Tango.PPC.Common.MachineUpdate; using Tango.PPC.UI.ViewsContracts; namespace Tango.PPC.UI.ViewModels { public class MachineUpdateViewVM : PPCViewModel { public enum MachineUpdateView { UpdateCheckView, UpdateCheckErrorView, UpdateAvailableView, UpToDateView, UpdateProgressView, UpdateCompletedView, UpdateFailedView, } private MachineUpdateResult _update_result; #region Properties /// /// Gets or sets the machine update manager. /// public IMachineUpdateManager MachineUpdateManager { get; set; } private String _latestVersion; /// /// Gets or sets the latest version. /// public String LatestVersion { get { return _latestVersion; } set { _latestVersion = value; RaisePropertyChangedAuto(); } } #endregion #region Commands /// /// Gets or sets the complete command. /// public RelayCommand CompleteCommand { get; set; } /// /// Gets or sets the install command. /// public RelayCommand UpdateCommand { get; set; } /// /// Gets or sets the restart command. /// public RelayCommand RestartCommand { get; set; } /// /// Gets or sets the close command. /// public RelayCommand CloseCommand { get; set; } #endregion #region Constructors public MachineUpdateViewVM(IMachineUpdateManager machineUpdateManager) { MachineUpdateManager = machineUpdateManager; CompleteCommand = new RelayCommand(CompleteUpdate); UpdateCommand = new RelayCommand(Update); RestartCommand = new RelayCommand(CheckForUpdates); CloseCommand = new RelayCommand(() => { NavigationManager.NavigateTo(Common.Navigation.NavigationView.HomeModule); NavigateTo(MachineUpdateView.UpdateCheckView); }); } #endregion #region Update public async void CheckForUpdates() { await NavigateTo(MachineUpdateView.UpdateCheckView); try { var response = await MachineUpdateManager.CheckForUpdate(MachineProvider.Machine.SerialNumber, "http://localhost:51581/"); if (response.IsUpdateAvailable) { LatestVersion = response.Version; await NavigateTo(MachineUpdateView.UpdateAvailableView); } else { await NavigateTo(MachineUpdateView.UpToDateView); } } catch (Exception ex) { LogManager.Log(ex, "Error while trying to check for updates."); await NavigateTo(MachineUpdateView.UpdateFailedView); } } private async void Update() { await NavigateTo(MachineUpdateView.UpdateProgressView); LogManager.Log("Starting machine update..."); try { _update_result = await MachineUpdateManager.Update(MachineProvider.Machine.SerialNumber, "http://localhost:51581/"); LogManager.Log("Machine update completed."); await NavigateTo(MachineUpdateView.UpdateCompletedView); } catch (Exception ex) { LogManager.Log(ex, "Machine update failed."); await NavigateTo(MachineUpdateView.UpdateFailedView); } } #endregion #region Complete private void CompleteUpdate() { String updater_exe = Path.Combine(_update_result.UpdatePackagePath, "Tango.PPC.Updater.exe"); LogManager.Log("Completing machine setup..."); LogManager.Log($"Executing '{updater_exe}' with arguments '{PathHelper.GetStartupPath()}'..."); Process.Start(updater_exe, PathHelper.GetStartupPath()); LogManager.Log("Terminating application process!"); Environment.Exit(0); } #endregion public override void OnApplicationStarted() { } /// /// Navigates to the specified view. /// /// The view. private Task NavigateTo(MachineUpdateView view) { return View.NavigateTo(view); } } }