From f2fc0560fdd55f0b9c3842e135814882c11bfd67 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Mon, 21 Aug 2023 21:36:07 +0200 Subject: [PATCH] [surepetcare] Fix possible timeout bug (#15411) Fix #11527 * Minor java 17 refactoring * Spotless Signed-off-by: lsiepel --- .../internal/SurePetcareAPIHelper.java | 7 +++++-- .../internal/SurePetcareConstants.java | 1 + .../SurePetcareDiscoveryService.java | 3 +-- .../handler/SurePetcareBridgeHandler.java | 20 +++++++++---------- .../handler/SurePetcareDeviceHandler.java | 6 +++--- .../handler/SurePetcarePetHandler.java | 8 ++++---- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareAPIHelper.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareAPIHelper.java index e9a1747384e..590231951bd 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareAPIHelper.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareAPIHelper.java @@ -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; diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareConstants.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareConstants.java index 1ffab13d3cc..13a47420336 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareConstants.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/SurePetcareConstants.java @@ -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"; diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/discovery/SurePetcareDiscoveryService.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/discovery/SurePetcareDiscoveryService.java index 282aa7ca53c..e350eb06c11 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/discovery/SurePetcareDiscoveryService.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/discovery/SurePetcareDiscoveryService.java @@ -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(); } } diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareBridgeHandler.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareBridgeHandler.java index 4078fcb3ed3..abd28ca5560 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareBridgeHandler.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareBridgeHandler.java @@ -172,28 +172,28 @@ public class SurePetcareBridgeHandler extends BaseBridgeHandler { String tid = th.getUID().getId(); Map 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(); } } } diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareDeviceHandler.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareDeviceHandler.java index bc9bc7d7642..0ca68304553 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareDeviceHandler.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcareDeviceHandler.java @@ -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: diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java index d069b013f23..baf5200cd88 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java @@ -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;