Skip to content

Commit

Permalink
added additional logging, and use a timer for gateway discovery (see …
Browse files Browse the repository at this point in the history
…issues #1

and #4)
  • Loading branch information
timothyb89 committed May 29, 2014
1 parent b947ac7 commit 027a33c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
</intent-filter>
</receiver>

<service android:name=".LIFXService_"/>
<service android:name=".ReceiverService_"/>
<service android:name=".LIFXService_" android:exported="false"/>
<service android:name=".ReceiverService_" android:exported="false"/>
</application>

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/timothyb89/lifx/tasker/FireReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public void onReceive(Context context, Intent intent) {
return;
}

log.info("Received Tasker event!");

Bundle bundle = intent.getBundleExtra(
com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);

Intent wrapped = new Intent(context, ReceiverService_.class);
wrapped.putExtra(KEY_BUNDLE, bundle);

context.startService(wrapped);
}



}
129 changes: 58 additions & 71 deletions src/main/java/org/timothyb89/lifx/tasker/LIFXService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.UiThread;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -29,7 +31,6 @@
import org.timothyb89.lifx.bulb.PowerState;
import org.timothyb89.lifx.gateway.Gateway;
import org.timothyb89.lifx.gateway.GatewayBulbDiscoveredEvent;
import org.timothyb89.lifx.gateway.GatewayManager;
import org.timothyb89.lifx.net.BroadcastListener;
import org.timothyb89.lifx.net.GatewayDiscoveredEvent;

Expand All @@ -45,8 +46,9 @@ public class LIFXService extends Service implements EventBusProvider {
public static final int DISCOVERY_ATTEMPTS = 5;
public static final long DISCOVERY_WAIT = 100; // milliseconds
public static final long DISCOVERY_WAIT_SMALL = 250;
public static final long DISCOVERY_WAIT_LONG = 5000;

public static final long DEFAULT_PULSE_DELAY = 1000;
public static final long DEFAULT_PULSE_DELAY = 1500;

private LIFXBinder binder;

Expand Down Expand Up @@ -82,14 +84,7 @@ public void onCreate() {
// todo: listen for network state events and clear the list of gateways
// on network change

try {
listener.startListen();
} catch (BindException ex) {
log.error("Unable to bind to LIFX port.", ex);
showToast(getString(R.string.service_bind_failed));
} catch (IOException ex) {
log.error("Unable to bind to lifx udp port", ex);
}
timedListen();
}

@Override
Expand All @@ -114,14 +109,44 @@ protected void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

private void timedListen() {
if (listener.isListening()) {
return;
}

try {
log.info("Starting gateway discovery...");
listener.startListen();

new Timer().schedule(new TimerTask() {

@Override
public void run() {
try {
listener.stopListen();
log.info(
"Gateway discovery ended, {} gateways found.",
gateways.size());
} catch (IOException ex) {
log.error("Unable to stop listener", ex);
}
}

}, DISCOVERY_WAIT_LONG);
} catch (BindException ex) {
log.error("Unable to bind to LIFX port.", ex);
showToast(getString(R.string.service_bind_failed));
} catch (IOException ex) {
log.error("Unable to listen for gateways", ex);
}
}

@EventHandler
public void gatewayDiscovered(GatewayDiscoveredEvent event) {
// stop listening after the first gateway for now
log.info("Found gateway: {}", event.getGateway());

synchronized (gateways) {
gateways.add(event.getGateway());
}
gateways.add(event.getGateway());

// connect to the gateway and try to discover bulbs
try {
Expand All @@ -130,15 +155,6 @@ public void gatewayDiscovered(GatewayDiscoveredEvent event) {
} catch (IOException ex) {
log.error("Unable to connect to gateway", ex);
}

// also stop listening
// this means we'll only ever discover one gateway (for now)
// TODO: be smarter about this
try {
listener.stopListen();
} catch (IOException ex) {
// ignore
}
}

@EventHandler
Expand Down Expand Up @@ -188,19 +204,18 @@ private List<Gateway> waitForGateways() {
return ret;
}

try {
listener.startListen();
timedListen();

Thread.sleep(DISCOVERY_WAIT_SMALL);
for (int i = 0; i < DISCOVERY_ATTEMPTS; i++) {
try {
Thread.sleep(DISCOVERY_WAIT);
} catch (InterruptedException ex) {
// ignore
}

listener.stopListen();
} catch (BindException ex) {
log.error("Unable to bind to LIFX port.", ex);
showToast(getString(R.string.service_bind_failed));
} catch (IOException ex) {
log.error("Unable to listen for gateways", ex);
} catch (InterruptedException ex) {
// ignore
if (!gateways.isEmpty()) {
break;
}
}

return getAvailableGateways();
Expand All @@ -213,6 +228,8 @@ private Bulb bulbSearch(String name) {
Arrays.toString(bulbs.toArray()));

for (Bulb b : bulbs) {
// TODO: add support for MAC address

if (b.getLabel().equalsIgnoreCase(name)) {
return b;
}
Expand Down Expand Up @@ -254,16 +271,7 @@ private Bulb findBulb(String name) {
// mainly we'll be waiting for discovery here

// also start listening for new gateways, in case we missed one
if (!listener.isListening()) {
try {
listener.startListen();
} catch (BindException ex) {
log.error("Unable to bind to LIFX port.", ex);
showToast(getString(R.string.service_bind_failed));
} catch (IOException ex) {
log.error("Unable to start listening", ex);
}
}
timedListen();

Bulb bulb = null;
for (int i = 0; i < DISCOVERY_ATTEMPTS; i++) {
Expand All @@ -274,18 +282,12 @@ private Bulb findBulb(String name) {
}

try {
Thread.sleep(DISCOVERY_WAIT);
Thread.sleep(DISCOVERY_WAIT_SMALL);
} catch (InterruptedException ex) {
// ignore
}
}

try {
listener.stopListen();
} catch (IOException ex) {
// ignore
}

// try one more time
if (bulb == null) {
bulb = bulbSearch(name);
Expand Down Expand Up @@ -313,22 +315,11 @@ private Bulb findBulb(String name) {

private List<Bulb> findBulbs(String[] bulbs) {
List<String> remaining = new ArrayList<>();
for (String bulb : bulbs) {
remaining.add(bulb);
}
remaining.addAll(Arrays.asList(bulbs));

List<Bulb> ret = new ArrayList<>();

if (!listener.isListening()) {
try {
listener.startListen();
} catch (BindException ex) {
log.error("Unable to bind to LIFX port.", ex);
showToast(getString(R.string.service_bind_failed));
} catch (IOException ex) {
log.error("Unable to start listening", ex);
}
}
timedListen();

for (int i = 0; i < DISCOVERY_ATTEMPTS; i++) {
ret.addAll(bulbSearch(remaining));
Expand All @@ -345,12 +336,6 @@ private List<Bulb> findBulbs(String[] bulbs) {
}
}

try {
listener.stopListen();
} catch (IOException ex) {
// ignore
}

// try one more time
if (!remaining.isEmpty()) {
ret.addAll(bulbSearch(remaining));
Expand Down Expand Up @@ -468,6 +453,8 @@ public void toggle(String bulbName) {
}

public void toggle(String[] bulbNames) {
log.info("Attempting toggle on: {}", Arrays.toString(bulbNames));

for (Bulb bulb : findBulbs(bulbNames)) {
log.info("Toggling {}", bulb);

Expand Down Expand Up @@ -516,9 +503,9 @@ public void setColor(String[] bulbNames, int color) {
public void pulse(String[] bulbNames, int color) {
List<Bulb> bulbs = findBulbs(bulbNames);

int red = Color.red(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int blue = Color.blue(color);
LIFXColor c = LIFXColor.fromRGB(red, green, blue);

Map<Bulb, LIFXColor> initialColors = new HashMap<>();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/timothyb89/lifx/tasker/ReceiverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public ReceiverService() {
protected void onHandleIntent(Intent intent) {
bundle = intent.getBundleExtra(FireReceiver.KEY_BUNDLE);

log.info("Attempting to start LIFX service...");

startService(new Intent(this, LIFXService_.class));
bindService(
new Intent(this, LIFXService_.class),
Expand Down Expand Up @@ -72,6 +74,8 @@ protected void process(LIFXService lifx) {
String actionId = bundle.getString(FireReceiver.KEY_ACTION);
Action action = Action.getAction(actionId);

log.debug("Processing action: {}", action);

switch (action) {
case POWER_ON: lifx.turnOn( getBulbs()); break;
case POWER_OFF: lifx.turnOff( getBulbs()); break;
Expand All @@ -90,6 +94,8 @@ protected void process(LIFXService lifx) {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
log.info("LIFX service started.");

LIFXService lifx = ((LIFXService.LIFXBinder) service).getService();

process(lifx);
Expand Down

0 comments on commit 027a33c

Please sign in to comment.