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;
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);
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;
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";
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
- if (handler instanceof SurePetcareBridgeHandler) {
- bridgeHandler = (SurePetcareBridgeHandler) handler;
+ if (handler instanceof SurePetcareBridgeHandler bridgeHandler) {
bridgeUID = bridgeHandler.getUID();
}
}
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);
}
}
}
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();
}
}
}
} 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);
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:
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)
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;