-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerAndClockPlugin.cs
189 lines (173 loc) · 6.38 KB
/
TimerAndClockPlugin.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
using Newtonsoft.Json.Linq;
using streamdeck_client_csharp;
using streamdeck_client_csharp.Events;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace TimerAndClock
{
public class TimerAndClockPlugin
{
private static readonly Dictionary<string, Type> s_ActionList = new Dictionary<string, Type>();
private StreamDeckConnection m_Connection;
private readonly ManualResetEvent m_ConnectEvent = new ManualResetEvent(false);
private readonly ManualResetEvent m_DisconnectEvent = new ManualResetEvent(false);
private readonly Dictionary<string, ActionBase> m_Actions = new Dictionary<string, ActionBase>();
private readonly SemaphoreSlim m_ActionsLock = new SemaphoreSlim(1);
static TimerAndClockPlugin()
{
Type[] types = typeof(TimerAndClockPlugin).Assembly.GetTypes();
foreach (Type type in types)
{
object[] attributes = type.GetCustomAttributes(typeof(ActionAttribute), false);
if (attributes.Length > 0)
{
ActionAttribute actionAttribute = attributes[0] as ActionAttribute;
if (actionAttribute != null)
{
s_ActionList[actionAttribute.ActionName.ToLower()] = type;
}
}
}
}
public async Task RunAsync(int port, string uuid, string registerEvent)
{
m_Connection = new StreamDeckConnection(port, uuid, registerEvent);
m_Connection.OnConnected += Connection_OnConnected;
m_Connection.OnDisconnected += Connection_OnDisconnected;
m_Connection.OnKeyDown += Connection_OnKeyDown;
m_Connection.OnKeyUp += Connection_OnKeyUp;
m_Connection.OnWillAppear += Connection_OnWillAppear;
m_Connection.OnWillDisappear += Connection_OnWillDisappear;
m_Connection.OnSendToPlugin += Connection_OnSendToPlugin;
// Start the connection
m_Connection.Run();
// Wait for up to 10 seconds to connect, if it fails, the app will exit
if (m_ConnectEvent.WaitOne(TimeSpan.FromSeconds(10)))
{
// We connected, loop every 1/2 second until we disconnect
while (!m_DisconnectEvent.WaitOne(TimeSpan.FromMilliseconds(500)))
{
await RunTickAsync();
}
}
else
{
Console.WriteLine("Plugin failed to connect to Stream Deck");
}
}
private async void Connection_OnSendToPlugin(object sender, StreamDeckEventReceivedEventArgs<SendToPluginEvent> e)
{
await m_ActionsLock.WaitAsync();
try
{
if (m_Actions.ContainsKey(e.Event.Context.ToLower()))
{
await m_Actions[e.Event.Context.ToLower()].ProcessPropertyInspectorAsync(e.Event);
}
}
finally
{
m_ActionsLock.Release();
}
}
private async void Connection_OnWillDisappear(object sender, StreamDeckEventReceivedEventArgs<WillDisappearEvent> e)
{
await m_ActionsLock.WaitAsync();
try
{
if (m_Actions.ContainsKey(e.Event.Context.ToLower()))
{
await m_Actions[e.Event.Context.ToLower()].SaveAsync();
m_Actions.Remove(e.Event.Context.ToLower());
}
}
finally
{
m_ActionsLock.Release();
}
}
private async void Connection_OnWillAppear(object sender, StreamDeckEventReceivedEventArgs<WillAppearEvent> e)
{
await m_ActionsLock.WaitAsync();
try
{
if (s_ActionList.ContainsKey(e.Event.Action.ToLower()))
{
ActionBase action = Activator.CreateInstance(s_ActionList[e.Event.Action.ToLower()]) as ActionBase;
await action.LoadAsync(m_Connection, e.Event.Action, e.Event.Context, e.Event.Payload.Settings);
m_Actions[e.Event.Context.ToLower()] = action;
}
}
finally
{
m_ActionsLock.Release();
}
}
private async void Connection_OnKeyDown(object sender, StreamDeckEventReceivedEventArgs<KeyDownEvent> e)
{
await m_ActionsLock.WaitAsync();
try
{
if (m_Actions.ContainsKey(e.Event.Context.ToLower()))
{
await m_Actions[e.Event.Context.ToLower()].KeyDownAsync();
}
}
finally
{
m_ActionsLock.Release();
}
}
private async void Connection_OnKeyUp(object sender, StreamDeckEventReceivedEventArgs<KeyUpEvent> e)
{
await m_ActionsLock.WaitAsync();
try
{
if (m_Actions.ContainsKey(e.Event.Context.ToLower()))
{
await m_Actions[e.Event.Context.ToLower()].KeyUpAsync();
}
}
finally
{
m_ActionsLock.Release();
}
}
private async Task RunTickAsync()
{
// If the plugin needs to do anything, this runs once per second.
// It could be used to ping to see if the app has arrived, etc.
await m_ActionsLock.WaitAsync();
try
{
foreach (ActionBase action in m_Actions.Values)
{
await action.RunTickAsync();
}
}
finally
{
m_ActionsLock.Release();
}
}
private async void Connection_OnDisconnected(object sender, EventArgs e)
{
await m_ActionsLock.WaitAsync();
try
{
m_Actions.Clear();
}
finally
{
m_ActionsLock.Release();
}
m_DisconnectEvent.Set();
}
private void Connection_OnConnected(object sender, EventArgs e)
{
m_ConnectEvent.Set();
}
}
}