Skip to content

Commit

Permalink
issue #3: prepare for raspberry pi integration
Browse files Browse the repository at this point in the history
reworked package structure and classes for more generic wired/wireless robots
added calliope as generic wired robot for serial monitor usage only
removed launcher class
extracted help popup time to properties
  • Loading branch information
boonto committed Jul 31, 2019
1 parent 2d6f3b2 commit aba5ce5
Show file tree
Hide file tree
Showing 38 changed files with 645 additions and 441 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
de.fhg.iais.roberta.main.OpenRobertaConnectorLauncher
de.fhg.iais.roberta.main.OpenRobertaConnector
</mainClass>
</manifest>
</archive>
Expand All @@ -312,7 +312,7 @@
<restartOnCrash>true</restartOnCrash>
<icon>src/main/resources/images/OR.ico</icon>
<classPath>
<mainClass>de.fhg.iais.roberta.main.OpenRobertaConnectorLauncher</mainClass>
<mainClass>de.fhg.iais.roberta.main.OpenRobertaConnector</mainClass>
<jarLocation>libs/</jarLocation>
<jarLocation>resources/</jarLocation>
<addDependencies>true</addDependencies>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/de/fhg/iais/roberta/connection/AutoConnector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.fhg.iais.roberta.connection;

/**
* Connector class for robots that do not need server connection.
* Automatically sets robots to connected.
*/
public class AutoConnector extends AbstractConnector<IRobot> {
public AutoConnector(IRobot robot) {
super(robot);
}

@Override
protected void runLoopBody() {
if ( this.state == State.DISCOVER ) {
this.fire(State.WAIT_FOR_CONNECT_BUTTON_PRESS);
this.fire(State.WAIT_FOR_CMD);
}
}
}
46 changes: 0 additions & 46 deletions src/main/java/de/fhg/iais/roberta/connection/ev3/Ev3.java

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/java/de/fhg/iais/roberta/connection/ev3/Ev3Detector.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package de.fhg.iais.roberta.connection.arduino;
package de.fhg.iais.roberta.connection.wired;

import java.util.Objects;

import de.fhg.iais.roberta.connection.IConnector;
import de.fhg.iais.roberta.connection.IRobot;

public class Arduino implements IRobot {
private final ArduinoType type;
/**
* Abstract class for a wired robot.
*/
public abstract class AbstractWiredRobot implements IWiredRobot {
private final WiredRobotType type;
private final String port;

public Arduino(ArduinoType type, String port) {
/**
* Constructor for wired robots.
*
* @param type the robot type
* @param port the robot port
*/
protected AbstractWiredRobot(WiredRobotType type, String port) {
this.type = type;
this.port = port;
}
Expand All @@ -20,23 +26,15 @@ public String getName() {
}

@Override
public ConnectionType getConnectionType() {
return ConnectionType.WIRED;
public String getPort() {
return this.port;
}

@Override
public IConnector<? extends IRobot> createConnector() {
return new ArduinoConnector(this);
}

public ArduinoType getType() {
public WiredRobotType getType() {
return this.type;
}

public String getPort() {
return this.port;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
Expand All @@ -45,8 +43,8 @@ public boolean equals(Object obj) {
if ( (obj == null) || (this.getClass() != obj.getClass()) ) {
return false;
}
Arduino arduino = (Arduino) obj;
return (this.type == arduino.type) && Objects.equals(this.port, arduino.port);
IWiredRobot robot = (IWiredRobot) obj;
return (this.type == robot.getType()) && Objects.equals(this.port, robot.getPort());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.fhg.iais.roberta.connection.wired;

import de.fhg.iais.roberta.connection.IRobot;

/**
* Interface for wired robots.
*/
public interface IWiredRobot extends IRobot {
@Override
default ConnectionType getConnectionType() {
return ConnectionType.WIRED;
}

/**
* Returns the port of the wired robot.
*
* @return the port of the wired robot
*/
String getPort();

/**
* Returns the type of the wired robot.
*
* @return the type of the wired robot
*/
WiredRobotType getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.fhg.iais.roberta.connection.wired;

import org.apache.http.entity.ContentType;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Response;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;

import de.fhg.iais.roberta.connection.IDetector;
import de.fhg.iais.roberta.connection.IRobot;
import de.fhg.iais.roberta.connection.wired.ev3.Ev3;
import de.fhg.iais.roberta.util.PropertyHelper;

import static de.fhg.iais.roberta.connection.IConnector.CMD_REGISTER;
import static de.fhg.iais.roberta.connection.IConnector.KEY_CMD;

public class RndisDetector implements IDetector {
private static final Logger LOG = LoggerFactory.getLogger(RndisDetector.class);

private static final Map<String, Class<? extends AbstractWiredRobot>> DEVICES = new HashMap<>(1);
static {
DEVICES.put(PropertyHelper.getInstance().getProperty("brickIp"), Ev3.class);
}

@Override
public List<IRobot> detectRobots() {
try (AsyncHttpClient client = Dsl.asyncHttpClient()) {
JSONObject request = new JSONObject();
request.put(KEY_CMD, CMD_REGISTER);

List<IRobot> robots = new ArrayList<>(5);
for ( Entry<String, Class<? extends AbstractWiredRobot>> entry : DEVICES.entrySet() ) {
ListenableFuture<String>
name =
client.preparePost("http://" + entry.getKey() + "/brickinfo")
.addHeader("ContentType", ContentType.APPLICATION_JSON)
.setBody(request.toString())
.execute(new StringAsyncCompletionHandler());
robots.add(entry.getValue().getConstructor(String.class).newInstance(name.get()));
}
return robots;
} catch ( IOException e ) {
LOG.error("Could not close async client: {}", e.getMessage());
} catch ( InterruptedException e ) {
LOG.error("Robot request was interrupted: {}", e.getMessage());
} catch ( ExecutionException e ) {
LOG.info("Could not find RNDIS robot: {}", e.getMessage());
} catch ( InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e ) {
LOG.error("Robot not implemented: {}", e.getMessage());
}

return Collections.emptyList();
}

private static class StringAsyncCompletionHandler extends AsyncCompletionHandler<String> {
@Override
public String onCompleted(Response response) {
JSONObject jsonObject = new JSONObject(response.getResponseBody());
return jsonObject.getString("brickname");
}
}
}
Loading

0 comments on commit aba5ce5

Please sign in to comment.