]> git.basschouten.com Git - openhab-addons.git/commitdiff
[autelis] Fix checkstyle en warnings (#15423)
authorlsiepel <leosiepel@gmail.com>
Sun, 27 Aug 2023 05:27:00 +0000 (07:27 +0200)
committerGitHub <noreply@github.com>
Sun, 27 Aug 2023 05:27:00 +0000 (07:27 +0200)
Signed-off-by: lsiepel <leosiepel@gmail.com>
bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/config/AutelisConfiguration.java
bundles/org.openhab.binding.autelis/src/main/java/org/openhab/binding/autelis/internal/handler/AutelisHandler.java

index 2af92d21a32e58ba46af0033e84ee8803d9f431a..b4ba627b5276e726623a390e324a61abe088d42e 100644 (file)
  */
 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;
 }
index 629395f6ba772b87182e4ea61a44084ddf4776ad..010371d04505ad13bb17f70174c079e17f174fdc 100644 (file)
@@ -29,6 +29,8 @@ import javax.xml.xpath.XPath;
 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 @@ import org.xml.sax.InputSource;
  * @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 class AutelisHandler extends BaseThingHandler {
                         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 @@ public class AutelisHandler extends BaseThingHandler {
         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 @@ public class AutelisHandler extends BaseThingHandler {
      * 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 @@ public class AutelisHandler extends BaseThingHandler {
             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 @@ public class AutelisHandler extends BaseThingHandler {
      * @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 @@ public class AutelisHandler extends BaseThingHandler {
      * @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 @@ public class AutelisHandler extends BaseThingHandler {
         }
     }
 
-    private void stopHttpClient(HttpClient client) {
+    private void stopHttpClient(@Nullable HttpClient client) {
         if (client != null) {
             client.getAuthenticationStore().clearAuthentications();
             client.getAuthenticationStore().clearAuthenticationResults();