From 7f389f8ce93f4138440bddcf49b439a1274eb682 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sun, 27 Aug 2023 07:27:00 +0200 Subject: [PATCH] [autelis] Fix checkstyle en warnings (#15423) Signed-off-by: lsiepel --- .../internal/config/AutelisConfiguration.java | 14 +++-- .../internal/handler/AutelisHandler.java | 52 +++++++------------ 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/config/AutelisConfiguration.java b/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/config/AutelisConfiguration.java index 2af92d21a32e5..b4ba627b5276e 100644 --- a/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/config/AutelisConfiguration.java +++ b/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/config/AutelisConfiguration.java @@ -12,35 +12,39 @@ */ package org.openhab.binding.autelis.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Configuration properties for connecting to an Autelis Controller * * @author Dan Cunningham - Initial contribution * */ +@NonNullByDefault public class AutelisConfiguration { /** * Host of the Autelis controller */ - public String host; + public String host = ""; + /** * port of the Autelis controller */ - public Integer port; + public int port = 80; /** * user to us when connecting to the Autelis controller */ - public String user; + public String user = ""; /** * password to us when connecting to the Autelis controller */ - public String password; + public String password = ""; /** * Rate we poll for new data */ - public Integer refresh; + public int refresh = 5; } diff --git a/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/handler/AutelisHandler.java b/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/handler/AutelisHandler.java index 629395f6ba772..010371d04505a 100644 --- a/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/handler/AutelisHandler.java +++ b/bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/handler/AutelisHandler.java @@ -29,6 +29,8 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; @@ -71,6 +73,7 @@ * @author Dan Cunningham - Initial contribution * @author Svilen Valkanov - Replaced Apache HttpClient with Jetty */ +@NonNullByDefault public class AutelisHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(AutelisHandler.class); @@ -110,11 +113,6 @@ public class AutelisHandler extends BaseThingHandler { */ private static final int THROTTLE_TIME_MILLISECONDS = 500; - /** - * Autelis web port - */ - private static final int WEB_PORT = 80; - /** * Pentair values for pump response */ @@ -133,12 +131,7 @@ public class AutelisHandler extends BaseThingHandler { /** * Constructed URL consisting of host and port */ - private String baseURL; - - /** - * Our poll rate - */ - private int refresh; + private String baseURL = ""; /** * The http client used for polling requests @@ -153,7 +146,7 @@ public class AutelisHandler extends BaseThingHandler { /** * Authentication for login */ - private String basicAuthentication; + private String basicAuthentication = ""; /** * Regex expression to match XML responses from the Autelis, this is used to @@ -165,7 +158,7 @@ public class AutelisHandler extends BaseThingHandler { /** * Future to poll for updated */ - private ScheduledFuture pollFuture; + private @Nullable ScheduledFuture pollFuture; public AutelisHandler(Thing thing) { super(thing); @@ -221,8 +214,8 @@ public void handleCommand(ChannelUID channelUID, Command command) { value = 0; } else if (command == OnOffType.ON) { value = 1; - } else if (command instanceof DecimalType) { - value = ((DecimalType) command).intValue(); + } else if (command instanceof DecimalType commandAsDecimalType) { + value = commandAsDecimalType.intValue(); if (!isJandy() && value >= 3) { // this is an autelis dim type. not sure what 2 does cmd = "dim"; @@ -289,37 +282,27 @@ private void configure() { clearPolling(); AutelisConfiguration configuration = getConfig().as(AutelisConfiguration.class); - Integer refreshOrNull = configuration.refresh; - Integer portOrNull = configuration.port; + int refresh = configuration.refresh; + int port = configuration.port; String host = configuration.host; String username = configuration.user; String password = configuration.password; - if (username == null || username.isBlank()) { + if (username.isBlank()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "username must not be empty"); return; } - if (password == null || password.isBlank()) { + if (password.isBlank()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "password must not be empty"); return; } - if (host == null || host.isBlank()) { + if (host.isBlank()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "hostname must not be empty"); return; } - refresh = DEFAULT_REFRESH_SECONDS; - if (refreshOrNull != null) { - refresh = refreshOrNull.intValue(); - } - - int port = WEB_PORT; - if (portOrNull != null) { - port = portOrNull.intValue(); - } - baseURL = "http://" + host + ":" + port; basicAuthentication = "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.ISO_8859_1)); @@ -347,6 +330,7 @@ private synchronized void initPolling(int initalDelay) { * Stops/clears this thing's polling future */ private void clearPolling() { + ScheduledFuture pollFuture = this.pollFuture; if (pollFuture != null && !pollFuture.isCancelled()) { logger.trace("Canceling future"); pollFuture.cancel(false); @@ -374,7 +358,7 @@ private void pollAutelisController() throws InterruptedException { logger.trace("{}/{}.xml \n {}", baseURL, status, response); if (response == null) { // all models and versions have the status.xml endpoint - if (status.equals("status")) { + if ("status".equals(status)) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR); return; } else { @@ -474,7 +458,7 @@ private void pollAutelisController() throws InterruptedException { * @param timeout * @return */ - private synchronized String getUrl(String url, int timeout) throws InterruptedException { + private synchronized @Nullable String getUrl(String url, int timeout) throws InterruptedException { // throttle commands for a very short time to avoid 'loosing' them long now = System.currentTimeMillis(); long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS; @@ -509,7 +493,7 @@ private synchronized String getUrl(String url, int timeout) throws InterruptedEx * @param value * @return {@link State} */ - private State toState(String type, String value) throws NumberFormatException { + private State toState(@Nullable String type, String value) throws NumberFormatException { if ("Number".equals(type)) { return new DecimalType(value); } else if ("Switch".equals(type)) { @@ -539,7 +523,7 @@ private void startHttpClient(HttpClient client) { } } - private void stopHttpClient(HttpClient client) { + private void stopHttpClient(@Nullable HttpClient client) { if (client != null) { client.getAuthenticationStore().clearAuthentications(); client.getAuthenticationStore().clearAuthenticationResults();