[autelis] Fix checkstyle en warnings (#15423)

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-08-27 07:27:00 +02:00 committed by GitHub
parent 7fb9efc885
commit 7f389f8ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 39 deletions

View File

@ -12,35 +12,39 @@
*/ */
package org.openhab.binding.autelis.internal.config; package org.openhab.binding.autelis.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* Configuration properties for connecting to an Autelis Controller * Configuration properties for connecting to an Autelis Controller
* *
* @author Dan Cunningham - Initial contribution * @author Dan Cunningham - Initial contribution
* *
*/ */
@NonNullByDefault
public class AutelisConfiguration { public class AutelisConfiguration {
/** /**
* Host of the Autelis controller * Host of the Autelis controller
*/ */
public String host; public String host = "";
/** /**
* port of the Autelis controller * port of the Autelis controller
*/ */
public Integer port; public int port = 80;
/** /**
* user to us when connecting to the Autelis controller * user to us when connecting to the Autelis controller
*/ */
public String user; public String user = "";
/** /**
* password to us when connecting to the Autelis controller * password to us when connecting to the Autelis controller
*/ */
public String password; public String password = "";
/** /**
* Rate we poll for new data * Rate we poll for new data
*/ */
public Integer refresh; public int refresh = 5;
} }

View File

@ -29,6 +29,8 @@ import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; 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.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Request;
@ -71,6 +73,7 @@ import org.xml.sax.InputSource;
* @author Dan Cunningham - Initial contribution * @author Dan Cunningham - Initial contribution
* @author Svilen Valkanov - Replaced Apache HttpClient with Jetty * @author Svilen Valkanov - Replaced Apache HttpClient with Jetty
*/ */
@NonNullByDefault
public class AutelisHandler extends BaseThingHandler { public class AutelisHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(AutelisHandler.class); 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; private static final int THROTTLE_TIME_MILLISECONDS = 500;
/**
* Autelis web port
*/
private static final int WEB_PORT = 80;
/** /**
* Pentair values for pump response * Pentair values for pump response
*/ */
@ -133,12 +131,7 @@ public class AutelisHandler extends BaseThingHandler {
/** /**
* Constructed URL consisting of host and port * Constructed URL consisting of host and port
*/ */
private String baseURL; private String baseURL = "";
/**
* Our poll rate
*/
private int refresh;
/** /**
* The http client used for polling requests * The http client used for polling requests
@ -153,7 +146,7 @@ public class AutelisHandler extends BaseThingHandler {
/** /**
* Authentication for login * Authentication for login
*/ */
private String basicAuthentication; private String basicAuthentication = "";
/** /**
* Regex expression to match XML responses from the Autelis, this is used to * 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 * Future to poll for updated
*/ */
private ScheduledFuture<?> pollFuture; private @Nullable ScheduledFuture<?> pollFuture;
public AutelisHandler(Thing thing) { public AutelisHandler(Thing thing) {
super(thing); super(thing);
@ -221,8 +214,8 @@ public class AutelisHandler extends BaseThingHandler {
value = 0; value = 0;
} else if (command == OnOffType.ON) { } else if (command == OnOffType.ON) {
value = 1; value = 1;
} else if (command instanceof DecimalType) { } else if (command instanceof DecimalType commandAsDecimalType) {
value = ((DecimalType) command).intValue(); value = commandAsDecimalType.intValue();
if (!isJandy() && value >= 3) { if (!isJandy() && value >= 3) {
// this is an autelis dim type. not sure what 2 does // this is an autelis dim type. not sure what 2 does
cmd = "dim"; cmd = "dim";
@ -289,37 +282,27 @@ public class AutelisHandler extends BaseThingHandler {
clearPolling(); clearPolling();
AutelisConfiguration configuration = getConfig().as(AutelisConfiguration.class); AutelisConfiguration configuration = getConfig().as(AutelisConfiguration.class);
Integer refreshOrNull = configuration.refresh; int refresh = configuration.refresh;
Integer portOrNull = configuration.port; int port = configuration.port;
String host = configuration.host; String host = configuration.host;
String username = configuration.user; String username = configuration.user;
String password = configuration.password; String password = configuration.password;
if (username == null || username.isBlank()) { if (username.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "username must not be empty"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "username must not be empty");
return; return;
} }
if (password == null || password.isBlank()) { if (password.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "password must not be empty"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "password must not be empty");
return; return;
} }
if (host == null || host.isBlank()) { if (host.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "hostname must not be empty"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "hostname must not be empty");
return; 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; baseURL = "http://" + host + ":" + port;
basicAuthentication = "Basic " basicAuthentication = "Basic "
+ Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.ISO_8859_1)); + 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 * Stops/clears this thing's polling future
*/ */
private void clearPolling() { private void clearPolling() {
ScheduledFuture<?> pollFuture = this.pollFuture;
if (pollFuture != null && !pollFuture.isCancelled()) { if (pollFuture != null && !pollFuture.isCancelled()) {
logger.trace("Canceling future"); logger.trace("Canceling future");
pollFuture.cancel(false); pollFuture.cancel(false);
@ -374,7 +358,7 @@ public class AutelisHandler extends BaseThingHandler {
logger.trace("{}/{}.xml \n {}", baseURL, status, response); logger.trace("{}/{}.xml \n {}", baseURL, status, response);
if (response == null) { if (response == null) {
// all models and versions have the status.xml endpoint // all models and versions have the status.xml endpoint
if (status.equals("status")) { if ("status".equals(status)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR);
return; return;
} else { } else {
@ -474,7 +458,7 @@ public class AutelisHandler extends BaseThingHandler {
* @param timeout * @param timeout
* @return * @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 // throttle commands for a very short time to avoid 'loosing' them
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS; long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS;
@ -509,7 +493,7 @@ public class AutelisHandler extends BaseThingHandler {
* @param value * @param value
* @return {@link State} * @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)) { if ("Number".equals(type)) {
return new DecimalType(value); return new DecimalType(value);
} else if ("Switch".equals(type)) { } 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) { if (client != null) {
client.getAuthenticationStore().clearAuthentications(); client.getAuthenticationStore().clearAuthentications();
client.getAuthenticationStore().clearAuthenticationResults(); client.getAuthenticationStore().clearAuthenticationResults();