Skip to content

Commit

Permalink
[autelis] Fix checkstyle en warnings (#15423)
Browse files Browse the repository at this point in the history
Signed-off-by: lsiepel <[email protected]>
  • Loading branch information
lsiepel authored Aug 27, 2023
1 parent 7fb9efc commit 7f389f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7f389f8

Please sign in to comment.