aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Graphics/propeller.xaml
blob: bc688df2632cb0acf0763daad3b6d562301199e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<!--This file is NOT compatible with Silverlight-->
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stretch="Uniform">
  <Canvas Name="Capa_1" Canvas.Left="0" Canvas.Top="0" Width="598.6" Height="598.3">
    <Canvas.RenderTransform>
      <TranslateTransform X="-143.7" Y="36"/>
    </Canvas.RenderTransform>
    <Canvas.Resources/>
    <!--Unknown tag: metadata-->
    <!--Unknown tag: sodipodi:namedview-->
    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path3" Fill="#000000">
      <Path.Data>
        <PathGeometry Figures="M728.4 312.2l-198.1-63.4c-0.5-5.9-1.6-11.6-3.4-17c8.4-2.8 17-5.9 25.7-9.2c78.8-29.7 124.3-63 135-98.8  c7.2-23.8-0.5-47.5-22.6-70.3c-17.5-18-56.2-44.9-57.8-46c-6.2-4.3-14.7-2.8-19.1 3.3L465.5 180.8c-3.2-0.4-6.4-0.6-9.6-0.6  c-2.2 0-4.5 0.1-6.7 0.3c-0.5-62.6-8.3-142.4-41.1-178.6c-12.7-14.1-28.3-21.2-46.1-21.2c0 0 0 0 0 0c-12.3 0-25.6 3.5-39.7 10.4  c-17.4 8.6-43.8 27.5-55.6 36.2c-3.5 2.6-5.7 4.2-6.1 4.5c-3.7 2.8-5.6 7.2-5.4 11.6c0.1 2.6 1 5.3 2.7 7.5l128.9 175.8  c-1.4 3.5-2.6 7.1-3.4 10.8c-42.1-13.7-94.9-27.4-138.4-27.4c-34.2 0-59.1 8.6-73.8 25.7c-13 15-17.7 35.8-14 61.7  c3.6 24.8 19.1 69.3 19.7 71.2c2 5.7 7.3 9.3 13 9.3c1.4 0 2.9-0.2 4.3-0.7L404 308.3c2.3 2.3 4.8 4.4 7.4 6.3  c-7.3 10-14.8 20.7-22.3 32c-46.3 70.4-63.5 124-51.1 159.3c8.2 23.5 28.3 38.2 59.6 43.6c16.2 2.8 41.8 3.4 60.4 3.4  c8.1 0 13.5-0.1 13.5-0.1c7.5-0.2 13.5-6.3 13.5-13.9L484 323.8c4.1-1.7 8.1-3.7 11.8-6.1c6.1 8.3 12.6 16.9 19.5 25.6  c52.2 65.4 97.5 98.6 134.8 98.6c17.6 0 42.4-7.5 61.3-43.3c11.7-22.2 25.4-67.3 25.9-69.2C739.5 322.2 735.5 314.5 728.4 312.2z   M455.9 293c-21.1 0-38.3-17.1-38.3-38.3s17.1-38.3 38.3-38.3c21.1 0 38.3 17.1 38.3 38.3C494.2 275.9 477 293 455.9 293z" FillRule="NonZero"/>
      </Path.Data>
    </Path>
  </Canvas>
</Viewbox>
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
{
    /// <summary>
    /// Represents the machine setup view ViewModel.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel{Tango.PPC.UI.ViewsContracts.IMachineSetupView}" />
    public class MachineSetupViewVM : PPCViewModel<IMachineSetupView>
    {
        /// <summary>
        /// Represents the various machine setup view states.
        /// </summary>
        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

        /// <summary>
        /// Gets or sets the machine setup manager.
        /// </summary>
        public IMachineSetupManager MachineSetupManager { get; set; }

        private String _serialNumber;
        /// <summary>
        /// Gets or sets the serial number.
        /// </summary>
        public String SerialNumber
        {
            get { return _serialNumber; }
            set { _serialNumber = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _hostAddress;
        /// <summary>
        /// Gets or sets the host address.
        /// </summary>
        public String HostAddress
        {
            get { return _hostAddress; }
            set { _hostAddress = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _log;
        /// <summary>
        /// Gets or sets the log.
        /// </summary>
        public String Log
        {
            get { return _log; }
            set { _log = value; RaisePropertyChangedAuto(); }
        }

        private MachineSetupStates _state;
        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        public MachineSetupStates State
        {
            get { return _state; }
            set { _state = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _failedError;
        /// <summary>
        /// Gets or sets the setup failed error.
        /// </summary>
        public String FailedError
        {
            get { return _failedError; }
            set { _failedError = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the available time zones.
        /// </summary>
        public List<TimeZoneInfo> TimeZones { get; set; }

        private TimeZoneInfo _selectedTimeZone;
        /// <summary>
        /// Gets or sets the selected time zone.
        /// </summary>
        public TimeZoneInfo SelectedTimeZone
        {
            get { return _selectedTimeZone; }
            set { _selectedTimeZone = value; RaisePropertyChangedAuto(); }
        }

        private DeploymentSlot _deploymentSlot;
        /// <summary>
        /// Gets or sets the deployment slot.
        /// </summary>
        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

        /// <summary>
        /// Gets or sets the complete command.
        /// </summary>
        public RelayCommand CompleteCommand { get; set; }

        /// <summary>
        /// Gets or sets the navigate to WiFi command.
        /// </summary>
        public RelayCommand NavigateToWiFiCommand { get; set; }

        /// <summary>
        /// Gets or sets the install command.
        /// </summary>
        public RelayCommand InstallCommand { get; set; }

        /// <summary>
        /// Gets or sets the restart command.
        /// </summary>
        public RelayCommand RestartCommand { get; set; }

        /// <summary>
        /// Gets or sets the time zone selected command.
        /// </summary>
        public RelayCommand TimeZoneSelectedCommand { get; set; }

        /// <summary>
        /// Gets or sets the environment selected command.
        /// </summary>
        public RelayCommand EnvironmentSelectedCommand { get; set; }
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineSetupViewVM"/> class.
        /// </summary>
        /// <param name="applicationManager">The application manager.</param>
        /// <param name="machineSetupManager">The machine setup manager.</param>
        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

        /// <summary>
        /// Handles the SetupRequired event of the ApplicationManager.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ApplicationManager_SetupRequired(object sender, EventArgs e)
        {
            if (!LogManager.Categories.Contains(LogCategory.Debug))
            {
                LogManager.Categories.Add(LogCategory.Debug);
            }

            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

        /// <summary>
        /// Appends the log.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        private void AppendLog(String msg)
        {
            if (msg != null && !msg.Contains("SQL Examiner"))
            {
                InvokeUI(() =>
                {
                    View.AppendLog(msg + Environment.NewLine);
                });
            }
        }

        /// <summary>
        /// Completes the setup.
        /// </summary>
        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());
        }

        /// <summary>
        /// Navigates to the specified view.
        /// </summary>
        /// <param name="view">The view.</param>
        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)
            {
                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;
                }
                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
    }
}