]> git.basschouten.com Git - openhab-addons.git/commitdiff
[bondhome] compile warnings and sat (#13986)
authorlsiepel <leosiepel@gmail.com>
Wed, 28 Dec 2022 18:40:19 +0000 (19:40 +0100)
committerGitHub <noreply@github.com>
Wed, 28 Dec 2022 18:40:19 +0000 (19:40 +0100)
* fix some compile warnings and checkstyle
* replace deprecated gson calls
* Refactor config init

Signed-off-by: lsiepel <leosiepel@gmail.com>
13 files changed:
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/BondException.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPListener.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BPUPUpdate.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDevice.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceProperties.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceState.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHash.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondSysVersion.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondBridgeConfiguration.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/config/BondDeviceConfiguration.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java
bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondBridgeHandler.java

index dd41d7bcccd3d9b62e387bfdca50d3385760a260..ceb07b8a844fea48c8a067837e2273871cc0fa91 100644 (file)
@@ -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) {
index b088a8bbddecbb6b873801c1d1ee8065fe400103..d0e0f42ad042e7b4ed6d729ffca41b11d836f38b 100644 (file)
@@ -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;
index 8f76f4d143ac519693252140c7de162561731e84..65a177d918be673443426d207d3baaac6c93575e 100644 (file)
@@ -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;
 
index cddc071d45c82ce71801915b89d228314c8b0230..e696f7a143f9f7ea9debbe935faa4aee907fab1a 100644 (file)
@@ -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;
 
index 077009546f6f88321d7418ac67b7c65545c3f245..7fe2168be5d157505c2eeb05d1dc3f05734aac75 100644 (file)
@@ -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;
 
index e1a49958f7951584ab81f86698b43d009bcfa8d6..905b951ed5971a8a1d5d54f793182b551e699464 100644 (file)
@@ -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;
 
index f41d1a470b6d2e56215321205b4f0aeff7acf3c5..fd414c51f7beabc0b0e33365c4fa1a71e0abab00 100644 (file)
@@ -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;
 
index 06d48b57c2a3e5512fc4e9d4af464c17da75f84f..c9fd6cd22e8572c596e2e541bee77b20ea470b28 100644 (file)
@@ -90,12 +90,10 @@ public class BondHttpApi {
      * @throws BondException
      */
     public List<String> getDevices() throws BondException {
-
         List<String> 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<Map.Entry<String, JsonElement>> entries = obj.entrySet();
             for (Map.Entry<String, JsonElement> entry : entries) {
index 86eb6445a04e551ddd41197de5b7b835ce6cbd3b..4aaf7d29ed1d9124bb17095a2c166251c8509c5d 100644 (file)
@@ -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;
 
index 0708e10a9a19eba4c227d525fe626139eb83722d..243cf55c80a4cfddb94763373721cfd8a33d6985 100644 (file)
  */
 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());
+    }
 }
index 7f821637dae08e813f5867170fba68e68391fae0..0e59da301f399b003d82b550db0d4c2430e6072d 100644 (file)
@@ -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;
 
index d94d2a847d3106a30c2d2bb757e9dd98469f7c3c..46d6dd05469f3a7460b4d25bbc744e9d50866dcb 100644 (file)
@@ -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)
index 93e852d0198c302c4638feb777838526e884ed0e..9c9328d1fa88fb7a1411e340c1b3932ddedb7269 100644 (file)
@@ -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);