8
8
*/
9
9
package org .openhab .binding .yeelight .internal ;
10
10
11
- import com .google .gson .JsonElement ;
12
- import com .google .gson .JsonObject ;
13
- import com .google .gson .JsonParser ;
11
+ import com .google .gson .Gson ;
14
12
import org .apache .commons .lang .StringUtils ;
15
13
import org .openhab .binding .yeelight .YeelightBindingProvider ;
14
+ import org .openhab .binding .yeelight .model .YeelightGetPropsResponse ;
16
15
import org .openhab .core .binding .AbstractActiveBinding ;
17
16
import org .openhab .core .items .ItemNotFoundException ;
18
17
import org .openhab .core .items .ItemRegistry ;
@@ -72,8 +71,8 @@ public class YeelightBinding extends AbstractActiveBinding<YeelightBindingProvid
72
71
//Socket
73
72
private MulticastSocket socket = null ;
74
73
75
- //Gson parser
76
- private JsonParser parser = new JsonParser ();
74
+ //Gson
75
+ private Gson gson = new Gson ();
77
76
78
77
//devices
79
78
Hashtable <String , YeelightDevice > devices ;
@@ -161,7 +160,7 @@ private void receiveData(MulticastSocket socket, DatagramPacket dgram) {
161
160
String sentence = new String (dgram .getData (), 0 ,
162
161
dgram .getLength ());
163
162
164
- logger .debug ("Yeelight received packet: " + sentence );
163
+ logger .debug ("Yeelight received packet: {}" , sentence );
165
164
166
165
if (isOKPacket (sentence ) || isNotifyPacket (sentence )) {
167
166
String [] lines = sentence .split ("\n " );
@@ -185,7 +184,7 @@ else if (line.startsWith("support: "))
185
184
if (!id .equals ("" ) && !devices .containsKey (id )) {
186
185
YeelightDevice device = new YeelightDevice (id , location , model , support );
187
186
devices .put (id , device );
188
- logger .info ("Found Yeelight device :\n " + device .toString ());
187
+ logger .info ("Found Yeelight device :\n {}" , device .toString ());
189
188
}
190
189
}
191
190
}
@@ -217,9 +216,9 @@ private void discoverYeelightDevices() {
217
216
218
217
socket .send (sendPacket );
219
218
} catch (MalformedURLException e ) {
220
- logger .error ("The URL '" + url + " ' is malformed: " + e . toString () );
219
+ logger .error ("The URL '{} ' is malformed: " , url , e );
221
220
} catch (Exception e ) {
222
- logger .error ("Cannot send Yeelight datagram packet : " + e . toString () );
221
+ logger .error ("Cannot get Yeelight login cookie : " , e );
223
222
}
224
223
}
225
224
@@ -285,7 +284,7 @@ protected void execute() {
285
284
return ;
286
285
}
287
286
288
- Hashtable <String , String > propList = new Hashtable <>();
287
+ Hashtable <String , YeelightGetPropsResponse > propList = new Hashtable <>();
289
288
290
289
//devices.clear();
291
290
discoverYeelightDevices ();
@@ -302,14 +301,14 @@ protected void execute() {
302
301
continue ;
303
302
304
303
String location = config .getLocation ();
305
- String result ;
304
+ YeelightGetPropsResponse result ;
306
305
307
306
if (!propList .containsKey (location )) {
308
307
result = sendYeelightGetPropCommand (location );
309
308
if (result == null )
310
309
continue ;
311
310
propList .put (location , result );
312
- logger .debug ("Result: " + result );
311
+ logger .debug ("Cached location: {}" , location );
313
312
} else {
314
313
result = propList .get (location );
315
314
}
@@ -319,57 +318,54 @@ protected void execute() {
319
318
}
320
319
}
321
320
322
- private void processYeelightResult (String result , String action , String itemName ) {
323
- JsonObject jo = parser .parse (result ).getAsJsonObject ();
321
+ private void processYeelightResult (YeelightGetPropsResponse result , String action , String itemName ) {
322
+ // JsonObject jo = parser.parse(result).getAsJsonObject();
324
323
State newState = null ;
325
324
State oldState = null ;
326
325
try {
327
326
switch (action ) {
328
327
case SET_POWER :
329
- String power = getJSONArrayResult ( jo , 0 ). getAsString ( );
328
+ String power = result . getResult (). get ( 0 );
330
329
newState = power .equals ("on" ) ? OnOffType .ON : OnOffType .OFF ;
331
330
break ;
332
331
case SET_BRIGHT :
333
- int bright = getJSONArrayResult ( jo , 1 ).getAsInt ( );
332
+ int bright = result . getResult (). get ( 1 ).isEmpty () ? 0 : Integer . parseInt ( result . getResult (). get ( 1 ) );
334
333
newState = new PercentType (bright == 1 ? 0 : bright );
335
334
break ;
336
335
case SET_CT :
337
- int ct = getJSONArrayResult (jo , 2 ).getAsInt ();
336
+ int ct = result .getResult ().get (2 ).isEmpty () ? 0 : Integer .parseInt (result .getResult ().get (2 ));
337
+ ;
338
338
newState = new PercentType ((ct - 1700 ) / 48 );
339
339
break ;
340
340
case SET_HSB :
341
- int hue = getJSONArrayResult ( jo , 3 ).getAsInt ( );
342
- int sat = getJSONArrayResult ( jo , 4 ).getAsInt ( );
343
- int br = getJSONArrayResult ( jo , 1 ).getAsInt ( );
341
+ int hue = result . getResult (). get ( 3 ).isEmpty () ? 0 : Integer . parseInt ( result . getResult (). get ( 3 ) );
342
+ int sat = result . getResult (). get ( 4 ).isEmpty () ? 0 : Integer . parseInt ( result . getResult (). get ( 4 ) );
343
+ int br = result . getResult (). get ( 1 ).isEmpty () ? 0 : Integer . parseInt ( result . getResult (). get ( 1 ) );
344
344
newState = new HSBType (new DecimalType (hue ), new PercentType (sat ), new PercentType (br == 1 ? 0 : br ));
345
345
break ;
346
346
case SET_RGB :
347
- int rgb = getJSONArrayResult ( jo , 5 ).getAsInt ( );
347
+ int rgb = result . getResult (). get ( 5 ).isEmpty () ? 0 : Integer . parseInt ( result . getResult (). get ( 5 ) );
348
348
Color col = getRGBColor (rgb );
349
349
newState = new HSBType (col );
350
350
break ;
351
351
case NIGHTLIGHT :
352
- String status = getJSONArrayResult ( jo , 6 ). getAsString ( );
352
+ String status = result . getResult (). get ( 6 );
353
353
newState = (status .equals ("0" ) || status .equals ("" )) ? OnOffType .OFF : OnOffType .ON ;
354
354
break ;
355
355
default :
356
- logger .error ("Unknown Yeelight action: " + action );
356
+ logger .error ("Unknown Yeelight action: {}" , action );
357
357
358
358
}
359
359
360
360
oldState = itemRegistry .getItem (itemName ).getState ();
361
361
} catch (ItemNotFoundException e ) {
362
362
logger .error (e .toString ());
363
363
}
364
- if (!oldState .equals (newState )) {
364
+ if (oldState == null || !oldState .equals (newState )) {
365
365
eventPublisher .postUpdate (itemName , newState );
366
366
}
367
367
}
368
368
369
- private JsonElement getJSONArrayResult (JsonObject jo , int pos ) {
370
- return jo .get (RESULT ).getAsJsonArray ().get (pos );
371
- }
372
-
373
369
/**
374
370
* @{inheritDoc}
375
371
*/
@@ -428,16 +424,17 @@ protected void internalReceiveCommand(String itemName, Command command) {
428
424
}
429
425
break ;
430
426
default :
431
- logger .error ("Unknown Yeelight command: " + action );
427
+ logger .error ("Unknown Yeelight command: {}" , action );
432
428
}
433
429
434
430
}
435
431
436
- private String sendYeelightGetPropCommand (String location ) {
437
- return sendYeelightCommand (location , GET_PROP , new Object []{"power" , "bright" , "ct" , "hue" , "sat" , "rgb" , "nl_br" });
432
+ private YeelightGetPropsResponse sendYeelightGetPropCommand (String location ) {
433
+ String result = sendYeelightCommand (location , GET_PROP , new Object []{"power" , "bright" , "ct" , "hue" , "sat" , "rgb" , "nl_br" });
434
+ logger .info ("location: {}, props: {}" , location , result );
435
+ return gson .fromJson (result , YeelightGetPropsResponse .class );
438
436
}
439
437
440
-
441
438
private String sendYeelightToggleCommand (String location ) {
442
439
return sendYeelightCommand (location , TOGGLE , new Object []{});
443
440
}
@@ -483,9 +480,8 @@ private String sendYeelightCommand(String location, String action, Object[] para
483
480
logger .debug ("Sending sentence: " + sentence );
484
481
outToServer .writeBytes (sentence );
485
482
return inFromServer .readLine ();
486
- //clientSocket.close();
487
483
} catch (NoRouteToHostException e ) {
488
- logger .debug ("Location " + location + " is probably offline" );
484
+ logger .debug ("Location {} is probably offline" , location );
489
485
if (action .equals (GET_PROP )) {
490
486
//update switches of not found location to OFF state
491
487
for (Object item : getOnSwitchItems (location )) {
0 commit comments