|
|
@@ -6,9 +6,11 @@ import io.connector.model.afarcloud.MultiSensor;
|
|
|
import io.connector.model.afarcloud.ResourceMeasurement;
|
|
|
import io.connector.model.afarcloud.SensorTelemetry;
|
|
|
import io.connector.model.sensorthings.*;
|
|
|
-import io.connector.module.afarcloud.AFCClient;
|
|
|
+import io.connector.module.afarcloud.AFCHttpClient;
|
|
|
import io.connector.module.afarcloud.Filter;
|
|
|
import io.vertx.core.json.JsonObject;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
import java.time.Instant;
|
|
|
import java.util.*;
|
|
|
@@ -24,11 +26,26 @@ import static java.lang.String.format;
|
|
|
import static java.util.Arrays.asList;
|
|
|
import static java.util.Optional.ofNullable;
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Lukas Cerny
|
|
|
+ * @version 1.0
|
|
|
+ * @since 1.0
|
|
|
+ */
|
|
|
public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
|
|
|
- private final AFCClient client;
|
|
|
+ private final static Logger logger = LogManager.getLogger(OGCSensorThingsGateway.class);
|
|
|
|
|
|
- public OGCSensorThingsGateway(String id, AFCClient client) {
|
|
|
+ /** Attribute of a client that accesses to the AFarCloud system. */
|
|
|
+ private final AFCHttpClient client;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Public constructor of the class that initializes final attributes.
|
|
|
+ * @param id - identifier of the gateway
|
|
|
+ * @param client - client allows access to the AFarCloud system
|
|
|
+ */
|
|
|
+ public OGCSensorThingsGateway(String id, AFCHttpClient client) {
|
|
|
super(id);
|
|
|
this.client = client;
|
|
|
}
|
|
|
@@ -37,6 +54,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
protected void run() {
|
|
|
|
|
|
router().get(create("Things")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
List<MultiSensor> afcMultiSensors = client.getAllSensors();
|
|
|
List<Thing> ogcThings = Converter.convertToThing(afcMultiSensors, uriComponent);
|
|
|
@@ -44,6 +62,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Things(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
MultiSensor afcMultiSensor = client.getSensorByResourceUrn(resourceUrn);
|
|
|
@@ -52,6 +71,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Datastreams(:id)/Thing")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
String[] idCmp = Converter.disassemblyId(id);
|
|
|
@@ -60,27 +80,25 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("HistoricalLocations(:id)/Thing")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty + time
|
|
|
-
|
|
|
String [] idCmp = Converter.disassemblyId(id);
|
|
|
String resourceUrn = idCmp[0];
|
|
|
-
|
|
|
MultiSensor afcMultiSensor = client.getSensorByResourceUrn(resourceUrn);
|
|
|
Thing ogcThing = Converter.convertToThing(afcMultiSensor, uriComponent);
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(ogcThing.encode());
|
|
|
});
|
|
|
|
|
|
router().get(create("Datastreams(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
String[] idCmp = Converter.disassemblyId(id);
|
|
|
String resourceUrn = idCmp[0], observedProperty = idCmp[1];
|
|
|
MultiSensor afcMultiSensor = client.getSensorByResourceUrn(resourceUrn);
|
|
|
List<ResourceMeasurement> afcMeasurements = client.getLatestObservationsBySensor(new Filter()
|
|
|
- .limit(1)
|
|
|
- .entityNames(afcMultiSensor.getResourceId())
|
|
|
- .measurements(observedProperty)
|
|
|
+ .limit(1).entityNames(afcMultiSensor.getResourceId()).measurements(observedProperty)
|
|
|
);
|
|
|
final Supplier<IllegalArgumentException> exception = () -> new IllegalArgumentException("Can not find Datastream with @iot.id \"" + id + "\".");
|
|
|
|
|
|
@@ -98,6 +116,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Things(:id)/Datastreams")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
MultiSensor afcMultiSensor = client.getSensorByResourceUrn(resourceUrn);
|
|
|
@@ -111,18 +130,21 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Sensors(:id)/Datastreams")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
ctx.reroute(format("%s/Things(%s)/Datastreams", uriComponent.getGatewayPath(), resourceUrn));
|
|
|
});
|
|
|
|
|
|
router().get(create("ObservedProperties(:id)/Datastream")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
ctx.reroute(format("%s/Datastreams(%s)", uriComponent.getGatewayPath(), id));
|
|
|
});
|
|
|
|
|
|
router().get(create("Observations(:id)/Datastream")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resource + measurement + time
|
|
|
String[] idCmp = Converter.disassemblyId(id);
|
|
|
@@ -133,6 +155,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Sensors(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
MultiSensor afcMultiSensor = client.getSensorByResourceUrn(resourceUrn);
|
|
|
@@ -141,6 +164,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Datastreams(:id)/Sensor")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
String[] idCmp = Converter.disassemblyId(id);
|
|
|
@@ -149,6 +173,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("ObservedProperties(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
String[] idCmp = Converter.disassemblyId(id);
|
|
|
@@ -162,12 +187,14 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Datastreams(:id)/ObservedProperty")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
ctx.reroute(format("%s/ObservedProperties(%s)", uriComponent.getGatewayPath(), id));
|
|
|
});
|
|
|
|
|
|
router().get(create("Observations(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resource + measurement + time
|
|
|
@@ -197,6 +224,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Datastreams(:id)/Observations")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty
|
|
|
@@ -238,16 +266,17 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("FeaturesOfInterest(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id");
|
|
|
FeatureOfInterest featureOfInterest = new FeatureOfInterest();
|
|
|
featureOfInterest.setId(id);
|
|
|
featureOfInterest.setSelfLink("https://storage07-afarcloud.qa.pdmfc.com/storage/rest/registry/getAllObservationTypes");
|
|
|
-
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON).end(featureOfInterest.encode());
|
|
|
});
|
|
|
|
|
|
router().get(create("Locations(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty + time
|
|
|
String [] idCmp = Converter.disassemblyId(id);
|
|
|
@@ -275,6 +304,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Things(:id)/Locations")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
|
|
|
@@ -297,12 +327,14 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("HistoricalLocations(:id)/Locations")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty + time
|
|
|
ctx.reroute(format("%s/Locations(%s)", uriComponent.getGatewayPath(), id));
|
|
|
});
|
|
|
|
|
|
router().get(create("HistoricalLocations(:id)")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty + time
|
|
|
|
|
|
@@ -331,6 +363,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Things(:id)/HistoricalLocations")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String resourceUrn = ctx.pathParam("id"); // resourceUrn
|
|
|
@@ -367,6 +400,7 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
});
|
|
|
|
|
|
router().get(create("Locations(:id)/HistoricalLocations")).handler(ctx -> {
|
|
|
+ logger.info("Handling a request: {}.", ctx.request().absoluteURI());
|
|
|
ctx.response().putHeader(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
RequestUriComponent uriComponent = parseUriToComponents(ctx.request());
|
|
|
String id = ctx.pathParam("id"); // resourceUrn + observedProperty + time
|
|
|
@@ -515,6 +549,8 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
|
|
|
private static class Converter {
|
|
|
|
|
|
+ private final static Logger logger = LogManager.getLogger(Converter.class);
|
|
|
+
|
|
|
private static final String DELIMITER = "&";
|
|
|
|
|
|
static String assemblyId(String... parts) {
|
|
|
@@ -526,7 +562,6 @@ public class OGCSensorThingsGateway extends AbstractGateway {
|
|
|
}
|
|
|
|
|
|
static Filter convertFilter(io.connector.model.sensorthings.Filter ogcFilter) {
|
|
|
-
|
|
|
if (ogcFilter == null) {
|
|
|
throw new IllegalArgumentException("Unsupported filter attributes in filter expression '" + ogcFilter + "'.");
|
|
|
}
|