using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Tango.Core.Commands;
using Tango.Core;
using Tango.Transport.Adapters;
using Tango.Transport.Routing;
using Tango.Transport.Servers;
namespace Tango.TransportRouter.UI.ViewModels
{
public class MainViewVM : ExtendedObject
{
private TransportRoutingChannel _channel;
private TcpServer _server;
private UsbTransportAdapter _usbAdapter;
private TcpTransportAdapter _tcpAdapter;
#region Properties
///
/// Gets or sets the usb ports.
///
public List UsbPorts { get; set; }
private String _selectedUsbPort;
///
/// Gets or sets the selected USB port.
///
public String SelectedUsbPort
{
get { return _selectedUsbPort; }
set { _selectedUsbPort = value; RaisePropertyChanged(nameof(SelectedUsbPort)); }
}
private bool _isStarted;
///
/// Gets or sets a value indicating whether this instance is started.
///
public bool IsStarted
{
get { return _isStarted; }
set { _isStarted = value; RaisePropertyChanged(nameof(IsStarted)); }
}
private bool _isWaitingForTCP;
///
/// Gets or sets a value indicating whether this instance is waiting for TCP.
///
public bool IsWaitingForTCP
{
get { return _isWaitingForTCP; }
set { _isWaitingForTCP = value; RaisePropertyChanged(nameof(IsWaitingForTCP)); }
}
private bool _tcpOn;
public bool TcpOn
{
get { return _tcpOn; }
set { _tcpOn = value; RaisePropertyChanged(nameof(TcpOn)); }
}
private bool _usbOn;
public bool UsbOn
{
get { return _usbOn; }
set { _usbOn = value; RaisePropertyChanged(nameof(UsbOn)); }
}
#endregion
#region Commands
///
/// Gets or sets the toggle start stop command.
///
public RelayCommand ToggleStartStopCommand { get; set; }
#endregion
public MainViewVM()
{
_server = new TcpServer(9999);
ToggleStartStopCommand = new RelayCommand(ToggleStartStop);
_server.ClientConnected += _server_ClientConnected;
UsbPorts = Enumerable.Range(1, 9).Select(x => "COM" + x).ToList();
SelectedUsbPort = UsbPorts.First();
}
private void ToggleStartStop()
{
if (IsStarted)
{
Stop();
}
else
{
Start();
}
}
private async void Stop()
{
try
{
_server.Stop();
if (_tcpAdapter != null)
{
await _tcpAdapter.Disconnect();
}
await _usbAdapter.Disconnect();
IsStarted = false;
IsWaitingForTCP = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async void Start()
{
try
{
_usbAdapter = new UsbTransportAdapter(SelectedUsbPort);
_server.Start();
await _usbAdapter.Connect();
IsStarted = true;
IsWaitingForTCP = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async void _server_ClientConnected(object sender, ClientConnectedEventArgs e)
{
IsWaitingForTCP = false;
_tcpAdapter = new TcpTransportAdapter(e.Socket);
await _tcpAdapter.Connect();
_channel = new TransportRoutingChannel(_usbAdapter, _tcpAdapter);
_usbAdapter.DataAvailable -= _usbAdapter_DataAvailable;
_tcpAdapter.DataAvailable -= _tcpAdapter_DataAvailable;
_usbAdapter.DataAvailable += _usbAdapter_DataAvailable;
_tcpAdapter.DataAvailable += _tcpAdapter_DataAvailable;
}
private void _tcpAdapter_DataAvailable(object sender, byte[] e)
{
InvokeUI(() =>
{
TcpOn = true;
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += (x, y) =>
{
TcpOn = false; timer.Stop();
};
timer.Start();
});
}
private void _usbAdapter_DataAvailable(object sender, byte[] e)
{
InvokeUI(() =>
{
UsbOn = true;
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += (x, y) =>
{
UsbOn = false; timer.Stop();
};
timer.Start();
});
}
}
}