aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MachineSerialViewVM.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MachineSerialViewVM.cs')
0 files changed, 0 insertions, 0 deletions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Emulations.ExternalBridge;
using Tango.Integration.ExternalBridge;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Notifications;
using Tango.Settings;
using Tango.SharedUI;

namespace Tango.MachineStudio.UI.ViewModels
{
    /// <summary>
    /// Represents the Machine Studio connection dialog, view model.
    /// </summary>
    /// <seealso cref="Tango.MachineStudio.Common.Notifications.DialogViewVM" />
    public class MachineConnectionViewVM : DialogViewVM
    {
        private static ExternalBridgeScanner _scanner;
        public ExternalBridgeScanner Scanner
        {
            get { return _scanner; }
            set { _scanner = value; RaisePropertyChangedAuto(); }
        }


        private EmulatorExternalBridge _emulator;

        private IExternalBridgeClient _selectedMachine;
        /// <summary>
        /// Gets or sets the selected machine.
        /// </summary>
        public IExternalBridgeClient SelectedMachine
        {
            get { return _selectedMachine; }
            set { _selectedMachine = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(RequiresAuthentication)); InvalidateRelayCommands(); }
        }

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

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConnectionViewVM"/> class.
        /// </summary>
        /// <param name="scanner">The scanner.</param>
        public MachineConnectionViewVM()
        {
            if (_scanner == null)
            {
                var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>();
                _scanner = new ExternalBridgeScanner();
                _scanner.SignalRConfiguration.Enabled = settings.EnableExternalBridgeSignalR;
                if (App.StartupArgs.Contains("-webDebug"))
                {
                    _scanner.SignalRConfiguration.Address = "http://localhost:1111/"; //settings.DeploymentSlot.ToAddress();
                }
                else
                {
                    _scanner.SignalRConfiguration.Address = settings.DeploymentSlot.ToAddress();
                }
                _scanner.SignalRConfiguration.Hub = settings.ExternalBridgeSignalRHub;
            }

            EnableDiagnostics = true;
            UploadHardwareConfiguration = true;
            EnableKeepAlive = true;
            ConnectCommand = new RelayCommand(Connect, (x) => SelectedMachine != null);
        }

        /// <summary>
        /// Connect to the currently selected machine.
        /// </summary>
        private void Connect()
        {
            if (SelectedMachine != null)
            {
                _scanner.Stop();
                Accept();
            }
        }

        /// <summary>
        /// Invokes the <see cref="E:Tango.SharedUI.DialogViewVM.Canceled" /> event.
        /// </summary>
        protected override void Cancel()
        {
            _scanner.Stop();
            base.Cancel();
        }

        /// <summary>
        /// Called when the dialog has been shown.
        /// </summary>
        public override void OnShow()
        {
            base.OnShow();

            try
            {
                _scanner.AvailableMachines.Clear();

                if (SettingsManager.Default.GetOrCreate<MachineStudioSettings>().UseExternalBridgeEmulator)
                {
                    if (_emulator != null)
                    {
                        _emulator.Disconnect();
                    }
                    _emulator = new EmulatorExternalBridge();
                }

                _scanner.AvailableMachines.Add(_emulator);
                _scanner.Start();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error starting external bridge scanner.");
                Cancel();
                TangoIOC.Default.GetInstance<INotificationProvider>().ShowError($"There is a problem with machine scanning.\n{ex.FlattenMessage()}");
            }
        }

        private bool _enableDiagnostics;
        /// <summary>
        /// Gets or sets a value indicating whether to enable diagnostics after connection.
        /// </summary>
        public bool EnableDiagnostics
        {
            get { return _enableDiagnostics; }
            set { _enableDiagnostics = value; RaisePropertyChangedAuto(); }
        }

        private bool _uploadHardwareConfiguration;
        /// <summary>
        /// Gets or sets a value indicating whether to upload hardware configuration after connection.
        /// </summary>
        public bool UploadHardwareConfiguration
        {
            get { return _uploadHardwareConfiguration; }
            set { _uploadHardwareConfiguration = value; RaisePropertyChangedAuto(); }
        }

        private bool _enableApplicationLogs;
        /// <summary>
        /// Gets or sets a value indicating whether to enable application logs.
        /// </summary>
        public bool EnableApplicationLogs
        {
            get { return _enableApplicationLogs; }
            set { _enableApplicationLogs = value; RaisePropertyChangedAuto(); }
        }


        private bool _enableKeepAlive;
        /// <summary>
        /// Gets or sets a value indicating whether to use the keep alive mechanism.
        /// </summary>
        public bool EnableKeepAlive
        {
            get { return _enableKeepAlive; }
            set { _enableKeepAlive = value; RaisePropertyChangedAuto(); }
        }

        public bool RequiresAuthentication
        {
            get
            {
                return SelectedMachine != null && SelectedMachine is IExternalBridgeSecureClient;
            }
        }

    }
}