Skip to content

Commit

Permalink
project cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed Jan 27, 2021
1 parent 8f86a9f commit 4b66f73
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 43 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ project(':rpi-powercontroller-client') {

project(':rpi-powercontroller') {
dependencies {
implementation project(':rpi-drivers')
implementation project(':rpi-powercontroller-client')
implementation (project(':rpi-drivers'))
implementation (project(':rpi-powercontroller-client')) {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
}
}
9 changes: 9 additions & 0 deletions rpi-powercontroller-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ implementation 'one.microproject.rpi:rpi-powercontroller-client:1.0.0'
```

### use in your java code
```java
PowerControllerReadClient powerControllerReadClient = PowerControllerClientBuilder.builder()
.baseUrl("http://localhost:8090")
.withCredentials("client-001", "secret")
.buildReadClient();
powerControllerReadClient.getSystemInfo();
...
```
```java
PowerControllerClient powerControllerClient = PowerControllerClientBuilder.builder()
.baseUrl("http://localhost:8090")
.withCredentials("client-001", "secret")
.build();
powerControllerClient.getSystemInfo();
powerControllerClient.getMeasurements();
...
```
2 changes: 1 addition & 1 deletion rpi-powercontroller-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ targetCompatibility = 1.8

group = 'one.microproject.rpi.powercontroller'
archivesBaseName = "rpi-powercontroller-client"
version = '1.0.0'
version = '1.2.0'

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
package one.microproject.rpi.powercontroller;

import one.microproject.rpi.powercontroller.dto.JobId;
import one.microproject.rpi.powercontroller.dto.JobInfo;
import one.microproject.rpi.powercontroller.dto.Measurements;
import one.microproject.rpi.powercontroller.dto.SystemInfo;
import one.microproject.rpi.powercontroller.dto.SystemState;
import one.microproject.rpi.powercontroller.dto.TaskFilter;
import one.microproject.rpi.powercontroller.dto.TaskId;
import one.microproject.rpi.powercontroller.dto.TaskInfo;

import java.util.Collection;
import java.util.Optional;

public interface PowerControllerClient {

SystemInfo getSystemInfo();

SystemState getSystemState();

Measurements getMeasurements();

Collection<JobInfo> getSystemJobs();

JobId killAllJobId();

Collection<TaskInfo> getAllTasks();

Collection<TaskInfo> getTasks(TaskFilter filter);
public interface PowerControllerClient extends PowerControllerReadClient {

/**
* Set OUTPUT port state (ON=true | OFF=false)
* @param port output port index.
* @param state required output port state.
* @return resulting output port state.
*/
boolean setPortState(Integer port, Boolean state);

/**
* Submit task for execution.
* @param id {@link JobId} unique ID of jop to be submitted as new task.
* @return unique {@link TaskId} of submitted task.
*/
Optional<TaskId> submitTask(JobId id);

/**
* Cancel running task.
* @param id unique {@link TaskId}.
* @return True if the task has been cancelled, false otherwise.
*/
boolean cancelTask(TaskId id);

/**
* Cancel all running task.
* @return True if action succeeded, False otherwise.
*/
boolean cancelAllTasks();

/**
* Blocking wait for the starting task.
* @param id unique {@link TaskId}.
* @return True if action succeeded, False otherwise.
*/
boolean waitForTaskStarted(TaskId id);

/**
* Blocking wait for the task termination.
* @param id unique {@link TaskId}.
* @return True if action succeeded, False otherwise.
*/
boolean waitForTaskTermination(TaskId id);

/**
* Clean task queue, remove all tasks in terminal state.
* @return True if action succeeded, False otherwise.
*/
boolean cleanTaskQueue();

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public PowerControllerClient build() {
return new PowerControllerClientImpl(baseURL, clientId, clientSecret);
}

public PowerControllerReadClient buildReadClient() {
return new PowerControllerClientImpl(baseURL, clientId, clientSecret);
}

public static PowerControllerClientBuilder builder() {
return new PowerControllerClientBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package one.microproject.rpi.powercontroller;

import one.microproject.rpi.powercontroller.dto.JobId;
import one.microproject.rpi.powercontroller.dto.JobInfo;
import one.microproject.rpi.powercontroller.dto.Measurements;
import one.microproject.rpi.powercontroller.dto.SystemInfo;
import one.microproject.rpi.powercontroller.dto.SystemState;
import one.microproject.rpi.powercontroller.dto.TaskFilter;
import one.microproject.rpi.powercontroller.dto.TaskInfo;

import java.util.Collection;

/**
* Read data from RPi Power Controller.
* https://github.com/jveverka/rpi-projects/tree/master/rpi-powercontroller
*/
public interface PowerControllerReadClient {

/**
* Get {@link SystemInfo} for this RPi Power Controller.
* @return {@link SystemInfo}
*/
SystemInfo getSystemInfo();

/**
* Get {@link SystemState} for this RPi Power Controller.
* @return {@link SystemState}
*/
SystemState getSystemState();

/**
* Get sensor(s) {@link Measurements} for this RPi Power Controller.
* @return {@link Measurements}
*/
Measurements getMeasurements();

/**
* Get {@link JobInfo} for available pre-configured Jobs for this RPi Power Controller.
* @return Collection of {@link JobInfo}.
*/
Collection<JobInfo> getSystemJobs();

/**
* Get {@link JobId} of 'kill all' job for this RPi Power Controller.
* @return {@link JobId}
*/
JobId getKillAllJobId();

/**
* Get task queue (waiting, in-progress, finished) tasks.
* @return Collection of {@link TaskInfo}
*/
Collection<TaskInfo> getAllTasks();

/**
* Get filtered task queue (waiting, in-progress, finished) tasks.
* @param filter {@link TaskFilter} task filter criteria.
* @return Collection of {@link TaskInfo}
*/
Collection<TaskInfo> getTasks(TaskFilter filter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PowerControllerClientImpl implements PowerControllerClient {
private static final Logger LOG = LoggerFactory.getLogger(PowerControllerClientImpl.class);
private static final String AUTHORIZATION = "Authorization";
private static final String APPLICATION_JSON = "application/json";
private static final String ERROR_MESSAGE = "Expected http=200, received http=";

private final OkHttpClient client;
private final ObjectMapper mapper;
Expand Down Expand Up @@ -59,7 +60,7 @@ public SystemInfo getSystemInfo() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), SystemInfo.class);
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -77,7 +78,7 @@ public SystemState getSystemState() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), SystemState.class);
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -95,7 +96,7 @@ public Measurements getMeasurements() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), Measurements.class);
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -113,14 +114,14 @@ public Collection<JobInfo> getSystemJobs() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), new TypeReference<Collection<JobInfo>>(){});
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
}

@Override
public JobId killAllJobId() {
public JobId getKillAllJobId() {
try {
Request request = new Request.Builder()
.url(baseURL + "/system/jobs/killalljobid")
Expand All @@ -131,7 +132,7 @@ public JobId killAllJobId() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), JobId.class);
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -149,7 +150,7 @@ public Collection<TaskInfo> getAllTasks() {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), new TypeReference<Collection<TaskInfo>>(){});
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -168,7 +169,7 @@ public Collection<TaskInfo> getTasks(TaskFilter filter) {
if (response.code() == 200) {
return mapper.readValue(response.body().string(), new TypeReference<Collection<TaskInfo>>(){});
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -188,7 +189,7 @@ public boolean setPortState(Integer port, Boolean state) {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -207,7 +208,7 @@ public Optional<TaskId> submitTask(JobId id) {
if (response.code() == 200) {
return Optional.of(mapper.readValue(response.body().string(), TaskId.class));
}
LOG.info("Expected http=200, received http={}", response.code());
LOG.info(ERROR_MESSAGE, response.code());
return Optional.empty();
} catch (IOException e) {
throw new ClientException(e);
Expand All @@ -227,7 +228,7 @@ public boolean cancelTask(TaskId id) {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -245,7 +246,7 @@ public boolean cancelAllTasks() {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -264,7 +265,7 @@ public boolean waitForTaskStarted(TaskId id) {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -283,7 +284,7 @@ public boolean waitForTaskTermination(TaskId id) {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand All @@ -301,7 +302,7 @@ public boolean cleanTaskQueue() {
if (response.code() == 200) {
return 200 == response.code();
}
throw new ClientException("Expected http=200, received http=" + response.code());
throw new ClientException(ERROR_MESSAGE + response.code());
} catch (IOException e) {
throw new ClientException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
class TestBuilder {

@Test
void testBuilder() {
void testClientBuilder() {
PowerControllerClient powerControllerClient = PowerControllerClientBuilder.builder()
.baseUrl("http://localhost:8090")
.withCredentials("client-001", "secret")
.build();
assertNotNull(powerControllerClient);
}

@Test
void testReadClientBuilder() {
PowerControllerReadClient powerControllerReadClient = PowerControllerClientBuilder.builder()
.baseUrl("http://localhost:8090")
.withCredentials("client-001", "secret")
.buildReadClient();
assertNotNull(powerControllerReadClient);
}

}
2 changes: 2 additions & 0 deletions rpi-powercontroller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {

sourceCompatibility = 1.8
targetCompatibility = 1.8

group = 'one.microproject.rpi.powercontroller'
version = '1.0.0'
mainClassName = 'one.microproject.rpi.powercontroller.PowerControllerApp'
Expand All @@ -25,6 +26,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.1'
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0'
testImplementation 'com.squareup.okhttp3:okhttp:4.9.0'
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private HandlerUtils() {
public static final int NOT_FOUND = 404;
public static final int FORBIDDEN = 403;
public static final int METHOD_NOT_ALLOWED = 405;
public static final int INTERNAL_SERVER_ERROR = 500;
public static final int OK = 200;

public static boolean validateRequest(AAService aaService, HttpServerExchange exchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
SetPortRequest setPortRequest = mapper.readValue(is, SetPortRequest.class);
Optional<Boolean> result = rPiService.setPortState(setPortRequest.getPort(), setPortRequest.getState());
if (result.isPresent()) {
exchange.setStatusCode(HandlerUtils.OK);
if (setPortRequest.getState().equals(result.get())) {
exchange.setStatusCode(HandlerUtils.OK);
} else {
exchange.setStatusCode(HandlerUtils.INTERNAL_SERVER_ERROR);
}
} else {
exchange.setStatusCode(HandlerUtils.NOT_FOUND);
}
Expand Down
Loading

0 comments on commit 4b66f73

Please sign in to comment.