mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[intesis] Improve session handling (#16476)
* [intesis] SESSION ID HANDLING IMPROVED Signed-off-by: Christoph <fd0cwp@gmx.de> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
9a7669a757
commit
3c211a2e9e
@ -25,6 +25,7 @@ public class IntesisHomeJSonDTO {
|
|||||||
public static class Response {
|
public static class Response {
|
||||||
public boolean success;
|
public boolean success;
|
||||||
public JsonElement data;
|
public JsonElement data;
|
||||||
|
public JsonElement error;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Data {
|
public static class Data {
|
||||||
@ -36,6 +37,11 @@ public class IntesisHomeJSonDTO {
|
|||||||
public JsonElement dpval;
|
public JsonElement dpval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ResponseError {
|
||||||
|
public int code;
|
||||||
|
public String message;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Id {
|
public static class Id {
|
||||||
public String sessionID; // Session ID
|
public String sessionID; // Session ID
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Dpval;
|
|||||||
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Id;
|
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Id;
|
||||||
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Info;
|
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Info;
|
||||||
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Response;
|
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Response;
|
||||||
|
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.ResponseError;
|
||||||
import org.openhab.core.library.types.DecimalType;
|
import org.openhab.core.library.types.DecimalType;
|
||||||
import org.openhab.core.library.types.OnOffType;
|
import org.openhab.core.library.types.OnOffType;
|
||||||
import org.openhab.core.library.types.QuantityType;
|
import org.openhab.core.library.types.QuantityType;
|
||||||
@ -86,6 +87,8 @@ public class IntesisHomeHandler extends BaseThingHandler {
|
|||||||
|
|
||||||
private IntesisHomeConfiguration config = new IntesisHomeConfiguration();
|
private IntesisHomeConfiguration config = new IntesisHomeConfiguration();
|
||||||
|
|
||||||
|
private String sessionId = "";
|
||||||
|
|
||||||
private @Nullable ScheduledFuture<?> refreshJob;
|
private @Nullable ScheduledFuture<?> refreshJob;
|
||||||
|
|
||||||
public IntesisHomeHandler(final Thing thing, final HttpClient httpClient,
|
public IntesisHomeHandler(final Thing thing, final HttpClient httpClient,
|
||||||
@ -109,6 +112,9 @@ 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(() -> {
|
||||||
populateProperties();
|
populateProperties();
|
||||||
@ -129,6 +135,8 @@ public class IntesisHomeHandler extends BaseThingHandler {
|
|||||||
refreshJob.cancel(true);
|
refreshJob.cancel(true);
|
||||||
this.refreshJob = null;
|
this.refreshJob = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logout(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -216,26 +224,29 @@ public class IntesisHomeHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable String login() {
|
public @Nullable String login() {
|
||||||
// lambda's can't modify local variables, so we use an array here to get around the issue
|
|
||||||
String[] sessionId = new String[1];
|
|
||||||
postRequest(
|
postRequest(
|
||||||
"{\"command\":\"login\",\"data\":{\"username\":\"Admin\",\"password\":\"" + config.password + "\"}}",
|
"{\"command\":\"login\",\"data\":{\"username\":\"Admin\",\"password\":\"" + config.password + "\"}}",
|
||||||
resp -> {
|
resp -> {
|
||||||
Data data = gson.fromJson(resp.data, Data.class);
|
Data data = gson.fromJson(resp.data, Data.class);
|
||||||
|
ResponseError error = gson.fromJson(resp.error, ResponseError.class);
|
||||||
|
if (error != null) {
|
||||||
|
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);
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
sessionId[0] = id.sessionID;
|
sessionId = id.sessionID.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (sessionId[0] != null && !sessionId[0].isEmpty()) {
|
logger.trace("Login - received session ID: {}", sessionId);
|
||||||
|
if (sessionId != null && !sessionId.isEmpty()) {
|
||||||
updateStatus(ThingStatus.ONLINE);
|
updateStatus(ThingStatus.ONLINE);
|
||||||
return sessionId[0];
|
|
||||||
} else {
|
} else {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "SessionId not received");
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "SessionId not received");
|
||||||
return null;
|
sessionId = "";
|
||||||
}
|
}
|
||||||
|
return sessionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable String logout(String sessionId) {
|
public @Nullable String logout(String sessionId) {
|
||||||
@ -309,18 +320,34 @@ public class IntesisHomeHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void postRequest(String request, Consumer<Response> handler) {
|
private void postRequest(String request, Consumer<Response> handler) {
|
||||||
|
postRequest(request, handler, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void postRequest(String request, Consumer<Response> handler, boolean retry) {
|
||||||
try {
|
try {
|
||||||
logger.trace("request : '{}'", request);
|
logger.trace("request : '{}'", request);
|
||||||
String response = api.postRequest(config.ipAddress, request);
|
String response = api.postRequest(config.ipAddress, request);
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Response resp = gson.fromJson(response, Response.class);
|
Response resp = gson.fromJson(response, Response.class);
|
||||||
if (resp != null) {
|
if (resp != null) {
|
||||||
boolean success = resp.success;
|
if (resp.success) {
|
||||||
if (success) {
|
|
||||||
handler.accept(resp);
|
handler.accept(resp);
|
||||||
} else {
|
} else {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||||
"Request unsuccessful");
|
"Request unsuccessful");
|
||||||
|
ResponseError respError = gson.fromJson(resp.error, ResponseError.class);
|
||||||
|
if (respError != null) {
|
||||||
|
logger.warn("postRequest failed - respErrorCode: {} / respErrorMessage: {} / retry {}",
|
||||||
|
respError.code, respError.message, retry);
|
||||||
|
if (!retry && respError.code == 1) {
|
||||||
|
logger.debug(
|
||||||
|
"postRequest: trying to log in and retry request - respErrorCode: {} / respErrorMessage: {} / retry {}",
|
||||||
|
respError.code, respError.message, retry);
|
||||||
|
login();
|
||||||
|
postRequest(request, handler, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -332,13 +359,11 @@ public class IntesisHomeHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void postRequestInSession(UnaryOperator<String> requestFactory, Consumer<Response> handler) {
|
private void postRequestInSession(UnaryOperator<String> requestFactory, Consumer<Response> handler) {
|
||||||
String sessionId = login();
|
|
||||||
if (sessionId != null) {
|
if (sessionId != null) {
|
||||||
try {
|
try {
|
||||||
String request = requestFactory.apply(sessionId);
|
String request = requestFactory.apply(sessionId);
|
||||||
postRequest(request, handler);
|
postRequest(request, handler);
|
||||||
} finally {
|
} finally {
|
||||||
logout(sessionId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user