Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled shared HttpClient and WebSocket; Fixed minor SAT findings; #3

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bundles/org.openhab.binding.vektiva/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ Default item=SmarwiD
## Note

This binding currently does not support controlling via vektiva.online cloud service and uses local device API, which is described here: https://vektiva.gitlab.io/vektivadocs/api/api.html

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
*/
package org.openhab.binding.vektiva.handler;

import static org.openhab.binding.vektiva.VektivaBindingConstants.*;

import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
Expand All @@ -32,11 +41,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.concurrent.*;

import static org.openhab.binding.vektiva.VektivaBindingConstants.*;

/**
* The {@link VektivaSmarwiHandler} is responsible for handling commands, which are
* sent to one of the channels.
Expand All @@ -50,11 +54,9 @@ public class VektivaSmarwiHandler extends BaseThingHandler {

private VektivaSmarwiConfiguration config = new VektivaSmarwiConfiguration();

private final @NonNullByDefault({})
HttpClient httpClient;
private final HttpClient httpClient;

private final @NonNullByDefault({})
WebSocketClient webSocketClient;
private final WebSocketClient webSocketClient;

private @Nullable Session session;

Expand Down Expand Up @@ -97,21 +99,7 @@ public void dispose() {
if (future != null && !(future.isCancelled() || future.isDone())) {
future.cancel(true);
}
if (httpClient.isStarted()) {
try {
httpClient.stop();
} catch (Exception e) {
//silence
}
}
closeSession();
if (webSocketClient.isStarted()) {
try {
webSocketClient.stop();
} catch (Exception e) {
//silence
}
}
}

private void closeSession() {
Expand Down Expand Up @@ -165,14 +153,6 @@ public void initialize() {
config = getConfigAs(VektivaSmarwiConfiguration.class);
logger.debug("IP address: {}", config.ip);

try {
httpClient.start();
} catch (Exception e) {
logger.debug("Cannot start http client!", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Cannot start http client!");
return;
}

future = scheduler.scheduleWithFixedDelay(this::checkStatus, 0, config.refreshInterval, TimeUnit.SECONDS);
}

Expand All @@ -195,10 +175,12 @@ private void checkStatus() {
if (resp.getStatus() == 200) {
processStatusResponse(resp.getContentAsString());
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "got response code: " + resp.getStatus());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"got response code: " + resp.getStatus());
}
// reconnect web socket if not connected
if (config.useWebSockets && (session == null || !session.isOpen()) && ThingStatus.ONLINE.equals(getThing().getStatus())) {
if (config.useWebSockets && (session == null || !session.isOpen())
&& ThingStatus.ONLINE.equals(getThing().getStatus())) {
logger.debug("Initializing WebSocket session");
initializeWebSocketSession();
}
Expand Down Expand Up @@ -239,7 +221,9 @@ public synchronized void processStatusResponse(String content) {
private String getPropertyValue(String[] values, String property) {
for (String val : values) {
String[] keyVal = val.split(":");
if (keyVal.length != 2) continue;
if (keyVal.length != 2) {
continue;
}
String key = keyVal[0];
String value = keyVal[1];
if (property.equals(key)) {
Expand All @@ -254,7 +238,6 @@ private String getPropertyValue(String[] values, String property) {
URI uri = URI.create(url);

try {
webSocketClient.start();
// The socket that receives events
VektivaSmarwiSocket socket = new VektivaSmarwiSocket(this);
// Attempt Connect
Expand All @@ -263,7 +246,8 @@ private String getPropertyValue(String[] values, String property) {
return fut.get();
} catch (Exception ex) {
logger.debug("Cannot create websocket client/session", ex);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Cannot create websocket client/session");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Cannot create websocket client/session");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,25 @@
*/
package org.openhab.binding.vektiva.internal;

import static org.openhab.binding.vektiva.VektivaBindingConstants.*;
import static org.openhab.binding.vektiva.VektivaBindingConstants.THING_TYPE_SMARWI;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.eclipse.smarthome.io.net.http.WebSocketFactory;
import org.openhab.binding.vektiva.handler.VektivaSmarwiHandler;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.eclipse.smarthome.io.net.http.WebSocketFactory;
import org.openhab.binding.vektiva.handler.VektivaSmarwiHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.Null;

/**
* The {@link VektivaHandlerFactory} is responsible for creating things and thing
Expand All @@ -46,8 +42,6 @@
@Component(configurationPid = "binding.vektiva", service = ThingHandlerFactory.class)
public class VektivaHandlerFactory extends BaseThingHandlerFactory {

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

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_SMARWI);

/**
Expand Down Expand Up @@ -78,12 +72,10 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {

@Reference
protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
logger.debug("setHttpClientFactory");
this.httpClient = httpClientFactory.getCommonHttpClient();
}

protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
logger.debug("unsetHttpClientFactory");
this.httpClient = null;
}

Expand Down