blob: 5199717ad877eb528fbbccb89190cd2c3aeda05b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
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();
});
}
}
}
|