[bondhome] compile warnings and sat (#13986)

* fix some compile warnings and checkstyle
* replace deprecated gson calls
* Refactor config init

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2022-12-28 19:40:19 +01:00 committed by GitHub
parent 3a4a387931
commit 1e7ec9bde9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 34 additions and 45 deletions

View File

@ -21,6 +21,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
*/ */
@NonNullByDefault @NonNullByDefault
public class BondException extends Exception { public class BondException extends Exception {
private static final long serialVersionUID = 1L;
private boolean wasBridgeSetOffline; private boolean wasBridgeSetOffline;
public BondException(String message) { public BondException(String message) {

View File

@ -130,9 +130,11 @@ public class BPUPListener implements Runnable {
sock.receive(inPacket); sock.receive(inPacket);
BPUPUpdate response = transformUpdatePacket(inPacket); BPUPUpdate response = transformUpdatePacket(inPacket);
if (response != null) { if (response != null) {
if (!response.bondId.equalsIgnoreCase(bridgeHandler.getBridgeId())) { @Nullable
String bondId = response.bondId;
if (bondId == null || !bondId.equalsIgnoreCase(bridgeHandler.getBridgeId())) {
logger.warn("Response isn't from expected Bridge! Expected: {} Got: {}", logger.warn("Response isn't from expected Bridge! Expected: {} Got: {}",
bridgeHandler.getBridgeId(), response.bondId); bridgeHandler.getBridgeId(), bondId);
} else { } else {
bridgeHandler.setBridgeOnline(inPacket.getAddress().getHostAddress()); bridgeHandler.setBridgeOnline(inPacket.getAddress().getHostAddress());
numberOfKeepAliveTimeouts = 0; numberOfKeepAliveTimeouts = 0;

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -90,12 +90,10 @@ public class BondHttpApi {
* @throws BondException * @throws BondException
*/ */
public List<String> getDevices() throws BondException { public List<String> getDevices() throws BondException {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
String json = request("/v2/devices/"); String json = request("/v2/devices/");
try { try {
JsonParser parser = new JsonParser(); JsonElement element = JsonParser.parseString(json);
JsonElement element = parser.parse(json);
JsonObject obj = element.getAsJsonObject(); JsonObject obj = element.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entries = obj.entrySet(); Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) { for (Map.Entry<String, JsonElement> entry : entries) {

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.api; package org.openhab.binding.bondhome.internal.api;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -12,10 +12,7 @@
*/ */
package org.openhab.binding.bondhome.internal.config; package org.openhab.binding.bondhome.internal.config;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/** /**
* The {@link BondBridgeConfiguration} class contains fields mapping thing * The {@link BondBridgeConfiguration} class contains fields mapping thing
@ -29,15 +26,19 @@ public class BondBridgeConfiguration {
/** /**
* Configuration for a Bond Bridge * Configuration for a Bond Bridge
*/ */
public @Nullable String serialNumber; public String serialNumber = "";
public @Nullable String localToken; public String localToken = "";
public @Nullable String ipAddress; public String ipAddress = "";
public @Nullable String getIpAddress() { public String getIpAddress() {
return ipAddress; return ipAddress;
} }
public void setIpAddress(String ipAddress) { public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress; this.ipAddress = ipAddress;
} }
public boolean isValid() {
return !(serialNumber.isEmpty() || localToken.isEmpty() || ipAddress.isEmpty());
}
} }

View File

@ -12,8 +12,6 @@
*/ */
package org.openhab.binding.bondhome.internal.config; package org.openhab.binding.bondhome.internal.config;
import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;

View File

@ -59,9 +59,11 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th
@Override @Override
public void setThingHandler(@Nullable ThingHandler handler) { public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof BondBridgeHandler) { if (handler instanceof BondBridgeHandler) {
bridgeHandler = (BondBridgeHandler) handler; @Nullable
bridgeHandler.setDiscoveryService(this); BondBridgeHandler localHandler = (BondBridgeHandler) handler;
api = bridgeHandler.getBridgeAPI(); bridgeHandler = localHandler;
localHandler.setDiscoveryService(this);
api = localHandler.getBridgeAPI();
} }
} }
@ -94,7 +96,7 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th
for (final String deviceId : deviceList) { for (final String deviceId : deviceList) {
BondDevice thisDevice = api.getDevice(deviceId); BondDevice thisDevice = api.getDevice(deviceId);
String deviceName; String deviceName;
if (thisDevice != null && (deviceName = thisDevice.name) != null) { if ((deviceName = thisDevice.name) != null) {
final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId); final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId);
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid) final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid)
.withBridge(bridgeUid).withLabel(thisDevice.name) .withBridge(bridgeUid).withLabel(thisDevice.name)

View File

@ -68,7 +68,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
private final BPUPListener udpListener; private final BPUPListener udpListener;
private final BondHttpApi api; private final BondHttpApi api;
private @Nullable BondBridgeConfiguration config; private BondBridgeConfiguration config = new BondBridgeConfiguration();
private @Nullable BondDiscoveryService discoveryService; private @Nullable BondDiscoveryService discoveryService;
@ -99,16 +99,15 @@ public class BondBridgeHandler extends BaseBridgeHandler {
} }
private void initializeThing() { private void initializeThing() {
BondBridgeConfiguration localConfig = config; if (config.localToken.isEmpty()) {
if (localConfig.localToken == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error.incorrect-local-token"); "@text/offline.conf-error.incorrect-local-token");
this.initializer = null; this.initializer = null;
return; return;
} }
if (localConfig.ipAddress == null) { if (config.ipAddress.isEmpty()) {
try { try {
String lookupAddress = localConfig.serialNumber + ".local"; String lookupAddress = config.serialNumber + ".local";
logger.debug("Attempting to get IP address for Bond Bridge {}", lookupAddress); logger.debug("Attempting to get IP address for Bond Bridge {}", lookupAddress);
InetAddress ia = InetAddress.getByName(lookupAddress); InetAddress ia = InetAddress.getByName(lookupAddress);
String ip = ia.getHostAddress(); String ip = ia.getHostAddress();
@ -124,7 +123,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
} }
} else { } else {
try { try {
InetAddress.getByName(localConfig.ipAddress); InetAddress.getByName(config.ipAddress);
} catch (UnknownHostException ignored) { } catch (UnknownHostException ignored) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error.invalid-host"); "@text/offline.conf-error.invalid-host");
@ -204,7 +203,6 @@ public class BondBridgeHandler extends BaseBridgeHandler {
* @param the {@link BPUPUpdate object} * @param the {@link BPUPUpdate object}
*/ */
public void forwardUpdateToThing(BPUPUpdate pushUpdate) { public void forwardUpdateToThing(BPUPUpdate pushUpdate) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
BondDeviceState updateState = pushUpdate.deviceState; BondDeviceState updateState = pushUpdate.deviceState;
@ -242,14 +240,13 @@ public class BondBridgeHandler extends BaseBridgeHandler {
* Returns the Id of the bridge associated with the handler * Returns the Id of the bridge associated with the handler
*/ */
public String getBridgeId() { public String getBridgeId() {
String serialNumber = config.serialNumber; return config.serialNumber;
return serialNumber == null ? "" : serialNumber;
} }
/** /**
* Returns the Ip Address of the bridge associated with the handler as a string * Returns the Ip Address of the bridge associated with the handler as a string
*/ */
public @Nullable String getBridgeIpAddress() { public String getBridgeIpAddress() {
return config.ipAddress; return config.ipAddress;
} }
@ -257,8 +254,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
* Returns the local token of the bridge associated with the handler as a string * Returns the local token of the bridge associated with the handler as a string
*/ */
public String getBridgeToken() { public String getBridgeToken() {
String localToken = config.localToken; return config.localToken;
return localToken == null ? "" : localToken;
} }
/** /**
@ -288,9 +284,11 @@ public class BondBridgeHandler extends BaseBridgeHandler {
* Called by the UDP listener when it gets a proper response. * Called by the UDP listener when it gets a proper response.
*/ */
public void setBridgeOnline(String bridgeAddress) { public void setBridgeOnline(String bridgeAddress) {
BondBridgeConfiguration localConfig = config; if (!config.isValid()) {
if (localConfig.ipAddress == null || !localConfig.ipAddress.equals(bridgeAddress)) { logger.warn("Configuration error, cannot set the bridghe online without configuration");
logger.debug("IP address of Bond {} has changed to {}", localConfig.serialNumber, bridgeAddress); return;
} else if (!config.ipAddress.equals(bridgeAddress)) {
logger.debug("IP address of Bond {} has changed to {}", config.serialNumber, bridgeAddress);
Configuration c = editConfiguration(); Configuration c = editConfiguration();
c.put(CONFIG_IP_ADDRESS, bridgeAddress); c.put(CONFIG_IP_ADDRESS, bridgeAddress);
updateConfiguration(c); updateConfiguration(c);