Lukas Cerny 5 éve
szülő
commit
6ecb99283d

+ 4 - 3
config/test.yaml

@@ -57,7 +57,8 @@ scheduler:
       period: 30
       consumer: "schedule-observations"
       config:
-        startDate: "2020-01-01T00:00:00.000" # yyyy-MM-DD hh:mm:ss.sss
+        startDate: "2020-01-01 12:10:00[Europe/Prague]" # yyyy-MM-DD hh:mm:ss[zone]
         allowedStations:
-          # unitId: [sensorId...] // [] = all sensors
-          10002222: []
+          # unitId: [sensorId...]
+#          10002222: [410010000, 560030000, 340020000, 380010000, 380090000]
+          10002376: [410010000, 560030000, 340020000, 380010000, 380090000]

+ 15 - 1
connector-core/src/main/java/io/connector/core/config/PropertyConfig.java

@@ -5,6 +5,7 @@ import cz.senslog.common.util.ClassUtils;
 
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 
@@ -108,7 +109,6 @@ public class PropertyConfig {
             return date.toInstant().atZone(ZoneOffset.systemDefault()).toLocalDateTime();
         } else if (object instanceof String) {
             return LocalDateTime.parse((String)object, DateTimeFormatter.ISO_DATE_TIME);
-
         } else {
             throw new ClassCastException(format(
                     "Property '%s' can not be cast to %s", getNewPropertyId(name), LocalDateTime.class)
@@ -116,6 +116,20 @@ public class PropertyConfig {
         }
     }
 
+    public ZonedDateTime getZonedDateTimeProperty(String name) {
+        Object object = getProperty(name);
+
+        if (object instanceof ZonedDateTime) {
+            return (ZonedDateTime)object;
+        } else if (object instanceof String) {
+            return ZonedDateTime.parse((String)object, DateTimeFormatter.ISO_ZONED_DATE_TIME);
+        } else {
+            throw new ClassCastException(format(
+                    "Property '%s' can not be cast to %s", getNewPropertyId(name), ZonedDateTime.class)
+            );
+        }
+    }
+
     /**
      * Returns property as a optional of LocalDateTime
      * @param name - name of property.

+ 2 - 4
connector-model/src/main/java/io/connector/model/senslog1/SensorData.java

@@ -20,10 +20,8 @@ public class SensorData {
         long id = jsonObject.getLong("id");
         JsonArray observationsJson = jsonObject.getJsonArray("observations");
         List<Observation> observations = new ArrayList<>(observationsJson.size());
-        for (Object observation : observationsJson) {
-            if (observation instanceof JsonObject) {
-                observations.add(Observation.parse((JsonObject)observation));
-            }
+        for (int i = 0; i < observationsJson.size(); i++) {
+            observations.add(Observation.parse(observationsJson.getJsonObject(i)));
         }
         return new SensorData(id, observations);
     }

+ 3 - 3
connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1Client.java → connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1HttpClient.java

@@ -19,9 +19,9 @@ import static cz.senslog.common.http.HttpContentType.TEXT_PLAIN;
 import static java.lang.String.format;
 import static java.time.format.DateTimeFormatter.ofPattern;
 
-public class SensLog1Client {
+public class SensLog1HttpClient {
 
-    private static final Logger logger = LogManager.getLogger(SensLog1Client.class);
+    private static final Logger logger = LogManager.getLogger(SensLog1HttpClient.class);
 
     private static final DateTimeFormatter FORMATTER = ofPattern("yyyy-MM-dd HH:mm:ssZ");
 
@@ -30,7 +30,7 @@ public class SensLog1Client {
     private final SensLog1Config config;
     private final HttpClient httpClient;
 
-    SensLog1Client(SensLog1Config config, HttpClient httpClient) {
+    SensLog1HttpClient(SensLog1Config config, HttpClient httpClient) {
         this.config = config;
         this.httpClient = httpClient;
     }

+ 9 - 5
connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1Module.java

@@ -5,24 +5,28 @@ import io.connector.core.config.SchedulerConfig;
 import io.connector.core.AbstractModule;
 import io.connector.core.ModuleInfo;
 import io.connector.module.senslog1.gateway.AFCGateway;
+import io.connector.module.senslog1.gateway.OGCSensorThingsGateway;
 import io.connector.module.senslog1.gateway.SensLog1Gateway;
 import io.vertx.core.json.JsonObject;
 
 public class SensLog1Module extends AbstractModule {
 
-    private final SensLog1Client client;
+    private final SensLog1HttpClient httpClient;
+    private final SensLog1SQLClient sqlClient;
 
-    protected SensLog1Module(String id, ModuleDescriptor descriptor, SensLog1Client client) {
+    protected SensLog1Module(String id, ModuleDescriptor descriptor, SensLog1HttpClient client, SensLog1SQLClient sqlClient) {
         super(id, descriptor);
-        this.client = client;
+        this.httpClient = client;
+        this.sqlClient = sqlClient;
     }
 
     @Override
     public void run() {
-        registerGateway(new AFCGateway("AFarCloud", client));
+        registerGateway(new AFCGateway("AFarCloud", httpClient));
+        registerGateway(new OGCSensorThingsGateway("OGCSensorThings", sqlClient));
 
         SensLog1SchedulerConfig schedulerConfig = new SensLog1SchedulerConfig(descriptor.getSchedulerConfig());
-        registerGateway(new SensLog1Gateway("SensLogV1", schedulerConfig, client));
+        registerGateway(new SensLog1Gateway("SensLogV1", schedulerConfig, httpClient));
     }
 
     @Override

+ 5 - 2
connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1ModuleProvider.java

@@ -11,8 +11,11 @@ public final class SensLog1ModuleProvider implements ModuleProvider {
     public AbstractModule createModule(ModuleDescriptor descriptor) {
 
         SensLog1Config config = new SensLog1Config(descriptor.getServiceConfig());
-        SensLog1Client client = new SensLog1Client(config, HttpClient.newHttpClient());
-        SensLog1Module module = new SensLog1Module("SensLogV1", descriptor, client);
+
+        SensLog1HttpClient httpClient = new SensLog1HttpClient(config, HttpClient.newHttpClient());
+        SensLog1SQLClient sqlClient = new SensLog1SQLClient();
+
+        SensLog1Module module = new SensLog1Module("SensLogV1", descriptor, httpClient, sqlClient);
 
         return module;
     }

+ 6 - 0
connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1SQLClient.java

@@ -0,0 +1,6 @@
+package io.connector.module.senslog1;
+
+public class SensLog1SQLClient {
+
+
+}

+ 5 - 5
connector-module-senslog1/src/main/java/io/connector/module/senslog1/SensLog1SchedulerConfig.java

@@ -3,23 +3,23 @@ package io.connector.module.senslog1;
 import io.connector.core.config.AllowedStation;
 import io.connector.core.config.SchedulerConfig;
 
-import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
 
 public class SensLog1SchedulerConfig {
 
     private final AllowedStation allowedStations;
-    private final LocalDateTime startDate;
+    private final OffsetDateTime startDate;
 
     SensLog1SchedulerConfig(SchedulerConfig config) {
-        this.allowedStations = new AllowedStation(config.getPropertyConfig("allowedStation"));
-        this.startDate = config.getLocalDateTimeProperty("startDate");
+        this.allowedStations = new AllowedStation(config.getPropertyConfig("allowedStations"));
+        this.startDate = config.getZonedDateTimeProperty("startDate").toOffsetDateTime();
     }
 
     public AllowedStation getAllowedStations() {
         return allowedStations;
     }
 
-    public LocalDateTime getStartDate() {
+    public OffsetDateTime getStartDate() {
         return startDate;
     }
 }

+ 3 - 3
connector-module-senslog1/src/main/java/io/connector/module/senslog1/gateway/AFCGateway.java

@@ -1,14 +1,14 @@
 package io.connector.module.senslog1.gateway;
 
 import io.connector.core.AbstractGateway;
-import io.connector.module.senslog1.SensLog1Client;
+import io.connector.module.senslog1.SensLog1HttpClient;
 
 
 public class AFCGateway extends AbstractGateway {
 
-    private final SensLog1Client client;
+    private final SensLog1HttpClient client;
 
-    public AFCGateway(String id, SensLog1Client client) {
+    public AFCGateway(String id, SensLog1HttpClient client) {
         super(id);
         this.client = client;
     }

+ 19 - 0
connector-module-senslog1/src/main/java/io/connector/module/senslog1/gateway/OGCSensorThingsGateway.java

@@ -0,0 +1,19 @@
+package io.connector.module.senslog1.gateway;
+
+import io.connector.core.AbstractGateway;
+import io.connector.module.senslog1.SensLog1SQLClient;
+
+public class OGCSensorThingsGateway extends AbstractGateway {
+
+    private final SensLog1SQLClient client;
+
+    public OGCSensorThingsGateway(String id, SensLog1SQLClient client) {
+        super(id);
+        this.client = client;
+    }
+
+    @Override
+    protected void run() {
+
+    }
+}

+ 27 - 25
connector-module-senslog1/src/main/java/io/connector/module/senslog1/gateway/SensLog1Gateway.java

@@ -7,7 +7,7 @@ import io.connector.core.Message;
 import io.connector.core.MessageHeader;
 import io.connector.core.config.AllowedStation;
 import io.connector.model.senslog1.*;
-import io.connector.module.senslog1.SensLog1Client;
+import io.connector.module.senslog1.SensLog1HttpClient;
 import io.connector.module.senslog1.SensLog1SchedulerConfig;
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
@@ -19,15 +19,14 @@ import java.util.*;
 
 import static java.time.OffsetDateTime.MAX;
 import static java.time.OffsetDateTime.MIN;
-import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;
 import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
 
 public class SensLog1Gateway extends AbstractGateway {
 
-    private final SensLog1Client client;
+    private final SensLog1HttpClient client;
     private final SensLog1SchedulerConfig schedulerConfig;
 
-    public SensLog1Gateway(String id, SensLog1SchedulerConfig schedulerConfig, SensLog1Client client) {
+    public SensLog1Gateway(String id, SensLog1SchedulerConfig schedulerConfig, SensLog1HttpClient client) {
         super(id, true);
         this.client = client;
         this.schedulerConfig = schedulerConfig;
@@ -71,14 +70,10 @@ public class SensLog1Gateway extends AbstractGateway {
                 }
             }
             JsonObject configBody = new JsonObject()
-                    .put("startDate", schedulerConfig.getStartDate().format(ISO_DATE_TIME))
+                    .put("startDate", schedulerConfig.getStartDate().format(ISO_OFFSET_DATE_TIME))
                     .put("allowedStations", stations);
 
-
-            message.reply(configBody).options()
-                    .addHeader("fromDate", "2020-02-10T06:30:00+01:00") // from.format(ISO_OFFSET_DATE_TIME))
-                    .addHeader("toDate", "2020-02-10T07:30:00+01:00") // to.format(ISO_OFFSET_DATE_TIME))
-                    .addHeader("unitId", "");
+            message.reply(configBody);
         });
 
         event().consume("units", message -> {
@@ -156,25 +151,32 @@ public class SensLog1Gateway extends AbstractGateway {
 
         event().consume("observations-with-info", message -> {
 
-            MultiMap params = message.headers();
-            Tuple<OffsetDateTime, OffsetDateTime> timeRange = getTimeRangeFromParam(message);
-            if (timeRange == null) { return; }
-            OffsetDateTime fromDate = timeRange.getItem1(), toDate = timeRange.getItem2();
-
-            List<Unit> unitData;
-            if (params.contains("unitId")) {
-                long unitId = Long.parseLong(params.get("unitId"));
-                if (params.contains("sensorId")) {
-                    long sensorId = Long.parseLong(params.get("sensorId"));
-                    unitData = client.observationsWithInfo(unitId, sensorId, fromDate, toDate);
+            final int hoursInterval = 2;
+
+            if (message.body() != null && message.body() instanceof JsonObject) {
+                JsonObject filter = (JsonObject)message.body();
+                String startDateStr = filter.getString("startDate");
+                OffsetDateTime startDate = startDateStr != null ? OffsetDateTime.parse(startDateStr, ISO_OFFSET_DATE_TIME) : null;
+                if (filter.containsKey("allowedStations")) {
+                    JsonArray allowedStations = filter.getJsonArray("allowedStations");
+                    List<Unit> unitData = new ArrayList<>();
+                    for (int i = 0; i < allowedStations.size(); i++) {
+                        JsonObject station = allowedStations.getJsonObject(i);
+                        String unitId = station.getString("id");
+                        OffsetDateTime fromDate = OffsetDateTime.parse(station.getString("fromDate"), ISO_OFFSET_DATE_TIME);
+                        unitData.addAll(client.observationsWithInfo(Long.parseLong(unitId), fromDate, fromDate.plusHours(hoursInterval)));
+                    }
+                    // TODO filter by sensors
+                    message.reply(new DataCollection<>(unitData));
+                } else if (startDate != null) {
+                    List<Unit> unitData = client.observationsWithInfo(startDate, startDate.plusHours(hoursInterval));
+                    message.reply(new DataCollection<>(unitData));
                 } else {
-                    unitData = client.observationsWithInfo(unitId, fromDate, toDate);
+                    message.fail(400, "Attribute 'fromDate' is required.");
                 }
             } else {
-                unitData = client.observationsWithInfo(fromDate, toDate);
+                message.fail(400, "Attribute 'fromDate' is required.");
             }
-
-            message.reply(new DataCollection<>(unitData));
         });
 
         event().consume("observations", message -> {