Merging dev to main #107
| @@ -1,7 +1,7 @@ | |||||||
| package ch.unisg.executorbase.executor.domain; | package ch.unisg.executorbase.executor.domain; | ||||||
|  |  | ||||||
| public enum ExecutorType { | public enum ExecutorType { | ||||||
|     COMPUTATION, ROBOT, HUMIDITY; |     COMPUTATION, SMALLROBOT, HUMIDITY; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|     *   Checks if the give executor type exists. |     *   Checks if the give executor type exists. | ||||||
|   | |||||||
| @@ -15,14 +15,6 @@ public class ExecutorcomputationApplication { | |||||||
|     static Logger logger = Logger.getLogger(ExecutorcomputationApplication.class.getName()); |     static Logger logger = Logger.getLogger(ExecutorcomputationApplication.class.getName()); | ||||||
|  |  | ||||||
| 	public static void main(String[] args) { | 	public static void main(String[] args) { | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             TimeUnit.SECONDS.sleep(1); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             logger.log(Level.SEVERE, e.getLocalizedMessage(), e); |  | ||||||
|             Thread.currentThread().interrupt(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
| 		SpringApplication.run(ExecutorcomputationApplication.class, args); | 		SpringApplication.run(ExecutorcomputationApplication.class, args); | ||||||
| 		Executor.getExecutor(); | 		Executor.getExecutor(); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -82,11 +82,10 @@ | |||||||
|             <scope>compile</scope> |             <scope>compile</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>com.github.Interactions-HSG</groupId> | 			<groupId>com.github.Interactions-HSG</groupId> | ||||||
|             <artifactId>wot-td-java</artifactId> | 			<artifactId>wot-td-java</artifactId> | ||||||
|             <version>0.1.1</version> | 			<version>master-SNAPSHOT</version> | ||||||
|             <scope>compile</scope> | 		</dependency> | ||||||
|         </dependency> |  | ||||||
|     </dependencies> |     </dependencies> | ||||||
|  |  | ||||||
| 	<build> | 	<build> | ||||||
|   | |||||||
| @@ -11,14 +11,6 @@ import ch.unisg.executorrobot.executor.domain.Executor; | |||||||
| public class ExecutorrobotApplication { | public class ExecutorrobotApplication { | ||||||
|  |  | ||||||
| 	public static void main(String[] args) { | 	public static void main(String[] args) { | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             TimeUnit.SECONDS.sleep(40); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             // TODO Auto-generated catch block |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
| 		SpringApplication.run(ExecutorrobotApplication.class, args); | 		SpringApplication.run(ExecutorrobotApplication.class, args); | ||||||
| 		Executor.getExecutor(); | 		Executor.getExecutor(); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -1,42 +0,0 @@ | |||||||
| package ch.unisg.executorrobot.executor.adapter.out; |  | ||||||
| import java.io.IOException; |  | ||||||
| import java.net.URI; |  | ||||||
| import java.net.http.HttpClient; |  | ||||||
| import java.net.http.HttpRequest; |  | ||||||
| import java.net.http.HttpResponse; |  | ||||||
|  |  | ||||||
| import org.springframework.context.annotation.Primary; |  | ||||||
| import org.springframework.stereotype.Component; |  | ||||||
|  |  | ||||||
| import ch.unisg.executorrobot.executor.application.port.out.DeleteUserFromRobotPort; |  | ||||||
|  |  | ||||||
| @Component |  | ||||||
| @Primary |  | ||||||
| public class DeleteUserFromRobotAdapter implements DeleteUserFromRobotPort { |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public boolean deleteUserFromRobot(String 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()); |  | ||||||
|             return true; |  | ||||||
|         } catch (IOException e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|         return false; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,121 +0,0 @@ | |||||||
| package ch.unisg.executorrobot.executor.adapter.out; |  | ||||||
| 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.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; |  | ||||||
|  |  | ||||||
| import ch.unisg.executorrobot.executor.application.port.out.InstructionToRobotPort; |  | ||||||
|  |  | ||||||
| @Component |  | ||||||
| @Primary |  | ||||||
| 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 'leubot1' }"; |  | ||||||
|  |  | ||||||
|         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)) |  | ||||||
|             .header("Content-Type", "application/json") |  | ||||||
|             .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()); |  | ||||||
|             System.out.println(response.headers()); |  | ||||||
|             return true; |  | ||||||
|         } catch (IOException e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|         return false; |  | ||||||
|     */ |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,5 +1,6 @@ | |||||||
| package ch.unisg.executorrobot.executor.adapter.out; | package ch.unisg.executorrobot.executor.adapter.out; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.io.StringReader; | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| import java.net.http.HttpClient; | import java.net.http.HttpClient; | ||||||
| import java.net.http.HttpRequest; | import java.net.http.HttpRequest; | ||||||
| @@ -7,6 +8,11 @@ import java.net.http.HttpResponse; | |||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  |  | ||||||
|  | import javax.xml.parsers.DocumentBuilder; | ||||||
|  | import javax.xml.parsers.DocumentBuilderFactory; | ||||||
|  | import javax.xml.parsers.ParserConfigurationException; | ||||||
|  |  | ||||||
| import ch.unisg.ics.interactions.wot.td.ThingDescription; | 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.ActionAffordance; | ||||||
| @@ -16,10 +22,19 @@ 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.io.TDGraphReader; | ||||||
| import ch.unisg.ics.interactions.wot.td.schemas.DataSchema; | 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.ObjectSchema; | ||||||
|  | import ch.unisg.ics.interactions.wot.td.security.APIKeySecurityScheme; | ||||||
|  | import ch.unisg.ics.interactions.wot.td.security.SecurityScheme; | ||||||
| import ch.unisg.ics.interactions.wot.td.vocabularies.TD; | import ch.unisg.ics.interactions.wot.td.vocabularies.TD; | ||||||
|  | import ch.unisg.ics.interactions.wot.td.vocabularies.WoTSec; | ||||||
|  |  | ||||||
| import org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration; | import org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration; | ||||||
| import org.springframework.context.annotation.Primary; | import org.springframework.context.annotation.Primary; | ||||||
| import org.springframework.stereotype.Component; | 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 ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort; | import ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort; | ||||||
|  |  | ||||||
| @@ -32,7 +47,7 @@ public class UserToRobotAdapter implements UserToRobotPort { | |||||||
|  |  | ||||||
|         String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine"; |         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' }"; |         String input = "@prefix dct: <http://purl.org/dc/terms/> . select ?title where { ?title dct:title 'leubot1' }"; | ||||||
|  |  | ||||||
|         var httpRequest = HttpRequest.newBuilder() |         var httpRequest = HttpRequest.newBuilder() | ||||||
|             .uri(URI.create(endpoint)) |             .uri(URI.create(endpoint)) | ||||||
| @@ -43,82 +58,184 @@ public class UserToRobotAdapter implements UserToRobotPort { | |||||||
|         var client = HttpClient.newHttpClient(); |         var client = HttpClient.newHttpClient(); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             var description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body(); |             String description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body(); | ||||||
|  |             String leubot1Uri = null; | ||||||
|  |  | ||||||
|             String uri = "http://yggdrasil.interactions.ics.unisg.ch/environments/61/workspaces/102/artifacts/leubot1"; |             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||||||
|  |             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | ||||||
|  |             Document doc = dBuilder.parse(new InputSource(new StringReader(description))); | ||||||
|  |  | ||||||
|             // Parse a TD from a string |             doc.getDocumentElement().normalize(); | ||||||
|             ThingDescription td = TDGraphReader.readFromURL(ThingDescription.TDFormat.RDF_TURTLE, uri); |  | ||||||
|  |  | ||||||
|             // Create the payload to be sent with the HTTP request |             NodeList results = doc.getElementsByTagName("uri"); | ||||||
|             Map<String, Object> logInPayload = new HashMap<>(); |             for (int temp = 0; temp < results.getLength(); temp += 1) { | ||||||
|             logInPayload.put("http://xmlns.com/foaf/0.1/Name", "keanu rahimian"); |                 Node nNode = results.item(temp); | ||||||
|             logInPayload.put("http://xmlns.com/foaf/0.1/Mbox", "keanu.rahimian@student.unisg.ch"); |                 if (nNode.getTextContent().contains("leubot1")) { | ||||||
|  |                     leubot1Uri = nNode.getTextContent(); | ||||||
|             // 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(); |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             if (leubot1Uri == null) { | ||||||
|  |                 // TODO implement logic if execution failed | ||||||
|  |                 return "ERROR"; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // Parse a TD from a string | ||||||
|  |             ThingDescription td = TDGraphReader.readFromURL(ThingDescription.TDFormat.RDF_TURTLE, leubot1Uri); | ||||||
|  |  | ||||||
|  |             String apiUrl = getAPIKey(td); | ||||||
|  |             System.out.println("FOUND API URL " + apiUrl); | ||||||
|  |  | ||||||
|  |             String apiKey = apiUrl.split("/")[apiUrl.split("/").length-1].split("]")[0]; | ||||||
|  |             System.out.println("FOUND KEY " + apiKey); | ||||||
|  |  | ||||||
|  |             if(apiKey == null) { | ||||||
|  |                 // TODO implement logic if execution failed | ||||||
|  |                 return "ERROR"; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             try { | ||||||
|  |                 TimeUnit.MILLISECONDS.sleep(1500); | ||||||
|  |             } catch (InterruptedException e) { | ||||||
|  |                 // TODO Auto-generated catch block | ||||||
|  |                 e.printStackTrace(); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (!moveRobot(td, apiKey)) { | ||||||
|  |                 return "ERROR"; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             try { | ||||||
|  |                 TimeUnit.MILLISECONDS.sleep(1500); | ||||||
|  |             } catch (InterruptedException e) { | ||||||
|  |                 // TODO Auto-generated catch block | ||||||
|  |                 e.printStackTrace(); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             delteUserFromRobot(apiUrl); | ||||||
|  |  | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } catch (InterruptedException e) { |         } catch (InterruptedException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|  |         } catch (SAXException e1) { | ||||||
|  |             e1.printStackTrace(); | ||||||
|  |         } catch (ParserConfigurationException e1) { | ||||||
|  |             e1.printStackTrace(); | ||||||
|  |         } | ||||||
|  |         return "OK"; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     private String getAPIKey(ThingDescription td) { | ||||||
|  |         // 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.isEmpty()) { | ||||||
|  |             // TODO implement logic if execution failed | ||||||
|  |             return null; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|     /* |         Optional<Form> form = action.get().getFirstFormForOperationType(TD.invokeAction); | ||||||
|         String inputJson = "{ \"name\":\"keanu rahimian\", \"email\":\"keanu.rahimian@student.unisg.ch\"}"; |  | ||||||
|         var request = HttpRequest.newBuilder() |         if (form.isEmpty()) { | ||||||
|             .uri(URI.create(postEndpoint)) |             // TODO implement logic if execution failed | ||||||
|             .header("Content-Type", "application/json") |             return null; | ||||||
|             .POST(HttpRequest.BodyPublishers.ofString(inputJson)) |         } | ||||||
|             .build(); |  | ||||||
|  |         // If a form is found, use it to create and execute the HTTP request | ||||||
|  |         TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction); | ||||||
|  |  | ||||||
|  |         // Retrieve the input data schema from the action affordance | ||||||
|  |         Optional<DataSchema> inputSchema = action.get().getInputSchema(); | ||||||
|  |  | ||||||
|  |         if(inputSchema.isPresent()) { | ||||||
|  |             request.setObjectPayload((ObjectSchema) inputSchema.get(), logInPayload); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |             TDHttpResponse response = request.execute(); | ||||||
|  |             System.out.println("Received response with status code: " + response.getStatusCode()); | ||||||
|  |  | ||||||
|  |             String url = response.getHeaders().get("Location"); | ||||||
|  |             return url; | ||||||
|  |  | ||||||
|  |         } catch (IOException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return null; | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private boolean moveRobot(ThingDescription td, String key) { | ||||||
|  |  | ||||||
|  |         // Create the payload to be sent with the Http request | ||||||
|  |         Map<String, Object> elbowPayload = new HashMap<>(); | ||||||
|  |         elbowPayload.put("value", 400); | ||||||
|  |  | ||||||
|  |         // Get the affordance "setElbow" from the TD | ||||||
|  |         Optional<ActionAffordance> action = td.getActionByName("setElbow"); | ||||||
|  |  | ||||||
|  |         // Get the first form | ||||||
|  |         if (action.isEmpty()) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Optional<Form> form = action.get().getFirstFormForOperationType(TD.invokeAction); | ||||||
|  |  | ||||||
|  |         if (form.isEmpty()) { | ||||||
|  |             // TODO implement logic if execution failed | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Retrieve the input data schema from the action affordance | ||||||
|  |         Optional<DataSchema> inputSchema = action.get().getInputSchema(); | ||||||
|  |  | ||||||
|  |         TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction); | ||||||
|  |  | ||||||
|  |         if(inputSchema.isPresent()) { | ||||||
|  |             request.setObjectPayload((ObjectSchema) inputSchema.get(), elbowPayload); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Optional<SecurityScheme> securityScheme = td.getFirstSecuritySchemeByType(WoTSec.APIKeySecurityScheme); | ||||||
|  |         if (securityScheme.isPresent()) { | ||||||
|  |             request.setAPIKey((APIKeySecurityScheme) securityScheme.get(), key); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |             TDHttpResponse response = request.execute(); | ||||||
|  |             System.out.println("Received response with status code: " + response.getStatusCode()); | ||||||
|  |             return true; | ||||||
|  |         } catch (IOException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void delteUserFromRobot(String apiUrl) { | ||||||
|  |         HttpRequest request = HttpRequest.newBuilder() | ||||||
|  |         .uri(URI.create(apiUrl)) | ||||||
|  |         .header("Content-Type", "application/json") | ||||||
|  |         .DELETE() | ||||||
|  |         .build(); | ||||||
|  |  | ||||||
|  |         HttpClient client = HttpClient.newHttpClient(); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             var response = client.send(request, HttpResponse.BodyHandlers.ofString()); |             var response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||||||
|             System.out.println(response.statusCode()); |             System.out.println(response.statusCode()); | ||||||
|             System.out.println(response.headers()); |  | ||||||
|             String url = response.headers().map().get("location").toString(); |  | ||||||
|             String key = url.split("/")[url.split("/").length-1].split("]")[0]; |  | ||||||
|             return key; |  | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } catch (InterruptedException e) { |         } catch (InterruptedException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } |         } | ||||||
|         return null; |  | ||||||
|     */ |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         return null; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +0,0 @@ | |||||||
| package ch.unisg.executorrobot.executor.application.port.out; |  | ||||||
|  |  | ||||||
| public interface DeleteUserFromRobotPort { |  | ||||||
|     boolean deleteUserFromRobot(String key); |  | ||||||
| } |  | ||||||
| @@ -1,5 +0,0 @@ | |||||||
| package ch.unisg.executorrobot.executor.application.port.out; |  | ||||||
|  |  | ||||||
| public interface InstructionToRobotPort { |  | ||||||
|     boolean instructionToRobot(String key); |  | ||||||
| } |  | ||||||
| @@ -1,22 +1,14 @@ | |||||||
| package ch.unisg.executorrobot.executor.domain; | package ch.unisg.executorrobot.executor.domain; | ||||||
|  |  | ||||||
| import java.util.concurrent.TimeUnit; |  | ||||||
|  |  | ||||||
| import ch.unisg.executorrobot.executor.adapter.out.DeleteUserFromRobotAdapter; |  | ||||||
| import ch.unisg.executorrobot.executor.adapter.out.InstructionToRobotAdapter; |  | ||||||
| import ch.unisg.executorrobot.executor.adapter.out.UserToRobotAdapter; | import ch.unisg.executorrobot.executor.adapter.out.UserToRobotAdapter; | ||||||
| import ch.unisg.executorrobot.executor.application.port.out.DeleteUserFromRobotPort; |  | ||||||
| import ch.unisg.executorrobot.executor.application.port.out.InstructionToRobotPort; |  | ||||||
| import ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort; | import ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort; | ||||||
| import ch.unisg.executorbase.executor.domain.ExecutorBase; | import ch.unisg.executorbase.executor.domain.ExecutorBase; | ||||||
| import ch.unisg.executorbase.executor.domain.ExecutorType; | import ch.unisg.executorbase.executor.domain.ExecutorType; | ||||||
|  |  | ||||||
| public class Executor extends ExecutorBase { | public class Executor extends ExecutorBase { | ||||||
|  |  | ||||||
|     private static final Executor executor = new Executor(ExecutorType.ROBOT); |     private static final Executor executor = new Executor(ExecutorType.SMALLROBOT); | ||||||
|     private final UserToRobotPort userToRobotPort = new UserToRobotAdapter(); |     private final UserToRobotPort userToRobotPort = new UserToRobotAdapter(); | ||||||
|     private final InstructionToRobotPort instructionToRobotPort = new InstructionToRobotAdapter(); |  | ||||||
|     private final DeleteUserFromRobotPort deleteUserFromRobotPort = new DeleteUserFromRobotAdapter(); |  | ||||||
|  |  | ||||||
|     public static Executor getExecutor() { |     public static Executor getExecutor() { | ||||||
|         return executor; |         return executor; | ||||||
| @@ -29,24 +21,7 @@ public class Executor extends ExecutorBase { | |||||||
|     @Override |     @Override | ||||||
|     protected |     protected | ||||||
|     String execution(String input) { |     String execution(String input) { | ||||||
|  |         return userToRobotPort.userToRobot(); | ||||||
|         String key = userToRobotPort.userToRobot(); |  | ||||||
|         try { |  | ||||||
|             TimeUnit.MILLISECONDS.sleep(1500); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             // TODO Auto-generated catch block |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|         boolean result1 = instructionToRobotPort.instructionToRobot(key); |  | ||||||
|         try { |  | ||||||
|             TimeUnit.MILLISECONDS.sleep(10000); |  | ||||||
|         } catch (InterruptedException e) { |  | ||||||
|             // TODO Auto-generated catch block |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|         deleteUserFromRobotPort.deleteUserFromRobot(key); |  | ||||||
|  |  | ||||||
|         return Boolean.toString(result1); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -4,6 +4,10 @@ import ch.unisg.tapas.auctionhouse.adapter.common.clients.TapasMqttClient; | |||||||
| import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher; | import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher; | ||||||
| import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber; | import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber; | ||||||
| import ch.unisg.tapas.auctionhouse.application.service.GetExecutorsService; | import ch.unisg.tapas.auctionhouse.application.service.GetExecutorsService; | ||||||
|  | import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscovery; | ||||||
|  | import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; | ||||||
|  | import ch.unisg.tapas.common.AuctionHouseResourceDirectory; | ||||||
|  |  | ||||||
| import org.apache.logging.log4j.LogManager; | import org.apache.logging.log4j.LogManager; | ||||||
| import org.apache.logging.log4j.Logger; | import org.apache.logging.log4j.Logger; | ||||||
| import org.eclipse.paho.client.mqttv3.MqttException; | import org.eclipse.paho.client.mqttv3.MqttException; | ||||||
| @@ -13,6 +17,9 @@ import org.springframework.core.env.ConfigurableEnvironment; | |||||||
|  |  | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||||
|  | import java.util.concurrent.Executors; | ||||||
|  | import java.util.concurrent.ScheduledExecutorService; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Main TAPAS Auction House application. |  * Main TAPAS Auction House application. | ||||||
| @@ -30,7 +37,7 @@ public class TapasAuctionHouseApplication { | |||||||
|         ENVIRONMENT = tapasAuctioneerApp.run(args).getEnvironment(); |         ENVIRONMENT = tapasAuctioneerApp.run(args).getEnvironment(); | ||||||
|         // TODO Set start up of message services with config |         // TODO Set start up of message services with config | ||||||
| 		// We will use these bootstrap methods in Week 6: | 		// We will use these bootstrap methods in Week 6: | ||||||
|         // bootstrapMarketplaceWithWebSub(); |         bootstrapMarketplaceWithWebSub(); | ||||||
|         bootstrapMarketplaceWithMqtt(); |         bootstrapMarketplaceWithMqtt(); | ||||||
|         var getExecutorsService = new GetExecutorsService(); |         var getExecutorsService = new GetExecutorsService(); | ||||||
|         getExecutorsService.getExecutorsFromExecutorPool(); |         getExecutorsService.getExecutorsFromExecutorPool(); | ||||||
| @@ -39,19 +46,20 @@ public class TapasAuctionHouseApplication { | |||||||
|      * Discovers auction houses and subscribes to WebSub notifications |      * Discovers auction houses and subscribes to WebSub notifications | ||||||
|      */ |      */ | ||||||
| 	private static void bootstrapMarketplaceWithWebSub() { | 	private static void bootstrapMarketplaceWithWebSub() { | ||||||
|         // discoverAuctionHouseEndpoints(); |         discoverAuctionHouseEndpoints(); | ||||||
|  |  | ||||||
|         WebSubSubscriber subscriber = new WebSubSubscriber(ENVIRONMENT.getProperty("auction.house.uri")); |         WebSubSubscriber subscriber = new WebSubSubscriber(ENVIRONMENT.getProperty("auction.house.uri")); | ||||||
|  |  | ||||||
|         // for (AuctionHouseDiscoveryInformation endpoint : AuctionHouseDiscovery.getInstance().getAuctionHouseDiscoveryList()) { |         for (AuctionHouseDiscoveryInformation endpoint : AuctionHouseDiscovery.getInstance().getAuctionHouseDiscoveryList()) { | ||||||
|         //     subscriber.subscribeToAuctionHouseEndpoint(endpoint.getWebSubUri().getValue()); |             System.out.println(endpoint.getWebSubUri().getValue()); | ||||||
|         // } |             subscriber.subscribeToAuctionHouseEndpoint(endpoint.getWebSubUri().getValue()); | ||||||
|         try { |  | ||||||
|             subscriber.subscribeToAuctionHouseEndpoint(new URI("http://6b4e-130-82-250-227.ngrok.io/websub-discovery")); |  | ||||||
|         } catch (URISyntaxException e) { |  | ||||||
|             // TODO Auto-generated catch block |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |         } | ||||||
|  |         // try { | ||||||
|  |         //     subscriber.subscribeToAuctionHouseEndpoint(new URI("http://6b4e-130-82-250-227.ngrok.io/websub-discovery")); | ||||||
|  |         // } catch (URISyntaxException e) { | ||||||
|  |         //     // TODO Auto-generated catch block | ||||||
|  |         //     e.printStackTrace(); | ||||||
|  |         // } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -76,17 +84,17 @@ public class TapasAuctionHouseApplication { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| 	// private static void discoverAuctionHouseEndpoints() { | 	private static void discoverAuctionHouseEndpoints() { | ||||||
|  |  | ||||||
|     //     AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( |         AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( | ||||||
|     //         URI.create(ENVIRONMENT.getProperty("discovery.endpoint.uri")) |             URI.create(ENVIRONMENT.getProperty("discovery.endpoint.uri")) | ||||||
|     //     ); |         ); | ||||||
|  |  | ||||||
|     //     AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); |         AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); | ||||||
|  |  | ||||||
|     //     ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |         // ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||||||
|     //     executor.scheduleAtFixedRate(crawlerRunnable, 300, 300, TimeUnit.SECONDS); |         // executor.scheduleAtFixedRate(crawlerRunnable, 300, 300, TimeUnit.SECONDS); | ||||||
|     // } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     // private static Runnable crawlerRunnable = new Runnable() { |     // private static Runnable crawlerRunnable = new Runnable() { | ||||||
|   | |||||||
| @@ -41,8 +41,8 @@ public class AuctionHouseDiscoveryHttpAdapter implements AuctionHouseDiscoveryPo | |||||||
|             { |             { | ||||||
|                 var informationJSON = arrayOfInformation.getJSONObject(i); |                 var informationJSON = arrayOfInformation.getJSONObject(i); | ||||||
|                 var information = new AuctionHouseDiscoveryInformation( |                 var information = new AuctionHouseDiscoveryInformation( | ||||||
|                     new AuctionHouseDiscoveryInformation.AuctionHouseUri(URI.create(informationJSON.getString("auctionHouseURI"))), |                     new AuctionHouseDiscoveryInformation.AuctionHouseUri(URI.create(informationJSON.getString("auctionHouseUri"))), | ||||||
|                     new AuctionHouseDiscoveryInformation.WebSubUri(URI.create(informationJSON.getString("webSubURI"))), |                     new AuctionHouseDiscoveryInformation.WebSubUri(URI.create(informationJSON.getString("webSubUri"))), | ||||||
|                     new AuctionHouseDiscoveryInformation.TaskTypes(getTaskTypes(informationJSON.getJSONArray("taskTypes"))), |                     new AuctionHouseDiscoveryInformation.TaskTypes(getTaskTypes(informationJSON.getJSONArray("taskTypes"))), | ||||||
|                     new AuctionHouseDiscoveryInformation.TimeStamp(Timestamp.valueOf(informationJSON.getString("timeStamp"))), |                     new AuctionHouseDiscoveryInformation.TimeStamp(Timestamp.valueOf(informationJSON.getString("timeStamp"))), | ||||||
|                     new AuctionHouseDiscoveryInformation.GroupName(informationJSON.getString("groupName")) |                     new AuctionHouseDiscoveryInformation.GroupName(informationJSON.getString("groupName")) | ||||||
|   | |||||||
| @@ -65,8 +65,17 @@ public class StartAuctionService implements LaunchAuctionUseCase { | |||||||
|         auctions.addAuction(auction); |         auctions.addAuction(auction); | ||||||
|  |  | ||||||
|         // Schedule the closing of the auction at the deadline |         // Schedule the closing of the auction at the deadline | ||||||
|         service.schedule(new CloseAuctionTask(auction.getAuctionId()), deadline.getValue().getTime() - System.currentTimeMillis(), |         System.out.println(deadline.getValue().getTime()); | ||||||
|  |         System.out.println(System.currentTimeMillis()); | ||||||
|  |         System.out.println(deadline.getValue().getTime() - System.currentTimeMillis()); | ||||||
|  |         if (deadline.getValue().getTime() == DEFAULT_AUCTION_DEADLINE_MILLIS.getTime()) { | ||||||
|  |             System.out.println("DEFAULT TIMESTAMP USED"); | ||||||
|  |             service.schedule(new CloseAuctionTask(auction.getAuctionId()), 10000, | ||||||
|             TimeUnit.MILLISECONDS); |             TimeUnit.MILLISECONDS); | ||||||
|  |         } else { | ||||||
|  |             service.schedule(new CloseAuctionTask(auction.getAuctionId()), deadline.getValue().getTime() - System.currentTimeMillis(), | ||||||
|  |                 TimeUnit.MILLISECONDS); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         // Publish an auction started event |         // Publish an auction started event | ||||||
|         AuctionStartedEvent auctionStartedEvent = new AuctionStartedEvent(auction); |         AuctionStartedEvent auctionStartedEvent = new AuctionStartedEvent(auction); | ||||||
|   | |||||||
| @@ -15,8 +15,9 @@ public class AuctionHouseDiscovery { | |||||||
|  |  | ||||||
|     private final List<String> tasktypes = new ArrayList<String>() { |     private final List<String> tasktypes = new ArrayList<String>() { | ||||||
|         { |         { | ||||||
|             add("computation"); |             add("COMPUTATION"); | ||||||
|             add("smallrobot"); |             add("SMALLROBOT"); | ||||||
|  |             add("HUMIDITY"); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
| @@ -31,8 +32,8 @@ public class AuctionHouseDiscovery { | |||||||
|         try { |         try { | ||||||
|             // Add our information to list |             // Add our information to list | ||||||
|             auctionHouseDiscoveryList.add(new AuctionHouseDiscoveryInformation( |             auctionHouseDiscoveryList.add(new AuctionHouseDiscoveryInformation( | ||||||
|                 new AuctionHouseDiscoveryInformation.AuctionHouseUri(new URI("http://localhost:8086")), |                 new AuctionHouseDiscoveryInformation.AuctionHouseUri(new URI("https://tapas-auction-house.86-119-35-40.nip.io")), | ||||||
|                 new AuctionHouseDiscoveryInformation.WebSubUri(new URI("http://localhost:8086/websub/auctions")), |                 new AuctionHouseDiscoveryInformation.WebSubUri(new URI("https://tapas-auction-house.86-119-35-40.nip.io/websub/auctions")), | ||||||
|                 new AuctionHouseDiscoveryInformation.TaskTypes(tasktypes), |                 new AuctionHouseDiscoveryInformation.TaskTypes(tasktypes), | ||||||
|                 new AuctionHouseDiscoveryInformation.TimeStamp(new Timestamp(new Date().getTime())), |                 new AuctionHouseDiscoveryInformation.TimeStamp(new Timestamp(new Date().getTime())), | ||||||
|                 new AuctionHouseDiscoveryInformation.GroupName("Group 1") |                 new AuctionHouseDiscoveryInformation.GroupName("Group 1") | ||||||
|   | |||||||
| @@ -15,4 +15,4 @@ websub.hub.uri=https://pubsubhubbub.appspot.com | |||||||
| mqtt.broker.uri=tcp://broker.hivemq.com | mqtt.broker.uri=tcp://broker.hivemq.com | ||||||
| # mqtt.broker.uri=tcp://localhost:1883 | # mqtt.broker.uri=tcp://localhost:1883 | ||||||
|  |  | ||||||
| discovery.endpoint.uri=http://localhost:3500 | discovery.endpoint.uri=https://tapas-auction-house.86-119-34-242.nip.io | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user