Merge remote-tracking branch 'origin/dev' into common
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package ch.unisg.executorrobot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import ch.unisg.executorrobot.executor.domain.Executor;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExecutorrobotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExecutorrobotApplication.class, args);
|
||||
Executor.getExecutor();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package ch.unisg.executorrobot.executor.adapter.in.web;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
|
||||
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorType;
|
||||
|
||||
@RestController
|
||||
public class TaskAvailableController {
|
||||
private final TaskAvailableUseCase taskAvailableUseCase;
|
||||
|
||||
public TaskAvailableController(TaskAvailableUseCase taskAvailableUseCase) {
|
||||
this.taskAvailableUseCase = taskAvailableUseCase;
|
||||
}
|
||||
|
||||
@GetMapping(path = "/newtask/{taskType}")
|
||||
public ResponseEntity<String> retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) {
|
||||
|
||||
if (ExecutorType.contains(taskType.toUpperCase())) {
|
||||
TaskAvailableCommand command = new TaskAvailableCommand(
|
||||
ExecutorType.valueOf(taskType.toUpperCase()));
|
||||
CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command));
|
||||
}
|
||||
|
||||
// Add the content type as a response header
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
|
||||
return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
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.0/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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
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.InstructionToRobotPort;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class InstructionToRobotAdapter implements InstructionToRobotPort {
|
||||
|
||||
@Override
|
||||
public boolean instructionToRobot(String key) {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
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.UserToRobotPort;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class UserToRobotAdapter implements UserToRobotPort {
|
||||
|
||||
@Override
|
||||
public String userToRobot() {
|
||||
String postEndpoint = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/user";
|
||||
|
||||
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());
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ch.unisg.executorrobot.executor.application.port.out;
|
||||
|
||||
public interface DeleteUserFromRobotPort {
|
||||
boolean deleteUserFromRobot(String key);
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ch.unisg.executorrobot.executor.application.port.out;
|
||||
|
||||
public interface InstructionToRobotPort {
|
||||
boolean instructionToRobot(String key);
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package ch.unisg.executorrobot.executor.application.port.out;
|
||||
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorType;
|
||||
|
||||
public interface UserToRobotPort {
|
||||
String userToRobot();
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package ch.unisg.executorrobot.executor.application.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executorrobot.executor.domain.Executor;
|
||||
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
|
||||
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorStatus;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
@Transactional
|
||||
public class TaskAvailableService implements TaskAvailableUseCase {
|
||||
|
||||
@Override
|
||||
public void newTaskAvailable(TaskAvailableCommand command) {
|
||||
|
||||
Executor executor = Executor.getExecutor();
|
||||
|
||||
if (executor.getExecutorType() == command.getTaskType() &&
|
||||
executor.getStatus() == ExecutorStatus.IDLING) {
|
||||
executor.getAssignment();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package ch.unisg.executorrobot.executor.domain;
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpResponse;
|
||||
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.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.executorbase.executor.domain.ExecutorBase;
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorType;
|
||||
|
||||
public class Executor extends ExecutorBase {
|
||||
|
||||
private static final Executor executor = new Executor(ExecutorType.ROBOT);
|
||||
private final UserToRobotPort userToRobotPort = new UserToRobotAdapter();
|
||||
private final InstructionToRobotPort instructionToRobotPort = new InstructionToRobotAdapter();
|
||||
private final DeleteUserFromRobotPort deleteUserFromRobotPort = new DeleteUserFromRobotAdapter();
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
private Executor(ExecutorType executorType) {
|
||||
super(executorType);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected
|
||||
String execution() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
1
executorrobot/src/main/resources/application.properties
Normal file
1
executorrobot/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
server.port=8084
|
@@ -0,0 +1,13 @@
|
||||
package ch.unisg.executorrobot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ExecutorrobotApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user