Skip to content

Commit d5494c1

Browse files
committed
Code cleanup & optimalization
1 parent 8734aa5 commit d5494c1

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.openhab.bundles</groupId>
77
<artifactId>binding</artifactId>
8-
<version>1.10.0-SNAPSHOT</version>
8+
<version>1.11.0-SNAPSHOT</version>
99
</parent>
1010

1111
<properties>

Diff for: src/main/java/org/openhab/binding/yeelight/internal/YeelightBinding.java

+30-34
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
*/
99
package org.openhab.binding.yeelight.internal;
1010

11-
import com.google.gson.JsonElement;
12-
import com.google.gson.JsonObject;
13-
import com.google.gson.JsonParser;
11+
import com.google.gson.Gson;
1412
import org.apache.commons.lang.StringUtils;
1513
import org.openhab.binding.yeelight.YeelightBindingProvider;
14+
import org.openhab.binding.yeelight.model.YeelightGetPropsResponse;
1615
import org.openhab.core.binding.AbstractActiveBinding;
1716
import org.openhab.core.items.ItemNotFoundException;
1817
import org.openhab.core.items.ItemRegistry;
@@ -72,8 +71,8 @@ public class YeelightBinding extends AbstractActiveBinding<YeelightBindingProvid
7271
//Socket
7372
private MulticastSocket socket = null;
7473

75-
//Gson parser
76-
private JsonParser parser = new JsonParser();
74+
//Gson
75+
private Gson gson = new Gson();
7776

7877
//devices
7978
Hashtable<String, YeelightDevice> devices;
@@ -161,7 +160,7 @@ private void receiveData(MulticastSocket socket, DatagramPacket dgram) {
161160
String sentence = new String(dgram.getData(), 0,
162161
dgram.getLength());
163162

164-
logger.debug("Yeelight received packet: " + sentence);
163+
logger.debug("Yeelight received packet: {}", sentence);
165164

166165
if (isOKPacket(sentence) || isNotifyPacket(sentence)) {
167166
String[] lines = sentence.split("\n");
@@ -185,7 +184,7 @@ else if (line.startsWith("support: "))
185184
if (!id.equals("") && !devices.containsKey(id)) {
186185
YeelightDevice device = new YeelightDevice(id, location, model, support);
187186
devices.put(id, device);
188-
logger.info("Found Yeelight device :\n" + device.toString());
187+
logger.info("Found Yeelight device :\n{}", device.toString());
189188
}
190189
}
191190
}
@@ -217,9 +216,9 @@ private void discoverYeelightDevices() {
217216

218217
socket.send(sendPacket);
219218
} catch (MalformedURLException e) {
220-
logger.error("The URL '" + url + "' is malformed: " + e.toString());
219+
logger.error("The URL '{}' is malformed: ", url, e);
221220
} catch (Exception e) {
222-
logger.error("Cannot send Yeelight datagram packet: " + e.toString());
221+
logger.error("Cannot get Yeelight login cookie: ", e);
223222
}
224223
}
225224

@@ -285,7 +284,7 @@ protected void execute() {
285284
return;
286285
}
287286

288-
Hashtable<String, String> propList = new Hashtable<>();
287+
Hashtable<String, YeelightGetPropsResponse> propList = new Hashtable<>();
289288

290289
//devices.clear();
291290
discoverYeelightDevices();
@@ -302,14 +301,14 @@ protected void execute() {
302301
continue;
303302

304303
String location = config.getLocation();
305-
String result;
304+
YeelightGetPropsResponse result;
306305

307306
if (!propList.containsKey(location)) {
308307
result = sendYeelightGetPropCommand(location);
309308
if (result == null)
310309
continue;
311310
propList.put(location, result);
312-
logger.debug("Result: " + result);
311+
logger.debug("Cached location: {}", location);
313312
} else {
314313
result = propList.get(location);
315314
}
@@ -319,57 +318,54 @@ protected void execute() {
319318
}
320319
}
321320

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();
324323
State newState = null;
325324
State oldState = null;
326325
try {
327326
switch (action) {
328327
case SET_POWER:
329-
String power = getJSONArrayResult(jo, 0).getAsString();
328+
String power = result.getResult().get(0);
330329
newState = power.equals("on") ? OnOffType.ON : OnOffType.OFF;
331330
break;
332331
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));
334333
newState = new PercentType(bright == 1 ? 0 : bright);
335334
break;
336335
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+
;
338338
newState = new PercentType((ct - 1700) / 48);
339339
break;
340340
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));
344344
newState = new HSBType(new DecimalType(hue), new PercentType(sat), new PercentType(br == 1 ? 0 : br));
345345
break;
346346
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));
348348
Color col = getRGBColor(rgb);
349349
newState = new HSBType(col);
350350
break;
351351
case NIGHTLIGHT:
352-
String status = getJSONArrayResult(jo, 6).getAsString();
352+
String status = result.getResult().get(6);
353353
newState = (status.equals("0") || status.equals("")) ? OnOffType.OFF : OnOffType.ON;
354354
break;
355355
default:
356-
logger.error("Unknown Yeelight action: " + action);
356+
logger.error("Unknown Yeelight action: {}", action);
357357

358358
}
359359

360360
oldState = itemRegistry.getItem(itemName).getState();
361361
} catch (ItemNotFoundException e) {
362362
logger.error(e.toString());
363363
}
364-
if (!oldState.equals(newState)) {
364+
if (oldState == null || !oldState.equals(newState)) {
365365
eventPublisher.postUpdate(itemName, newState);
366366
}
367367
}
368368

369-
private JsonElement getJSONArrayResult(JsonObject jo, int pos) {
370-
return jo.get(RESULT).getAsJsonArray().get(pos);
371-
}
372-
373369
/**
374370
* @{inheritDoc}
375371
*/
@@ -428,16 +424,17 @@ protected void internalReceiveCommand(String itemName, Command command) {
428424
}
429425
break;
430426
default:
431-
logger.error("Unknown Yeelight command: " + action);
427+
logger.error("Unknown Yeelight command: {}", action);
432428
}
433429

434430
}
435431

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);
438436
}
439437

440-
441438
private String sendYeelightToggleCommand(String location) {
442439
return sendYeelightCommand(location, TOGGLE, new Object[]{});
443440
}
@@ -483,9 +480,8 @@ private String sendYeelightCommand(String location, String action, Object[] para
483480
logger.debug("Sending sentence: " + sentence);
484481
outToServer.writeBytes(sentence);
485482
return inFromServer.readLine();
486-
//clientSocket.close();
487483
} catch (NoRouteToHostException e) {
488-
logger.debug("Location " + location + " is probably offline");
484+
logger.debug("Location {} is probably offline", location);
489485
if (action.equals(GET_PROP)) {
490486
//update switches of not found location to OFF state
491487
for (Object item : getOnSwitchItems(location)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.openhab.binding.yeelight.model;
2+
3+
import java.util.List;
4+
5+
public class YeelightGetPropsResponse {
6+
private int id;
7+
List<String> result;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public List<String> getResult() {
14+
return result;
15+
}
16+
}

0 commit comments

Comments
 (0)