mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-26 15:21:41 +01:00
Signed-off-by: Jan Gustafsson <jannegpriv@gmail.com>
This commit is contained in:
parent
62f169d649
commit
02d8e43d37
@ -117,8 +117,11 @@ public class VerisureSession {
|
|||||||
public boolean refresh() {
|
public boolean refresh() {
|
||||||
try {
|
try {
|
||||||
if (logIn()) {
|
if (logIn()) {
|
||||||
updateStatus();
|
if (updateStatus()) {
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -246,13 +249,20 @@ public class VerisureSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable String getPinCode(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() {
|
private void setPasswordFromCookie() {
|
||||||
CookieStore c = httpClient.getCookieStore();
|
CookieStore c = httpClient.getCookieStore();
|
||||||
List<HttpCookie> cookies = c.getCookies();
|
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);
|
logger.trace("Response Cookie: {}", cookie);
|
||||||
if (cookie.getName().equals(PASSWORD_NAME)) {
|
if (cookie.getName().equals(PASSWORD_NAME)) {
|
||||||
password = cookie.getValue();
|
password = cookie.getValue();
|
||||||
@ -347,6 +357,9 @@ public class VerisureSession {
|
|||||||
// Maybe Verisure has switched API server in use?
|
// Maybe Verisure has switched API server in use?
|
||||||
logger.debug("Changed API server! Response: {}", content);
|
logger.debug("Changed API server! Response: {}", content);
|
||||||
setApiServerInUse(getNextApiServer());
|
setApiServerInUse(getNextApiServer());
|
||||||
|
} else if (content.contains("\"message\":\"Request Failed")
|
||||||
|
&& content.contains("Invalid session cookie")) {
|
||||||
|
throw new PostToAPIException("Invalid session cookie");
|
||||||
} else {
|
} else {
|
||||||
String contentChomped = content.trim();
|
String contentChomped = content.trim();
|
||||||
logger.trace("Response body: {}", content);
|
logger.trace("Response body: {}", content);
|
||||||
@ -515,9 +528,10 @@ public class VerisureSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStatus() {
|
private boolean updateStatus() {
|
||||||
logger.debug("Update status");
|
logger.debug("Update status");
|
||||||
verisureInstallations.forEach((installationId, installation) -> {
|
for (Map.Entry<BigDecimal, VerisureInstallation> verisureInstallations : verisureInstallations.entrySet()) {
|
||||||
|
VerisureInstallation installation = verisureInstallations.getValue();
|
||||||
try {
|
try {
|
||||||
configureInstallationInstance(installation.getInstallationId());
|
configureInstallationInstance(installation.getInstallationId());
|
||||||
int httpResultCode = setSessionCookieAuthLogin();
|
int httpResultCode = setSessionCookieAuthLogin();
|
||||||
@ -534,11 +548,14 @@ public class VerisureSession {
|
|||||||
updateGatewayStatus(installation);
|
updateGatewayStatus(installation);
|
||||||
} else {
|
} else {
|
||||||
logger.debug("Failed to set session cookie and auth login, HTTP result code: {}", httpResultCode);
|
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());
|
logger.debug("Failed to update status {}", e.getMessage());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createOperationJSON(String operation, VariablesDTO variables, String query) {
|
private String createOperationJSON(String operation, VariablesDTO variables, String query) {
|
||||||
@ -549,7 +566,7 @@ public class VerisureSession {
|
|||||||
return gson.toJson(Collections.singletonList(operationJSON));
|
return gson.toJson(Collections.singletonList(operationJSON));
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void updateAlarmStatus(VerisureInstallation installation) {
|
private synchronized void updateAlarmStatus(VerisureInstallation installation) throws PostToAPIException {
|
||||||
BigDecimal installationId = installation.getInstallationId();
|
BigDecimal installationId = installation.getInstallationId();
|
||||||
String url = START_GRAPHQL;
|
String url = START_GRAPHQL;
|
||||||
String operation = "ArmState";
|
String operation = "ArmState";
|
||||||
@ -567,8 +584,7 @@ public class VerisureSession {
|
|||||||
String deviceId = "alarm" + installationId;
|
String deviceId = "alarm" + installationId;
|
||||||
thing.setDeviceId(deviceId);
|
thing.setDeviceId(deviceId);
|
||||||
notifyListenersIfChanged(thing, installation, deviceId);
|
notifyListenersIfChanged(thing, installation, deviceId);
|
||||||
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException
|
} catch (ExecutionException | InterruptedException | TimeoutException | JsonSyntaxException 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,6 +129,8 @@ public class VerisureBridgeHandler extends BaseBridgeHandler {
|
|||||||
logger.warn("Failed to initialize bridge, please check your credentials!");
|
logger.warn("Failed to initialize bridge, please check your credentials!");
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_REGISTERING_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_REGISTERING_ERROR,
|
||||||
"Failed to login to Verisure, please check your credentials!");
|
"Failed to login to Verisure, please check your credentials!");
|
||||||
|
dispose();
|
||||||
|
initialize();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
startAutomaticRefresh();
|
startAutomaticRefresh();
|
||||||
|
Loading…
Reference in New Issue
Block a user