|
@@ -0,0 +1,386 @@
|
|
|
|
|
+package io.connector.module.senslog1;
|
|
|
|
|
+
|
|
|
|
|
+import cz.senslog.common.http.HttpClient;
|
|
|
|
|
+import cz.senslog.common.http.HttpRequest;
|
|
|
|
|
+import cz.senslog.common.http.HttpResponse;
|
|
|
|
|
+import cz.senslog.common.http.URLBuilder;
|
|
|
|
|
+import io.connector.core.config.HostConfig;
|
|
|
|
|
+import io.connector.model.senslog1.*;
|
|
|
|
|
+import io.vertx.core.json.JsonArray;
|
|
|
|
|
+import io.vertx.core.json.JsonObject;
|
|
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.OffsetDateTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+import static cz.senslog.common.http.HttpContentType.TEXT_PLAIN;
|
|
|
|
|
+import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
|
|
+
|
|
|
|
|
+public class SensLog1Client {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger logger = LogManager.getLogger(SensLog1Client.class);
|
|
|
|
|
+
|
|
|
|
|
+ private static final DateTimeFormatter FORMATTER = ofPattern("yyyy-MM-dd HH:mm:ssZ");
|
|
|
|
|
+
|
|
|
|
|
+ private Map<Long, UnitInfo> unitInfoList;
|
|
|
|
|
+
|
|
|
|
|
+ private final SensLog1Config config;
|
|
|
|
|
+ private final HttpClient httpClient;
|
|
|
|
|
+
|
|
|
|
|
+ SensLog1Client(SensLog1Config config, HttpClient httpClient) {
|
|
|
|
|
+ this.config = config;
|
|
|
|
|
+ this.httpClient = httpClient;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<Long, UnitInfo> getUnitInfos() {
|
|
|
|
|
+ if (unitInfoList == null || unitInfoList.isEmpty()) {
|
|
|
|
|
+ units();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// {
|
|
|
|
|
+// Map<Long, UnitInfo> infos = new HashMap<>(1);
|
|
|
|
|
+// for (Map.Entry<Long, UnitInfo> unitEntry : unitInfoList.entrySet()) {
|
|
|
|
|
+// for (SensorInfo sensor : unitEntry.getValue().getSensors()) {
|
|
|
|
|
+// List<SensorInfo> sensors = new ArrayList<>();
|
|
|
|
|
+// sensors.add(sensor);
|
|
|
|
|
+// unitEntry.getValue().setSensors(sensors);
|
|
|
|
|
+// break;
|
|
|
|
|
+// }
|
|
|
|
|
+// infos.put(unitEntry.getKey(), unitEntry.getValue());
|
|
|
|
|
+// break;
|
|
|
|
|
+// }
|
|
|
|
|
+// unitInfoList = infos;
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ return unitInfoList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> lastObservations() {
|
|
|
|
|
+
|
|
|
|
|
+ HostConfig host = config.getSensorServiceHost();
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "GetLastObservations")
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .addParam("group", config.getGroup())
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ JsonArray jsonArray = new JsonArray(response.getBody());
|
|
|
|
|
+ Map<Long, UnitData> units = new HashMap<>(1);
|
|
|
|
|
+ for (Object jsonObject : jsonArray) {
|
|
|
|
|
+ if (jsonObject instanceof JsonObject) {
|
|
|
|
|
+ JsonObject jsonObservations = (JsonObject)jsonObject;
|
|
|
|
|
+ long unitId = jsonObservations.getLong("unitId");
|
|
|
|
|
+ long sensorId = jsonObservations.getLong("sensorId");
|
|
|
|
|
+ SensorObservation sensor = units.computeIfAbsent(unitId, UnitData::new).getSensor(sensorId);
|
|
|
|
|
+ if (sensor == null) {
|
|
|
|
|
+ sensor = new SensorObservation(sensorId);
|
|
|
|
|
+ units.get(unitId).addSensor(sensor);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Observation observation = Observation.parse(jsonObservations);
|
|
|
|
|
+ if (observation != null) {
|
|
|
|
|
+ sensor.addObservation(observation);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new ArrayList<>(units.values());
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void uploadPositions(List<UnitData> unitData) {
|
|
|
|
|
+ HostConfig host = config.getFeederServiceHost();
|
|
|
|
|
+
|
|
|
|
|
+ for (UnitData unit : unitData) {
|
|
|
|
|
+ for (Position position : unit.getPositions()) {
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
+ .contentType(TEXT_PLAIN)
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "InsertPosition")
|
|
|
|
|
+ .addParam("date", position.getTimestamp().format(FORMATTER))
|
|
|
|
|
+ .addParam("unit_id", unit.getId())
|
|
|
|
|
+ .addParam("lat", position.getLatitude())
|
|
|
|
|
+ .addParam("lon", position.getLongitude())
|
|
|
|
|
+ .addParam("alt", position.getAltitude())
|
|
|
|
|
+ .addParam("speed", position.getSpeed())
|
|
|
|
|
+ .addParam("dop", position.getDilutionOfPrecision())
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .GET().build();
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ logger.debug("Parsing body of the response.");
|
|
|
|
|
+ boolean result = Boolean.parseBoolean(response.getBody());
|
|
|
|
|
+
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ logger.warn("Position {} was rejected.", position);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.warn("Position {} was rejected.", position);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> uploadObservations(List<UnitData> unitData) {
|
|
|
|
|
+
|
|
|
|
|
+ HostConfig host = config.getFeederServiceHost();
|
|
|
|
|
+
|
|
|
|
|
+ Map<Long, UnitData> badObservations = new HashMap<>();
|
|
|
|
|
+ for (UnitData unit : unitData) {
|
|
|
|
|
+ for (SensorObservation sensor : unit.getSensors()) {
|
|
|
|
|
+ for (Observation observation : sensor.getObservations()) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
+ .contentType(TEXT_PLAIN)
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "InsertObservation")
|
|
|
|
|
+ .addParam("value", observation.getValue())
|
|
|
|
|
+ .addParam("date", observation.getTimestamp().format(FORMATTER))
|
|
|
|
|
+ .addParam("unit_id", unit.getId())
|
|
|
|
|
+ .addParam("sensor_id", sensor.getId())
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .GET().build();
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ logger.debug("Parsing body of the response.");
|
|
|
|
|
+ boolean result = Boolean.parseBoolean(response.getBody());
|
|
|
|
|
+
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ logger.warn("Observation {} was rejected.", observation);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ SensorObservation sensorObservation = badObservations
|
|
|
|
|
+ .computeIfAbsent(unit.getId(), UnitData::new)
|
|
|
|
|
+ .getSensor(sensor.getId());
|
|
|
|
|
+
|
|
|
|
|
+ if (sensorObservation == null) {
|
|
|
|
|
+ sensorObservation = new SensorObservation(sensor.getId());
|
|
|
|
|
+ badObservations.get(unit.getId()).addSensor(sensorObservation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sensorObservation.addObservation(observation);
|
|
|
|
|
+
|
|
|
|
|
+ logger.warn("Observation {} was rejected.", observation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ */
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new ArrayList<>(badObservations.values());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> positions(OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ List<UnitData> units = new ArrayList<>();
|
|
|
|
|
+ for (UnitInfo unit : getUnitInfos().values()) {
|
|
|
|
|
+ units.addAll(positions(unit.getId(), fromDate, toDate));
|
|
|
|
|
+ }
|
|
|
|
|
+ return units;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> positions(long unitId, OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ UnitData unitData = new UnitData(unitId);
|
|
|
|
|
+ positions(unitData, fromDate, toDate);
|
|
|
|
|
+ return Collections.singletonList(unitData);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void positions(UnitData unit, OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ HostConfig host = config.getDataServiceHost();
|
|
|
|
|
+ logger.info("Getting observations from {}.", host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "GetPositionsDay")
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .addParam("unit_id", unit.getId())
|
|
|
|
|
+ .addParam("fromTime", fromDate.format(FORMATTER))
|
|
|
|
|
+ .addParam("toTime", toDate.format(FORMATTER))
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ logger.info("Creating a http request to {}.", request);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+ logger.info("Received a response with a status: {} for the domain {}.", response.getStatus(), host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ JsonArray json = new JsonArray(response.getBody());
|
|
|
|
|
+ for (Object objJson : json) {
|
|
|
|
|
+ if (objJson instanceof JsonObject) {
|
|
|
|
|
+ Position position = Position.parse((JsonObject)objJson);
|
|
|
|
|
+
|
|
|
|
|
+ if (position != null) {
|
|
|
|
|
+ unit.addPosition(position);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> observations(OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ List<UnitData> units = new ArrayList<>();
|
|
|
|
|
+ for (UnitInfo unit : getUnitInfos().values()) {
|
|
|
|
|
+ units.addAll(observations(unit.getId(), fromDate, toDate));
|
|
|
|
|
+ }
|
|
|
|
|
+ return units;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> observations(long unitId, OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ UnitInfo unitInfo = getUnitInfos().get(unitId);
|
|
|
|
|
+ if (unitInfo != null) {
|
|
|
|
|
+ UnitData unit = new UnitData(unitInfo.getId());
|
|
|
|
|
+ for (SensorInfo sensorInfo : unitInfo.getSensors()) {
|
|
|
|
|
+ SensorObservation sensor = new SensorObservation(sensorInfo.getId());
|
|
|
|
|
+ unit.addSensor(sensor);
|
|
|
|
|
+ observations(unit, sensor, fromDate, toDate);
|
|
|
|
|
+ }
|
|
|
|
|
+ return Arrays.asList(unit);
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitData> observations(long unitId, long sensorId, OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+ UnitData unit = new UnitData(unitId);
|
|
|
|
|
+ SensorObservation sensor = new SensorObservation(sensorId);
|
|
|
|
|
+ unit.addSensor(sensor);
|
|
|
|
|
+ observations(unit, sensor, fromDate, toDate);
|
|
|
|
|
+ return Arrays.asList(unit);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void observations(UnitData unit, SensorObservation sensor, OffsetDateTime fromDate, OffsetDateTime toDate) {
|
|
|
|
|
+
|
|
|
|
|
+ HostConfig host = config.getSensorServiceHost();
|
|
|
|
|
+ logger.info("Getting observations from {}.", host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "GetObservations")
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .addParam("unit_id", unit.getId())
|
|
|
|
|
+ .addParam("sensor_id", sensor.getId())
|
|
|
|
|
+ .addParam("from", fromDate.format(FORMATTER))
|
|
|
|
|
+ .addParam("to", toDate.format(FORMATTER))
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ logger.info("Creating a http request to {}.", request);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+ logger.info("Received a response with a status: {} for the domain {}.", response.getStatus(), host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+
|
|
|
|
|
+ JsonArray json = new JsonArray(response.getBody());
|
|
|
|
|
+ for (Object obsJson : json) {
|
|
|
|
|
+ if (obsJson instanceof JsonObject) {
|
|
|
|
|
+ Observation observation = Observation.parse((JsonObject)obsJson);
|
|
|
|
|
+
|
|
|
|
|
+ if (observation != null) {
|
|
|
|
|
+ sensor.addObservation(observation);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<UnitInfo> units() {
|
|
|
|
|
+ HostConfig host = config.getDataServiceHost();
|
|
|
|
|
+ logger.info("Getting last observations from {}.", host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "GetUnitsList")
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ logger.info("Creating a http request to {}.", request);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+ logger.info("Received a response with a status: {} for the domain {}.", response.getStatus(), host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ logger.debug("Parsing body of the response to the list of class {}.", UnitInfo.class);
|
|
|
|
|
+ JsonArray json = new JsonArray(response.getBody());
|
|
|
|
|
+ List<UnitInfo> units = new ArrayList<>(json.size());
|
|
|
|
|
+ unitInfoList = new HashMap<>(json.size());
|
|
|
|
|
+ for (Object unitJson : json) {
|
|
|
|
|
+ if (unitJson instanceof JsonObject) {
|
|
|
|
|
+ UnitInfo unit = UnitInfo.parse((JsonObject)unitJson);
|
|
|
|
|
+ if (unit != null) {
|
|
|
|
|
+ units.add(unit);
|
|
|
|
|
+ unitInfoList.put(unit.getId(), unit);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (UnitInfo unit : units) {
|
|
|
|
|
+ List<SensorInfo> sensors = sensors(unit.getId());
|
|
|
|
|
+ unit.setSensors(sensors);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return units;
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.error("Can not get data from the server {}. Error {} {}",
|
|
|
|
|
+ host.getDomain(), response.getStatus(), response.getBody());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<SensorInfo> sensors(long unitId) {
|
|
|
|
|
+ HostConfig host = config.getSensorServiceHost();
|
|
|
|
|
+ logger.info("Getting last observations from {}.", host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(host.getDomain(), host.getPath())
|
|
|
|
|
+ .addParam("Operation", "GetSensors")
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .addParam("unit_id", unitId)
|
|
|
|
|
+ .build())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ logger.info("Creating a http request to {}.", request);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+ logger.info("Received a response with a status: {} for the domain {}.", response.getStatus(), host.getDomain());
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isOk()) {
|
|
|
|
|
+ logger.debug("Parsing body of the response to the list of class {}.", SensorInfo.class);
|
|
|
|
|
+ JsonArray json = new JsonArray(response.getBody());
|
|
|
|
|
+ List<SensorInfo> sensors = new ArrayList<>(json.size());
|
|
|
|
|
+ for (Object sensorJson : json) {
|
|
|
|
|
+ if (sensorJson instanceof JsonObject) {
|
|
|
|
|
+ SensorInfo sensor = SensorInfo.parse((JsonObject)sensorJson);
|
|
|
|
|
+ if (sensor != null) {
|
|
|
|
|
+ sensors.add(sensor);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logger.info("For the unit {} was added {} sensors.", unitId, sensors.size());
|
|
|
|
|
+
|
|
|
|
|
+ return sensors;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.error("Can not get data from the server {}. Error {} {}",
|
|
|
|
|
+ host.getDomain(), response.getStatus(), response.getBody());
|
|
|
|
|
+
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|