7
7
import com .philips .lighting .hue .sdk .utilities .PHUtilities ;
8
8
import com .philips .lighting .model .*;
9
9
10
+ import javax .sound .sampled .*;
10
11
import java .util .List ;
11
12
import java .util .Random ;
12
13
import java .util .Scanner ;
20
21
public class Controller {
21
22
22
23
//TODO make bridge a field!
24
+ private PHBridge connectedBridge ;
23
25
private PHHueSDK phHueSDK ;
24
26
private Controller instance ;
25
27
@@ -54,7 +56,7 @@ private void listAllLights() {
54
56
55
57
if (choice <= allLights .size ()) {
56
58
57
- selectedLightMenu ((allLights .get (choice - 1 )), bridge );
59
+ selectedLightMenu ((allLights .get (choice - 1 )));
58
60
59
61
} else {
60
62
@@ -64,13 +66,13 @@ private void listAllLights() {
64
66
}
65
67
66
68
67
- private void selectedLightMenu (PHLight light , PHBridge bridge ) {
69
+ private void selectedLightMenu (PHLight light ) {
68
70
69
71
System .out .println ("WHAT DO YOU WANT TO DO?" );
70
72
System .out .println ("1: Change brightness" );
71
73
System .out .println ("2: Change power state" );
72
74
73
- if (light .getLightType () == CT_COLOR_LIGHT || light .getLightType () == COLOR_LIGHT ) {
75
+ if (light .getLightType () == CT_COLOR_LIGHT || light .getLightType () == COLOR_LIGHT ) {
74
76
System .out .println ("3: Change color of the light" );
75
77
}
76
78
@@ -83,15 +85,19 @@ private void selectedLightMenu(PHLight light, PHBridge bridge) {
83
85
switch (choice ) {
84
86
85
87
case 1 :
86
- changeBrightness (light , bridge );
88
+ changeBrightness (light );
87
89
break ;
88
90
89
- case 2 :
90
- changePowerState (light , bridge );
91
+ case 2 :
92
+ changePowerState (light );
91
93
break ;
92
94
93
95
case 3 :
94
- changeColor (light , bridge );
96
+ changeColor (light );
97
+ break ;
98
+
99
+ case 4 :
100
+ changeBrightnessBasedOnMicIn (light );
95
101
break ;
96
102
97
103
case 9 :
@@ -100,19 +106,21 @@ private void selectedLightMenu(PHLight light, PHBridge bridge) {
100
106
101
107
default :
102
108
System .out .println ("ERROR! Enter a valid number" );
103
- selectedLightMenu (light , bridge );
109
+ selectedLightMenu (light );
104
110
}
105
111
106
112
107
113
}
108
114
109
- private void changeColor (PHLight light , PHBridge bridge ) {
115
+ private void changeColor (PHLight light ) {
110
116
111
- if (light .getLightType () != CT_COLOR_LIGHT || light .getLightType () != COLOR_LIGHT ){
117
+ System .out .println (light .getLightType ());
118
+
119
+ /*if (light.getLightType().toString().equals("CT_COLOR_LIGHT") || light.getLightType().toString().equals("COLOR_LIGHT")){
112
120
System.out.println("This bulb does not support colors!");
113
121
selectedLightMenu(light,bridge);
114
122
return;
115
- }
123
+ }*/
116
124
117
125
Scanner scanner = new Scanner (System .in );
118
126
int r = 0 ;
@@ -192,52 +200,49 @@ private void changeColor(PHLight light, PHBridge bridge){
192
200
193
201
case 9 :
194
202
//BACK
195
- selectedLightMenu (light , bridge );
203
+ selectedLightMenu (light );
196
204
break ;
197
205
198
206
default :
199
207
System .out .println ("ERROR! Enter a valid number" );
200
- changeColor (light , bridge );
208
+ changeColor (light );
201
209
}
202
210
203
211
//Setting the colors
204
212
PHLightState lightState = new PHLightState ();
213
+
205
214
float [] xy = PHUtilities .calculateXYFromRGB (r , g , b , PHLight .PHLightColorMode .COLORMODE_XY .getValue ());
206
- System .out .println (xy [0 ] + " " + xy [1 ]);
207
215
lightState .setX (xy [0 ]);
208
216
lightState .setY (xy [1 ]);
209
217
210
- bridge .updateLightState (light , lightState );
218
+ phHueSDK . getSelectedBridge () .updateLightState (light , lightState );
211
219
212
- selectedLightMenu (light , bridge );
220
+ selectedLightMenu (light );
213
221
}
214
222
215
223
/**
216
224
* This method checks the last known light state and sets the opposite power state
217
225
*
218
226
* @param light light to be changed
219
- * @param bridge connected bridge
220
227
*/
221
- private void changePowerState (PHLight light , PHBridge bridge ) {
228
+ private void changePowerState (PHLight light ) {
222
229
PHLightState lightState = light .getLastKnownLightState ();
223
230
224
- if (lightState .isOn ()){
231
+ if (lightState .isOn ()) {
225
232
lightState .setOn (false );
226
233
System .out .println ("Turned " + light .getName () + " OFF" );
227
234
} else {
228
235
lightState .setOn (true );
229
236
System .out .println ("Turned " + light .getName () + " ON" );
230
237
}
231
238
232
- bridge .updateLightState (light , lightState );
239
+ phHueSDK . getSelectedBridge () .updateLightState (light , lightState );
233
240
234
- selectedLightMenu (light , bridge );
241
+ selectedLightMenu (light );
235
242
}
236
243
237
244
238
- public void changeBrightness (PHLight light , PHBridge bridge ) {
239
-
240
- System .out .println (light );
245
+ public void changeBrightness (PHLight light ) {
241
246
242
247
Scanner scanner = new Scanner (System .in );
243
248
System .out .println ("ENTER A NEW BRIGHTNESS VALUE: " );
@@ -249,32 +254,111 @@ public void changeBrightness(PHLight light, PHBridge bridge) {
249
254
250
255
PHLightState lightState = new PHLightState ();
251
256
lightState .setBrightness (updatedBrightness );
252
- bridge .updateLightState (light , lightState );
257
+ phHueSDK . getSelectedBridge () .updateLightState (light , lightState );
253
258
254
259
Thread .sleep (1000 );
255
260
256
261
} catch (InterruptedException e ) {
257
262
e .printStackTrace ();
258
263
}
259
264
260
- selectedLightMenu (light , bridge );
265
+ selectedLightMenu (light );
266
+
267
+ }
268
+
269
+ private void changeBrightnessBasedOnMicIn (PHLight light ) {
270
+
271
+ boolean stopRecording = false ;
272
+ int counter = 0 ;
273
+ int level = 0 ;
274
+ byte tempBuffer [] = new byte [6000 ];
275
+
276
+ try {
277
+
278
+ AudioFormat format = new AudioFormat (AudioFormat .Encoding .PCM_SIGNED , 44100 , 16 , 2 , 4 , 44100 , false );
279
+ DataLine .Info info = new DataLine .Info (TargetDataLine .class , format );
280
+
281
+ PHLightState lightState = new PHLightState ();
282
+
283
+ if (!AudioSystem .isLineSupported (info )) {
284
+ System .out .println ("This line is not supported" );
285
+ return ;
286
+ }
287
+
288
+ TargetDataLine targetDataLine = (TargetDataLine ) AudioSystem .getLine ((info ));
289
+ targetDataLine .open ();
290
+
291
+ System .out .println ("Starting to record..." );
292
+ targetDataLine .start ();
293
+
294
+ while (counter <= 200 ) {
295
+
296
+ if (targetDataLine .read (tempBuffer , 0 , tempBuffer .length ) > 0 ) {
297
+ level = calculateRMSLevel (tempBuffer );
298
+
299
+ lightState .setBrightness (level );
300
+ phHueSDK .getSelectedBridge ().updateLightState (light , lightState );
301
+
302
+ System .out .println (level );
303
+ counter ++;
304
+ }
305
+ }
306
+
307
+ targetDataLine .stop ();
308
+ targetDataLine .close ();
309
+ System .out .println ("Recording stopped" );
310
+
311
+
312
+ } catch (LineUnavailableException e ) {
313
+ e .printStackTrace ();
314
+ }
315
+ }
316
+
317
+
318
+ /**
319
+ * Algorithm is copyed from https://stackoverflow.com/a/32622121
320
+ *
321
+ * @param audioData data segment to analyze
322
+ * @return procent of input audio level
323
+ */
324
+ public int calculateRMSLevel (byte [] audioData )
325
+ {
326
+ long lSum = 0 ;
327
+ for (int i =0 ; i < audioData .length ; i ++)
328
+ lSum = lSum + audioData [i ];
329
+
330
+ double dAvg = lSum / audioData .length ;
331
+ double sumMeanSquare = 0d ;
261
332
333
+ for (int j =0 ; j < audioData .length ; j ++)
334
+ sumMeanSquare += Math .pow (audioData [j ] - dAvg , 2d );
335
+
336
+ double averageMeanSquare = sumMeanSquare / audioData .length ;
337
+
338
+ return (int )(Math .pow (averageMeanSquare ,0.5d ) + 0.5 );
262
339
}
263
340
341
+
264
342
private PHSDKListener listener = new PHSDKListener () {
265
343
@ Override
266
344
public void onCacheUpdated (List <Integer > list , PHBridge phBridge ) {
267
-
345
+
268
346
}
269
347
270
348
@ Override
271
349
public void onBridgeConnected (PHBridge phBridge , String s ) {
272
350
273
351
phHueSDK .setSelectedBridge (phBridge );
352
+ phHueSDK .enableHeartbeat (phBridge , PHHueSDK .HB_INTERVAL );
274
353
275
354
System .out .println ("BRIDGE CONNECTED" );
276
355
277
- phHueSDK .enableHeartbeat (phBridge , PHHueSDK .HB_INTERVAL );
356
+ try {
357
+ Thread .sleep (1000 );
358
+
359
+ } catch (InterruptedException e ) {
360
+ e .printStackTrace ();
361
+ }
278
362
279
363
listAllLights ();
280
364
@@ -321,22 +405,22 @@ public void onAccessPointsFound(List<PHAccessPoint> list) {
321
405
322
406
@ Override
323
407
public void onError (int i , String s ) {
324
-
408
+
325
409
}
326
410
327
411
@ Override
328
412
public void onConnectionResumed (PHBridge phBridge ) {
329
-
413
+
330
414
}
331
415
332
416
@ Override
333
417
public void onConnectionLost (PHAccessPoint phAccessPoint ) {
334
-
418
+
335
419
}
336
420
337
421
@ Override
338
422
public void onParsingErrors (List <PHHueParsingError > list ) {
339
-
423
+
340
424
}
341
425
};
342
426
@@ -347,4 +431,12 @@ public PHSDKListener getListener() {
347
431
public void setListener (PHSDKListener listener ) {
348
432
this .listener = listener ;
349
433
}
434
+
435
+ public PHBridge getConnectedBridge () {
436
+ return connectedBridge ;
437
+ }
438
+
439
+ public void setConnectedBridge (PHBridge connectedBridge ) {
440
+ this .connectedBridge = connectedBridge ;
441
+ }
350
442
}
0 commit comments