[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:
Jan Gustafsson 2021-10-31 21:45:36 +01:00 committed by GitHub
parent 4423b166cb
commit ba9d8b3edb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 139 additions and 138 deletions

View File

@ -109,7 +109,7 @@ public class VerisureSession {
public boolean initialize(@Nullable String authstring, @Nullable String pinCode, String userName, String password) { public boolean initialize(@Nullable String authstring, @Nullable String pinCode, String userName, String password) {
if (authstring != null) { if (authstring != null) {
this.authstring = authstring.substring(0); this.authstring = authstring;
this.pinCode = pinCode; this.pinCode = pinCode;
this.userName = userName; this.userName = userName;
this.password = password; this.password = password;
@ -124,17 +124,12 @@ public class VerisureSession {
} }
public boolean refresh() { public boolean refresh() {
try { if (logIn()) {
if (logIn()) { if (updateStatus()) {
if (updateStatus()) { return true;
return true;
}
} }
return false;
} catch (HttpResponseException e) {
logger.warn("Failed to do a refresh {}", e.getMessage());
return false;
} }
return false;
} }
public int sendCommand(String url, String data, BigDecimal installationId) { public int sendCommand(String url, String data, BigDecimal installationId) {
@ -550,7 +545,8 @@ public class VerisureSession {
} else { } else {
return true; return true;
} }
} catch (ExecutionException | InterruptedException | TimeoutException | URISyntaxException e) { } catch (ExecutionException | InterruptedException | TimeoutException | URISyntaxException
| HttpResponseException e) {
logger.warn("Failed to login {}", e.getMessage()); logger.warn("Failed to login {}", e.getMessage());
} }
return false; return false;
@ -654,33 +650,34 @@ public class VerisureSession {
VerisureSmartLocksDTO thing = postJSONVerisureAPI(url, queryQLSmartLock, VerisureSmartLocksDTO.class); VerisureSmartLocksDTO thing = postJSONVerisureAPI(url, queryQLSmartLock, VerisureSmartLocksDTO.class);
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
List<VerisureSmartLocksDTO.Doorlock> doorLockList = thing.getData().getInstallation().getDoorlocks(); List<VerisureSmartLocksDTO.Doorlock> doorLockList = thing.getData().getInstallation().getDoorlocks();
doorLockList.forEach(doorLock -> { if (doorLockList != null) {
VerisureSmartLocksDTO slThing = new VerisureSmartLocksDTO(); doorLockList.forEach(doorLock -> {
VerisureSmartLocksDTO.Installation inst = new VerisureSmartLocksDTO.Installation(); VerisureSmartLocksDTO slThing = new VerisureSmartLocksDTO();
inst.setDoorlocks(Collections.singletonList(doorLock)); VerisureSmartLocksDTO.Installation inst = new VerisureSmartLocksDTO.Installation();
VerisureSmartLocksDTO.Data data = new VerisureSmartLocksDTO.Data(); inst.setDoorlocks(Collections.singletonList(doorLock));
data.setInstallation(inst); VerisureSmartLocksDTO.Data data = new VerisureSmartLocksDTO.Data();
slThing.setData(data); data.setInstallation(inst);
// Set unique deviceID slThing.setData(data);
String deviceId = doorLock.getDevice().getDeviceLabel(); // Set unique deviceID
if (deviceId != null) { String deviceId = doorLock.getDevice().getDeviceLabel();
// Set location if (deviceId != null) {
slThing.setLocation(doorLock.getDevice().getArea()); // Set location
slThing.setDeviceId(deviceId); slThing.setLocation(doorLock.getDevice().getArea());
slThing.setDeviceId(deviceId);
// Fetch more info from old endpoint // Fetch more info from old endpoint
try { try {
VerisureSmartLockDTO smartLockThing = getJSONVerisureAPI(SMARTLOCK_PATH + slThing.getDeviceId(), VerisureSmartLockDTO smartLockThing = getJSONVerisureAPI(
VerisureSmartLockDTO.class); SMARTLOCK_PATH + slThing.getDeviceId(), VerisureSmartLockDTO.class);
logger.debug("REST Response ({})", smartLockThing); logger.debug("REST Response ({})", smartLockThing);
slThing.setSmartLockJSON(smartLockThing); slThing.setSmartLockJSON(smartLockThing);
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) { } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) {
logger.warn("Failed to query for smartlock status: {}", e.getMessage()); logger.warn("Failed to query for smartlock status: {}", e.getMessage());
}
notifyListenersIfChanged(slThing, installation, deviceId);
} }
notifyListenersIfChanged(slThing, installation, deviceId); });
} }
});
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) { | PostToAPIException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage()); logger.warn("Failed to send a POST to the API {}", e.getMessage());
@ -702,21 +699,23 @@ public class VerisureSession {
VerisureSmartPlugsDTO thing = postJSONVerisureAPI(url, queryQLSmartPlug, VerisureSmartPlugsDTO.class); VerisureSmartPlugsDTO thing = postJSONVerisureAPI(url, queryQLSmartPlug, VerisureSmartPlugsDTO.class);
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
List<VerisureSmartPlugsDTO.Smartplug> smartPlugList = thing.getData().getInstallation().getSmartplugs(); List<VerisureSmartPlugsDTO.Smartplug> smartPlugList = thing.getData().getInstallation().getSmartplugs();
smartPlugList.forEach(smartPlug -> { if (smartPlugList != null) {
VerisureSmartPlugsDTO spThing = new VerisureSmartPlugsDTO(); smartPlugList.forEach(smartPlug -> {
VerisureSmartPlugsDTO.Installation inst = new VerisureSmartPlugsDTO.Installation(); VerisureSmartPlugsDTO spThing = new VerisureSmartPlugsDTO();
inst.setSmartplugs(Collections.singletonList(smartPlug)); VerisureSmartPlugsDTO.Installation inst = new VerisureSmartPlugsDTO.Installation();
VerisureSmartPlugsDTO.Data data = new VerisureSmartPlugsDTO.Data(); inst.setSmartplugs(Collections.singletonList(smartPlug));
data.setInstallation(inst); VerisureSmartPlugsDTO.Data data = new VerisureSmartPlugsDTO.Data();
spThing.setData(data); data.setInstallation(inst);
// Set unique deviceID spThing.setData(data);
String deviceId = smartPlug.getDevice().getDeviceLabel(); // Set unique deviceID
if (deviceId != null) { String deviceId = smartPlug.getDevice().getDeviceLabel();
// Set location if (deviceId != null) {
spThing.setLocation(smartPlug.getDevice().getArea()); // Set location
notifyListenersIfChanged(spThing, installation, deviceId); spThing.setLocation(smartPlug.getDevice().getArea());
} notifyListenersIfChanged(spThing, installation, deviceId);
}); }
});
}
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) { | PostToAPIException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage()); logger.warn("Failed to send a POST to the API {}", e.getMessage());
@ -821,32 +820,35 @@ public class VerisureSession {
VerisureDoorWindowsDTO thing = postJSONVerisureAPI(url, queryQLDoorWindow, VerisureDoorWindowsDTO.class); VerisureDoorWindowsDTO thing = postJSONVerisureAPI(url, queryQLDoorWindow, VerisureDoorWindowsDTO.class);
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
List<VerisureDoorWindowsDTO.DoorWindow> doorWindowList = thing.getData().getInstallation().getDoorWindows(); List<VerisureDoorWindowsDTO.DoorWindow> doorWindowList = thing.getData().getInstallation().getDoorWindows();
doorWindowList.forEach(doorWindow -> { if (doorWindowList != null) {
VerisureDoorWindowsDTO dThing = new VerisureDoorWindowsDTO(); doorWindowList.forEach(doorWindow -> {
VerisureDoorWindowsDTO.Installation inst = new VerisureDoorWindowsDTO.Installation(); VerisureDoorWindowsDTO dThing = new VerisureDoorWindowsDTO();
inst.setDoorWindows(Collections.singletonList(doorWindow)); VerisureDoorWindowsDTO.Installation inst = new VerisureDoorWindowsDTO.Installation();
VerisureDoorWindowsDTO.Data data = new VerisureDoorWindowsDTO.Data(); inst.setDoorWindows(Collections.singletonList(doorWindow));
data.setInstallation(inst); VerisureDoorWindowsDTO.Data data = new VerisureDoorWindowsDTO.Data();
dThing.setData(data); data.setInstallation(inst);
// Set unique deviceID dThing.setData(data);
String deviceId = doorWindow.getDevice().getDeviceLabel(); // Set unique deviceID
if (deviceId != null) { String deviceId = doorWindow.getDevice().getDeviceLabel();
try { if (deviceId != null) {
VerisureBatteryStatusDTO[] batteryStatusThingArray = getJSONVerisureAPI(BATTERY_STATUS, try {
VerisureBatteryStatusDTO[].class); VerisureBatteryStatusDTO[] batteryStatusThingArray = getJSONVerisureAPI(BATTERY_STATUS,
VerisureBatteryStatusDTO batteryStatus = getBatteryStatus(deviceId, batteryStatusThingArray); VerisureBatteryStatusDTO[].class);
if (batteryStatus != null) { VerisureBatteryStatusDTO batteryStatus = getBatteryStatus(deviceId,
logger.debug("REST Response ({})", batteryStatus); batteryStatusThingArray);
dThing.setBatteryStatus(batteryStatus); if (batteryStatus != null) {
logger.debug("REST Response ({})", batteryStatus);
dThing.setBatteryStatus(batteryStatus);
}
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) {
logger.warn("Failed to query for door&window status: {}", e.getMessage());
} }
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) { // Set location
logger.warn("Failed to query for door&window status: {}", e.getMessage()); dThing.setLocation(doorWindow.getDevice().getArea());
notifyListenersIfChanged(dThing, installation, deviceId);
} }
// Set location });
dThing.setLocation(doorWindow.getDevice().getArea()); }
notifyListenersIfChanged(dThing, installation, deviceId);
}
});
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) { | PostToAPIException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage()); logger.warn("Failed to send a POST to the API {}", e.getMessage());
@ -896,20 +898,22 @@ public class VerisureSession {
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
List<VerisureUserPresencesDTO.UserTracking> userTrackingList = thing.getData().getInstallation() List<VerisureUserPresencesDTO.UserTracking> userTrackingList = thing.getData().getInstallation()
.getUserTrackings(); .getUserTrackings();
userTrackingList.forEach(userTracking -> { if (userTrackingList != null) {
String localUserTrackingStatus = userTracking.getStatus(); userTrackingList.forEach(userTracking -> {
if ("ACTIVE".equals(localUserTrackingStatus)) { String localUserTrackingStatus = userTracking.getStatus();
VerisureUserPresencesDTO upThing = new VerisureUserPresencesDTO(); if ("ACTIVE".equals(localUserTrackingStatus)) {
VerisureUserPresencesDTO.Installation inst = new VerisureUserPresencesDTO.Installation(); VerisureUserPresencesDTO upThing = new VerisureUserPresencesDTO();
inst.setUserTrackings(Collections.singletonList(userTracking)); VerisureUserPresencesDTO.Installation inst = new VerisureUserPresencesDTO.Installation();
VerisureUserPresencesDTO.Data data = new VerisureUserPresencesDTO.Data(); inst.setUserTrackings(Collections.singletonList(userTracking));
data.setInstallation(inst); VerisureUserPresencesDTO.Data data = new VerisureUserPresencesDTO.Data();
upThing.setData(data); data.setInstallation(inst);
// Set unique deviceID upThing.setData(data);
String deviceId = "up" + userTracking.getWebAccount() + installationId; // Set unique deviceID
notifyListenersIfChanged(upThing, installation, deviceId); String deviceId = "up" + userTracking.getWebAccount() + installationId;
} notifyListenersIfChanged(upThing, installation, deviceId);
}); }
});
}
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) { | PostToAPIException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage()); logger.warn("Failed to send a POST to the API {}", e.getMessage());
@ -933,22 +937,24 @@ public class VerisureSession {
VerisureMiceDetectionDTO.class); VerisureMiceDetectionDTO.class);
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
List<VerisureMiceDetectionDTO.Mouse> miceList = thing.getData().getInstallation().getMice(); List<VerisureMiceDetectionDTO.Mouse> miceList = thing.getData().getInstallation().getMice();
miceList.forEach(mouse -> { if (miceList != null) {
VerisureMiceDetectionDTO miceThing = new VerisureMiceDetectionDTO(); miceList.forEach(mouse -> {
VerisureMiceDetectionDTO.Installation inst = new VerisureMiceDetectionDTO.Installation(); VerisureMiceDetectionDTO miceThing = new VerisureMiceDetectionDTO();
inst.setMice(Collections.singletonList(mouse)); VerisureMiceDetectionDTO.Installation inst = new VerisureMiceDetectionDTO.Installation();
VerisureMiceDetectionDTO.Data data = new VerisureMiceDetectionDTO.Data(); inst.setMice(Collections.singletonList(mouse));
data.setInstallation(inst); VerisureMiceDetectionDTO.Data data = new VerisureMiceDetectionDTO.Data();
miceThing.setData(data); data.setInstallation(inst);
// Set unique deviceID miceThing.setData(data);
String deviceId = mouse.getDevice().getDeviceLabel(); // Set unique deviceID
logger.debug("Mouse id: {} for thing: {}", deviceId, mouse); String deviceId = mouse.getDevice().getDeviceLabel();
if (deviceId != null) { logger.debug("Mouse id: {} for thing: {}", deviceId, mouse);
// Set location if (deviceId != null) {
miceThing.setLocation(mouse.getDevice().getArea()); // Set location
notifyListenersIfChanged(miceThing, installation, deviceId); miceThing.setLocation(mouse.getDevice().getArea());
} notifyListenersIfChanged(miceThing, installation, deviceId);
}); }
});
}
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) { | PostToAPIException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage()); logger.warn("Failed to send a POST to the API {}", e.getMessage());
@ -1006,10 +1012,12 @@ public class VerisureSession {
logger.debug("REST Response ({})", thing); logger.debug("REST Response ({})", thing);
// Set unique deviceID // Set unique deviceID
List<CommunicationState> communicationStateList = thing.getData().getInstallation().getCommunicationState(); List<CommunicationState> communicationStateList = thing.getData().getInstallation().getCommunicationState();
if (!communicationStateList.isEmpty()) { if (communicationStateList != null) {
String deviceId = communicationStateList.get(0).getDevice().getDeviceLabel(); if (!communicationStateList.isEmpty()) {
if (deviceId != null) { String deviceId = communicationStateList.get(0).getDevice().getDeviceLabel();
notifyListenersIfChanged(thing, installation, deviceId); if (deviceId != null) {
notifyListenersIfChanged(thing, installation, deviceId);
}
} }
} }
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException } catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException

View File

@ -272,7 +272,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
this.climates = climates; this.climates = climates;
} }
public List<DoorWindow> getDoorWindows() { public @Nullable List<DoorWindow> getDoorWindows() {
return doorWindows; return doorWindows;
} }
@ -284,11 +284,11 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
return eventLog; return eventLog;
} }
public List<CommunicationState> getCommunicationState() { public @Nullable List<CommunicationState> getCommunicationState() {
return communicationState; return communicationState;
} }
public List<Mouse> getMice() { public @Nullable List<Mouse> getMice() {
return mice; return mice;
} }
@ -296,7 +296,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
this.mice = mice; this.mice = mice;
} }
public List<Doorlock> getDoorlocks() { public @Nullable List<Doorlock> getDoorlocks() {
return doorlocks; return doorlocks;
} }
@ -304,7 +304,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
this.doorlocks = doorlocks; this.doorlocks = doorlocks;
} }
public List<Smartplug> getSmartplugs() { public @Nullable List<Smartplug> getSmartplugs() {
return smartplugs; return smartplugs;
} }
@ -312,7 +312,7 @@ public abstract class VerisureBaseThingDTO implements VerisureThingDTO {
this.smartplugs = smartplugs; this.smartplugs = smartplugs;
} }
public List<UserTracking> getUserTrackings() { public @Nullable List<UserTracking> getUserTrackings() {
return userTrackings; return userTrackings;
} }

View File

@ -110,28 +110,22 @@ public class VerisureBridgeHandler extends BaseBridgeHandler {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Refresh time is lower than min value of 10!"); "Refresh time is lower than min value of 10!");
} else { } else {
try { authstring = "j_username=" + config.username;
authstring = "j_username=" + config.username; scheduler.execute(() -> {
scheduler.execute(() -> { if (session == null) {
if (session == null) { logger.debug("Session is null, let's create a new one");
logger.debug("Session is null, let's create a new one"); session = new VerisureSession(this.httpClient);
session = new VerisureSession(this.httpClient); }
VerisureSession session = this.session;
updateStatus(ThingStatus.UNKNOWN);
if (session != null) {
if (!session.initialize(authstring, pinCode, config.username, config.password)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Failed to login to Verisure, please check your account settings! Is MFA activated?");
} }
VerisureSession session = this.session; }
updateStatus(ThingStatus.UNKNOWN); startAutomaticRefresh(config.refresh);
if (session != null) { });
if (!session.initialize(authstring, pinCode, config.username, config.password)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_REGISTERING_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!"); logger.debug("Refresh and update status!");
VerisureSession session = this.session; VerisureSession session = this.session;
if (session != null) { if (session != null) {
boolean success = session.refresh(); if (session.refresh()) {
if (success) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);