diff options
Diffstat (limited to 'Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs new file mode 100644 index 000000000..7b70b277e --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs @@ -0,0 +1,242 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Emulations.Emulators; +using Tango.Logging; +using Tango.SharedUI; +using Tango.SharedUI.Commands; +using Tango.Stubs; +using Tango.Transport.Adapters; +using Tango.Transport.Servers; +using Tango.Transport.Transporters; + +namespace Tango.MachineEM.UI.ViewModels +{ + public class MainViewVM : ExtendedObject + { + private TcpServer TcpServer; + private bool _running; + + #region Properties + + private MachineEmulator _emulator; + /// <summary> + /// Gets or sets the machine emulator. + /// </summary> + public MachineEmulator Emulator + { + get { return _emulator; } + set { _emulator = value; RaisePropertyChanged(nameof(Emulator)); } + } + + private String _log; + /// <summary> + /// Gets or sets the log. + /// </summary> + public String Log + { + get { return _log; } + set { _log = value; RaisePropertyChanged(nameof(Log)); } + } + + /// <summary> + /// Gets or sets the available stubs. + /// </summary> + public List<AvailableStub> AvailableStubs { get; set; } + + private AvailableStub _selectedAvailableStub; + /// <summary> + /// Gets or sets the selected available stub. + /// </summary> + public AvailableStub SelectedAvailableStub + { + get { return _selectedAvailableStub; } + set + { + _selectedAvailableStub = value; RaisePropertyChanged(nameof(SelectedAvailableStub)); + OnStubSelected(value); + } + } + + private StubBase _selectedStub; + /// <summary> + /// Gets or sets the selected stub. + /// </summary> + public StubBase SelectedStub + { + get { return _selectedStub; } + set { _selectedStub = value; RaisePropertyChanged(nameof(SelectedStub)); } + } + + + #endregion + + #region Private Methods + + /// <summary> + /// Called when available stub is selected. + /// </summary> + /// <param name="availableStub">The available stub.</param> + private void OnStubSelected(AvailableStub availableStub) + { + if (availableStub != null) + { + SelectedStub = availableStub.CreateInstance(Emulator.Transporter); + } + } + + #endregion + + #region Commands + + /// <summary> + /// Gets or sets the start command. + /// </summary> + public RelayCommand StartCommand { get; set; } + + /// <summary> + /// Gets or sets the stop command. + /// </summary> + public RelayCommand StopCommand { get; set; } + + /// <summary> + /// Gets or sets the run command. + /// </summary> + public RelayCommand RunCommand { get; set; } + + /// <summary> + /// Gets or sets the cancel command. + /// </summary> + public RelayCommand CancelCommand { get; set; } + + /// <summary> + /// Gets or sets the clear command. + /// </summary> + public RelayCommand ClearCommand { get; set; } + + #endregion + + #region Constructors + + /// <summary> + /// Initializes a new instance of the <see cref="MainViewVM"/> class. + /// </summary> + public MainViewVM() + { + MainViewLogger logger = new MainViewLogger(); + logger.NewLog += (output) => + { + Log += output + Environment.NewLine; + }; + + LogManager.RegisterLogger(logger); + + Emulator = new MachineEmulator(new ProtoTransporter()); + + StartCommand = new RelayCommand(Start, (x) => !Emulator.IsStarted); + StopCommand = new RelayCommand(Stop,(x) => Emulator.IsStarted); + RunCommand = new RelayCommand(RunSelectedStub, (x) => !_running); + CancelCommand = new RelayCommand(Cancel, (x) => _running); + ClearCommand = new RelayCommand(() => Log = String.Empty); + + AvailableStubs = StubBase.GetAvailableStubs(StubDirection.ToMobile); + } + + #endregion + + #region Event Handlers + + /// <summary> + /// Handles the ClientConnected event of the TcpServer control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="ClientConnectedEventArgs"/> instance containing the event data.</param> + private void TcpServer_ClientConnected(object sender, ClientConnectedEventArgs e) + { + Emulator.Transporter.Adapters.Add(new TcpTransportAdapter(e.Socket)); + } + + #endregion + + #region Command Handlers + + /// <summary> + /// Stops the TCP server and emulator. + /// </summary> + private async void Start() + { + TcpServer = new TcpServer(9999); + TcpServer.ClientConnected += TcpServer_ClientConnected; + TcpServer.Start(); + await Emulator.Start(); + InvalidateRelayCommands(); + } + + /// <summary> + /// Starts the TCP server/USB and emulator. + /// </summary> + private async void Stop() + { + TcpServer.Stop(); + await Emulator.Stop(); + InvalidateRelayCommands(); + } + + /// <summary> + /// Runs the selected stub. + /// </summary> + private async void RunSelectedStub() + { + _running = true; + InvalidateRelayCommands(); + var result = await SelectedStub.Run((response) => + { + _running = false; + Log += response + Environment.NewLine; + InvalidateRelayCommands(); + + }); + + Log += result + Environment.NewLine; + + _running = false; + InvalidateRelayCommands(); + } + + private async void Cancel() + { + + } + + #endregion + + #region Custom Logger + + public class MainViewLogger : ILogger + { + public bool Enabled { get; set; } + public bool Immediate { get; set; } + public event Action<String> NewLog; + + public MainViewLogger() + { + Enabled = true; + Immediate = true; + } + + public void OnError(LogItemBase output) + { + NewLog?.Invoke(output.ToString()); + } + + public void OnTrace(LogItemBase output) + { + NewLog?.Invoke(output.ToString()); + } + } + + #endregion + } +} |
