Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[verisure] Fix Binding not working properly HANDLER_REGISTERING_ERROR #11108

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ public boolean initialize(@Nullable String authstring, @Nullable String pinCode,
public boolean refresh() {
try {
if (logIn()) {
updateStatus();
return true;
if (updateStatus()) {
return true;
} else {
return false;
}
} else {
return false;
}
Expand Down Expand Up @@ -246,13 +249,20 @@ public void configureInstallationInstance(BigDecimal installationId)
}

public @Nullable String getPinCode(BigDecimal installationId) {
return verisureInstallations.get(installationId).getPinCode();
VerisureInstallation inst = verisureInstallations.get(installationId);
if (inst != null) {
return inst.getPinCode();
} else {
logger.debug("Installation is null!");
return null;
}
}

private void setPasswordFromCookie() {
CookieStore c = httpClient.getCookieStore();
List<HttpCookie> cookies = c.getCookies();
cookies.forEach(cookie -> {
final List<HttpCookie> unmodifiableList = List.of(cookies.toArray(new HttpCookie[] {}));
unmodifiableList.forEach(cookie -> {
logger.trace("Response Cookie: {}", cookie);
if (cookie.getName().equals(PASSWORD_NAME)) {
password = cookie.getValue();
Expand Down Expand Up @@ -347,6 +357,9 @@ private <T> T postJSONVerisureAPI(String url, String data, Class<T> jsonClass)
// Maybe Verisure has switched API server in use?
logger.debug("Changed API server! Response: {}", content);
setApiServerInUse(getNextApiServer());
} else if (content.contains("\"message\":\"Request Failed")
&& content.contains("Invalid session cookie")) {
throw new PostToAPIException("Invalid session cookie");
} else {
String contentChomped = content.trim();
logger.trace("Response body: {}", content);
Expand Down Expand Up @@ -515,9 +528,10 @@ private void notifyListenersIfChanged(VerisureThingDTO thing, VerisureInstallati
}
}

private void updateStatus() {
private boolean updateStatus() {
logger.debug("Update status");
verisureInstallations.forEach((installationId, installation) -> {
for (Map.Entry<BigDecimal, VerisureInstallation> verisureInstallations : verisureInstallations.entrySet()) {
VerisureInstallation installation = verisureInstallations.getValue();
try {
configureInstallationInstance(installation.getInstallationId());
int httpResultCode = setSessionCookieAuthLogin();
Expand All @@ -534,11 +548,14 @@ private void updateStatus() {
updateGatewayStatus(installation);
} else {
logger.debug("Failed to set session cookie and auth login, HTTP result code: {}", httpResultCode);
return false;
}
} catch (ExecutionException | InterruptedException | TimeoutException e) {
} catch (ExecutionException | InterruptedException | TimeoutException | PostToAPIException e) {
logger.debug("Failed to update status {}", e.getMessage());
return false;
}
});
}
return true;
}

private String createOperationJSON(String operation, VariablesDTO variables, String query) {
Expand All @@ -549,7 +566,7 @@ private String createOperationJSON(String operation, VariablesDTO variables, Str
return gson.toJson(Collections.singletonList(operationJSON));
}

private synchronized void updateAlarmStatus(VerisureInstallation installation) {
private synchronized void updateAlarmStatus(VerisureInstallation installation) throws PostToAPIException {
BigDecimal installationId = installation.getInstallationId();
String url = START_GRAPHQL;
String operation = "ArmState";
Expand All @@ -567,8 +584,7 @@ private synchronized void updateAlarmStatus(VerisureInstallation installation) {
String deviceId = "alarm" + installationId;
thing.setDeviceId(deviceId);
notifyListenersIfChanged(thing, installation, deviceId);
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
| PostToAPIException e) {
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException e) {
logger.warn("Failed to send a POST to the API {}", e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void initialize() {
logger.warn("Failed to initialize bridge, please check your credentials!");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_REGISTERING_ERROR,
"Failed to login to Verisure, please check your credentials!");
dispose();
initialize();
return;
}
startAutomaticRefresh();
Expand Down