[bsblan] Minor SAT fixes (#14138)

* Minor SAT fixes

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-01-12 21:07:31 +01:00 committed by GitHub
parent 32909fa237
commit bbc885725f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 22 deletions

View File

@ -17,7 +17,7 @@ import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.API_TIM
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -92,10 +92,10 @@ public class BsbLanApiCaller {
} }
private String createApiBaseUrl() { private String createApiBaseUrl() {
final String host = bridgeConfig.host == null ? "" : bridgeConfig.host.trim(); final String host = bridgeConfig.host.trim();
final String username = bridgeConfig.username == null ? "" : bridgeConfig.username.trim(); final String username = bridgeConfig.username.trim();
final String password = bridgeConfig.password == null ? "" : bridgeConfig.password.trim(); final String password = bridgeConfig.password.trim();
final String passkey = bridgeConfig.passkey == null ? "" : bridgeConfig.passkey.trim(); final String passkey = bridgeConfig.passkey.trim();
StringBuilder url = new StringBuilder(); StringBuilder url = new StringBuilder();
url.append("http://"); url.append("http://");
@ -131,7 +131,7 @@ public class BsbLanApiCaller {
String content = BsbLanApiContentConverter.toJson(request); String content = BsbLanApiContentConverter.toJson(request);
logger.trace("api request content: '{}'", content); logger.trace("api request content: '{}'", content);
if (!content.isBlank()) { if (!content.isBlank()) {
contentStream = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8"))); contentStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
contentType = "application/json"; contentType = "application/json";
} }
} }

View File

@ -12,40 +12,44 @@
*/ */
package org.openhab.binding.bsblan.internal.configuration; package org.openhab.binding.bsblan.internal.configuration;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.bsblan.internal.BsbLanBindingConstants;
/** /**
* The {@link BsbLanBridgeConfiguration} is the class used to match the * The {@link BsbLanBridgeConfiguration} is the class used to match the
* bridge configuration. * bridge configuration.
* *
* @author Peter Schraffl - Initial contribution * @author Peter Schraffl - Initial contribution
*/ */
@NonNullByDefault
public class BsbLanBridgeConfiguration { public class BsbLanBridgeConfiguration {
/** /**
* Hostname or IP address of the device * Hostname or IP address of the device
*/ */
public String host; public String host = "";
/** /**
* HTTP port where device is listening * HTTP port where device is listening
*/ */
public Integer port; public Integer port = BsbLanBindingConstants.DEFAULT_API_PORT;
/** /**
* For "security" feature of BSB-LAN devices * For "security" feature of BSB-LAN devices
*/ */
public String passkey; public String passkey = "";
/** /**
* HTTP Basic Authentication User * HTTP Basic Authentication User
*/ */
public String username; public String username = "";
/** /**
* HTTP Basic Authentication Password * HTTP Basic Authentication Password
*/ */
public String password; public String password = "";
/** /**
* Value refresh interval * Value refresh interval
*/ */
public Integer refreshInterval; public Integer refreshInterval = BsbLanBindingConstants.DEFAULT_REFRESH_INTERVAL;
} }

View File

@ -78,23 +78,19 @@ public class BsbLanBridgeHandler extends BaseBridgeHandler {
// validate 'host' configuration // validate 'host' configuration
String host = bridgeConfig.host; String host = bridgeConfig.host;
if (host == null || host.isBlank()) { if (host.isBlank()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Parameter 'host' is mandatory and must be configured"); "Parameter 'host' is mandatory and must be configured");
return; return;
} }
// validate 'refreshInterval' configuration // validate 'refreshInterval' configuration
if (bridgeConfig.refreshInterval != null && bridgeConfig.refreshInterval < MIN_REFRESH_INTERVAL) { if (bridgeConfig.refreshInterval < MIN_REFRESH_INTERVAL) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
String.format("Parameter 'refreshInterval' must be at least %d seconds", MIN_REFRESH_INTERVAL)); String.format("Parameter 'refreshInterval' must be at least %d seconds", MIN_REFRESH_INTERVAL));
return; return;
} }
if (bridgeConfig.port == null) {
bridgeConfig.port = DEFAULT_API_PORT;
}
// all checks succeeded, start refreshing // all checks succeeded, start refreshing
startAutomaticRefresh(bridgeConfig); startAutomaticRefresh(bridgeConfig);
} }
@ -160,9 +156,7 @@ public class BsbLanBridgeHandler extends BaseBridgeHandler {
// use a local variable to avoid the build warning "Potential null pointer access" // use a local variable to avoid the build warning "Potential null pointer access"
ScheduledFuture<?> localRefreshJob = refreshJob; ScheduledFuture<?> localRefreshJob = refreshJob;
if (localRefreshJob == null || localRefreshJob.isCancelled()) { if (localRefreshJob == null || localRefreshJob.isCancelled()) {
int interval = (config.refreshInterval != null) ? config.refreshInterval.intValue() refreshJob = scheduler.scheduleWithFixedDelay(this::doRefresh, 0, config.refreshInterval, TimeUnit.SECONDS);
: DEFAULT_REFRESH_INTERVAL;
refreshJob = scheduler.scheduleWithFixedDelay(this::doRefresh, 0, interval, TimeUnit.SECONDS);
} }
} }
} }

View File

@ -18,7 +18,6 @@ import org.apache.commons.lang3.StringEscapeUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO; import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
import org.openhab.binding.bsblan.internal.handler.BsbLanParameterHandler;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;