-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[intesis] Improve session handling #16476
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9757abf
[intesis] SESSION ID HANDLING IMPROVED
fd0cwp 3aaea09
Update bundles/org.openhab.binding.intesis/src/main/java/org/openhab/…
fd0cwp 35f5f2a
Merge branch 'openhab:main' into main
fd0cwp 38906ef
[intesis] Improve session handling
fd0cwp 56b77c7
Merge branch 'main' of https://github.com/fd0cwp/openhab-addons
fd0cwp 1b72283
[intesis] Improve session handling
fd0cwp File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
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.Response; | ||
import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.ResponseError; | ||
import org.openhab.core.library.types.DecimalType; | ||
import org.openhab.core.library.types.OnOffType; | ||
import org.openhab.core.library.types.QuantityType; | ||
|
@@ -86,6 +87,8 @@ public class IntesisHomeHandler extends BaseThingHandler { | |
|
||
private IntesisHomeConfiguration config = new IntesisHomeConfiguration(); | ||
|
||
private String sessionId = ""; | ||
|
||
private @Nullable ScheduledFuture<?> refreshJob; | ||
|
||
public IntesisHomeHandler(final Thing thing, final HttpClient httpClient, | ||
|
@@ -109,6 +112,9 @@ public void initialize() { | |
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Password not set"); | ||
return; | ||
} else { | ||
logger.trace("trying to log in - current session ID: {}", sessionId); | ||
login(); | ||
|
||
// start background initialization: | ||
scheduler.submit(() -> { | ||
populateProperties(); | ||
|
@@ -129,6 +135,8 @@ public void dispose() { | |
refreshJob.cancel(true); | ||
this.refreshJob = null; | ||
} | ||
|
||
logout(sessionId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
|
||
@Override | ||
|
@@ -216,26 +224,29 @@ public void handleCommand(ChannelUID channelUID, Command command) { | |
} | ||
|
||
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( | ||
"{\"command\":\"login\",\"data\":{\"username\":\"Admin\",\"password\":\"" + config.password + "\"}}", | ||
resp -> { | ||
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) { | ||
Id id = gson.fromJson(data.id, Id.class); | ||
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); | ||
return sessionId[0]; | ||
} else { | ||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "SessionId not received"); | ||
return null; | ||
sessionId = ""; | ||
} | ||
return sessionId; | ||
} | ||
|
||
public @Nullable String logout(String sessionId) { | ||
|
@@ -309,18 +320,34 @@ public void addChannel(String channelId, String itemType, @Nullable final Collec | |
} | ||
|
||
private void postRequest(String request, Consumer<Response> handler) { | ||
postRequest(request, handler, false); | ||
} | ||
|
||
private void postRequest(String request, Consumer<Response> handler, boolean retry) { | ||
try { | ||
logger.trace("request : '{}'", request); | ||
String response = api.postRequest(config.ipAddress, request); | ||
if (response != null) { | ||
Response resp = gson.fromJson(response, Response.class); | ||
if (resp != null) { | ||
boolean success = resp.success; | ||
if (success) { | ||
if (resp.success) { | ||
handler.accept(resp); | ||
} else { | ||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, | ||
"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 { | ||
|
@@ -332,13 +359,11 @@ private void postRequest(String request, Consumer<Response> handler) { | |
} | ||
|
||
private void postRequestInSession(UnaryOperator<String> requestFactory, Consumer<Response> handler) { | ||
String sessionId = login(); | ||
if (sessionId != null) { | ||
try { | ||
String request = requestFactory.apply(sessionId); | ||
postRequest(request, handler); | ||
} finally { | ||
logout(sessionId); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialize()
should return fast and not do any blocking network calls. This is the reason for background initialization below.