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 | 148 |
1 files changed, 148 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..eb8621152 --- /dev/null +++ b/Software/Visual Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs @@ -0,0 +1,148 @@ +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.Transport.Adapters; +using Tango.Transport.Servers; +using Tango.Transport.Transporters; + +namespace Tango.MachineEM.UI.ViewModels +{ + public class MainViewVM : ExtendedObject + { + private TcpServer TcpServer; + + #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)); } + } + + #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; } + + #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); + } + + #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(); + } + + #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 + } +} |
