|
|
@@ -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 -> {
|