[verisure] Keep refreshing after RuntimeException (#11397)
* Fix that binding stops refreshing after RuntimException (#11396) Signed-off-by: Jan Gustafsson <jannegpriv@gmail.com> * Updated after review comments Signed-off-by: Jan Gustafsson <jannegpriv@gmail.com>
This commit is contained in:
parent
4423b166cb
commit
ba9d8b3edb
|
@ -109,7 +109,7 @@ public class VerisureSession {
|
|||
|
||||
public boolean initialize(@Nullable String authstring, @Nullable String pinCode, String userName, String password) {
|
||||
if (authstring != null) {
|
||||
this.authstring = authstring.substring(0);
|
||||
this.authstring = authstring;
|
||||
this.pinCode = pinCode;
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
|
@ -124,17 +124,12 @@ public class VerisureSession {
|
|||
}
|
||||
|
||||
public boolean refresh() {
|
||||
try {
|
||||
if (logIn()) {
|
||||
if (updateStatus()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (HttpResponseException e) {
|
||||
logger.warn("Failed to do a refresh {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int sendCommand(String url, String data, BigDecimal installationId) {
|
||||
|
@ -550,7 +545,8 @@ public class VerisureSession {
|
|||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | URISyntaxException e) {
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | URISyntaxException
|
||||
| HttpResponseException e) {
|
||||
logger.warn("Failed to login {}", e.getMessage());
|
||||
}
|
||||
return false;
|
||||
|
@ -654,6 +650,7 @@ public class VerisureSession {
|
|||
VerisureSmartLocksDTO thing = postJSONVerisureAPI(url, queryQLSmartLock, VerisureSmartLocksDTO.class);
|
||||
logger.debug("REST Response ({})", thing);
|
||||
List<VerisureSmartLocksDTO.Doorlock> doorLockList = thing.getData().getInstallation().getDoorlocks();
|
||||
if (doorLockList != null) {
|
||||
doorLockList.forEach(doorLock -> {
|
||||
VerisureSmartLocksDTO slThing = new VerisureSmartLocksDTO();
|
||||
VerisureSmartLocksDTO.Installation inst = new VerisureSmartLocksDTO.Installation();
|
||||
|
@ -670,8 +667,8 @@ public class VerisureSession {
|
|||
|
||||
// Fetch more info from old endpoint
|
||||
try {
|
||||
VerisureSmartLockDTO smartLockThing = getJSONVerisureAPI(SMARTLOCK_PATH + slThing.getDeviceId(),
|
||||
VerisureSmartLockDTO.class);
|
||||
VerisureSmartLockDTO smartLockThing = getJSONVerisureAPI(
|
||||
SMARTLOCK_PATH + slThing.getDeviceId(), VerisureSmartLockDTO.class);
|
||||
logger.debug("REST Response ({})", smartLockThing);
|
||||
slThing.setSmartLockJSON(smartLockThing);
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) {
|
||||
|
@ -680,7 +677,7 @@ public class VerisureSession {
|
|||
notifyListenersIfChanged(slThing, installation, deviceId);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
@ -702,6 +699,7 @@ public class VerisureSession {
|
|||
VerisureSmartPlugsDTO thing = postJSONVerisureAPI(url, queryQLSmartPlug, VerisureSmartPlugsDTO.class);
|
||||
logger.debug("REST Response ({})", thing);
|
||||
List<VerisureSmartPlugsDTO.Smartplug> smartPlugList = thing.getData().getInstallation().getSmartplugs();
|
||||
if (smartPlugList != null) {
|
||||
smartPlugList.forEach(smartPlug -> {
|
||||
VerisureSmartPlugsDTO spThing = new VerisureSmartPlugsDTO();
|
||||
VerisureSmartPlugsDTO.Installation inst = new VerisureSmartPlugsDTO.Installation();
|
||||
|
@ -717,6 +715,7 @@ public class VerisureSession {
|
|||
notifyListenersIfChanged(spThing, installation, deviceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
@ -821,6 +820,7 @@ public class VerisureSession {
|
|||
VerisureDoorWindowsDTO thing = postJSONVerisureAPI(url, queryQLDoorWindow, VerisureDoorWindowsDTO.class);
|
||||
logger.debug("REST Response ({})", thing);
|
||||
List<VerisureDoorWindowsDTO.DoorWindow> doorWindowList = thing.getData().getInstallation().getDoorWindows();
|
||||
if (doorWindowList != null) {
|
||||
doorWindowList.forEach(doorWindow -> {
|
||||
VerisureDoorWindowsDTO dThing = new VerisureDoorWindowsDTO();
|
||||
VerisureDoorWindowsDTO.Installation inst = new VerisureDoorWindowsDTO.Installation();
|
||||
|
@ -834,7 +834,8 @@ public class VerisureSession {
|
|||
try {
|
||||
VerisureBatteryStatusDTO[] batteryStatusThingArray = getJSONVerisureAPI(BATTERY_STATUS,
|
||||
VerisureBatteryStatusDTO[].class);
|
||||
VerisureBatteryStatusDTO batteryStatus = getBatteryStatus(deviceId, batteryStatusThingArray);
|
||||
VerisureBatteryStatusDTO batteryStatus = getBatteryStatus(deviceId,
|
||||
batteryStatusThingArray);
|
||||
if (batteryStatus != null) {
|
||||
logger.debug("REST Response ({})", batteryStatus);
|
||||
dThing.setBatteryStatus(batteryStatus);
|
||||
|
@ -847,6 +848,7 @@ public class VerisureSession {
|
|||
notifyListenersIfChanged(dThing, installation, deviceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
@ -896,6 +898,7 @@ public class VerisureSession {
|
|||
logger.debug("REST Response ({})", thing);
|
||||
List<VerisureUserPresencesDTO.UserTracking> userTrackingList = thing.getData().getInstallation()
|
||||
.getUserTrackings();
|
||||
if (userTrackingList != null) {
|
||||
userTrackingList.forEach(userTracking -> {
|
||||
String localUserTrackingStatus = userTracking.getStatus();
|
||||
if ("ACTIVE".equals(localUserTrackingStatus)) {
|
||||
|
@ -910,6 +913,7 @@ public class VerisureSession {
|
|||
notifyListenersIfChanged(upThing, installation, deviceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
@ -933,6 +937,7 @@ public class VerisureSession {
|
|||
VerisureMiceDetectionDTO.class);
|
||||
logger.debug("REST Response ({})", thing);
|
||||
List<VerisureMiceDetectionDTO.Mouse> miceList = thing.getData().getInstallation().getMice();
|
||||
if (miceList != null) {
|
||||
miceList.forEach(mouse -> {
|
||||
VerisureMiceDetectionDTO miceThing = new VerisureMiceDetectionDTO();
|
||||
VerisureMiceDetectionDTO.Installation inst = new VerisureMiceDetectionDTO.Installation();
|
||||
|
@ -949,6 +954,7 @@ public class VerisureSession {
|
|||
notifyListenersIfChanged(miceThing, installation, deviceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
@ -1006,12 +1012,14 @@ public class VerisureSession {
|
|||
logger.debug("REST Response ({})", thing);
|
||||
// Set unique deviceID
|
||||
List<CommunicationState> communicationStateList = thing.getData().getInstallation().getCommunicationState();
|
||||
if (communicationStateList != null) {
|
||||
if (!communicationStateList.isEmpty()) {
|
||||
String deviceId = communicationStateList.get(0).getDevice().getDeviceLabel();
|
||||
if (deviceId != null) {
|
||||
notifyListenersIfChanged(thing, installation, deviceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
||||
| PostToAPIException e) {
|
||||
logger.warn("Failed to send a POST to the API {}", e.getMessage());
|
||||
|
|
|
@ -272,7 +272,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
|
|||
this.climates = climates;
|
||||
}
|
||||
|
||||
public List<DoorWindow> getDoorWindows() {
|
||||
public @Nullable List<DoorWindow> getDoorWindows() {
|
||||
return doorWindows;
|
||||
}
|
||||
|
||||
|
@ -284,11 +284,11 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
|
|||
return eventLog;
|
||||
}
|
||||
|
||||
public List<CommunicationState> getCommunicationState() {
|
||||
public @Nullable List<CommunicationState> getCommunicationState() {
|
||||
return communicationState;
|
||||
}
|
||||
|
||||
public List<Mouse> getMice() {
|
||||
public @Nullable List<Mouse> getMice() {
|
||||
return mice;
|
||||
}
|
||||
|
||||
|
@ -296,7 +296,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
|
|||
this.mice = mice;
|
||||
}
|
||||
|
||||
public List<Doorlock> getDoorlocks() {
|
||||
public @Nullable List<Doorlock> getDoorlocks() {
|
||||
return doorlocks;
|
||||
}
|
||||
|
||||
|
@ -304,7 +304,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
|
|||
this.doorlocks = doorlocks;
|
||||
}
|
||||
|
||||
public List<Smartplug> getSmartplugs() {
|
||||
public @Nullable List<Smartplug> getSmartplugs() {
|
||||
return smartplugs;
|
||||
}
|
||||
|
||||
|
@ -312,7 +312,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
|
|||
this.smartplugs = smartplugs;
|
||||
}
|
||||
|
||||
public List<UserTracking> getUserTrackings() {
|
||||
public @Nullable List<UserTracking> getUserTrackings() {
|
||||
return userTrackings;
|
||||
}
|
||||
|
||||
|
|
|
@ -110,7 +110,6 @@ public class VerisureBridgeHandler extends BaseBridgeHandler {
|
|||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||
"Refresh time is lower than min value of 10!");
|
||||
} else {
|
||||
try {
|
||||
authstring = "j_username=" + config.username;
|
||||
scheduler.execute(() -> {
|
||||
if (session == null) {
|
||||
|
@ -121,17 +120,12 @@ public class VerisureBridgeHandler extends BaseBridgeHandler {
|
|||
updateStatus(ThingStatus.UNKNOWN);
|
||||
if (session != null) {
|
||||
if (!session.initialize(authstring, pinCode, config.username, config.password)) {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_REGISTERING_ERROR,
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||
"Failed to login to Verisure, please check your account settings! Is MFA activated?");
|
||||
return;
|
||||
}
|
||||
}
|
||||
startAutomaticRefresh(config.refresh);
|
||||
}
|
||||
});
|
||||
} catch (RuntimeException e) {
|
||||
logger.warn("Failed to initialize: {}", e.getMessage());
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,8 +166,7 @@ public class VerisureBridgeHandler extends BaseBridgeHandler {
|
|||
logger.debug("Refresh and update status!");
|
||||
VerisureSession session = this.session;
|
||||
if (session != null) {
|
||||
boolean success = session.refresh();
|
||||
if (success) {
|
||||
if (session.refresh()) {
|
||||
updateStatus(ThingStatus.ONLINE);
|
||||
} else {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
|
||||
|
|
Loading…
Reference in New Issue