diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/BondException.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/BondException.java index dd41d7bcc..ceb07b8a8 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/BondException.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/BondException.java @@ -21,6 +21,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault; */ @NonNullByDefault public class BondException extends Exception { + private static final long serialVersionUID = 1L; + private boolean wasBridgeSetOffline; public BondException(String message) { diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPListener.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPListener.java index b088a8bbd..d0e0f42ad 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPListener.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPListener.java @@ -130,9 +130,11 @@ public class BPUPListener implements Runnable { sock.receive(inPacket); BPUPUpdate response = transformUpdatePacket(inPacket); 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: {}", - bridgeHandler.getBridgeId(), response.bondId); + bridgeHandler.getBridgeId(), bondId); } else { bridgeHandler.setBridgeOnline(inPacket.getAddress().getHostAddress()); numberOfKeepAliveTimeouts = 0; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPUpdate.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPUpdate.java index 8f76f4d14..65a177d91 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPUpdate.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPUpdate.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDevice.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDevice.java index cddc071d4..e696f7a14 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDevice.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDevice.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.bondhome.internal.api; -import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*; - import java.util.Arrays; import java.util.List; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceProperties.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceProperties.java index 077009546..7fe2168be 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceProperties.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceProperties.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceState.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceState.java index e1a49958f..905b951ed 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceState.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceState.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHash.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHash.java index f41d1a470..fd414c51f 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHash.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHash.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java index 06d48b57c..c9fd6cd22 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java @@ -90,12 +90,10 @@ public class BondHttpApi { * @throws BondException */ public List getDevices() throws BondException { - List list = new ArrayList<>(); String json = request("/v2/devices/"); try { - JsonParser parser = new JsonParser(); - JsonElement element = parser.parse(json); + JsonElement element = JsonParser.parseString(json); JsonObject obj = element.getAsJsonObject(); Set> entries = obj.entrySet(); for (Map.Entry entry : entries) { diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondSysVersion.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondSysVersion.java index 86eb6445a..4aaf7d29e 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondSysVersion.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondSysVersion.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondBridgeConfiguration.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondBridgeConfiguration.java index 0708e10a9..243cf55c8 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondBridgeConfiguration.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondBridgeConfiguration.java @@ -12,10 +12,7 @@ */ 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.Nullable; /** * The {@link BondBridgeConfiguration} class contains fields mapping thing @@ -29,15 +26,19 @@ public class BondBridgeConfiguration { /** * Configuration for a Bond Bridge */ - public @Nullable String serialNumber; - public @Nullable String localToken; - public @Nullable String ipAddress; + public String serialNumber = ""; + public String localToken = ""; + public String ipAddress = ""; - public @Nullable String getIpAddress() { + public String getIpAddress() { return ipAddress; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } + + public boolean isValid() { + return !(serialNumber.isEmpty() || localToken.isEmpty() || ipAddress.isEmpty()); + } } diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondDeviceConfiguration.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondDeviceConfiguration.java index 7f821637d..0e59da301 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondDeviceConfiguration.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondDeviceConfiguration.java @@ -12,8 +12,6 @@ */ 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.Nullable; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java index d94d2a847..46d6dd054 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java @@ -59,9 +59,11 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th @Override public void setThingHandler(@Nullable ThingHandler handler) { if (handler instanceof BondBridgeHandler) { - bridgeHandler = (BondBridgeHandler) handler; - bridgeHandler.setDiscoveryService(this); - api = bridgeHandler.getBridgeAPI(); + @Nullable + BondBridgeHandler localHandler = (BondBridgeHandler) handler; + bridgeHandler = localHandler; + localHandler.setDiscoveryService(this); + api = localHandler.getBridgeAPI(); } } @@ -94,7 +96,7 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th for (final String deviceId : deviceList) { BondDevice thisDevice = api.getDevice(deviceId); 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 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid) .withBridge(bridgeUid).withLabel(thisDevice.name) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondBridgeHandler.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondBridgeHandler.java index 93e852d01..9c9328d1f 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondBridgeHandler.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondBridgeHandler.java @@ -68,7 +68,7 @@ public class BondBridgeHandler extends BaseBridgeHandler { private final BPUPListener udpListener; private final BondHttpApi api; - private @Nullable BondBridgeConfiguration config; + private BondBridgeConfiguration config = new BondBridgeConfiguration(); private @Nullable BondDiscoveryService discoveryService; @@ -99,16 +99,15 @@ public class BondBridgeHandler extends BaseBridgeHandler { } private void initializeThing() { - BondBridgeConfiguration localConfig = config; - if (localConfig.localToken == null) { + if (config.localToken.isEmpty()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error.incorrect-local-token"); this.initializer = null; return; } - if (localConfig.ipAddress == null) { + if (config.ipAddress.isEmpty()) { try { - String lookupAddress = localConfig.serialNumber + ".local"; + String lookupAddress = config.serialNumber + ".local"; logger.debug("Attempting to get IP address for Bond Bridge {}", lookupAddress); InetAddress ia = InetAddress.getByName(lookupAddress); String ip = ia.getHostAddress(); @@ -124,7 +123,7 @@ public class BondBridgeHandler extends BaseBridgeHandler { } } else { try { - InetAddress.getByName(localConfig.ipAddress); + InetAddress.getByName(config.ipAddress); } catch (UnknownHostException ignored) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error.invalid-host"); @@ -204,7 +203,6 @@ public class BondBridgeHandler extends BaseBridgeHandler { * @param the {@link BPUPUpdate object} */ public void forwardUpdateToThing(BPUPUpdate pushUpdate) { - updateStatus(ThingStatus.ONLINE); BondDeviceState updateState = pushUpdate.deviceState; @@ -242,14 +240,13 @@ public class BondBridgeHandler extends BaseBridgeHandler { * Returns the Id of the bridge associated with the handler */ public String getBridgeId() { - String serialNumber = config.serialNumber; - return serialNumber == null ? "" : serialNumber; + return config.serialNumber; } /** * Returns the Ip Address of the bridge associated with the handler as a string */ - public @Nullable String getBridgeIpAddress() { + public String getBridgeIpAddress() { 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 */ public String getBridgeToken() { - String localToken = config.localToken; - return localToken == null ? "" : localToken; + return config.localToken; } /** @@ -288,9 +284,11 @@ public class BondBridgeHandler extends BaseBridgeHandler { * Called by the UDP listener when it gets a proper response. */ public void setBridgeOnline(String bridgeAddress) { - BondBridgeConfiguration localConfig = config; - if (localConfig.ipAddress == null || !localConfig.ipAddress.equals(bridgeAddress)) { - logger.debug("IP address of Bond {} has changed to {}", localConfig.serialNumber, bridgeAddress); + if (!config.isValid()) { + logger.warn("Configuration error, cannot set the bridghe online without configuration"); + return; + } else if (!config.ipAddress.equals(bridgeAddress)) { + logger.debug("IP address of Bond {} has changed to {}", config.serialNumber, bridgeAddress); Configuration c = editConfiguration(); c.put(CONFIG_IP_ADDRESS, bridgeAddress); updateConfiguration(c);