using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.Core.Commands; using Tango.Core.Helpers; using Tango.Integration.Operation; using Tango.Logging; using Tango.PMR.Connection; using Tango.PPC.Common; using Tango.PPC.Common.Application; using Tango.PPC.Common.Connection; using Tango.PPC.Common.MachineSetup; using Tango.PPC.Common.Navigation; using Tango.PPC.Common.OS; using Tango.PPC.Common.Web; using Tango.PPC.UI.ViewsContracts; using Tango.Settings; using Tango.SharedUI.Helpers; using Tango.SQLExaminer; using Tango.Transport.Adapters; using Tango.Web; namespace Tango.PPC.UI.ViewModels { /// /// Represents the machine setup view ViewModel. /// /// public class MachineSetupViewVM : PPCViewModel { /// /// Represents the various machine setup view states. /// public enum MachineSetupStates { None, Working, Completed, Failed, } public enum MachineSetupView { WelcomeView, WiFiSelectionView, WiFiTestView, TimeZoneView, EnvironmentView, SetupWelcomeView, SetupProgressView, SetupCompletedView, SetupFailedView, } private MachineSetupResult _setup_result; private IOperationSystemManager _operationSystemManager; private IPPCApplicationManager _appManager; private PPCWebClient _ppcWebClient; #region Properties /// /// Gets or sets the machine setup manager. /// public IMachineSetupManager MachineSetupManager { get; set; } private String _serialNumber; /// /// Gets or sets the serial number. /// public String SerialNumber { get { return _serialNumber; } set { _serialNumber = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _hostAddress; /// /// Gets or sets the host address. /// public String HostAddress { get { return _hostAddress; } set { _hostAddress = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _log; /// /// Gets or sets the log. /// public String Log { get { return _log; } set { _log = value; RaisePropertyChangedAuto(); } } private MachineSetupStates _state; /// /// Gets or sets the state. /// public MachineSetupStates State { get { return _state; } set { _state = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _failedError; /// /// Gets or sets the setup failed error. /// public String FailedError { get { return _failedError; } set { _failedError = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the available time zones. /// public List TimeZones { get; set; } private TimeZoneInfo _selectedTimeZone; /// /// Gets or sets the selected time zone. /// public TimeZoneInfo SelectedTimeZone { get { return _selectedTimeZone; } set { _selectedTimeZone = value; RaisePropertyChangedAuto(); } } private DeploymentSlot _deploymentSlot; /// /// Gets or sets the deployment slot. /// public DeploymentSlot DeploymentSlot { get { return _deploymentSlot; } set { _deploymentSlot = value; RaisePropertyChangedAuto(); } } private MachineSetupView _currentView; public MachineSetupView CurrentView { get { return _currentView; } set { _currentView = value; RaisePropertyChangedAuto(); } } #endregion #region Commands /// /// Gets or sets the complete command. /// public RelayCommand CompleteCommand { get; set; } /// /// Gets or sets the navigate to WiFi command. /// public RelayCommand NavigateToWiFiCommand { get; set; } /// /// Gets or sets the install command. /// public RelayCommand InstallCommand { get; set; } /// /// Gets or sets the restart command. /// public RelayCommand RestartCommand { get; set; } /// /// Gets or sets the time zone selected command. /// public RelayCommand TimeZoneSelectedCommand { get; set; } /// /// Gets or sets the environment selected command. /// public RelayCommand EnvironmentSelectedCommand { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The application manager. /// The machine setup manager. public MachineSetupViewVM(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager, IOperationSystemManager operationSystemManager) { _ppcWebClient = ppcWebClient; _appManager = applicationManager; MachineSetupManager = machineSetupManager; DeploymentSlot = Settings.DeploymentSlot; SerialNumber = ""; CompleteCommand = new RelayCommand(CompleteSetup, () => State == MachineSetupStates.Completed); applicationManager.SetupRequired += ApplicationManager_SetupRequired; NavigateToWiFiCommand = new RelayCommand(EnsureWiFi); InstallCommand = new RelayCommand(Install); RestartCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.WelcomeView); }); TimeZoneSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.EnvironmentView); }); EnvironmentSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.SetupWelcomeView); Settings.DeploymentSlot = DeploymentSlot; HostAddress = Settings.GetMachineServiceAddress(); Settings.Save(); }); _operationSystemManager = operationSystemManager; TimeZones = _operationSystemManager.GetAvailableTimeZones().ToList(); SelectedTimeZone = TimeZones.SingleOrDefault(x => x.ToString() == "(UTC+02:00) Jerusalem"); } #endregion #region Event Handlers /// /// Handles the SetupRequired event of the ApplicationManager. /// /// The source of the event. /// The instance containing the event data. private void ApplicationManager_SetupRequired(object sender, EventArgs e) { var logger = new SimpleStringLogger(); logger.Categories.Remove(LogCategory.Debug); LogManager.RegisterLogger(logger); logger.LogReceived += Logger_LogReceived; LogManager.Log("SetupRequired event received. Navigating to MachineSetupView..."); NavigationManager.NavigateTo(NavigationView.MachineSetupView); } private void Logger_LogReceived(object sender, LogItemBase e) { AppendLog(e.Message); } #endregion #region Private Methods /// /// Appends the log. /// /// The MSG. private void AppendLog(String msg) { if (msg != null && !msg.Contains("SQL Examiner")) { InvokeUI(() => { View.AppendLog(msg + Environment.NewLine); }); } } /// /// Completes the setup. /// private void CompleteSetup() { LogManager.Log("Completing machine setup..."); String updater_exe = Path.Combine(_setup_result.UpdatePackagePath, "Tango.PPC.Updater.exe"); _appManager.UpdateApplication(updater_exe, PathHelper.GetStartupPath()); } /// /// Navigates to the specified view. /// /// The view. private Task NavigateTo(MachineSetupView view) { CurrentView = view; return View.NavigateTo(view); } #endregion #region WiFi private async void EnsureWiFi() { await NavigateTo(MachineSetupView.WiFiTestView); await Task.Delay(2000); bool connected = await ConnectivityProvider.CheckInternetConnection(); if (connected) { if (BuildProvider.IsEureka) { await NavigateTo(MachineSetupView.EnvironmentView); } else { await NavigateTo(MachineSetupView.TimeZoneView); } } else { await NavigateTo(MachineSetupView.WiFiSelectionView); await ConnectivityProvider.RefreshAvailableWiFiNetworks(); ConnectivityProvider.ConnectionStateChanged += ConnectivityProvider_ConnectionStateChanged; } } private async void ConnectivityProvider_ConnectionStateChanged(object sender, Common.Connectivity.ConnectionStateEventArgs e) { if (e.IsConnected) { ConnectivityProvider.ConnectionStateChanged -= ConnectivityProvider_ConnectionStateChanged; await NavigateTo(MachineSetupView.WiFiTestView); await Task.Delay(5000); EnsureWiFi(); } } #endregion #region Setup private async void Install() { await NavigateTo(MachineSetupView.SetupProgressView); LogManager.Log("Starting machine setup..."); State = MachineSetupStates.Working; try { if (!App.StartupArgs.Contains("-webDebug")) { _ppcWebClient.Environment = DeploymentSlot; } if (!BuildProvider.IsEureka) { await _operationSystemManager.ChangeTimeZone(SelectedTimeZone); } _setup_result = await MachineSetupManager.Setup(SerialNumber); State = MachineSetupStates.Completed; LogManager.Log("Machine setup completed."); await NavigateTo(MachineSetupView.SetupCompletedView); } catch (Exception ex) { LogManager.Log(ex, "Machine setup failed."); FailedError = ex.FlattenMessage(); State = MachineSetupStates.Failed; await NavigateTo(MachineSetupView.SetupFailedView); } } #endregion } }