Search in sources :

Example 36 with RoboContext

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

the class RemoteContextTests method startRemoteSenderTest.

@Disabled("for individual testing")
@Test
void startRemoteSenderTest() throws Exception {
    // 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();
    // context has been discovered
    RoboContextDescriptor descriptor = getRoboContextDescriptor(service, CONTEXT_ID_REMOTE_RECEIVER);
    assertTrue(service.getDiscoveredContexts().size() > 0);
    assertNotNull(descriptor);
    // build the producer system
    RoboContext producerEmitterSystem = buildEmitterContext(String.class, UNIT_STRING_CONSUMER, REMOTE_UNIT_EMITTER);
    localLookup.addContext(producerEmitterSystem);
    producerEmitterSystem.start();
    RoboReference<String> remoteTestMessageProducer = producerEmitterSystem.getReference(REMOTE_UNIT_EMITTER);
    for (int i = 0; i < 10; i++) {
        remoteTestMessageProducer.sendMessage("REMOTE MESSAGE :" + i);
    }
    System.in.read();
}
Also used : RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 37 with RoboContext

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

the class RemoteTestMessageProducer method sendRandomMessage.

public void sendRandomMessage() {
    final int number = TestToolkit.getRND().nextInt() % 100;
    final String text = StringToolkit.getRandomMessage(10);
    // We're sending a reference to ourself for getting the acks...
    final TestMessageType message = new TestMessageType(number, text, getContext().getReference(getId()));
    RoboContext ctx = LookupServiceProvider.getDefaultLookupService().getContext(targetContext);
    ctx.getReference(target).sendMessage(message);
}
Also used : RoboContext(com.robo4j.RoboContext)

Example 38 with RoboContext

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

the class VectorEventListenerUnit method onMessage.

@Override
public void onMessage(VectorEvent message) {
    SimpleLoggingUtil.info(getClass(), "received:" + message);
    RoboContext remoteContext = LookupServiceProvider.getDefaultLookupService().getContext(targetContext);
    if (remoteContext != null) {
        RoboReference<VectorEvent> roboReference = remoteContext.getReference(remoteUnit);
        if (roboReference != null) {
            roboReference.sendMessage(message);
        }
    } else {
        SimpleLoggingUtil.info(getClass(), String.format("context not found: %s", targetContext));
    }
}
Also used : RoboContext(com.robo4j.RoboContext) VectorEvent(com.robo4j.hw.rpi.imu.bno.VectorEvent)

Example 39 with RoboContext

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

Example 40 with RoboContext

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

the class ColorDemo method runDemo.

@Override
public void runDemo() {
    String prefix = "Color changes:\n";
    lcd.sendMessage(LcdMessage.MESSAGE_CLEAR);
    int delay = 0;
    int i = 0;
    for (; i < Color.values().length - 1; i++) {
        Color c = Color.values()[i];
        scheduler.schedule(lcd, getColorMessage(prefix, c), delay += 1, 1, TimeUnit.SECONDS, 1);
    }
    scheduler.schedule(lcd, getColorMessage(prefix, Color.values()[i]), delay += 1, 1, TimeUnit.SECONDS, 1, (RoboContext context) -> {
        lcd.sendMessage(new LcdMessage(getName() + " Demo:\nDone!", Color.ON));
        setDone();
    });
}
Also used : Color(com.robo4j.hw.rpi.i2c.adafruitlcd.Color) RoboContext(com.robo4j.RoboContext)

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