using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
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
{
///
/// Represents the Machine Studio connection dialog, view model.
///
///
public class MachineConnectionViewVM : DialogViewVM
{
private static ExternalBridgeScanner _scanner;
public ExternalBridgeScanner Scanner
{
get { return _scanner; }
set { _scanner = value; RaisePropertyChangedAuto(); }
}
private EmulatorExternalBridge _emulator;
private IExternalBridgeClient _selectedMachine;
///
/// Gets or sets the selected machine.
///
public IExternalBridgeClient SelectedMachine
{
get { return _selectedMachine; }
set { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
///
/// Gets or sets the connect command.
///
public RelayCommand ConnectCommand { get; set; }
///
/// Initializes a new instance of the class.
///
/// The scanner.
public MachineConnectionViewVM()
{
if (_scanner == null)
{
_scanner = new ExternalBridgeScanner();
}
EnableDiagnostics = true;
UploadHardwareConfiguration = true;
ConnectCommand = new RelayCommand(Connect, (x) => SelectedMachine != null);
}
///
/// Connect to the currently selected machine.
///
private void Connect()
{
if (SelectedMachine != null)
{
_scanner.Stop();
Accept();
}
}
///
/// Invokes the event.
///
protected override void Cancel()
{
_scanner.Stop();
base.Cancel();
}
///
/// Called when the dialog has been shown.
///
public override void OnShow()
{
base.OnShow();
try
{
_scanner.AvailableMachines.Clear();
if (SettingsManager.Default.GetOrCreate().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.");
}
}
private bool _enableDiagnostics;
///
/// Gets or sets a value indicating whether to enable diagnostics after connection.
///
public bool EnableDiagnostics
{
get { return _enableDiagnostics; }
set { _enableDiagnostics = value; RaisePropertyChangedAuto(); }
}
private bool _uploadHardwareConfiguration;
///
/// Gets or sets a value indicating whether to upload hardware configuration after connection.
///
public bool UploadHardwareConfiguration
{
get { return _uploadHardwareConfiguration; }
set { _uploadHardwareConfiguration = value; RaisePropertyChangedAuto(); }
}
private bool _enableKeepAlive;
///
/// Gets or sets a value indicating whether to use the keep alive mechanism.
///
public bool EnableKeepAlive
{
get { return _enableKeepAlive; }
set { _enableKeepAlive = value; RaisePropertyChangedAuto(); }
}
}
}