]> git.basschouten.com Git - openhab-addons.git/commitdiff
[surepetcare] Fix possible timeout bug (#15411)
authorlsiepel <leosiepel@gmail.com>
Mon, 21 Aug 2023 19:36:07 +0000 (21:36 +0200)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 19:36:07 +0000 (21:36 +0200)
Fix #11527

* Minor java 17 refactoring
* Spotless

Signed-off-by: lsiepel <leosiepel@gmail.com>
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareAPIHelper.java
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareConstants.java
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/discovery/SurePetcareDiscoveryService.java
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareBridgeHandler.java
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareDeviceHandler.java
bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java

index e9a1747384e3053f2d94ed742f4c0563eefb4870..590231951bdcf753838b19f6c249d3df609f7bb8 100644 (file)
@@ -23,6 +23,7 @@ import java.time.ZonedDateTime;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -108,7 +109,8 @@ public class SurePetcareAPIHelper {
             setConnectionHeaders(request);
             request.content(new StringContentProvider(SurePetcareConstants.GSON
                     .toJson(new SurePetcareLoginCredentials(username, password, getDeviceId().toString()))));
-            ContentResponse response = request.send();
+            ContentResponse response = request.timeout(SurePetcareConstants.DEFAULT_HTTP_TIMEOUT, TimeUnit.SECONDS)
+                    .send();
             if (response.getStatus() == HttpURLConnection.HTTP_OK) {
                 SurePetcareLoginResponse loginResponse = SurePetcareConstants.GSON
                         .fromJson(response.getContentAsString(), SurePetcareLoginResponse.class);
@@ -450,7 +452,8 @@ public class SurePetcareAPIHelper {
         while (retries > 0) {
             try {
                 setConnectionHeaders(request);
-                ContentResponse response = request.send();
+                ContentResponse response = request.timeout(SurePetcareConstants.DEFAULT_HTTP_TIMEOUT, TimeUnit.SECONDS)
+                        .send();
                 if ((response.getStatus() == HttpURLConnection.HTTP_OK)
                         || (response.getStatus() == HttpURLConnection.HTTP_CREATED)) {
                     return response;
index 1ffab13d3cc8d78fa282fbd08da2a98a6b9c8576..13a474203364e07f355a520c9cdd80130e28e28a 100644 (file)
@@ -54,6 +54,7 @@ public class SurePetcareConstants {
 
     public static final long DEFAULT_REFRESH_INTERVAL_TOPOLOGY = 36000; // 10 hours
     public static final long DEFAULT_REFRESH_INTERVAL_STATUS = 300; // 5 mins
+    public static final int DEFAULT_HTTP_TIMEOUT = 8;
 
     public static final String PROPERTY_NAME_ID = "id";
 
index 282aa7ca53cfce15082a21c35ac6c289bee910de..e350eb06c11182c9ef56de5cfbdb7f82e8c85a15 100644 (file)
@@ -89,8 +89,7 @@ public class SurePetcareDiscoveryService extends AbstractDiscoveryService
 
     @Override
     public void setThingHandler(@Nullable ThingHandler handler) {
-        if (handler instanceof SurePetcareBridgeHandler) {
-            bridgeHandler = (SurePetcareBridgeHandler) handler;
+        if (handler instanceof SurePetcareBridgeHandler bridgeHandler) {
             bridgeUID = bridgeHandler.getUID();
         }
     }
index 4078fcb3ed3e7b7b9cecb52ed5e53723c7240724..abd28ca556093ef589e9d9f7c1545e4d6b92acf5 100644 (file)
@@ -172,28 +172,28 @@ public class SurePetcareBridgeHandler extends BaseBridgeHandler {
             String tid = th.getUID().getId();
             Map<String, String> properties = null;
             ThingHandler handler = th.getHandler();
-            if (handler instanceof SurePetcarePetHandler) {
-                ((SurePetcarePetHandler) handler).updateThing();
+            if (handler instanceof SurePetcarePetHandler surePetcareHandler) {
+                surePetcareHandler.updateThing();
                 SurePetcarePet pet = petcareAPI.getTopology().getById(petcareAPI.getTopology().pets, tid);
                 if (pet != null) {
                     properties = pet.getThingProperties();
                 }
-            } else if (handler instanceof SurePetcareHouseholdHandler) {
-                ((SurePetcareHouseholdHandler) handler).updateThing();
+            } else if (handler instanceof SurePetcareHouseholdHandler surePetcareHouseholdHandler) {
+                surePetcareHouseholdHandler.updateThing();
                 SurePetcareHousehold household = petcareAPI.getTopology().getById(petcareAPI.getTopology().households,
                         tid);
                 if (household != null) {
                     properties = household.getThingProperties();
                 }
-            } else if (handler instanceof SurePetcareDeviceHandler) {
-                ((SurePetcareDeviceHandler) handler).updateThing();
+            } else if (handler instanceof SurePetcareDeviceHandler surePetcareDevicedHandler) {
+                surePetcareDevicedHandler.updateThing();
                 SurePetcareDevice device = petcareAPI.getTopology().getById(petcareAPI.getTopology().devices, tid);
                 if (device != null) {
                     properties = device.getThingProperties();
                 }
             }
-            if ((properties != null) && (handler instanceof SurePetcareBaseObjectHandler)) {
-                ((SurePetcareBaseObjectHandler) handler).updateProperties(properties);
+            if ((properties != null) && (handler instanceof SurePetcareBaseObjectHandler surePetcareBaseHandler)) {
+                surePetcareBaseHandler.updateProperties(properties);
             }
         }
     }
@@ -203,8 +203,8 @@ public class SurePetcareBridgeHandler extends BaseBridgeHandler {
         for (Thing th : getThing().getThings()) {
             if (th.getThingTypeUID().equals(THING_TYPE_PET)) {
                 ThingHandler handler = th.getHandler();
-                if (handler != null) {
-                    ((SurePetcarePetHandler) handler).updateThing();
+                if (handler instanceof SurePetcarePetHandler surePetcarePetHandler) {
+                    surePetcarePetHandler.updateThing();
                 }
             }
         }
index bc9bc7d7642443d61cea3032a55d2d51600a1592..0ca6830455340eee405cd9aeede2feb267fb0557 100644 (file)
@@ -72,11 +72,11 @@ public class SurePetcareDeviceHandler extends SurePetcareBaseObjectHandler {
         } else {
             switch (channelUID.getId()) {
                 case DEVICE_CHANNEL_LOCKING_MODE:
-                    if (command instanceof StringType) {
+                    if (command instanceof StringType commandAsStringType) {
                         synchronized (petcareAPI) {
                             SurePetcareDevice device = petcareAPI.getDevice(thing.getUID().getId());
                             if (device != null) {
-                                String newLockingModeIdStr = ((StringType) command).toString();
+                                String newLockingModeIdStr = commandAsStringType.toString();
                                 try {
                                     Integer newLockingModeId = Integer.valueOf(newLockingModeIdStr);
                                     petcareAPI.setDeviceLockingMode(device, newLockingModeId);
@@ -212,7 +212,7 @@ public class SurePetcareDeviceHandler extends SurePetcareBaseObjectHandler {
                                     logger.debug("Enabling curfew slot: {}", slot);
                                     requiresUpdate = true;
                                 }
-                                curfew.enabled = (command.equals(OnOffType.ON));
+                                curfew.enabled = command.equals(OnOffType.ON);
                             }
                             break;
                         case DEVICE_CHANNEL_CURFEW_LOCK_TIME:
index d069b013f2342ee452afa05d0ec4728b9e25c906..baf5200cd882ff4b62c58f3d70784f2ca23e7fd7 100644 (file)
@@ -72,11 +72,11 @@ public class SurePetcarePetHandler extends SurePetcareBaseObjectHandler {
             switch (channelUID.getId()) {
                 case PET_CHANNEL_LOCATION:
                     logger.debug("Received location update command: {}", command.toString());
-                    if (command instanceof StringType) {
+                    if (command instanceof StringType commandAsStringType) {
                         synchronized (petcareAPI) {
                             SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
                             if (pet != null) {
-                                String newLocationIdStr = ((StringType) command).toString();
+                                String newLocationIdStr = commandAsStringType.toString();
                                 try {
                                     Integer newLocationId = Integer.valueOf(newLocationIdStr);
                                     // Only update if location has changed. (Needed for Group:Switch item)
@@ -103,11 +103,11 @@ public class SurePetcarePetHandler extends SurePetcareBaseObjectHandler {
                     break;
                 case PET_CHANNEL_LOCATION_TIMEOFFSET:
                     logger.debug("Received location time offset update command: {}", command.toString());
-                    if (command instanceof StringType) {
+                    if (command instanceof StringType commandAsStringType) {
                         synchronized (petcareAPI) {
                             SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
                             if (pet != null) {
-                                String commandIdStr = ((StringType) command).toString();
+                                String commandIdStr = commandAsStringType.toString();
                                 try {
                                     Integer commandId = Integer.valueOf(commandIdStr);
                                     Integer currentLocation = pet.status.activity.where;