diff --git a/bundles/org.openhab.binding.vektiva/README.md b/bundles/org.openhab.binding.vektiva/README.md index 2cde6e85c8065..0b327d2eec5dc 100644 --- a/bundles/org.openhab.binding.vektiva/README.md +++ b/bundles/org.openhab.binding.vektiva/README.md @@ -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 - \ No newline at end of file diff --git a/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/handler/VektivaSmarwiHandler.java b/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/handler/VektivaSmarwiHandler.java index 6c213bc274eea..54162bbeb6119 100644 --- a/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/handler/VektivaSmarwiHandler.java +++ b/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/handler/VektivaSmarwiHandler.java @@ -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; @@ -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. @@ -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; @@ -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() { @@ -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); } @@ -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(); } @@ -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)) { @@ -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 @@ -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; } diff --git a/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/internal/VektivaHandlerFactory.java b/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/internal/VektivaHandlerFactory.java index 53f2d4792b1e9..b06814ad173e0 100644 --- a/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/internal/VektivaHandlerFactory.java +++ b/bundles/org.openhab.binding.vektiva/src/main/java/org/openhab/binding/vektiva/internal/VektivaHandlerFactory.java @@ -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 @@ -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 SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_SMARWI); /** @@ -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; }