WingMan – Diff between revs 7 and 9
?pathlinks?
Rev 7 | Rev 9 | |||
---|---|---|---|---|
1 | using System.Collections.Concurrent; |
1 | using System.Collections.Concurrent; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.IO; |
3 | using System.IO; |
|
4 | using System.Linq; |
4 | using System.Linq; |
|
5 | using System.Threading; |
5 | using System.Threading; |
|
6 | using System.Threading.Tasks; |
6 | using System.Threading.Tasks; |
|
7 | using MQTTnet; |
7 | using MQTTnet; |
|
8 | using WingMan.Communication; |
8 | using WingMan.Communication; |
|
9 | |
9 | |
|
10 | namespace WingMan.MouseKey |
10 | namespace WingMan.MouseKey |
|
11 | { |
11 | { |
|
12 | public class MouseKeyBindingsSynchronizer |
12 | public class MouseKeyBindingsSynchronizer |
|
13 | { |
13 | { |
|
14 | public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); |
14 | public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); |
|
15 | |
15 | |
|
16 | public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication, |
16 | public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MqttCommunication mqttCommunication, |
|
17 | TaskScheduler taskScheduler, CancellationToken cancellationToken) |
17 | TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
18 | { |
18 | { |
|
19 | MouseKeyBindings = mouseKeyBindings; |
19 | MouseKeyBindings = mouseKeyBindings; |
|
20 | this.MQTTCommunication = MQTTCommunication; |
20 | MqttCommunication = mqttCommunication; |
|
21 | CancellationToken = cancellationToken; |
21 | CancellationToken = cancellationToken; |
|
22 | TaskScheduler = taskScheduler; |
22 | TaskScheduler = taskScheduler; |
|
23 | |
23 | |
|
24 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>(); |
24 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>(); |
|
25 | |
25 | |
|
26 | MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; |
26 | mqttCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; |
|
27 | |
27 | |
|
28 | Task.Run(Synchronize, CancellationToken); |
28 | Task.Run(Synchronize, CancellationToken); |
|
29 | } |
29 | } |
|
30 | |
30 | |
|
31 | private MouseKeyBindings MouseKeyBindings { get; } |
31 | private MouseKeyBindings MouseKeyBindings { get; } |
|
32 | |
32 | |
|
33 | private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; } |
33 | private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; } |
|
34 | |
34 | |
|
35 | private MQTTCommunication MQTTCommunication { get; } |
35 | private MqttCommunication MqttCommunication { get; } |
|
36 | |
36 | |
|
37 | private CancellationToken CancellationToken { get; } |
37 | private CancellationToken CancellationToken { get; } |
|
38 | private TaskScheduler TaskScheduler { get; } |
38 | private TaskScheduler TaskScheduler { get; } |
|
39 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
39 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
|
40 | |
40 | |
|
41 | private async void MqttCommunicationOnOnMessageReceived(object sender, |
41 | private async void MqttCommunicationOnOnMessageReceived(object sender, |
|
42 | MqttApplicationMessageReceivedEventArgs e) |
42 | MqttApplicationMessageReceivedEventArgs e) |
|
43 | { |
43 | { |
|
44 | if (e.ApplicationMessage.Topic != "exchange") |
44 | if (e.ApplicationMessage.Topic != "exchange") |
|
45 | return; |
45 | return; |
|
46 | |
46 | |
|
47 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
47 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
|
48 | { |
48 | { |
|
49 | memoryStream.Position = 0L; |
49 | memoryStream.Position = 0L; |
|
50 | |
50 | |
|
51 | var mouseKeyBindingsExchange = |
51 | var mouseKeyBindingsExchange = |
|
52 | (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream); |
52 | (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream); |
|
53 | |
53 | |
|
54 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
54 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
|
55 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings)) |
55 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings)) |
|
56 | return; |
56 | return; |
|
57 | |
57 | |
|
58 | // Nick does not exist so the bindings will be added. |
58 | // Nick does not exist so the bindings will be added. |
|
59 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
59 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
|
60 | mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings); |
60 | mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings); |
|
61 | |
61 | |
|
62 | await Task.Delay(0) |
62 | await Task.Delay(0) |
|
63 | .ContinueWith( |
63 | .ContinueWith( |
|
64 | _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, |
64 | _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, |
|
65 | new MouseKeyBindingsSynchronizedEventArgs( |
65 | new MouseKeyBindingsSynchronizedEventArgs( |
|
66 | mouseKeyBindingsExchange)), |
66 | mouseKeyBindingsExchange)), |
|
67 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
67 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
68 | } |
68 | } |
|
69 | } |
69 | } |
|
70 | |
70 | |
|
71 | private async Task Synchronize() |
71 | private async Task Synchronize() |
|
72 | { |
72 | { |
|
73 | do |
73 | do |
|
74 | { |
74 | { |
|
75 | await Task.Delay(1000, CancellationToken).ConfigureAwait(false); |
75 | await Task.Delay(1000, CancellationToken).ConfigureAwait(false); |
|
76 | |
76 | |
|
77 | if (!MQTTCommunication.Running) |
77 | if (!MqttCommunication.Running) |
|
78 | continue; |
78 | continue; |
|
79 | |
79 | |
|
80 | using (var memoryStream = new MemoryStream()) |
80 | using (var memoryStream = new MemoryStream()) |
|
81 | { |
81 | { |
|
82 | MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream, |
82 | MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream, |
|
83 | new MouseKeyBindingExchange(MQTTCommunication.Nick, MouseKeyBindings.Bindings)); |
83 | new MouseKeyBindingExchange(MqttCommunication.Nick, MouseKeyBindings.Bindings)); |
|
84 | |
84 | |
|
85 | memoryStream.Position = 0L; |
85 | memoryStream.Position = 0L; |
|
86 | |
86 | |
|
87 | await MQTTCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false); |
87 | await MqttCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false); |
|
88 | } |
88 | } |
|
89 | } while (!CancellationToken.IsCancellationRequested); |
89 | } while (!CancellationToken.IsCancellationRequested); |
|
90 | } |
90 | } |
|
91 | } |
91 | } |
|
92 | } |
92 | } |
|
93 | |
93 | |