From e2822236eebed39637656785a3e7937e85c90cd3 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 12 Nov 2017 16:57:52 +0200 Subject: Added some features for machine emulator. Implemented information logs on TranspoterBase! --- .../Tango.MachineEM.UI/ViewModels/MainViewVM.cs | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 Software/Visual Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs (limited to 'Software/Visual Studio/Utilities/Tango.MachineEM.UI/ViewModels/MainViewVM.cs') 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; + /// + /// Gets or sets the machine emulator. + /// + public MachineEmulator Emulator + { + get { return _emulator; } + set { _emulator = value; RaisePropertyChanged(nameof(Emulator)); } + } + + private String _log; + /// + /// Gets or sets the log. + /// + public String Log + { + get { return _log; } + set { _log = value; RaisePropertyChanged(nameof(Log)); } + } + + #endregion + + #region Commands + + /// + /// Gets or sets the start command. + /// + public RelayCommand StartCommand { get; set; } + + /// + /// Gets or sets the stop command. + /// + public RelayCommand StopCommand { get; set; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + 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 + + /// + /// Handles the ClientConnected event of the TcpServer control. + /// + /// The source of the event. + /// The instance containing the event data. + private void TcpServer_ClientConnected(object sender, ClientConnectedEventArgs e) + { + Emulator.Transporter.Adapters.Add(new TcpTransportAdapter(e.Socket)); + } + + #endregion + + #region Command Handlers + + /// + /// Stops the TCP server and emulator. + /// + private async void Start() + { + TcpServer = new TcpServer(9999); + TcpServer.ClientConnected += TcpServer_ClientConnected; + TcpServer.Start(); + await Emulator.Start(); + InvalidateRelayCommands(); + } + + /// + /// Starts the TCP server/USB and emulator. + /// + 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 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 + } +} -- cgit v1.3.1