-
Notifications
You must be signed in to change notification settings - Fork 1
/
MacroDeckClient.cs
230 lines (203 loc) · 7.3 KB
/
MacroDeckClient.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using DeckSurf.SDK.Core;
using DeckSurf.SDK.Models;
using DeckSurf.SDK.Util;
using MacroDeck.StreamDeckConnector.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Websocket.Client;
namespace MacroDeck.StreamDeckConnector
{
internal class MacroDeckClient
{
private IntPtr _bufferPtr;
private int BUFFER_SIZE = 1024 * 1024;
private bool _disposed = false;
public MacroDeckClient()
{
_bufferPtr = Marshal.AllocHGlobal(BUFFER_SIZE);
}
public bool IsDisposed
{
get
{
return this._disposed;
}
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
}
Close();
Marshal.FreeHGlobal(_bufferPtr);
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~MacroDeckClient()
{
Dispose(false);
}
private WebsocketClient _websocketClient;
private ConnectedDevice _connectedDevice;
private Timer _frameUpdateTimer;
private List<ActionButtonModel> _buttons = new List<ActionButtonModel>();
public bool Closed { get; private set; } = false;
public MacroDeckClient(Uri uri, ConnectedDevice connectedDevice)
{
_connectedDevice = connectedDevice;
_websocketClient = new WebsocketClient(uri)
{
ReconnectTimeout = null,
};
_websocketClient.MessageReceived.Subscribe(msg => HandleMessage(msg.Text));
_websocketClient.Start();
SendAsync(new ConnectedMessageModel() { ClientId = _connectedDevice.SerialNumber });
_connectedDevice.OnButtonPress += ConnectedDevice_OnButtonPress;
_connectedDevice.InitializeDevice();
_connectedDevice.OnStreamClosed += _connectedDevice_OnStreamClosed;
_frameUpdateTimer = new Timer()
{
Interval = 66, // ~15 frames per second
Enabled = true,
};
_frameUpdateTimer.Elapsed += FrameUpdateTimer_Elapsed;
_frameUpdateTimer.Start();
}
internal void Close()
{
if (Closed) return;
Closed = true;
foreach (var button in this._buttons)
{
button?.Dispose();
}
if (_websocketClient != null)
{
_websocketClient.Dispose();
}
if (_frameUpdateTimer != null)
{
_frameUpdateTimer.Stop();
_frameUpdateTimer.Dispose();
}
if (_connectedDevice != null)
{
_connectedDevice.Close();
}
}
private void _connectedDevice_OnStreamClosed(object sender, EventArgs e)
{
Close();
}
private void ConnectedDevice_OnButtonPress(object source, ButtonPressEventArgs e)
{
int row = e.Id / this._connectedDevice.Columns;
int column = e.Id % this._connectedDevice.Columns;
string id = $"{row}_{column}";
switch (e.Kind)
{
case ButtonEventKind.DOWN:
SendAsync(new ButtonPressMessageModel() { Id = id });
break;
case ButtonEventKind.UP:
SendAsync(new ButtonReleaseMessageModel() { Id = id });
break;
case ButtonEventKind.LONG_DOWN:
SendAsync(new ButtonLongPressMessageModel() { Id = id });
break;
case ButtonEventKind.LONG_UP:
SendAsync(new ButtonLongPressReleaseMessageModel() { Id = id });
break;
}
}
private void FrameUpdateTimer_Elapsed(object? sender, ElapsedEventArgs e)
{
UpdateAllButtons();
}
private void HandleMessage(string message)
{
var receivedMessageModel = BasicMessageModel.Deserialize(message);
switch (receivedMessageModel.Method)
{
case Enums.MessageMethod.GET_CONFIG:
SendAsync(new BasicMessageModel() { Method = Enums.MessageMethod.GET_BUTTONS });
break;
case Enums.MessageMethod.GET_BUTTONS:
var buttonMessageModel = GetButtonsMessageModel.Deserialize(message);
foreach (var button in this._buttons)
{
button?.Dispose();
}
this._buttons = buttonMessageModel.Buttons;
UpdateAllButtons();
break;
case Enums.MessageMethod.UPDATE_BUTTON:
var updateMessageModel = UpdateButtonMessageModel.Deserialize(message);
var actionButton = updateMessageModel.Buttons[0];
if (actionButton == null) return;
var actionButtonOld = this._buttons.Find(x => x.Row == actionButton.Row && x.Column == actionButton.Column);
if (actionButtonOld != null)
{
actionButtonOld.Dispose();
this._buttons.Remove(actionButtonOld);
}
this._buttons.Add(actionButton);
int id = actionButton.Row * this._connectedDevice.Columns + actionButton.Column;
UpdateButton(id, actionButton);
break;
}
}
private void UpdateAllButtons()
{
if (_connectedDevice == null) return;
int buttonIndex = 0;
for (int row = 0; row < _connectedDevice?.Rows; row++)
{
for (int col = 0; col < _connectedDevice?.Columns; col++)
{
var actionButton = this._buttons.Find(x => x.Column == col && x.Row == row);
UpdateButton(buttonIndex, actionButton);
buttonIndex++;
}
}
}
private void UpdateButton(int id, ActionButtonModel? actionButton)
{
if (_connectedDevice == null) return;
if (actionButton != null)
{
actionButton.FrameTick();
var frame = actionButton.GetCurrentFrame(this._connectedDevice.ButtonSize);
if (frame == null) return;
this._connectedDevice?.SetKey(id, frame);
}
else
{
this._connectedDevice?.SetKey(id, DeviceConstants.XLDefaultBlackButton);
}
}
public void SendAsync(IBaseMessageModel messageModel)
{
if (_websocketClient == null) return;
Task.Run(() =>
{
string message = messageModel.Serialize();
_websocketClient?.Send(message);
});
}
}
}