Skip to content

Commit

Permalink
Cleans up our button handler lifecycle. (openhab#2437)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored and martinvw committed Jul 16, 2017
1 parent 40f1c10 commit b910739
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyHubLi

private static final int HEARTBEAT_INTERVAL = 30;

private ScheduledExecutorService buttonExecutor = Executors.newSingleThreadScheduledExecutor();
private ScheduledExecutorService buttonExecutor;

private List<HubStatusListener> listeners = new CopyOnWriteArrayList<HubStatusListener>();

Expand Down Expand Up @@ -124,14 +124,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@Override
public void initialize() {
buttonExecutor = Executors.newSingleThreadScheduledExecutor();
cancelRetry();
connect();
}

@Override
public void dispose() {
listeners.clear();
buttonExecutor.shutdownNow();
listeners.clear();
cancelRetry();
disconnectFromHub();
factory.removeChannelTypesForThing(getThing().getUID());
Expand Down Expand Up @@ -331,14 +332,16 @@ public HarmonyClient getClient() {
* @param button
*/
public void pressButton(int device, String button) {
buttonExecutor.execute(new Runnable() {
@Override
public void run() {
if (client != null) {
client.pressButton(device, button);
if (buttonExecutor != null && !buttonExecutor.isShutdown()) {
buttonExecutor.execute(new Runnable() {
@Override
public void run() {
if (client != null) {
client.pressButton(device, button);
}
}
}
});
});
}
}

/**
Expand All @@ -348,14 +351,16 @@ public void run() {
* @param button
*/
public void pressButton(String device, String button) {
buttonExecutor.execute(new Runnable() {
@Override
public void run() {
if (client != null) {
client.pressButton(device, button);
if (buttonExecutor != null && !buttonExecutor.isShutdown()) {
buttonExecutor.execute(new Runnable() {
@Override
public void run() {
if (client != null) {
client.pressButton(device, button);
}
}
}
});
});
}
}

/**
Expand Down

0 comments on commit b910739

Please sign in to comment.