Search in sources :

Example 31 with RoboBuilder

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

the class RoboContextUtils method loadRoboContextByXml.

public static RoboContext loadRoboContextByXml(String xmlFilename) throws RoboBuilderException {
    Objects.requireNonNull(xmlFilename, "not allowed");
    RoboBuilder builder = new RoboBuilder();
    InputStream contextIS = RoboContextUtils.class.getClassLoader().getResourceAsStream(xmlFilename);
    builder.add(contextIS);
    return builder.build();
}
Also used : InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder)

Example 32 with RoboBuilder

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

the class RemoteContextTests method messageIncludingReferenceToDiscoveredContextTest.

@Test
void messageIncludingReferenceToDiscoveredContextTest() throws RoboBuilderException, IOException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverAckSystem.xml"));
    AckingStringConsumer consumer = new AckingStringConsumer(builder.getContext(), ACK_CONSUMER);
    builder.add(consumer);
    RoboContext receiverCtx = builder.build();
    receiverCtx.start();
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("9") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("9");
    assertNotNull(descriptor);
    builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testMessageEmitterSystem_8.xml"));
    RemoteTestMessageProducer remoteTestMessageProducer = new RemoteTestMessageProducer(builder.getContext(), REMOTE_UNIT_EMITTER);
    remoteTestMessageProducer.initialize(getEmitterConfiguration("9", ACK_CONSUMER));
    builder.add(remoteTestMessageProducer);
    RoboContext emitterContext = builder.build();
    localLookup.addContext(emitterContext);
    emitterContext.start();
    remoteTestMessageProducer.sendMessage("sendMessage");
    for (int i = 0; i < NUMBER_ITERATIONS && consumer.getReceivedMessages().size() == 0; i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(consumer.getReceivedMessages().size() > 0);
    System.out.println("Got messages: " + consumer.getReceivedMessages());
    assertTrue(remoteTestMessageProducer.getAckCount() > 0);
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 33 with RoboBuilder

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

the class RemoteContextTests method buildEmitterContext.

private <T> RoboContext buildEmitterContext(Class<T> clazz, String target, String unitName) throws Exception {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testMessageEmitterSystem_8.xml"));
    if (clazz.equals(String.class)) {
        StringProducerRemote<T> remoteTestMessageProducer = new StringProducerRemote<>(clazz, builder.getContext(), unitName);
        remoteTestMessageProducer.initialize(getEmitterConfiguration(REMOTE_CONTEXT_RECEIVER, target));
        builder.add(remoteTestMessageProducer);
    }
    if (clazz.equals(TestMessageType.class)) {
        RemoteTestMessageProducer remoteTestMessageProducer = new RemoteTestMessageProducer(builder.getContext(), unitName);
        remoteTestMessageProducer.initialize(getEmitterConfiguration(REMOTE_CONTEXT_RECEIVER, target));
        builder.add(remoteTestMessageProducer);
    }
    return builder.build();
}
Also used : StringProducerRemote(com.robo4j.StringProducerRemote) RoboBuilder(com.robo4j.RoboBuilder)

Example 34 with RoboBuilder

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

the class RemoteContextTests method messageToDiscoveredContextTest.

@Test
void messageToDiscoveredContextTest() throws RoboBuilderException, IOException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverSystem.xml"));
    StringConsumer consumer = new StringConsumer(builder.getContext(), ACK_CONSUMER);
    builder.add(consumer);
    RoboContext receiverCtx = builder.build();
    receiverCtx.start();
    // Note that all this cludging about with local lookup service
    // implementations etc would normally not be needed.
    // This is just to isolate this test from other tests.
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("7") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("7");
    assertNotNull(descriptor);
    builder = new RoboBuilder(RemoteContextTests.class.getClassLoader().getResourceAsStream("testMessageEmitterSystem_10.xml"));
    RemoteStringProducer remoteStringProducer = new RemoteStringProducer(builder.getContext(), REMOTE_UNIT_EMITTER);
    remoteStringProducer.initialize(getEmitterConfiguration("7", ACK_CONSUMER));
    builder.add(remoteStringProducer);
    RoboContext emitterContext = builder.build();
    localLookup.addContext(emitterContext);
    emitterContext.start();
    remoteStringProducer.sendMessage("sendRandomMessage");
    for (int i = 0; i < NUMBER_ITERATIONS && consumer.getReceivedMessages().size() == 0; i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(consumer.getReceivedMessages().size() > 0);
    System.out.println("Got messages: " + consumer.getReceivedMessages());
    emitterContext.shutdown();
    receiverCtx.shutdown();
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer) Test(org.junit.jupiter.api.Test)

Example 35 with RoboBuilder

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

the class AdafruitBiColor24BackpackExample method main.

public static void main(String[] args) throws Exception {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    InputStream settings = AdafruitBiColor24BackpackExample.class.getClassLoader().getResourceAsStream("bargraph24example.xml");
    RoboContext ctx = new RoboBuilder().add(settings).build();
    ctx.start();
    RoboReference<DrawMessage> barUnit = ctx.getReference("bargraph");
    DrawMessage clearMessage = new DrawMessage(BackpackMessageCommand.CLEAR);
    AtomicInteger position = new AtomicInteger();
    executor.scheduleAtFixedRate(() -> {
        if (position.get() > BiColor24BarDevice.MAX_BARS - 1) {
            position.set(0);
        }
        barUnit.sendMessage(clearMessage);
        barUnit.sendMessage(new DrawMessage(BackpackMessageCommand.PAINT, new short[] { (short) position.getAndIncrement() }, new short[] { 0 }, new BiColor[] { BiColor.getByValue(position.get() % 3 + 1) }));
    }, 2, 1, TimeUnit.SECONDS);
    System.out.println("Press enter to quit\n");
    System.in.read();
    executor.shutdown();
    ctx.shutdown();
}
Also used : BiColor(com.robo4j.hw.rpi.i2c.adafruitbackpack.BiColor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) RoboContext(com.robo4j.RoboContext) RoboBuilder(com.robo4j.RoboBuilder)

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