[surepetcare] Fix possible timeout bug (#15411)

Fix #11527

* Minor java 17 refactoring
* Spotless

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-08-21 21:36:07 +02:00 committed by GitHub
parent bf892b6b96
commit f2fc0560fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 21 deletions

View 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;

View 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";

View 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();
}
}

View 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();
}
}
}

View 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:

View 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;