WingMan – Diff between revs 6 and 7
?pathlinks?
Rev 6 | Rev 7 | |||
---|---|---|---|---|
Line 9... | Line 9... | |||
9 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
9 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
|
10 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
10 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
|
Line 11... | Line 11... | |||
11 | |
11 | |
|
12 | namespace WingMan.Communication |
12 | namespace WingMan.Communication |
|
13 | { |
13 | { |
|
14 | public class MQTTCommunication |
14 | public class MQTTCommunication : IDisposable |
|
- | 15 | { |
||
- | 16 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
||
- | 17 | |
||
- | 18 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
||
- | 19 | |
||
- | 20 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
||
- | 21 | |
||
- | 22 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
||
- | 23 | |
||
- | 24 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
||
- | 25 | |
||
- | 26 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
||
- | 27 | |
||
- | 28 | public delegate void ServerClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e); |
||
- | 29 | |
||
- | 30 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
||
- | 31 | |
||
- | 32 | public delegate void ServerStarted(object sender, EventArgs e); |
||
- | 33 | |
||
- | 34 | public delegate void ServerStopped(object sender, EventArgs e); |
||
15 | { |
35 | |
|
16 | public MQTTCommunication(TaskScheduler taskScheduler) |
36 | public MQTTCommunication(TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
17 | { |
37 | { |
|
18 | TaskScheduler = taskScheduler; |
38 | TaskScheduler = taskScheduler; |
|
Line 19... | Line 39... | |||
19 | CancellationTokenSource = new CancellationTokenSource(); |
39 | CancellationToken = cancellationToken; |
|
20 | |
40 | |
|
21 | Client = new MqttFactory().CreateManagedMqttClient(); |
41 | Client = new MqttFactory().CreateManagedMqttClient(); |
|
Line 22... | Line 42... | |||
22 | Server = new MqttFactory().CreateMqttServer(); |
42 | Server = new MqttFactory().CreateMqttServer(); |
|
Line 23... | Line 43... | |||
23 | } |
43 | } |
|
Line 24... | Line 44... | |||
24 | |
44 | |
|
Line 34... | Line 54... | |||
34 | |
54 | |
|
Line 35... | Line 55... | |||
35 | private IPAddress IPAddress { get; set; } |
55 | private IPAddress IPAddress { get; set; } |
|
Line 36... | Line 56... | |||
36 | |
56 | |
|
Line 37... | Line 57... | |||
37 | private int Port { get; set; } |
57 | private int Port { get; set; } |
|
Line 38... | Line 58... | |||
38 | |
58 | |
|
- | 59 | private CancellationToken CancellationToken { get; } |
||
- | 60 | |
||
- | 61 | public MQTTCommunicationType Type { get; set; } |
||
Line 39... | Line 62... | |||
39 | private CancellationTokenSource CancellationTokenSource { get; set; } |
62 | |
|
Line 40... | Line -... | |||
40 | |
- | ||
41 | public MQTTCommunicationType Type { get; set; } |
- | ||
42 | |
63 | public async void Dispose() |
|
Line 43... | Line -... | |||
43 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
- | ||
44 | |
- | ||
45 | public event MessageReceived OnMessageReceived; |
64 | { |
|
Line 46... | Line -... | |||
46 | |
- | ||
47 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
- | ||
48 | |
65 | await Stop(); |
|
Line 49... | Line -... | |||
49 | public event ClientConnected OnClientConnected; |
- | ||
50 | |
- | ||
51 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
66 | } |
|
Line 52... | Line -... | |||
52 | |
- | ||
53 | public event ClientDisconnected OnClientDisconnected; |
- | ||
54 | |
67 | |
|
Line 55... | Line -... | |||
55 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
- | ||
56 | |
- | ||
57 | public event ClientConnectionFailed OnClientConnectionFailed; |
68 | public event MessageReceived OnMessageReceived; |
|
Line 58... | Line -... | |||
58 | |
- | ||
59 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
- | ||
60 | |
69 | |
|
Line 61... | Line -... | |||
61 | public event ClientUnsubscribed OnClientUnsubscribed; |
- | ||
62 | |
- | ||
63 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
70 | public event ClientConnected OnClientConnected; |
|
Line 64... | Line -... | |||
64 | |
- | ||
65 | public event ClientSubscribed OnClientSubscribed; |
- | ||
66 | |
71 | |
|
Line 67... | Line 72... | |||
67 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
72 | public event ClientDisconnected OnClientDisconnected; |
|
68 | |
73 | |
|
69 | public event ServerClientDisconnected OnServerClientDisconnected; |
74 | public event ClientConnectionFailed OnClientConnectionFailed; |
|
Line 152... | Line 157... | |||
152 | } |
157 | } |
|
Line 153... | Line 158... | |||
153 | |
158 | |
|
154 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
159 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
155 | { |
160 | { |
|
156 | await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
161 | await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
157 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler); |
162 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
Line 158... | Line 163... | |||
158 | } |
163 | } |
|
159 | |
164 | |
|
160 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
165 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
|
161 | { |
166 | { |
|
162 | await Task.Delay(0).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
167 | await Task.Delay(0).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
|
Line 163... | Line 168... | |||
163 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
168 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
164 | } |
169 | } |
|
165 | |
170 | |
|
166 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
171 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
|
167 | { |
172 | { |
|
Line 168... | Line 173... | |||
168 | await Task.Delay(0).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
173 | await Task.Delay(0).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
|
169 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
174 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
170 | } |
175 | } |
|
171 | |
176 | |
|
172 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
177 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
|
Line 173... | Line 178... | |||
173 | { |
178 | { |
|
174 | await Task.Delay(0).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
179 | await Task.Delay(0).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
|
175 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
180 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
Line 233... | Line 238... | |||
233 | } |
238 | } |
|
Line 234... | Line 239... | |||
234 | |
239 | |
|
235 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
240 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
236 | { |
241 | { |
|
237 | await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
242 | await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
238 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
243 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
Line 239... | Line 244... | |||
239 | } |
244 | } |
|
240 | |
245 | |
|
241 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
246 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
|
242 | { |
247 | { |
|
243 | await Task.Delay(0).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
248 | await Task.Delay(0).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
|
Line 244... | Line 249... | |||
244 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
249 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
245 | } |
250 | } |
|
246 | |
251 | |
|
247 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
252 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
|
248 | { |
253 | { |
|
Line 249... | Line 254... | |||
249 | await Task.Delay(0).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
254 | await Task.Delay(0).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
|
250 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
255 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
251 | } |
256 | } |
|
252 | |
257 | |
|
253 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
258 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
|
Line 254... | Line 259... | |||
254 | { |
259 | { |
|
255 | await Task.Delay(0).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
260 | await Task.Delay(0).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
|
256 | CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
261 | CancellationToken, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
|
257 | } |
262 | } |
|
258 | |
263 | |
|
Line 259... | Line 264... | |||
259 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
264 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
|
260 | { |
265 | { |
|
261 | await Task.Delay(0).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
266 | await Task.Delay(0).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
|
Line 292... | Line 297... | |||
292 | switch (Type) |
297 | switch (Type) |
|
293 | { |
298 | { |
|
294 | case MQTTCommunicationType.Client: |
299 | case MQTTCommunicationType.Client: |
|
295 | await Client.PublishAsync(new ManagedMqttApplicationMessage |
300 | await Client.PublishAsync(new ManagedMqttApplicationMessage |
|
296 | { |
301 | { |
|
297 | ApplicationMessage = new MqttApplicationMessage { Topic = topic, Payload = payload } |
302 | ApplicationMessage = new MqttApplicationMessage {Topic = topic, Payload = payload} |
|
298 | }).ConfigureAwait(false); |
303 | }).ConfigureAwait(false); |
|
299 | break; |
304 | break; |
|
300 | case MQTTCommunicationType.Server: |
305 | case MQTTCommunicationType.Server: |
|
301 | await Server.PublishAsync(new MqttApplicationMessage {Topic = topic, Payload = payload}).ConfigureAwait(false); |
306 | await Server.PublishAsync(new MqttApplicationMessage {Topic = topic, Payload = payload}) |
|
- | 307 | .ConfigureAwait(false); |
||
302 | break; |
308 | break; |
|
303 | } |
309 | } |
|
304 | } |
310 | } |
|
305 | } |
311 | } |
|
306 | } |
312 | } |
|
307 | |
313 | |