added td discovery for humidity and robot executor
This commit is contained in:
@@ -3,10 +3,23 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.5</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.unisg</groupId>
|
||||
<artifactId>executor-humidity</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>executor-humidity</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -30,6 +43,12 @@
|
||||
<groupId>org.eclipse.californium</groupId>
|
||||
<artifactId>californium-core</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.Interactions-HSG</groupId>
|
||||
<artifactId>wot-td-java</artifactId>
|
||||
<version>0.1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -38,4 +57,21 @@
|
||||
<maven.compiler.target>16</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
@@ -1,12 +1,26 @@
|
||||
package ch.unisg.executorhumidity.executor.adapter.out;
|
||||
|
||||
import ch.unisg.executorhumidity.executor.application.port.out.GetHumidityPort;
|
||||
import ch.unisg.ics.interactions.wot.td.ThingDescription;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.Form;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.PropertyAffordance;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDCoapRequest;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDCoapResponse;
|
||||
import ch.unisg.ics.interactions.wot.td.io.TDGraphReader;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.vocabularies.TD;
|
||||
import org.eclipse.californium.core.CoapClient;
|
||||
import org.eclipse.californium.elements.exception.ConnectorException;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@Component
|
||||
@@ -16,17 +30,49 @@ public class GetHumidityAdapter implements GetHumidityPort {
|
||||
@Override
|
||||
public String getHumidity() {
|
||||
|
||||
CoapClient client1 = new CoapClient("coap://130.82.171.10:5683/humidity");
|
||||
String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine";
|
||||
|
||||
String input = "@prefix dct: <http://purl.org/dc/terms/> . select ?title where { ?title dct:title 'Mirogate' }";
|
||||
|
||||
var httpRequest = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint))
|
||||
.header("Content-Type", "application/sparql-query")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(input))
|
||||
.build();
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
String result = null;
|
||||
try {
|
||||
result = client1.get().getResponseText();
|
||||
} catch (ConnectorException e) {
|
||||
e.printStackTrace();
|
||||
String description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body();
|
||||
|
||||
// Parse a TD from a string
|
||||
ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description);
|
||||
|
||||
Optional<PropertyAffordance> humidity = td.getPropertyByName("humidity");
|
||||
|
||||
if (humidity.isPresent()) {
|
||||
Optional<Form> form = humidity.get().getFirstFormForOperationType(TD.readProperty);
|
||||
|
||||
if (form.isPresent()) {
|
||||
TDCoapRequest request = new TDCoapRequest(form.get(), TD.readProperty);
|
||||
try {
|
||||
TDCoapResponse response = request.execute();
|
||||
|
||||
Map<String, Object> payload = response.getPayloadAsObject((ObjectSchema) humidity.get().getDataSchema());
|
||||
Object result = payload.get("https://interactions.ics.unisg.ch/mirogate#HumidityValue");
|
||||
return result.toString();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -11,9 +11,8 @@ import java.io.IOException;
|
||||
public class Executor extends ExecutorBase{
|
||||
|
||||
private static final Executor executor = new Executor(ExecutorType.HUMIDITY);
|
||||
// TODO: Add the necessary ports
|
||||
private final GetHumidityPort getHumidityPort = new GetHumidityAdapter();
|
||||
|
||||
private final GetHumidityPort getHumidityPort = new GetHumidityAdapter();
|
||||
|
||||
private Executor(ExecutorType executorType) {super(executorType);}
|
||||
|
||||
|
Reference in New Issue
Block a user