Lukas Cerny пре 10 месеци
родитељ
комит
887ad9c71d

+ 2 - 1
config/senslog1Analytics.yaml

@@ -29,4 +29,5 @@ connectors:
         fetcher: "Senslog"
         pusher: "Analytics"
         period: 600 # 10 minutes
-        initDelay: 5
+        startAt: "00:10:00" # at 10 AM
+#        initDelay: 5

+ 3 - 0
config/soilscountToSenslog.yaml

@@ -6,6 +6,9 @@ settings:
       startDate: "2024-11-10T00:00:00"
       period: 1  # period in hours
 
+#      OpenAPI Spec for login: https://www.soilscouts.fi/api/v1/
+#      OpenAPI Spec for measurements: https://www.soilscouts.fi/api/v2/
+
       authUrl: "https://www.soilscouts.fi/api/v1/auth/login//" # keep the // (cos the bad implementation of the resource-side)
       refreshUrl: "https://www.soilscouts.fi/api/v1/auth/token/refresh//" # keep the //
       devicesUrl: "https://www.soilscouts.fi/api/v1/devices/"

+ 1 - 1
connector-app/src/main/java/cz/senslog/connector/app/Application.java

@@ -142,6 +142,6 @@ class Application extends Thread {
             logger.warn("No connectors were loaded.");
         }
 
-        interrupt();
+//        interrupt();
     }
 }

+ 13 - 4
connector-fetch-soilscount/src/main/java/cz/senslog/connector/fetch/soilscount/SoilScountFetcher.java

@@ -115,10 +115,15 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
 
     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();
         }
 
+        logger.info("Access token expired. Requesting new one.");
         Map<String, String> body = new HashMap<>();
         body.put("refresh", authTokens.refresh);
 
@@ -158,6 +163,11 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
         OffsetDateTime endAt = startAt.plusHours(config.getPeriod());
 
         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()
                 .url(URLBuilder.newBuilder(config.getMeasurementsUrl())
                         .addParam("since", startAt.format(DateTimeFormatter.ISO_DATE_TIME))
@@ -171,9 +181,8 @@ public class SoilScountFetcher implements ConnectorFetcher<SessionModel, Soilsco
         HttpResponse response = httpClient.send(request);
 
         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);

+ 5 - 0
connector-model/src/main/java/cz/senslog/connector/model/soilscount/SoilscountModel.java

@@ -3,10 +3,15 @@ package cz.senslog.connector.model.soilscount;
 import cz.senslog.connector.model.api.AbstractModel;
 
 import java.time.OffsetDateTime;
+import java.util.Collections;
 import java.util.List;
 
 public class SoilscountModel extends AbstractModel {
 
+    public static SoilscountModel emptyModel() {
+        return new SoilscountModel(Collections.emptyList(), Collections.emptyList(), OffsetDateTime.MIN, OffsetDateTime.MAX);
+    }
+
     private final List<Device> devices;
     private final List<Measurement> measurement;
 

+ 5 - 0
connector-model/src/main/java/cz/senslog/connector/model/v1/Record.java

@@ -6,6 +6,7 @@ package cz.senslog.connector.model.v1;
 import java.time.OffsetDateTime;
 import java.time.ZonedDateTime;
 
+import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
 import static java.time.format.DateTimeFormatter.ofPattern;
 
 
@@ -37,4 +38,8 @@ public abstract class Record {
         this.time = time;
     }
 
+    @Override
+    public String toString() {
+        return String.format("%s<unitId=%s | time=%s>", this.getClass().getSimpleName(), unitId, time.format(ISO_OFFSET_DATE_TIME));
+    }
 }

+ 10 - 7
connector-push-senslog-v1/src/main/java/cz/senslog/connector/push/rest/senslog/v1/SenslogV1Pusher.java

@@ -126,15 +126,18 @@ class  SenslogV1Pusher implements ConnectorPusher<SenslogV1Model> {
     public void push(SenslogV1Model model) {
         if (model == null) { return; }
 
-        if (model.getObservations() != null) {
+        if (model.getObservations() == null || model.getObservations().isEmpty()) {
+            logger.warn("Model has no observations."); return;
+        }
 
-            List<Record> observations = model.getObservations();
-            logger.info("Received {} new observations from {} to {} to push.",
-                    observations.size(), model.getFrom(), model.getTo());
 
-            logger.debug("Adding all new observations to the queue.");
-            observationQueue.addAll(observations);
-        }
+        List<Record> observations = model.getObservations();
+        logger.info("Received {} new observations from {} to {} to push.",
+                observations.size(), model.getFrom(), model.getTo());
+
+        logger.debug("Adding all new observations to the queue.");
+        observationQueue.addAll(observations);
+
 
         if (!failedObservations.isEmpty()) {
             logger.info("Adding {} failed observations to the queue.", failedObservations.size());