Skip to content

Commit

Permalink
Changed to usign new websocket based library
Browse files Browse the repository at this point in the history
Signed-off-by: digitaldan <[email protected]>
  • Loading branch information
digitaldan committed Dec 27, 2018
1 parent adae151 commit 863c4b7
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 125 deletions.
1 change: 1 addition & 0 deletions addons/binding/org.openhab.binding.harmonyhub/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<classpathentry kind="lib" path="lib/smack-tcp-4.2.0.jar"/>
<classpathentry kind="lib" path="lib/xpp3-1.1.4c.jar"/>
<classpathentry kind="lib" path="lib/harmony-java-client-1.2.2.jar"/>
<classpathentry kind="lib" path="lib/harmony-client-1.0-SNAPSHOT.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
28 changes: 15 additions & 13 deletions addons/binding/org.openhab.binding.harmonyhub/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@ Automatic-Module-Name: org.openhab.binding.harmonyhub
Bundle-ActivationPolicy: lazy
Bundle-ClassPath:
.,
lib/harmony-java-client-1.2.2.jar,
lib/jackson-annotations-2.8.0.jar,
lib/jackson-core-2.8.7.jar,
lib/jackson-databind-2.8.7.jar,
lib/jxmpp-core-0.5.0.jar,
lib/jxmpp-jid-0.5.0.jar,
lib/jxmpp-util-cache-0.5.0.jar,
lib/smack-core-4.2.0.jar,
lib/smack-java7-4.2.0.jar,
lib/smack-resolver-javax-4.2.0.jar,
lib/smack-sasl-javax-4.2.0.jar,
lib/smack-tcp-4.2.0.jar,
lib/xpp3-1.1.4c.jar
lib/harmony-client-1.0-SNAPSHOT.jar
Bundle-ManifestVersion: 2
Bundle-Name: HarmonyHub Binding
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Expand All @@ -26,10 +14,24 @@ Import-Package:
com.google.common.base,
com.google.common.collect,
com.google.common.io,
com.google.gson,
com.google.gson.annotations,
com.google.inject,
javax.inject,
org.apache.commons.lang,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.jetty.client,
org.eclipse.jetty.client.api,
org.eclipse.jetty.client.util,
org.eclipse.jetty.http,
org.eclipse.jetty.io,
org.eclipse.jetty.server.session,
org.eclipse.jetty.util.component,
org.eclipse.jetty.util.ssl,
org.eclipse.jetty.websocket.api,
org.eclipse.jetty.websocket.client,
org.eclipse.jetty.websocket.common,
org.eclipse.jetty.websocket.common.scopes,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.config.discovery,
org.eclipse.smarthome.core.cache,
Expand Down
14 changes: 1 addition & 13 deletions addons/binding/org.openhab.binding.harmonyhub/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ bin.includes = META-INF/,\
.,\
OSGI-INF/,\
ESH-INF/,\
lib/harmony-java-client-1.2.2.jar,\
lib/jackson-annotations-2.8.0.jar,\
lib/jackson-core-2.8.7.jar,\
lib/jackson-databind-2.8.7.jar,\
lib/jxmpp-core-0.5.0.jar,\
lib/jxmpp-jid-0.5.0.jar,\
lib/jxmpp-util-cache-0.5.0.jar,\
lib/smack-core-4.2.0.jar,\
lib/smack-java7-4.2.0.jar,\
lib/smack-resolver-javax-4.2.0.jar,\
lib/smack-sasl-javax-4.2.0.jar,\
lib/smack-tcp-4.2.0.jar,\
lib/xpp3-1.1.4c.jar,\
lib/harmony-client-1.0-SNAPSHOT.jar, \
about.html

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.whistlingfish.harmony.config.Device;
import net.whistlingfish.harmony.config.HarmonyConfig;
import com.digitaldan.harmony.config.Device;
import com.digitaldan.harmony.config.HarmonyConfig;

/**
* The {@link HarmonyDeviceDiscoveryService} class discovers Harmony Devices connected to a Harmony Hub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.whistlingfish.harmony.config.ControlGroup;
import net.whistlingfish.harmony.config.Device;
import net.whistlingfish.harmony.config.Function;
import net.whistlingfish.harmony.config.HarmonyConfig;
import com.digitaldan.harmony.config.ControlGroup;
import com.digitaldan.harmony.config.Device;
import com.digitaldan.harmony.config.Function;
import com.digitaldan.harmony.config.HarmonyConfig;

/**
* The {@link HarmonyDeviceHandler} is responsible for handling commands for Harmony Devices, which are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -52,12 +49,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.whistlingfish.harmony.ActivityChangeListener;
import net.whistlingfish.harmony.ActivityStatusListener;
import net.whistlingfish.harmony.HarmonyClient;
import net.whistlingfish.harmony.HarmonyHubListener;
import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.config.HarmonyConfig;
import com.digitaldan.harmony.HarmonyClient;
import com.digitaldan.harmony.HarmonyClientListener;
import com.digitaldan.harmony.config.Activity;
import com.digitaldan.harmony.config.Activity.Status;
import com.digitaldan.harmony.config.HarmonyConfig;

/**
* The {@link HarmonyHubHandler} is responsible for handling commands for Harmony Hubs, which are
Expand All @@ -68,7 +64,7 @@
* @author Wouter Born - Add null annotations
*/
@NonNullByDefault
public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyHubListener {
public class HarmonyHubHandler extends BaseBridgeHandler {

private final Logger logger = LoggerFactory.getLogger(HarmonyHubHandler.class);

Expand All @@ -87,13 +83,15 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyHubLi
private final ExpiringCacheAsync<@Nullable HarmonyConfig> configCache = new ExpiringCacheAsync<>(CONFIG_CACHE_TIME);
private final HarmonyHubHandlerFactory factory;

private @NonNullByDefault({}) ScheduledExecutorService buttonExecutor;
// private @NonNullByDefault({}) ScheduledExecutorService buttonExecutor;
private @NonNullByDefault({}) HarmonyHubConfig config;

private @Nullable HarmonyClient client;
private @Nullable ScheduledFuture<?> retryJob;
private @Nullable ScheduledFuture<?> heartBeatJob;

private @Nullable HarmonyClientListener harmonyClientListener;

private int heartBeatInterval;

public HarmonyHubHandler(Bridge bridge, HarmonyHubHandlerFactory factory) {
Expand All @@ -112,7 +110,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

if (command instanceof RefreshType) {
updateState(localClient.getCurrentActivity());
localClient.getCurrentActivity().thenAccept(activity -> {
updateState(activity);
});
} else if (command instanceof StringType) {
try {
try {
Expand All @@ -139,7 +139,6 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@Override
public void initialize() {
buttonExecutor = Executors.newSingleThreadScheduledExecutor();
config = getConfigAs(HarmonyHubConfig.class);
cancelRetry();
updateStatus(ThingStatus.UNKNOWN);
Expand All @@ -148,7 +147,6 @@ public void initialize() {

@Override
public void dispose() {
buttonExecutor.shutdownNow();
listeners.clear();
cancelRetry();
disconnectFromHub();
Expand All @@ -168,41 +166,12 @@ protected void updateStatus(ThingStatus status, ThingStatusDetail detail, @Nulla
public void channelLinked(ChannelUID channelUID) {
HarmonyClient localClient = client;
if (localClient != null) {
updateState(channelUID, new StringType(localClient.getCurrentActivity().getLabel()));
localClient.getCurrentActivity().thenAccept((activity) -> {
updateState(channelUID, new StringType(activity.getLabel()));
});
}
}

/**
* HarmonyHubListener interface
*/
@Override
public void removeFrom(@Nullable HarmonyClient hc) {
// we have been removed from listening
}

/**
* HarmonyHubListener interface
*/
@Override
public void addTo(@Nullable HarmonyClient hc) {
if (hc == null) {
logger.warn("Cannot add listeners to HarmonyClient that is null");
return;
}
hc.addListener(new ActivityChangeListener() {
@Override
public void activityStarted(@Nullable Activity activity) {
updateState(activity);
}
});
hc.addListener(new ActivityStatusListener() {
@Override
public void activityStatusChanged(@Nullable Activity activity, Activity.@Nullable Status status) {
updateActivityStatus(activity, status);
}
});
}

/**
* Starts the connection process
*/
Expand All @@ -229,27 +198,55 @@ private synchronized void connect() {
}
}

HarmonyClient localClient = HarmonyClient.getInstance();
localClient.addListener(this);
client = localClient;
HarmonyClient localClient = new HarmonyClient();
harmonyClientListener = new HarmonyClientListener() {

@Override
public void hubDisconnected(@Nullable String reason) {
setOfflineAndReconnect(String.format("Could not connect: %s", reason));

}

@Override
public void hubConnected() {
client = localClient;
heartBeatJob = scheduler.scheduleWithFixedDelay(() -> {
try {
client.sendPing();
} catch (Exception e) {
logger.warn("heartbeat failed", e);
setOfflineAndReconnect("Hearbeat failed");
}
}, heartBeatInterval, heartBeatInterval, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
getConfigFuture()
.thenAcceptAsync(harmonyConfig -> updateCurrentActivityChannel(harmonyConfig), scheduler)
.exceptionally(e -> {
setOfflineAndReconnect("Getting config failed: " + e.getMessage());
return null;
});

}

@Override
public void activityStatusChanged(@Nullable Activity activity, @Nullable Status status) {
updateActivityStatus(activity, status);

}

@Override
public void activityStarted(@Nullable Activity activity) {
updateState(activity);

}
};

localClient.addListener(harmonyClientListener);

try {
logger.debug("Connecting: host {}", host);
localClient.connect(host);
heartBeatJob = scheduler.scheduleWithFixedDelay(() -> {
try {
localClient.sendPing();
} catch (Exception e) {
logger.warn("heartbeat failed", e);
setOfflineAndReconnect("Hearbeat failed");
}
}, heartBeatInterval, heartBeatInterval, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
getConfigFuture().thenAcceptAsync(harmonyConfig -> updateCurrentActivityChannel(harmonyConfig), scheduler)
.exceptionally(e -> {
setOfflineAndReconnect("Getting config failed: " + e.getMessage());
return null;
});

} catch (Exception e) {
logger.debug("Could not connect to HarmonyHub at {}", host, e);
setOfflineAndReconnect("Could not connect: " + e.getMessage());
Expand All @@ -264,7 +261,7 @@ private void disconnectFromHub() {

HarmonyClient localClient = client;
if (localClient != null) {
localClient.removeListener(this);
localClient.removeListener(harmonyClientListener);
localClient.disconnect();
}
}
Expand Down Expand Up @@ -386,12 +383,8 @@ private void updateCurrentActivityChannel(@Nullable HarmonyConfig config) {
* @param button
*/
public void pressButton(int device, String button) {
if (!buttonExecutor.isShutdown()) {
buttonExecutor.execute(() -> {
if (client != null) {
client.pressButton(device, button);
}
});
if (client != null) {
client.pressButton(device, button);
}
}

Expand All @@ -402,33 +395,17 @@ public void pressButton(int device, String button) {
* @param button
*/
public void pressButton(String device, String button) {
if (!buttonExecutor.isShutdown()) {
buttonExecutor.execute(() -> {
if (client != null) {
client.pressButton(device, button);
}
});
if (client != null) {
client.pressButton(device, button);
}
}

public CompletableFuture<@Nullable HarmonyConfig> getConfigFuture() {
Supplier<@Nullable HarmonyConfig> configSupplier = () -> {
HarmonyClient localClient = client;
if (localClient == null) {
throw new IllegalStateException("Client is null");
}
try {
logger.debug("Getting config from client");
return localClient.getConfig();
} catch (Exception e) {
logger.debug("Could not get config from client", e);
throw e;
}
};

return configCache.getValue(() -> {
return CompletableFuture.supplyAsync(configSupplier, scheduler);
});
HarmonyClient localClient = client;
if (localClient == null) {
throw new IllegalStateException("Client is null");
}
return localClient.getConfig();
}

/**
Expand Down

0 comments on commit 863c4b7

Please sign in to comment.