-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboblight-client.cs
504 lines (440 loc) · 16.1 KB
/
boblight-client.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Drawing;
namespace boblight_net
{
public class light
{
private string name = null;
private float hscan_min, hscan_max, vscan_min, vscan_max = 0.0F;
private bool inUse = true;
public float red, green, blue;
public light(string blname, float blvscan_min, float blvscan_max, float blhscan_min, float blhscan_max)
{
name = blname;
hscan_min = blhscan_min;
hscan_max = blhscan_max;
vscan_min = blvscan_min;
vscan_max = blvscan_max;
}
public string getName()
{
return name;
}
public float getHmax()
{
return hscan_max;
}
public float getHmin()
{
return hscan_min;
}
public float getVmax()
{
return vscan_max;
}
public float getVmin()
{
return vscan_min;
}
public bool use()
{
return inUse;
}
public void setUse(bool use)
{
inUse = use;
}
}
public class lightGroup
{
public light[] lights { get; set; }
public string name { get; set; }
public lightGroup(light[] lights, string name)
{
this.lights = lights;
this.name = name;
}
}
public class boblightClient
{
private Socket lightSocket = null;
private byte[] bytes = new byte[8192];
private light[] lights = null;
//Global Color Modifiers
public double luminosityModifier = 1D, saturationModifier = 1D;
public double hueModifier = 0D;
public boblightClient(string ip, int port, int priority = 128)
{
try
{
//Connect to boblightd
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
lightSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
lightSocket.Connect(remoteEP);
//Okay, we're now connected.
//Handshake time. Shake that hand.
sendData("hello\n");
if (receiveData() == "hello\n")
{
sendData("get version\n");
if (receiveData() == "version 5\n")
{
//Hooray, the server said hi back.
//Set the priority of the client, the lights won't work without it.
setPriority(priority);
//Oh, we might actually need to know something about the lights too.
retrieveLights();
}
else
{
throw new Exception("Handshake failed (wrong version)");
}
}
else
{
throw new Exception("Handshake failed (no hello)");
}
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//Set color Hex version
public void setColor(light light, string color, float brightness = 1.0F)
{
if (light.use())
{
//set a light via hex color
int red, green, blue;
red = Int32.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
green = Int32.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
blue = Int32.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
setLight(light, calculateBobColor(red), calculateBobColor(green), calculateBobColor(blue));
}
}
//Set color Hex version
//Array override
public void setColor(light[] lights, string color)
{
//set array of lights via hex color
int red, green, blue;
red = Int32.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
green = Int32.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
blue = Int32.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
foreach (light light in lights)
{
if (light.use())
setLight(light, calculateBobColor(red), calculateBobColor(green), calculateBobColor(blue));
}
}
//Set color RGB integer version
public void setColor(light light, int red, int green, int blue)
{
if (light.use())
setLight(light, calculateBobColor(red), calculateBobColor(green), calculateBobColor(blue));
}
//Set color RGB integer version
//Array override
public void setColor(light[] lights, int red, int green, int blue)
{
foreach (light light in lights)
{
if (light.use())
setLight(light, calculateBobColor(red), calculateBobColor(green), calculateBobColor(blue));
}
}
public void setLight(light light, float red, float green, float blue)
{
//Store these colors for future alterations
light.red = red;
light.green = green;
light.blue = blue;
//Do we have any color alterations to perform?
if (luminosityModifier != 1.0D || saturationModifier != 1.0D || hueModifier != 0)
{
HSLColor color = new HSLColor((int)(red * 255), (int)(green * 255), (int)(blue * 255));
color.Luminosity = color.Luminosity * luminosityModifier;
color.Saturation = color.Saturation * saturationModifier;
color.Hue = (color.Hue + hueModifier) % 240;
int[] rgbColor = color.getRGB();
red = calculateBobColor(rgbColor[0]);
green = calculateBobColor(rgbColor[1]);
blue = calculateBobColor(rgbColor[2]);
}
//Send the colors to the boblight server
sendColor(light.getName(), red, green, blue);
}
public float calculateBobColor(int color, float brightness)
{
//calculate the r/g/b color format for boblightd
return (float)color / 255F * brightness;
}
public float calculateBobColor(int color)
{
return (float)color / 255F;
}
private void sendColor(string light, float red, float green, float blue)
{
//boblightd version 5 set light
sendData("set light " + light + " rgb " + red.ToString() + " " + green.ToString() + " " + blue.ToString() + "\n");
}
public void setSpeed(light light, float speed)
{
//set speed of light
if (light.use())
sendSpeed(light.getName(), speed);
}
public void setSpeed(light[] lights, float speed)
{
//set speed of array of lights
foreach (light light in lights)
{
if(light.use())
sendSpeed(light.getName(), speed);
}
}
private void sendSpeed(string light, float speed)
{
//boblightd version 5 set light speed
sendData("set light " + light + " speed " + speed.ToString() + "\n");
}
public void setUse(light light, bool use)
{
//set use of light
light.setUse(use);
sendUse(light.getName(), use);
}
public void setUse(light[] lights, bool use)
{
//set use of array of lights
foreach (light light in lights)
{
light.setUse(use);
sendUse(light.getName(), use);
}
}
private void sendUse(string light, bool use)
{
//boblightd version 5 set light use
sendData("set light " + light + " use " + use.ToString().ToLower() + "\n");
}
public void setInterpolation(light light, bool interpolate)
{
//set interpolation of light
sendInterpolation(light.getName(), interpolate);
}
public void setInterpolation(light[] lights, bool interpolate)
{
//set interpolation of array of lights
foreach (light light in lights)
{
sendInterpolation(light.getName(), interpolate);
}
}
private void sendInterpolation(string light, bool interpolate)
{
//boblightd version 5 set light interpolation
sendData("set light " + light + " interpolation " + interpolate.ToString().ToLower() + "\n");
}
public void syncLights()
{
//boblightd version 5 sync lights
sendData("sync\n");
}
public void setPriority(int priority)
{
//boblightd version 5 set client priority
sendData("set priority " + priority.ToString() + "\n");
}
public bool pingLights()
{
//boblightd version 5 change accepted verification
sendData("ping\n");
return (receiveData() == "1\n");
}
private void retrieveLights()
{
//Communicate with boblight server and get back the lights
sendData("get lights\n");
string lightsData = receiveData();
//Remove the last new line, then split everything into something useful
string[] lightArray = lightsData.Remove(lightsData.Length - 1, 1).Split(UnicodeEncoding.Unicode.GetChars(UnicodeEncoding.Unicode.GetBytes("\n")));
lights = new light[Convert.ToInt32(lightArray[0].Substring(7))];
for (int i = 1; i < lightArray.Length; i++)
{
string[] lightData = lightArray[i].Split(UnicodeEncoding.Unicode.GetChars(UnicodeEncoding.Unicode.GetBytes(" ")));
lights[i - 1] = new light(lightData[1], float.Parse(lightData[3]) / 100, float.Parse(lightData[4]) / 100, float.Parse(lightData[5]) / 100, float.Parse(lightData[6]) / 100);
}
}
public light[] getLights()
{
return lights;
}
public bool isConnected()
{
if (lightSocket != null)
return lightSocket.Connected;
else
return false;
}
public void disconnect()
{
lightSocket.Disconnect(true);
lights = null;
}
private void sendData(string data)
{
lightSocket.Send(Encoding.UTF8.GetBytes(data));
}
private string receiveData()
{
int bytesRec = lightSocket.Receive(bytes);
string data = Encoding.UTF8.GetString(bytes, 0, bytesRec);
return data;
}
}
public class HSLColor
{
// Private data members below are on scale 0-1
// They are scaled for use externally based on scale
private double hue = 1.0;
private double saturation = 1.0;
private double luminosity = 1.0;
private const double scale = 240.0;
public double Hue
{
get { return hue * scale; }
set { hue = CheckRange(value / scale); }
}
public double Saturation
{
get { return saturation * scale; }
set { saturation = CheckRange(value / scale); }
}
public double Luminosity
{
get { return luminosity * scale; }
set { luminosity = CheckRange(value / scale); }
}
private double CheckRange(double value)
{
if (value < 0.0)
value = 0.0;
else if (value > 1.0)
value = 1.0;
return value;
}
public override string ToString()
{
return String.Format("H: {0:#0.##} S: {1:#0.##} L: {2:#0.##}", Hue, Saturation, Luminosity);
}
public string ToRGBString()
{
Color color = (Color)this;
return String.Format("R: {0:#0.##} G: {1:#0.##} B: {2:#0.##}", color.R, color.G, color.B);
}
public int[] getRGB()
{
Color color = (Color)this;
int[] colorArray = new int[3];
colorArray[0] = color.R;
colorArray[1] = color.G;
colorArray[2] = color.B;
return colorArray;
}
#region Casts to/from System.Drawing.Color
public static implicit operator Color(HSLColor hslColor)
{
double r = 0, g = 0, b = 0;
if (hslColor.luminosity != 0)
{
if (hslColor.saturation == 0)
r = g = b = hslColor.luminosity;
else
{
double temp2 = GetTemp2(hslColor);
double temp1 = 2.0 * hslColor.luminosity - temp2;
r = GetColorComponent(temp1, temp2, hslColor.hue + 1.0 / 3.0);
g = GetColorComponent(temp1, temp2, hslColor.hue);
b = GetColorComponent(temp1, temp2, hslColor.hue - 1.0 / 3.0);
}
}
return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
}
private static double GetColorComponent(double temp1, double temp2, double temp3)
{
temp3 = MoveIntoRange(temp3);
if (temp3 < 1.0 / 6.0)
return temp1 + (temp2 - temp1) * 6.0 * temp3;
else if (temp3 < 0.5)
return temp2;
else if (temp3 < 2.0 / 3.0)
return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0);
else
return temp1;
}
private static double MoveIntoRange(double temp3)
{
if (temp3 < 0.0)
temp3 += 1.0;
else if (temp3 > 1.0)
temp3 -= 1.0;
return temp3;
}
private static double GetTemp2(HSLColor hslColor)
{
double temp2;
if (hslColor.luminosity < 0.5) //<=??
temp2 = hslColor.luminosity * (1.0 + hslColor.saturation);
else
temp2 = hslColor.luminosity + hslColor.saturation - (hslColor.luminosity * hslColor.saturation);
return temp2;
}
public static implicit operator HSLColor(Color color)
{
HSLColor hslColor = new HSLColor();
hslColor.hue = color.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360
hslColor.luminosity = color.GetBrightness();
hslColor.saturation = color.GetSaturation();
return hslColor;
}
#endregion
public void SetRGB(int red, int green, int blue)
{
HSLColor hslColor = (HSLColor)Color.FromArgb(red, green, blue);
this.hue = hslColor.hue;
this.saturation = hslColor.saturation;
this.luminosity = hslColor.luminosity;
}
public HSLColor() { }
public HSLColor(Color color)
{
SetRGB(color.R, color.G, color.B);
}
public HSLColor(int red, int green, int blue)
{
SetRGB(red, green, blue);
}
public HSLColor(double hue, double saturation, double luminosity)
{
this.Hue = hue;
this.Saturation = saturation;
this.Luminosity = luminosity;
}
}
}