Search in sources :

Example 1 with RoboBuilder

use of com.robo4j.RoboBuilder in project robo4j by Robo4J.

the class RemoteContextTests method buildRemoteReceiverContext.

/*
	 * Builds a system ready to receive TestMessageType messages, which contains a
	 * reference to which the String message "acknowledge" will be sent when a
	 * message is received.
	 */
private RoboContext buildRemoteReceiverContext(String name) throws RoboBuilderException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteReceiver.xml"));
    Configuration configuration = new ConfigurationBuilder().addInteger(AckingStringConsumer.ATTR_TOTAL_NUMBER_MESSAGES, 1).build();
    builder.add(AckingStringConsumer.class, configuration, name);
    return builder.build();
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder)

Example 2 with RoboBuilder

use of com.robo4j.RoboBuilder in project robo4j by Robo4J.

the class RemoteContextTests method discoveryOfDiscoveryEnabledRoboContextTest.

@Test
void discoveryOfDiscoveryEnabledRoboContextTest() throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testDiscoverableSystem.xml"));
    RoboContext ctx = builder.build();
    ctx.start();
    final LookupService service = LookupServiceTests.getLookupService(new LocalLookupServiceImpl());
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("6") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("6");
    assertEquals(descriptor.getMetadata().get("name"), "Caprica");
    assertEquals(descriptor.getMetadata().get("class"), "Cylon");
    ctx.shutdown();
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 3 with RoboBuilder

use of com.robo4j.RoboBuilder in project robo4j by Robo4J.

the class RemoteContextTests method buildReceiverSystemStringConsumer.

/*
	 * Builds a system ready to receive strings.
	 */
private RoboContext buildReceiverSystemStringConsumer() throws RoboBuilderException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteReceiver.xml"));
    Configuration configuration = new ConfigurationBuilder().addInteger("totalNumberMessages", 1).build();
    StringConsumer stringConsumer = new StringConsumer(builder.getContext(), UNIT_STRING_CONSUMER);
    stringConsumer.initialize(configuration);
    builder.add(stringConsumer);
    RoboContext receiverSystem = builder.build();
    receiverSystem.start();
    return receiverSystem;
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer)

Example 4 with RoboBuilder

use of com.robo4j.RoboBuilder in project robo4j by Robo4J.

the class CounterUnitTests method test.

@Test
void test() throws RoboBuilderException, InterruptedException, ExecutionException {
    // FIXME(Marcus/Aug 20, 2017): We really should get rid of the sleeps
    // here and use waits with timeouts...
    RoboBuilder builder = new RoboBuilder();
    builder.add(IntegerConsumer.class, ID_CONSUMER);
    builder.add(CounterUnit.class, getCounterConfiguration(ID_CONSUMER, 1000), ID_COUNTER);
    RoboContext context = builder.build();
    context.start();
    assertEquals(LifecycleState.STARTED, context.getState());
    RoboReference<CounterCommand> counter = context.getReference(ID_COUNTER);
    RoboReference<Integer> consumer = context.getReference(ID_CONSUMER);
    counter.sendMessage(CounterCommand.START);
    Thread.sleep(2500);
    assertTrue(consumer.getAttribute(NUMBER_OF_MESSAGES).get() > 2);
    counter.sendMessage(CounterCommand.STOP);
    Thread.sleep(200);
    Integer count = consumer.getAttribute(NUMBER_OF_MESSAGES).get();
    Thread.sleep(2500);
    assertEquals(count, consumer.getAttribute(NUMBER_OF_MESSAGES).get());
    ArrayList<Integer> messages = consumer.getAttribute(MESSAGES).get();
    assertNotEquals(0, messages.size());
    assertNotEquals(0, (int) messages.get(messages.size() - 1));
    counter.sendMessage(CounterCommand.RESET);
    Thread.sleep(1000);
    assertEquals(0, (int) counter.getAttribute(COUNTER).get());
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 5 with RoboBuilder

use of com.robo4j.RoboBuilder 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)

Aggregations

RoboBuilder (com.robo4j.RoboBuilder)45 RoboContext (com.robo4j.RoboContext)33 InputStream (java.io.InputStream)20 Configuration (com.robo4j.configuration.Configuration)18 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)17 Test (org.junit.jupiter.api.Test)13 FileInputStream (java.io.FileInputStream)5 HttpPathConfigJsonBuilder (com.robo4j.socket.http.util.HttpPathConfigJsonBuilder)4 RoboBuilderException (com.robo4j.RoboBuilderException)3 IOException (java.io.IOException)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 CameraMessage (com.robo4j.socket.http.codec.CameraMessage)2 BnoRequest (com.robo4j.units.rpi.imu.BnoRequest)2 FileNotFoundException (java.io.FileNotFoundException)2 Path (java.nio.file.Path)2 Scanner (java.util.Scanner)2