Search in sources :

Example 6 with RoboContext

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();
}
Also used : Scanner(java.util.Scanner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) FileInputStream(java.io.FileInputStream) RoboBuilderException(com.robo4j.RoboBuilderException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with RoboContext

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;
}
Also used : SimpleLoggingUtil(com.robo4j.logging.SimpleLoggingUtil) CodecRegistry(com.robo4j.socket.http.units.CodecRegistry) Arrays(java.util.Arrays) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) Collectors(java.util.stream.Collectors) ResponseUnitDTO(com.robo4j.socket.http.dto.ResponseUnitDTO) ResponseAttributeDTO(com.robo4j.socket.http.dto.ResponseAttributeDTO) JsonUtil(com.robo4j.socket.http.util.JsonUtil) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) HttpServerUnit(com.robo4j.socket.http.units.HttpServerUnit) List(java.util.List) SocketDecoder(com.robo4j.socket.http.units.SocketDecoder) HttpMethod(com.robo4j.socket.http.HttpMethod) RoboContext(com.robo4j.RoboContext) RoboReference(com.robo4j.RoboReference) ResponseDecoderUnitDTO(com.robo4j.socket.http.dto.ResponseDecoderUnitDTO) ReflectUtils(com.robo4j.socket.http.util.ReflectUtils) ResponseUnitDTO(com.robo4j.socket.http.dto.ResponseUnitDTO)

Example 8 with RoboContext

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);
}
Also used : RoboContext(com.robo4j.RoboContext) DatagramDenominator(com.robo4j.socket.http.message.DatagramDenominator) CountDownLatch(java.util.concurrent.CountDownLatch) DatagramDecoratedRequest(com.robo4j.socket.http.message.DatagramDecoratedRequest) Test(org.junit.jupiter.api.Test)

Example 9 with RoboContext

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");
}
Also used : RoboContext(com.robo4j.RoboContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 10 with RoboContext

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));
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

RoboContext (com.robo4j.RoboContext)50 RoboBuilder (com.robo4j.RoboBuilder)33 Test (org.junit.jupiter.api.Test)20 InputStream (java.io.InputStream)17 Configuration (com.robo4j.configuration.Configuration)10 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 FileInputStream (java.io.FileInputStream)5 RoboReference (com.robo4j.RoboReference)4 RoboBuilderException (com.robo4j.RoboBuilderException)3 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)3 List (java.util.List)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StringConsumer (com.robo4j.StringConsumer)2 BiColor (com.robo4j.hw.rpi.i2c.adafruitbackpack.BiColor)2 DataEvent3f (com.robo4j.hw.rpi.imu.bno.DataEvent3f)2 HttpMethod (com.robo4j.socket.http.HttpMethod)2 CameraMessage (com.robo4j.socket.http.codec.CameraMessage)2 HttpPathMethodDTO (com.robo4j.socket.http.dto.HttpPathMethodDTO)2