added td discovery for humidity and robot executor
This commit is contained in:
@@ -17,16 +17,16 @@ public class DeleteUserFromRobotAdapter implements DeleteUserFromRobotPort {
|
||||
@Override
|
||||
public boolean deleteUserFromRobot(String key) {
|
||||
|
||||
String url = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/user/" + key;
|
||||
|
||||
String url = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.4/user/" + key;
|
||||
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("Content-Type", "application/json")
|
||||
.DELETE()
|
||||
.build();
|
||||
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
|
||||
try {
|
||||
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println(response.statusCode());
|
||||
@@ -38,5 +38,5 @@ public class DeleteUserFromRobotAdapter implements DeleteUserFromRobotPort {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -4,7 +4,21 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import ch.unisg.ics.interactions.wot.td.ThingDescription;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.Form;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDHttpRequest;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDHttpResponse;
|
||||
import ch.unisg.ics.interactions.wot.td.io.TDGraphReader;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.DataSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.StringSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.security.APIKeySecurityScheme;
|
||||
import ch.unisg.ics.interactions.wot.td.vocabularies.TD;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -16,9 +30,70 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort {
|
||||
|
||||
@Override
|
||||
public boolean instructionToRobot(String key) {
|
||||
|
||||
|
||||
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/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(input))
|
||||
.build();
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
try {
|
||||
var description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body();
|
||||
|
||||
// Parse a TD from a string
|
||||
ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description);
|
||||
|
||||
// Create the payload to be sent with the Http request
|
||||
Map<String, Object> elbowPayload = new HashMap<>();
|
||||
elbowPayload.put("http://www.w3.org/2001/XMLSchema#int", 400);
|
||||
|
||||
// Get the affordance "setElbow" from the TD
|
||||
Optional<ActionAffordance> action = td.getActionByName("setElbow");
|
||||
|
||||
// Get the first form
|
||||
if (action.isPresent()) {
|
||||
Optional<Form> form = action.get().getFirstForm();
|
||||
|
||||
// Retrieve the input data schema from the action affordance
|
||||
Optional<DataSchema> inputSchema = action.get().getInputSchema();
|
||||
|
||||
// If a form is found, use it to create and execute the Http request
|
||||
if (form.isPresent()) {
|
||||
TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction);
|
||||
|
||||
if (inputSchema.isPresent()) {
|
||||
request.setObjectPayload((ObjectSchema) inputSchema.get(), elbowPayload);
|
||||
|
||||
try {
|
||||
TDHttpResponse response = request.execute();
|
||||
System.out.println("Received response with status code: " + response.getStatusCode());
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
String putEndpoint = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/elbow";
|
||||
|
||||
|
||||
String inputJson = "{ \"value\": 400}";
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(putEndpoint))
|
||||
@@ -26,9 +101,9 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort {
|
||||
.header("X-API-KEY", key)
|
||||
.PUT(HttpRequest.BodyPublishers.ofString(inputJson))
|
||||
.build();
|
||||
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
|
||||
try {
|
||||
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println(response.statusCode());
|
||||
@@ -40,7 +115,7 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -4,7 +4,20 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import ch.unisg.ics.interactions.wot.td.ThingDescription;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance;
|
||||
import ch.unisg.ics.interactions.wot.td.affordances.Form;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDHttpRequest;
|
||||
import ch.unisg.ics.interactions.wot.td.clients.TDHttpResponse;
|
||||
import ch.unisg.ics.interactions.wot.td.io.TDGraphReader;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.DataSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema;
|
||||
import ch.unisg.ics.interactions.wot.td.vocabularies.TD;
|
||||
import org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -16,17 +29,77 @@ public class UserToRobotAdapter implements UserToRobotPort {
|
||||
|
||||
@Override
|
||||
public String userToRobot() {
|
||||
String postEndpoint = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/user";
|
||||
|
||||
|
||||
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/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(input))
|
||||
.build();
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
try {
|
||||
var description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body();
|
||||
|
||||
// Parse a TD from a string
|
||||
ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description);
|
||||
|
||||
// Create the payload to be sent with the HTTP request
|
||||
Map<String, Object> logInPayload = new HashMap<>();
|
||||
logInPayload.put("http://xmlns.com/foaf/0.1/Name", "keanu rahimian");
|
||||
logInPayload.put("http://xmlns.com/foaf/0.1/Mbox", "keanu.rahimian@student.unisg.ch");
|
||||
|
||||
// Get the affordance "Log-In" from the TD
|
||||
Optional<ActionAffordance> action = td.getActionByName("logIn");
|
||||
|
||||
// Get the first form
|
||||
if (action.isPresent()) {
|
||||
Optional<Form> form = action.get().getFirstForm();
|
||||
|
||||
// Retrieve the input data schema from the action affordance
|
||||
Optional<DataSchema> inputSchema = action.get().getInputSchema();
|
||||
|
||||
// If a form is found, use it to create and execute the HTTP request
|
||||
if (form.isPresent()) {
|
||||
TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction);
|
||||
|
||||
if (inputSchema.isPresent()) {
|
||||
request.setObjectPayload((ObjectSchema) inputSchema.get(), logInPayload);
|
||||
|
||||
try {
|
||||
TDHttpResponse response = request.execute();
|
||||
System.out.println("Received response with status code: " + response.getStatusCode());
|
||||
|
||||
// TODO: Get the key from the response and return it
|
||||
// Not exactly sure how to get the headers from the payload, as we need them
|
||||
// to get the key
|
||||
|
||||
return null;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
/*
|
||||
String inputJson = "{ \"name\":\"keanu rahimian\", \"email\":\"keanu.rahimian@student.unisg.ch\"}";
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(postEndpoint))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(inputJson))
|
||||
.build();
|
||||
|
||||
var client = HttpClient.newHttpClient();
|
||||
|
||||
|
||||
try {
|
||||
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println(response.statusCode());
|
||||
@@ -40,7 +113,10 @@ public class UserToRobotAdapter implements UserToRobotPort {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
*/
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user