|
|
@@ -0,0 +1,246 @@
|
|
|
+package io.connector.module.ogc.sensorthings.gateway;
|
|
|
+
|
|
|
+import io.connector.core.AbstractGateway;
|
|
|
+import io.connector.model.afarcloud.Observation;
|
|
|
+import io.connector.model.afarcloud.ResourceMeasurement;
|
|
|
+import io.connector.model.afarcloud.ResponseModel;
|
|
|
+import io.connector.model.afarcloud.SensorTelemetry;
|
|
|
+import io.connector.module.ogc.sensorthings.SensorThingsClient;
|
|
|
+import io.vertx.core.MultiMap;
|
|
|
+import io.vertx.core.http.HttpServerRequest;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static cz.senslog.common.http.HttpContentType.APPLICATION_JSON;
|
|
|
+import static cz.senslog.common.http.HttpHeader.CONTENT_TYPE;
|
|
|
+import static io.connector.core.AddressPath.Creator.create;
|
|
|
+import static io.vertx.core.json.Json.encode;
|
|
|
+import static java.lang.Double.parseDouble;
|
|
|
+import static java.lang.Integer.parseInt;
|
|
|
+import static java.lang.String.format;
|
|
|
+import static java.util.Arrays.asList;
|
|
|
+import static java.util.Collections.emptyList;
|
|
|
+import static java.util.Collections.singletonList;
|
|
|
+import static java.util.Comparator.comparing;
|
|
|
+
|
|
|
+public class AFarCloudGateway extends AbstractGateway {
|
|
|
+
|
|
|
+ private final SensorThingsClient client;
|
|
|
+
|
|
|
+ public AFarCloudGateway(String id, SensorThingsClient client) {
|
|
|
+ super(id);
|
|
|
+ this.client = client;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getRequiredParam(MultiMap params, String paramName) {
|
|
|
+ String param = getParam(params, paramName);
|
|
|
+ if (param == null) {
|
|
|
+ throw new IllegalArgumentException(format("Parameter '%s' is required.", paramName));
|
|
|
+ }
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getParam(MultiMap params, String paramName) {
|
|
|
+ return params.contains(paramName) ? params.get(paramName) : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getParam(MultiMap params, String paramName, String defValue) {
|
|
|
+ return params.contains(paramName) ? params.get(paramName) : defValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> getSplittedParam(MultiMap params, String paramName, String splitter) {
|
|
|
+ String param = getParam(params, paramName);
|
|
|
+ return param != null ? asList(param.split(splitter)) : emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void run() {
|
|
|
+
|
|
|
+ // Query sensor telemetry
|
|
|
+ router().get(create("getSensorTelemetry/latest")).handler(ctx -> {
|
|
|
+
|
|
|
+ HttpServerRequest req = ctx.request();
|
|
|
+ MultiMap params = req.params();
|
|
|
+
|
|
|
+ final int limit = parseInt(getRequiredParam(params, "limit"));
|
|
|
+ final List<String> entityNames = getSplittedParam(params, "entityNames", ",");
|
|
|
+ final List<String> devices = getSplittedParam(params, "devices", ",");
|
|
|
+ final List<String> services = getSplittedParam(params, "services", ",");
|
|
|
+ final List<String> types = getSplittedParam(params, "types", ",");
|
|
|
+ final List<String> providers = getSplittedParam(params, "providers", ",");
|
|
|
+ final List<String> measurements = getSplittedParam(params, "measurements", ",");
|
|
|
+ final Integer minAltitude = params.contains("altitude") ? parseInt(params.get("altitude")) : null; // TODO
|
|
|
+ final String order = getParam(params, "order", "DESC").toUpperCase();
|
|
|
+
|
|
|
+ ResponseModel<SensorTelemetry> responseModel = new ResponseModel<>();
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> entry : params.entries()) {
|
|
|
+ responseModel.addParam(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ responseModel.setQuery(req.path()+"&"+req.query());
|
|
|
+
|
|
|
+ // GET all telemetries
|
|
|
+ List<SensorTelemetry> telemetries = new ArrayList<>(limit);
|
|
|
+ for (int i = 0; i < limit; i++) {
|
|
|
+ SensorTelemetry telemetry = new SensorTelemetry();
|
|
|
+ telemetry.setMeasurement("wind_speed");
|
|
|
+
|
|
|
+ Observation observation = new Observation();
|
|
|
+ observation.setTime("2020-04-19T14:17:00Z");
|
|
|
+ observation.setAltitude(282);
|
|
|
+ observation.setLatitude(50.0393219);
|
|
|
+ observation.setLongitude(14.6106731);
|
|
|
+ observation.setGeohash("u2fksmrhjmeq");
|
|
|
+ observation.setValue(0.55);
|
|
|
+ observation.setEntityName("1305167562287832");
|
|
|
+ observation.setSequenceNumber("123");
|
|
|
+ observation.setService("environmentalObservations");
|
|
|
+ observation.setProvider("LESP");
|
|
|
+ observation.setType("air_sensor");
|
|
|
+ observation.setUom("http://qudt.org/vocab/unit/M-PER-SEC");
|
|
|
+ telemetry.addObservation(observation);
|
|
|
+
|
|
|
+ telemetries.add(telemetry);
|
|
|
+ }
|
|
|
+ responseModel.setNumResults(telemetries.size());
|
|
|
+
|
|
|
+ Comparator<SensorTelemetry> comparator = comparing(SensorTelemetry::getMeasurement);
|
|
|
+ if (order.equals("DESC")) {
|
|
|
+ comparator = comparator.reversed();
|
|
|
+ }
|
|
|
+ telemetries.sort(comparator);
|
|
|
+
|
|
|
+ responseModel.setResults(telemetries);
|
|
|
+ ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(encode(responseModel));
|
|
|
+ });
|
|
|
+
|
|
|
+ router().get(create("getObservationsBySensor/latest")).handler(ctx -> {
|
|
|
+
|
|
|
+ HttpServerRequest req = ctx.request();
|
|
|
+ MultiMap params = req.params();
|
|
|
+
|
|
|
+ final int limit = parseInt(getRequiredParam(params, "limit"));
|
|
|
+ final List<String> entityNames = getSplittedParam(params, "entityNames", ",");
|
|
|
+ final List<String> devices = getSplittedParam(params, "devices", ",");
|
|
|
+ final List<String> services = getSplittedParam(params, "services", ",");
|
|
|
+ final List<String> types = getSplittedParam(params, "types", ",");
|
|
|
+ final List<String> providers = getSplittedParam(params, "providers", ",");
|
|
|
+ final List<String> measurements = getSplittedParam(params, "measurements", ",");
|
|
|
+ final String order = getParam(params, "order", "DESC").toUpperCase();
|
|
|
+ final Integer minAltitude = params.contains("altitude") ? parseInt(params.get("altitude")) : null; // TODO
|
|
|
+ final Double centrLong = params.contains("centr_long") ? parseDouble(params.get("centr_long")) : null; // TODO
|
|
|
+ final Double centrLat = params.contains("centr_lat") ? parseDouble(params.get("centr_lat")) : null; // TODO
|
|
|
+ final Integer radius = params.contains("radius") ? parseInt(params.get("radius")) : null; // TODO
|
|
|
+
|
|
|
+ ResponseModel<ResourceMeasurement> responseModel = new ResponseModel<>();
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> entry : params.entries()) {
|
|
|
+ responseModel.addParam(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ responseModel.setQuery(req.path()+"&"+req.query());
|
|
|
+
|
|
|
+ ResourceMeasurement measurement = new ResourceMeasurement();
|
|
|
+ measurement.setResource("10002376");
|
|
|
+ responseModel.setResults(singletonList(measurement));
|
|
|
+
|
|
|
+ SensorTelemetry telemetry = new SensorTelemetry();
|
|
|
+ telemetry.setMeasurement("wind_speed");
|
|
|
+ measurement.setMeasurements(singletonList(telemetry));
|
|
|
+
|
|
|
+ for (int i = 0; i < limit; i++) {
|
|
|
+ Observation observation = new Observation();
|
|
|
+ observation.setTime("2020-04-19T14:17:00Z");
|
|
|
+ observation.setAltitude(282);
|
|
|
+ observation.setLatitude(50.0393219);
|
|
|
+ observation.setLongitude(14.6106731);
|
|
|
+ observation.setGeohash("u2fksmrhjmeq");
|
|
|
+ observation.setValue(0.55);
|
|
|
+ observation.setEntityName("1305167562287832");
|
|
|
+ observation.setSequenceNumber("123");
|
|
|
+ observation.setService("environmentalObservations");
|
|
|
+ observation.setProvider("LESP");
|
|
|
+ observation.setType("air_sensor");
|
|
|
+ observation.setUom("http://qudt.org/vocab/unit/M-PER-SEC");
|
|
|
+ telemetry.addObservation(observation);
|
|
|
+ }
|
|
|
+
|
|
|
+ int counter = 0;
|
|
|
+ for (ResourceMeasurement result : responseModel.getResults()) {
|
|
|
+
|
|
|
+ Comparator<SensorTelemetry> comparator = comparing(SensorTelemetry::getMeasurement);
|
|
|
+ if (order.equals("DESC")) {
|
|
|
+ comparator = comparator.reversed();
|
|
|
+ }
|
|
|
+ result.getMeasurements().sort(comparator);
|
|
|
+
|
|
|
+ for (SensorTelemetry resultMeasurement : result.getMeasurements()) {
|
|
|
+ counter += resultMeasurement.getObservations().size();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ responseModel.setNumResults(counter);
|
|
|
+
|
|
|
+ ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(encode(responseModel));
|
|
|
+ });
|
|
|
+
|
|
|
+ // Query sensor telemetry interval
|
|
|
+ router().get(create("getSensorTelemetry/historic")).handler(ctx -> {
|
|
|
+
|
|
|
+ HttpServerRequest req = ctx.request();
|
|
|
+ MultiMap params = req.params();
|
|
|
+
|
|
|
+ final int limit = 100;
|
|
|
+ final String startTime = getRequiredParam(params, "start_time");
|
|
|
+ final String endTime = getParam(params, "end_time");
|
|
|
+
|
|
|
+ ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(encode(null));
|
|
|
+ });
|
|
|
+
|
|
|
+ router().get(create("getObservationsBySensor/historic")).handler(ctx -> {
|
|
|
+
|
|
|
+ HttpServerRequest req = ctx.request();
|
|
|
+ MultiMap params = req.params();
|
|
|
+
|
|
|
+ final int limit = 100;
|
|
|
+ final String startTime = getRequiredParam(params, "start_time");
|
|
|
+ final String endTime = getParam(params, "end_time");
|
|
|
+
|
|
|
+ ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(encode(null));
|
|
|
+ });
|
|
|
+
|
|
|
+ // Query region telemetry
|
|
|
+ router().get(create("getRegionTelemetry/latest")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // Query region telemetry interval
|
|
|
+ router().get(create("getRegionTelemetry/historic")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // Query collar telemetry
|
|
|
+ router().get(create("getCollarTelemetry/latest")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ router().get(create("getObservationsByCollar/latest")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ router().get(create("getObservationsByCollar/historic")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // Query collar telemetry interval
|
|
|
+ router().get(create("getCollarTelemetry/historic")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // Schema-measurement
|
|
|
+ router().get(create("getMeasurements")).handler(ctx -> {
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|