[intesis] Session Handling improved (#16620)

Signed-off-by: Christoph <fd0cwp@gmx.de>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Christoph 2024-04-06 17:10:47 +02:00 committed by Ciprian Pascu
parent 3706757de3
commit e6829288fd

View File

@ -112,11 +112,10 @@ public class IntesisHomeHandler extends BaseThingHandler {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Password not set"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Password not set");
return; return;
} else { } else {
logger.trace("trying to log in - current session ID: {}", sessionId);
login();
// start background initialization: // start background initialization:
scheduler.submit(() -> { scheduler.submit(() -> {
logger.trace("initialize() - trying to log in");
login();
populateProperties(); populateProperties();
// query available dataPoints and build dynamic channels // query available dataPoints and build dynamic channels
postRequestInSession(sessionId -> "{\"command\":\"getavailabledatapoints\",\"data\":{\"sessionID\":\"" postRequestInSession(sessionId -> "{\"command\":\"getavailabledatapoints\",\"data\":{\"sessionID\":\""
@ -136,7 +135,10 @@ public class IntesisHomeHandler extends BaseThingHandler {
this.refreshJob = null; this.refreshJob = null;
} }
logout(sessionId); // start background dispose:
scheduler.submit(() -> {
logout();
});
} }
@Override @Override
@ -230,7 +232,7 @@ public class IntesisHomeHandler extends BaseThingHandler {
Data data = gson.fromJson(resp.data, Data.class); Data data = gson.fromJson(resp.data, Data.class);
ResponseError error = gson.fromJson(resp.error, ResponseError.class); ResponseError error = gson.fromJson(resp.error, ResponseError.class);
if (error != null) { if (error != null) {
logger.debug("Login - Error: {}", error); logger.debug("login() - error: {}", error);
} }
if (data != null) { if (data != null) {
Id id = gson.fromJson(data.id, Id.class); Id id = gson.fromJson(data.id, Id.class);
@ -239,7 +241,7 @@ public class IntesisHomeHandler extends BaseThingHandler {
} }
} }
}); });
logger.trace("Login - received session ID: {}", sessionId); logger.trace("login() - received session ID: {}", sessionId);
if (sessionId != null && !sessionId.isEmpty()) { if (sessionId != null && !sessionId.isEmpty()) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
@ -249,10 +251,16 @@ public class IntesisHomeHandler extends BaseThingHandler {
return sessionId; return sessionId;
} }
public @Nullable String logout(String sessionId) { public @Nullable String logout() {
if (!sessionId.isEmpty()) { // we have a running session
String contentString = "{\"command\":\"logout\",\"data\":{\"sessionID\":\"" + sessionId + "\"}}"; String contentString = "{\"command\":\"logout\",\"data\":{\"sessionID\":\"" + sessionId + "\"}}";
logger.trace("logout() - session ID: {}", sessionId);
sessionId = ""; // not really necessary as it is called after dispose(), so sessionID is not used anympre,
// but it is a cleaner way
return api.postRequest(config.ipAddress, contentString); return api.postRequest(config.ipAddress, contentString);
} }
return null;
}
public void populateProperties() { public void populateProperties() {
postRequest("{\"command\":\"getinfo\",\"data\":\"\"}", resp -> { postRequest("{\"command\":\"getinfo\",\"data\":\"\"}", resp -> {