bugfixes
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package ch.unisg.executorhumidity;
|
||||
|
||||
|
||||
import ch.unisg.executorhumidity.executor.domain.Executor;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import ch.unisg.executorhumidity.executor.domain.Executor;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExecutorhumidityApplication {
|
||||
|
||||
|
@@ -9,12 +9,18 @@ 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.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@@ -22,16 +28,21 @@ import java.net.http.HttpResponse;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class GetHumidityAdapter implements GetHumidityPort {
|
||||
public class GetHumidityAdapter implements GetHumidityPort {
|
||||
|
||||
String endpoint = System.getenv("SEARCH_ENGINE_URI") == null ?
|
||||
"https://api.interactions.ics.unisg.ch/search/searchEngine" : System.getenv("SEARCH_ENGINE_URI");
|
||||
|
||||
@Override
|
||||
public String getHumidity() {
|
||||
|
||||
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()
|
||||
@@ -44,20 +55,39 @@ public class GetHumidityAdapter implements GetHumidityPort {
|
||||
|
||||
try {
|
||||
String description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body();
|
||||
String mirogateUri = null;
|
||||
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(new InputSource(new StringReader(description)));
|
||||
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
NodeList results = doc.getElementsByTagName("uri");
|
||||
for (int temp = 0; temp < results.getLength(); temp += 1) {
|
||||
Node nNode = results.item(temp);
|
||||
if (nNode.getTextContent().contains("mirogate")) {
|
||||
mirogateUri = nNode.getTextContent();
|
||||
}
|
||||
}
|
||||
if (mirogateUri == null) {
|
||||
// TODO implement logic if execution failed
|
||||
return "";
|
||||
}
|
||||
|
||||
// Parse a TD from a string
|
||||
ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description);
|
||||
ThingDescription td = TDGraphReader.readFromURL(ThingDescription.TDFormat.RDF_TURTLE, mirogateUri);
|
||||
|
||||
Optional<PropertyAffordance> humidity = td.getPropertyByName("humidity");
|
||||
|
||||
if (humidity.isPresent()) {
|
||||
Optional<Form> form = humidity.get().getFirstFormForOperationType(TD.readProperty);
|
||||
|
||||
// System.out.println(humidity.get().getDataSchema().getDatatype());
|
||||
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();
|
||||
@@ -72,6 +102,12 @@ public class GetHumidityAdapter implements GetHumidityPort {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParserConfigurationException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (SAXException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import org.eclipse.californium.elements.exception.ConnectorException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Executor extends ExecutorBase{
|
||||
public class Executor extends ExecutorBase {
|
||||
|
||||
private static final Executor executor = new Executor(ExecutorType.HUMIDITY);
|
||||
|
||||
|
16
executor-humidity/src/main/resources/application.properties
Normal file
16
executor-humidity/src/main/resources/application.properties
Normal file
@@ -0,0 +1,16 @@
|
||||
server.port=8087
|
||||
|
||||
search.engine.uri=https://api.interactions.ics.unisg.ch/search/searchEngine
|
||||
|
||||
spring.profiles.active=chaos-monkey
|
||||
chaos.monkey.enabled=false
|
||||
management.endpoint.chaosmonkey.enabled=true
|
||||
management.endpoint.chaosmonkeyjmx.enabled=true
|
||||
# include specific endpoints
|
||||
management.endpoints.web.exposure.include=health,info,chaosmonkey
|
||||
chaos.monkey.watcher.controller=true
|
||||
chaos.monkey.watcher.restController=true
|
||||
chaos.monkey.watcher.service=true
|
||||
chaos.monkey.watcher.repository=true
|
||||
chaos.monkey.watcher.component=true
|
||||
|
Reference in New Issue
Block a user