[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.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -108,7 +109,8 @@ public class SurePetcareAPIHelper {
setConnectionHeaders(request); setConnectionHeaders(request);
request.content(new StringContentProvider(SurePetcareConstants.GSON request.content(new StringContentProvider(SurePetcareConstants.GSON
.toJson(new SurePetcareLoginCredentials(username, password, getDeviceId().toString())))); .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) { if (response.getStatus() == HttpURLConnection.HTTP_OK) {
SurePetcareLoginResponse loginResponse = SurePetcareConstants.GSON SurePetcareLoginResponse loginResponse = SurePetcareConstants.GSON
.fromJson(response.getContentAsString(), SurePetcareLoginResponse.class); .fromJson(response.getContentAsString(), SurePetcareLoginResponse.class);
@ -450,7 +452,8 @@ public class SurePetcareAPIHelper {
while (retries > 0) { while (retries > 0) {
try { try {
setConnectionHeaders(request); setConnectionHeaders(request);
ContentResponse response = request.send(); ContentResponse response = request.timeout(SurePetcareConstants.DEFAULT_HTTP_TIMEOUT, TimeUnit.SECONDS)
.send();
if ((response.getStatus() == HttpURLConnection.HTTP_OK) if ((response.getStatus() == HttpURLConnection.HTTP_OK)
|| (response.getStatus() == HttpURLConnection.HTTP_CREATED)) { || (response.getStatus() == HttpURLConnection.HTTP_CREATED)) {
return response; 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_TOPOLOGY = 36000; // 10 hours
public static final long DEFAULT_REFRESH_INTERVAL_STATUS = 300; // 5 mins 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"; public static final String PROPERTY_NAME_ID = "id";

View File

@ -89,8 +89,7 @@ public class SurePetcareDiscoveryService extends AbstractDiscoveryService
@Override @Override
public void setThingHandler(@Nullable ThingHandler handler) { public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof SurePetcareBridgeHandler) { if (handler instanceof SurePetcareBridgeHandler bridgeHandler) {
bridgeHandler = (SurePetcareBridgeHandler) handler;
bridgeUID = bridgeHandler.getUID(); bridgeUID = bridgeHandler.getUID();
} }
} }

View File

@ -172,28 +172,28 @@ public class SurePetcareBridgeHandler extends BaseBridgeHandler {
String tid = th.getUID().getId(); String tid = th.getUID().getId();
Map<String, String> properties = null; Map<String, String> properties = null;
ThingHandler handler = th.getHandler(); ThingHandler handler = th.getHandler();
if (handler instanceof SurePetcarePetHandler) { if (handler instanceof SurePetcarePetHandler surePetcareHandler) {
((SurePetcarePetHandler) handler).updateThing(); surePetcareHandler.updateThing();
SurePetcarePet pet = petcareAPI.getTopology().getById(petcareAPI.getTopology().pets, tid); SurePetcarePet pet = petcareAPI.getTopology().getById(petcareAPI.getTopology().pets, tid);
if (pet != null) { if (pet != null) {
properties = pet.getThingProperties(); properties = pet.getThingProperties();
} }
} else if (handler instanceof SurePetcareHouseholdHandler) { } else if (handler instanceof SurePetcareHouseholdHandler surePetcareHouseholdHandler) {
((SurePetcareHouseholdHandler) handler).updateThing(); surePetcareHouseholdHandler.updateThing();
SurePetcareHousehold household = petcareAPI.getTopology().getById(petcareAPI.getTopology().households, SurePetcareHousehold household = petcareAPI.getTopology().getById(petcareAPI.getTopology().households,
tid); tid);
if (household != null) { if (household != null) {
properties = household.getThingProperties(); properties = household.getThingProperties();
} }
} else if (handler instanceof SurePetcareDeviceHandler) { } else if (handler instanceof SurePetcareDeviceHandler surePetcareDevicedHandler) {
((SurePetcareDeviceHandler) handler).updateThing(); surePetcareDevicedHandler.updateThing();
SurePetcareDevice device = petcareAPI.getTopology().getById(petcareAPI.getTopology().devices, tid); SurePetcareDevice device = petcareAPI.getTopology().getById(petcareAPI.getTopology().devices, tid);
if (device != null) { if (device != null) {
properties = device.getThingProperties(); properties = device.getThingProperties();
} }
} }
if ((properties != null) && (handler instanceof SurePetcareBaseObjectHandler)) { if ((properties != null) && (handler instanceof SurePetcareBaseObjectHandler surePetcareBaseHandler)) {
((SurePetcareBaseObjectHandler) handler).updateProperties(properties); surePetcareBaseHandler.updateProperties(properties);
} }
} }
} }
@ -203,8 +203,8 @@ public class SurePetcareBridgeHandler extends BaseBridgeHandler {
for (Thing th : getThing().getThings()) { for (Thing th : getThing().getThings()) {
if (th.getThingTypeUID().equals(THING_TYPE_PET)) { if (th.getThingTypeUID().equals(THING_TYPE_PET)) {
ThingHandler handler = th.getHandler(); ThingHandler handler = th.getHandler();
if (handler != null) { if (handler instanceof SurePetcarePetHandler surePetcarePetHandler) {
((SurePetcarePetHandler) handler).updateThing(); surePetcarePetHandler.updateThing();
} }
} }
} }

View File

@ -72,11 +72,11 @@ public class SurePetcareDeviceHandler extends SurePetcareBaseObjectHandler {
} else { } else {
switch (channelUID.getId()) { switch (channelUID.getId()) {
case DEVICE_CHANNEL_LOCKING_MODE: case DEVICE_CHANNEL_LOCKING_MODE:
if (command instanceof StringType) { if (command instanceof StringType commandAsStringType) {
synchronized (petcareAPI) { synchronized (petcareAPI) {
SurePetcareDevice device = petcareAPI.getDevice(thing.getUID().getId()); SurePetcareDevice device = petcareAPI.getDevice(thing.getUID().getId());
if (device != null) { if (device != null) {
String newLockingModeIdStr = ((StringType) command).toString(); String newLockingModeIdStr = commandAsStringType.toString();
try { try {
Integer newLockingModeId = Integer.valueOf(newLockingModeIdStr); Integer newLockingModeId = Integer.valueOf(newLockingModeIdStr);
petcareAPI.setDeviceLockingMode(device, newLockingModeId); petcareAPI.setDeviceLockingMode(device, newLockingModeId);
@ -212,7 +212,7 @@ public class SurePetcareDeviceHandler extends SurePetcareBaseObjectHandler {
logger.debug("Enabling curfew slot: {}", slot); logger.debug("Enabling curfew slot: {}", slot);
requiresUpdate = true; requiresUpdate = true;
} }
curfew.enabled = (command.equals(OnOffType.ON)); curfew.enabled = command.equals(OnOffType.ON);
} }
break; break;
case DEVICE_CHANNEL_CURFEW_LOCK_TIME: case DEVICE_CHANNEL_CURFEW_LOCK_TIME:

View File

@ -72,11 +72,11 @@ public class SurePetcarePetHandler extends SurePetcareBaseObjectHandler {
switch (channelUID.getId()) { switch (channelUID.getId()) {
case PET_CHANNEL_LOCATION: case PET_CHANNEL_LOCATION:
logger.debug("Received location update command: {}", command.toString()); logger.debug("Received location update command: {}", command.toString());
if (command instanceof StringType) { if (command instanceof StringType commandAsStringType) {
synchronized (petcareAPI) { synchronized (petcareAPI) {
SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId()); SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
if (pet != null) { if (pet != null) {
String newLocationIdStr = ((StringType) command).toString(); String newLocationIdStr = commandAsStringType.toString();
try { try {
Integer newLocationId = Integer.valueOf(newLocationIdStr); Integer newLocationId = Integer.valueOf(newLocationIdStr);
// Only update if location has changed. (Needed for Group:Switch item) // Only update if location has changed. (Needed for Group:Switch item)
@ -103,11 +103,11 @@ public class SurePetcarePetHandler extends SurePetcareBaseObjectHandler {
break; break;
case PET_CHANNEL_LOCATION_TIMEOFFSET: case PET_CHANNEL_LOCATION_TIMEOFFSET:
logger.debug("Received location time offset update command: {}", command.toString()); logger.debug("Received location time offset update command: {}", command.toString());
if (command instanceof StringType) { if (command instanceof StringType commandAsStringType) {
synchronized (petcareAPI) { synchronized (petcareAPI) {
SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId()); SurePetcarePet pet = petcareAPI.getPet(thing.getUID().getId());
if (pet != null) { if (pet != null) {
String commandIdStr = ((StringType) command).toString(); String commandIdStr = commandAsStringType.toString();
try { try {
Integer commandId = Integer.valueOf(commandIdStr); Integer commandId = Integer.valueOf(commandIdStr);
Integer currentLocation = pet.status.activity.where; Integer currentLocation = pet.status.activity.where;