aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.TransportRouter.UI/ViewModels/MainViewVM.cs
blob: 5199717ad877eb528fbbccb89190cd2c3aeda05b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.ScreenCapture
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

        /// <summary>
        /// Gets or sets the usb ports.
        /// </summary>
        public List<String> UsbPorts { get; set; }

        private String _selectedUsbPort;
        /// <summary>
        /// Gets or sets the selected USB port.
        /// </summary>
        public String SelectedUsbPort
        {
            get { return _selectedUsbPort; }
            set { _selectedUsbPort = value; RaisePropertyChanged(nameof(SelectedUsbPort)); }
        }

        private bool _isStarted;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is started.
        /// </summary>
        public bool IsStarted
        {
            get { return _isStarted; }
            set { _isStarted = value; RaisePropertyChanged(nameof(IsStarted)); }
        }

        private bool _isWaitingForTCP;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is waiting for TCP.
        /// </summary>
        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

        /// <summary>
        /// Gets or sets the toggle start stop command.
        /// </summary>
        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();
            });
        }
    }
}