|
@@ -115,10 +115,15 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
|
|
|
|
|
|
|
|
private String getAccessToken() {
|
|
private String getAccessToken() {
|
|
|
|
|
|
|
|
- if (ofEpochSecond(authTokens.access.getPayload().getExp()).isAfter(Instant.now())) {
|
|
|
|
|
|
|
+ Instant tokenExp = ofEpochSecond(authTokens.access.getPayload().getExp());
|
|
|
|
|
+ Instant now = Instant.now();
|
|
|
|
|
+
|
|
|
|
|
+ logger.info("Access token: exp: {}, now: {}", tokenExp, now);
|
|
|
|
|
+ if (tokenExp.isAfter(now)) {
|
|
|
return authTokens.access.getRaw();
|
|
return authTokens.access.getRaw();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ logger.info("Access token expired. Requesting new one.");
|
|
|
Map<String, String> body = new HashMap<>();
|
|
Map<String, String> body = new HashMap<>();
|
|
|
body.put("refresh", authTokens.refresh);
|
|
body.put("refresh", authTokens.refresh);
|
|
|
|
|
|
|
@@ -158,6 +163,11 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
|
|
|
OffsetDateTime endAt = startAt.plusHours(config.getPeriod());
|
|
OffsetDateTime endAt = startAt.plusHours(config.getPeriod());
|
|
|
|
|
|
|
|
String accessToken = getAccessToken();
|
|
String accessToken = getAccessToken();
|
|
|
|
|
+ if (accessToken == null) {
|
|
|
|
|
+ logger.warn("Can not get valid access token. Try the next period.");
|
|
|
|
|
+ return SoilscountModel.emptyModel();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
.url(URLBuilder.newBuilder(config.getMeasurementsUrl())
|
|
.url(URLBuilder.newBuilder(config.getMeasurementsUrl())
|
|
|
.addParam("since", startAt.format(DateTimeFormatter.ISO_DATE_TIME))
|
|
.addParam("since", startAt.format(DateTimeFormatter.ISO_DATE_TIME))
|
|
@@ -171,9 +181,8 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
|
|
|
HttpResponse response = httpClient.send(request);
|
|
HttpResponse response = httpClient.send(request);
|
|
|
|
|
|
|
|
if (response.isError()) {
|
|
if (response.isError()) {
|
|
|
- throw logger.throwing(new IllegalStateException(format(
|
|
|
|
|
- "Can not get new measurements. %s", response.getBody()
|
|
|
|
|
- )));
|
|
|
|
|
|
|
+ logger.error("Can not get new measurements. {}", response.getBody());
|
|
|
|
|
+ return SoilscountModel.emptyModel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Map<?, ?> measureMap = jsonToObject(response.getBody(), Map.class);
|
|
Map<?, ?> measureMap = jsonToObject(response.getBody(), Map.class);
|