-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrismatikApi.cs
More file actions
212 lines (172 loc) · 6.95 KB
/
PrismatikApi.cs
File metadata and controls
212 lines (172 loc) · 6.95 KB
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
using System.Net.Sockets;
using System.Threading;
using PrimS.Telnet;
/// <summary>
/// Just a simple Wrapper for the Prismatik API
/// </summary>
static class PrismatikApiClient
{
public static Client PRISMATIC_CLIENT;
private static readonly object ro_PRISMATIC_CLIENT_LOCK_OBJ = new object();
/// <summary>Setup the connection to the Prismatik API.</summary>
/// <param name="apiKey">Your API-Key.</param>
public static void SetupTelnetClient(string apiKey)
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (PRISMATIC_CLIENT != null && PRISMATIC_CLIENT.IsConnected) return;
try
{
PRISMATIC_CLIENT = new Client("127.0.0.1", 3636, new CancellationToken());
}
catch (SocketException)
{
PRISMATIC_CLIENT = null;
return;
}
if (!PRISMATIC_CLIENT.IsConnected)
{
PRISMATIC_CLIENT = null;
return;
}
var welcomeMessage = PRISMATIC_CLIENT.ReadAsync().Result;
if (!welcomeMessage.Contains("Lightpack API"))
{
PRISMATIC_CLIENT = null;
return;
}
if (!AuthenticateTelnet(apiKey)) PRISMATIC_CLIENT = null;
}
}
private static bool AuthenticateTelnet(string apiKey)
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
PRISMATIC_CLIENT.WriteLine($"apikey:{apiKey}");
var authResponse = PRISMATIC_CLIENT.ReadAsync().Result;
return authResponse.Contains("ok");
}
}
private static bool GetStatusApi()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
PRISMATIC_CLIENT.WriteLine("getstatusapi");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
return useResponse.Contains("statusapi:idle");
}
}
private static bool Lock()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
PRISMATIC_CLIENT.WriteLine($"lock");
var authResponse = PRISMATIC_CLIENT.ReadAsync().Result;
return authResponse.Contains("lock:success");
}
}
private static bool Unlock()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
PRISMATIC_CLIENT.WriteLine($"unlock");
var authResponse = PRISMATIC_CLIENT.ReadAsync().Result;
return authResponse.Contains("unlock:success") || authResponse.Contains("unlock:not");
}
}
/// <summary>Returnes full profiles list of controlling program settings. Profiles are kept in Profiles folder.</summary>
/// <returns>srray with profile-names</returns>
public static string[] GetProfiles()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return new string[0];
if (!Lock()) return new string[0];
PRISMATIC_CLIENT.WriteLine("getprofiles");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
useResponse = useResponse.Replace("profiles:", "");
Unlock();
return useResponse.Split(';');
}
}
/// <summary>Returnes the name of the current active profile.</summary>
/// <returns>profil-name</returns>
public static string GetProfile()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return "";
if (!Lock()) return "";
PRISMATIC_CLIENT.WriteLine("getprofiles");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
useResponse = useResponse.Replace("profile:", "");
Unlock();
return useResponse;
}
}
/// <summary>Returnes the status of device, which, while being connected to PC, might be turned off (LEDs are not burning) or might be turned on in the capturing software settings. The command returnes device error in case of error of the access to device (the icon in tray changes to the forbidding one). Unknown is a hardly probable event that occurs upon the expiry of request timeout to GUI (~1 s.)</summary>
/// <returns>on, off, device error or unknown</returns>
public static string GetStatus()
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return "";
if (!Lock()) return "";
PRISMATIC_CLIENT.WriteLine("getstatus");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
useResponse = (useResponse.Replace("status:", ""));
Unlock();
return useResponse;
}
}
/// <summary>Sets brightness level to all device's LEDs at the same time within the range of 0 (lighting disabled) to 100.</summary>
/// <param name="brightness">brigtness from 0-100</param>
/// <returns>true if success false if it fails</returns>
public static bool SetBrightness(uint brightness)
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
if (!Lock()) return false;
PRISMATIC_CLIENT.WriteLine($"setbrightness:{brightness}");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
Unlock();
return useResponse.Contains("ok");
}
}
/// <summary>Turns on the defined settings profile. Profiles list is available on command getprofiles.</summary>
/// <param name="profile">profile-name as string</param>
/// <returns>true if success false if it fails</returns>
public static bool SetProfile(string profile)
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
if (!Lock()) return false;
PRISMATIC_CLIENT.WriteLine($"setprofile:{profile}");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
Unlock();
return useResponse.Contains("ok");
}
}
/// <summary>Turnes the device on or off. Represents the "turn the lighting on/off" button in the software settings.</summary>
/// <param name="status">desired status</param>
/// <returns>true if success false if it fails</returns>
public static bool SetStatus(bool status)
{
lock (ro_PRISMATIC_CLIENT_LOCK_OBJ)
{
if (!PRISMATIC_CLIENT.IsConnected) return false;
if (!Lock()) return false;
var statusString = status ? "on" : "off" ;
PRISMATIC_CLIENT.WriteLine($"setprofile:{statusString}");
var useResponse = PRISMATIC_CLIENT.ReadAsync().Result;
Unlock();
return useResponse.Contains("ok");
}
}
}