Skip to content

Commit

Permalink
Fixed version fetching. Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
seime committed Jul 16, 2024
1 parent 49fedfa commit b7dd366
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -154,6 +158,8 @@ public void init(String username, String password, String configuredAppVersion)
"Could not fetch appVersion dynamically, and no value is provided on the bridge thing. Defaulting to {}",
DEFAULT_APP_VERSION);
appVersion = DEFAULT_APP_VERSION;
} else {
logger.debug("Fetched appVersion from AppBrain: {}", appVersion);
}
} else {
appVersion = configuredAppVersion;
Expand Down Expand Up @@ -439,10 +445,15 @@ private Token refreshToken(Token currentToken) throws IOException, Communication

private String getAppVersion() {
try {
Request req = new Request.Builder().url(APPBRAIN_URL).get().build();
Response rsp = client.newCall(req).execute();

String body = rsp.body().string();
HttpRequest request = HttpRequest.newBuilder().uri(new URI(APPBRAIN_URL)).headers("User-Agent",
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36")
.GET().build();

HttpClient httpClient = HttpClient.newHttpClient();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

String body = response.body();
return parseAppBrainAppVersion(body);

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,17 @@ public synchronized void doPoll(boolean triggerDeviceUpdate) {

int retryDelay = 60 * (errorCounter * errorCounter);
// Try init again
if (errorCounter < MAX_RETRIES_BEFORE_GIVING_UP) {
logger.info("Will try to re-init in {} seconds", retryDelay);
if (errorCounter <= MAX_RETRIES_BEFORE_GIVING_UP) {
logger.info("Will try to re-init in {} seconds. Attempt {} of {}", retryDelay, errorCounter,
MAX_RETRIES_BEFORE_GIVING_UP);
statusFuture = Optional.of(scheduler.schedule(this::initialize, retryDelay, TimeUnit.SECONDS));
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error fetching data: " + e.getMessage() + ", will retry in " + retryDelay + " seconds");
} else {
logger.info("Too many communication errors, giving up");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error fetching data: " + e.getMessage() + ", not retrying due to too many errors");
errorCounter = 0;
}
}
}
Expand Down

0 comments on commit b7dd366

Please sign in to comment.