Skip to content

Instantly share code, notes, and snippets.

@timothyb89
Last active August 29, 2015 14:02
Show Gist options
  • Save timothyb89/2fdad0f3e1ac1550dbb2 to your computer and use it in GitHub Desktop.
Save timothyb89/2fdad0f3e1ac1550dbb2 to your computer and use it in GitHub Desktop.
lifx-java bug
package org.timothyb89.lifx.java.test;
import java.io.IOException;
import org.timothyb89.eventbus.EventHandler;
import org.timothyb89.lifx.bulb.BulbStatusUpdatedEvent;
import org.timothyb89.lifx.bulb.LIFXColor;
import org.timothyb89.lifx.bulb.PowerState;
import org.timothyb89.lifx.gateway.Gateway;
import org.timothyb89.lifx.gateway.GatewayBulbDiscoveredEvent;
import org.timothyb89.lifx.gateway.GatewayDisconnectedEvent;
import org.timothyb89.lifx.net.BroadcastListener;
import org.timothyb89.lifx.net.GatewayDiscoveredEvent;
/**
* Simple test case to reproduce bulb action bug.
* Requires 2 bulbs connected to the same gateway, which output from this can
* also verify - only 1 "Found gateway:" message should be printed.
* See line #52 for actual failure location.
* @author tim
*/
public class Main {
public Main() throws IOException {
BroadcastListener listener = new BroadcastListener();
listener.bus().register(this);
listener.startListen();
}
@EventHandler
public void gatewayFound(GatewayDiscoveredEvent ev) {
Gateway g = ev.getGateway();
System.out.println("Found gateway: " + g);
g.bus().register(this);
try {
g.connect();
} catch (IOException ex) {}
}
@EventHandler
public void bulbDiscovered(GatewayBulbDiscoveredEvent event) throws IOException {
event.getBulb().bus().register(this);
System.out.println("Discovered: " + event.getBulb()
+ " (parent: " + event.getGateway() + ")");
// will attempt to turn on a bulb named "Lamp"
// expected result: single bulb named "Lamp" will turn on
// actual result: all bulbs on gateway turn on
if (event.getBulb().getLabel().equalsIgnoreCase("Lamp")) {
event.getBulb().setPowerState(PowerState.ON);
System.out.println("Powered on");
}
}
@EventHandler
public void bulbUpdated(BulbStatusUpdatedEvent event) {
System.out.println("Updated: " + event.getBulb());
}
@EventHandler
public void gatewayDisconnected(GatewayDisconnectedEvent event) {
System.out.println("Disconnected from " + event.getGateway()
+ ", reconnecting...");
try {
event.getGateway().connect();
} catch (IOException ex) {
System.err.println("Couldn't reconnect");
}
}
public static void main(String[] args) throws IOException {
new Main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment