use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class CalibrationUtility method main.
public static void main(String[] args) throws RoboBuilderException, FileNotFoundException {
InputStream settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("calibration.xml");
if (args.length != 1) {
System.out.println("No file specified, using default calibration.xml");
} else {
settings = new FileInputStream(args[0]);
}
RoboBuilder builder = new RoboBuilder();
if (settings == null) {
System.out.println("Could not find the settings for servo calibration test!");
System.exit(2);
}
builder.add(settings);
RoboContext ctx = builder.build();
System.out.println("State before start:");
System.out.println(SystemUtil.printStateReport(ctx));
ctx.start();
System.out.println("State after start:");
System.out.println(SystemUtil.printStateReport(ctx));
String lastCommand;
Scanner scanner = new Scanner(System.in);
System.out.println("Type the servo to control and how much to move the servo, between -1 and 1. For example:\npan -1.0\nType q and enter to quit!\n");
while (!"q".equals(lastCommand = scanner.nextLine())) {
lastCommand = lastCommand.trim();
String[] split = lastCommand.split(" ");
if (split.length != 2) {
System.out.println("Could not parse " + lastCommand + ". Please try again!");
continue;
}
RoboReference<Float> servoRef = ctx.getReference(split[0]);
if (servoRef == null) {
System.out.println("Could not find any robo unit named " + split[0] + ". Please try again!");
continue;
}
try {
float value = Float.parseFloat(split[1]);
servoRef.sendMessage(value);
} catch (Exception e) {
System.out.println("Could not parse " + split[1] + " as a float number. Error message was: " + e.getMessage() + ". Please try again!");
continue;
}
}
ctx.shutdown();
scanner.close();
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RoboRequestFactory method processGet.
/**
* Generic robo context overview. It returns all units registered into the context including system id.
* The 1st position is reserved for the system
*
* @param context
* robo context
* @return descripton of desired context
*/
@Override
public Object processGet(RoboContext context) {
if (!context.getUnits().isEmpty()) {
final List<ResponseUnitDTO> unitList = context.getUnits().stream().map(u -> new ResponseUnitDTO(u.getId(), u.getState())).collect(Collectors.toList());
unitList.add(0, new ResponseUnitDTO(context.getId(), context.getState()));
return JsonUtil.toJsonArray(unitList);
} else {
SimpleLoggingUtil.error(getClass(), "internal error: no units available");
}
return null;
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RoboDatagramPingPongTest method datagramPingPongTest.
@Test
void datagramPingPongTest() throws Exception {
RoboContext pongSystem = configurePongSystem(TOTAL_NUMBER);
RoboContext pingSystem = configurePingSystem();
pongSystem.start();
pingSystem.start();
System.out.println("UDP pongSystem: State after start:");
System.out.println(SystemUtil.printStateReport(pongSystem));
System.out.println("UDP pingSystem: State after start:");
System.out.println(SystemUtil.printStateReport(pingSystem));
RoboReference<String> pongStringConsumerReference = pongSystem.getReference(StringConsumer.NAME);
CountDownLatch totalMessageLatch = pongStringConsumerReference.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_LATCH).get();
RoboReference<DatagramDecoratedRequest> udpClient = pingSystem.getReference(UDP_CLIENT);
for (int i = 0; i < TOTAL_NUMBER; i++) {
DatagramDenominator denominator = new DatagramDenominator(DatagramBodyType.JSON.getType(), "/units/stringConsumer");
DatagramDecoratedRequest request = new DatagramDecoratedRequest(denominator);
String message = "{\"message\": \"Hello i:" + i + "\"}";
request.addMessage(message.getBytes());
udpClient.sendMessage(request);
}
totalMessageLatch.await(TIMEOUT, TIME_UNIT);
final int pongConsumerTotalNumber = pongStringConsumerReference.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_TOTAL).get();
System.out.println("UDP pongSystem: State after shutdown:");
System.out.println(SystemUtil.printStateReport(pongSystem));
System.out.println("UDP pingSystem: State after shutdown:");
System.out.println(SystemUtil.printStateReport(pingSystem));
pingSystem.shutdown();
pongSystem.shutdown();
assertEquals(TOTAL_NUMBER, pongConsumerTotalNumber);
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RoboHttpDynamicTests method simpleHttpNonUnitTest.
/**
* Motivation Client system is sending messages to the main system over HTTP
* Main System receives desired number of messages.
*
* Values are requested by Attributes
*
* @throws Exception
* exception
*/
@Test
void simpleHttpNonUnitTest() throws Exception {
/* tested system configuration */
RoboContext mainSystem = getServerRoboSystem(MESSAGES_NUMBER);
/* system which is testing main system */
RoboContext clientSystem = getClientRoboSystem();
System.out.println("Client system state after start:");
System.out.println(SystemUtil.printStateReport(clientSystem));
System.out.println("Main system state after start:");
System.out.println(SystemUtil.printStateReport(mainSystem));
/* client system sending a messages to the main system */
RoboReference<Object> decoratedProducer = clientSystem.getReference(DECORATED_PRODUCER);
decoratedProducer.sendMessage(MESSAGES_NUMBER);
CountDownLatch countDownLatchDecoratedProducer = decoratedProducer.getAttribute(SocketMessageDecoratedProducerUnit.DESCRIPTOR_MESSAGES_LATCH).get();
countDownLatchDecoratedProducer.await(TIMEOUT, TIME_UNIT);
final RoboReference<String> stringConsumer = mainSystem.getReference(StringConsumer.NAME);
final CountDownLatch countDownLatch = stringConsumer.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_LATCH).get();
countDownLatch.await(TIMEOUT, TIME_UNIT);
final int receivedMessages = stringConsumer.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_TOTAL).get();
clientSystem.shutdown();
mainSystem.shutdown();
System.out.println("System is Down!");
assertNotNull(mainSystem.getUnits());
assertEquals(MESSAGES_NUMBER, receivedMessages, "wrong received messages");
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RoboHttpDynamicTests method pingExternalSystem.
/**
* testing ping external system
*
* @throws Exception
* exception
*/
@Disabled("intent to run manual")
@Test
void pingExternalSystem() throws Exception {
RoboBuilder pingSystemBuilder = getHttpClientRobotBuilder("127.0.0.1", 8080);
pingSystemBuilder.add(StringConsumer.class, StringConsumer.NAME);
RoboContext pingSystemContext = pingSystemBuilder.build();
pingSystemContext.start();
System.out.println("PingSystem state after start:");
System.out.println(SystemUtil.printStateReport(pingSystemContext));
RoboReference<HttpDecoratedRequest> httpClient = pingSystemContext.getReference(ID_CLIENT_UNIT);
Thread.sleep(1000);
for (int i = 0; i < 1; i++) {
HttpRequestDenominator denominator = new HttpRequestDenominator(HttpMethod.GET, "/noparams", HttpVersion.HTTP_1_1);
HttpDecoratedRequest request = new HttpDecoratedRequest(denominator);
request.addCallback(StringConsumer.NAME);
httpClient.sendMessage(request);
}
Thread.sleep(1000);
pingSystemContext.stop();
System.out.println("PingSystem state after stop:");
System.out.println(SystemUtil.printStateReport(pingSystemContext));
}
Aggregations