Search in sources :

Example 41 with RoboBuilder

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

the class CameraDecoratedImageProducerConsumerTests method decoratorProducerConsumerTest.

@Test
void decoratorProducerConsumerTest() throws Exception {
    RoboBuilder builderProducer = new RoboBuilder(Thread.currentThread().getContextClassLoader().getResourceAsStream("robo4jSystemProducer.xml"));
    InputStream clientConfigInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("robo_camera_producer_decorated_test.xml");
    builderProducer.add(clientConfigInputStream);
    RoboContext producerSystem = builderProducer.build();
    RoboBuilder builderConsumer = new RoboBuilder(Thread.currentThread().getContextClassLoader().getResourceAsStream("robo4jSystemConsumer.xml"));
    InputStream serverConfigInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("robo_camera_consumer_decorated_test.xml");
    builderConsumer.add(serverConfigInputStream);
    RoboContext consumerSystem = builderConsumer.build();
    long startTime = System.currentTimeMillis();
    consumerSystem.start();
    producerSystem.start();
    RoboReference<Boolean> imageProducer = producerSystem.getReference("imageController");
    RoboReference<CameraMessage> imageConsumer = consumerSystem.getReference("imageProcessor");
    CountDownLatch startLatchConsumer = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_START_LATCH).get();
    startLatchConsumer.await(5, TimeUnit.MINUTES);
    CountDownLatch imagesLatchProducer = imageProducer.getAttribute(CameraImageProducerDesTestUnit.DESCRIPTOR_GENERATED_IMAGES_LATCH).get();
    imagesLatchProducer.await(5, TimeUnit.MINUTES);
    Integer totalImagesProducer = imageProducer.getAttribute(CameraImageProducerDesTestUnit.DESCRIPTOR_TOTAL_IMAGES).get();
    CountDownLatch imagesLatchConsumer = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_IMAGES_LATCH).get();
    imagesLatchConsumer.await(5, TimeUnit.MINUTES);
    Integer totalImagesConsumer = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_RECEIVED_IMAGES).get();
    RoboHttpUtils.printMeasuredTime(getClass(), "duration", startTime);
    assertEquals(totalImagesProducer, totalImagesConsumer);
    producerSystem.shutdown();
    consumerSystem.shutdown();
    System.out.println("Press <Enter> to quit!");
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 42 with RoboBuilder

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

the class CameraImageProducerConsumerTests method cameraImageProdConTest.

@Test
void cameraImageProdConTest() throws Exception {
    RoboBuilder builderProducer = new RoboBuilder(Thread.currentThread().getContextClassLoader().getResourceAsStream("robo4jSystemProducer.xml"));
    InputStream clientConfigInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("robo_camera_producer_test.xml");
    builderProducer.add(clientConfigInputStream);
    RoboContext producerSystem = builderProducer.build();
    RoboBuilder builderConsumer = new RoboBuilder(Thread.currentThread().getContextClassLoader().getResourceAsStream("robo4jSystemConsumer.xml"));
    InputStream serverConfigInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("robo_camera_consumer_test.xml");
    builderConsumer.add(serverConfigInputStream);
    RoboContext consumerSystem = builderConsumer.build();
    long startTime = System.currentTimeMillis();
    consumerSystem.start();
    producerSystem.start();
    RoboReference<CameraMessage> imageConsumer = consumerSystem.getReference("imageProcessor");
    RoboReference<Boolean> imageProducer = producerSystem.getReference("imageController");
    CountDownLatch startConsumerLatch = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_START_LATCH).get();
    startConsumerLatch.await(5, TimeUnit.MINUTES);
    Integer totalImagesProducer = imageProducer.getAttribute(CameraImageProducerDesTestUnit.DESCRIPTOR_TOTAL_IMAGES).get();
    CountDownLatch imageProducerLatch = imageProducer.getAttribute(CameraImageProducerDesTestUnit.DESCRIPTOR_GENERATED_IMAGES_LATCH).get();
    CountDownLatch imageConsumerLatch = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_IMAGES_LATCH).get();
    System.out.println("LATCH");
    imageProducerLatch.await(5, TimeUnit.MINUTES);
    System.out.println("ONE");
    imageConsumerLatch.await(5, TimeUnit.MINUTES);
    System.out.println("TWO");
    Integer receivedImagesConsumer = imageConsumer.getAttribute(CameraImageConsumerTestUnit.DESCRIPTOR_RECEIVED_IMAGES).get();
    assertEquals(totalImagesProducer, receivedImagesConsumer);
    RoboHttpUtils.printMeasuredTime(getClass(), "duration", startTime);
    System.out.println("receivedImagesConsumer: " + receivedImagesConsumer);
    producerSystem.shutdown();
    consumerSystem.shutdown();
    System.out.println("Press <Enter> to quit!");
}
Also used : CameraMessage(com.robo4j.socket.http.codec.CameraMessage) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 43 with RoboBuilder

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

the class VectorEventListenerExample method main.

public static void main(String[] args) throws Exception {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final InputStream systemIS;
    final InputStream contextIS;
    switch(args.length) {
        case 0:
            systemIS = classLoader.getResourceAsStream("bno080VectorSystemEmitterExample.xml");
            contextIS = classLoader.getResourceAsStream("bno080VectorExample.xml");
            System.out.println("Default configuration used");
            break;
        case 1:
            systemIS = classLoader.getResourceAsStream("bno080VectorSystemEmitterExample.xml");
            Path contextPath = Paths.get(args[0]);
            contextIS = Files.newInputStream(contextPath);
            System.out.println("Robo4j config file has been used: " + args[0]);
            break;
        case 2:
            Path systemPath2 = Paths.get(args[0]);
            Path contextPath2 = Paths.get(args[1]);
            systemIS = Files.newInputStream(systemPath2);
            contextIS = Files.newInputStream(contextPath2);
            System.out.println(String.format("Custom configuration used system: %s, context: %s", args[0], args[1]));
            break;
        default:
            System.out.println("Could not find the *.xml settings for the CameraClient!");
            System.out.println("java -jar camera.jar system.xml context.xml");
            System.exit(2);
            throw new IllegalStateException("see configuration");
    }
    if (systemIS == null && contextIS == null) {
        System.out.println("Could not find the settings for the BNO080 Example!");
        System.exit(2);
    }
    RoboBuilder builder = new RoboBuilder(systemIS);
    builder.add(contextIS);
    RoboContext ctx = builder.build();
    ctx.start();
    LookupService service = LookupServiceProvider.getDefaultLookupService();
    try {
        service.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<BnoRequest> bnoUnit = ctx.getReference("bno");
    RoboReference<DataEvent3f> bnoListenerUnit = ctx.getReference("listener");
    BnoRequest requestToRegister = new BnoRequest(bnoListenerUnit, BnoRequest.ListenerAction.REGISTER);
    bnoUnit.sendMessage(requestToRegister);
    System.out.println("Press <Enter> to start!");
    System.in.read();
    ctx.shutdown();
}
Also used : Path(java.nio.file.Path) LookupService(com.robo4j.net.LookupService) InputStream(java.io.InputStream) DataEvent3f(com.robo4j.hw.rpi.imu.bno.DataEvent3f) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) IOException(java.io.IOException) BnoRequest(com.robo4j.units.rpi.imu.BnoRequest)

Example 44 with RoboBuilder

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

the class SimpleTankUnitTests method simpleTankUnitMockTest.

@Test
void simpleTankUnitMockTest() throws Exception {
    RoboBuilder builder = new RoboBuilder();
    Configuration config = ConfigurationFactory.createEmptyConfiguration();
    builder.add(SimpleTankUnitMock.class, config, "tank");
    DefaultAttributeDescriptor<Boolean> descriptor = DefaultAttributeDescriptor.create(Boolean.class, "getStatus");
    RoboContext context = builder.build();
    RoboReference<LegoPlatformMessage> tank = context.getReference("tank");
    tank.sendMessage(new LegoPlatformMessage("right"));
    tank.sendMessage(new LegoPlatformMessage("left"));
    tank.sendMessage(new LegoPlatformMessage("move"));
    tank.sendMessage(new LegoPlatformMessage("back"));
    tank.sendMessage(new LegoPlatformMessage("stop"));
    assertTrue(tank.getAttribute(descriptor).get());
    context.shutdown();
}
Also used : Configuration(com.robo4j.configuration.Configuration) LegoPlatformMessage(com.robo4j.units.lego.platform.LegoPlatformMessage) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 45 with RoboBuilder

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

the class DataEventListenerExample method main.

public static void main(String[] args) throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final InputStream systemIS;
    final InputStream contextIS;
    switch(args.length) {
        case 0:
            systemIS = classLoader.getResourceAsStream("bno080DataSystemEmitterExample.xml");
            contextIS = classLoader.getResourceAsStream("bno080GyroExample.xml");
            System.out.println("Default configuration used");
            break;
        case 1:
            systemIS = classLoader.getResourceAsStream("bno080DataSystemEmitterExample.xml");
            Path contextPath = Paths.get(args[0]);
            contextIS = Files.newInputStream(contextPath);
            System.out.println("Robo4j config file has been used: " + args[0]);
            break;
        case 2:
            Path systemPath2 = Paths.get(args[0]);
            Path contextPath2 = Paths.get(args[1]);
            systemIS = Files.newInputStream(systemPath2);
            contextIS = Files.newInputStream(contextPath2);
            System.out.println(String.format("Custom configuration used system: %s, context: %s", args[0], args[1]));
            break;
        default:
            System.out.println("Could not find the *.xml settings for the CameraClient!");
            System.out.println("java -jar camera.jar system.xml context.xml");
            System.exit(2);
            throw new IllegalStateException("see configuration");
    }
    RoboBuilder builder = new RoboBuilder(systemIS);
    builder.add(contextIS);
    RoboContext ctx = builder.build();
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<BnoRequest> bnoUnit = ctx.getReference("bno");
    RoboReference<DataEvent3f> bnoListenerUnit = ctx.getReference("listener");
    BnoRequest requestToRegister = new BnoRequest(bnoListenerUnit, BnoRequest.ListenerAction.REGISTER);
    bnoUnit.sendMessage(requestToRegister);
    System.out.println("Press <Enter> to start!");
    System.in.read();
    ctx.shutdown();
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) DataEvent3f(com.robo4j.hw.rpi.imu.bno.DataEvent3f) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) BnoRequest(com.robo4j.units.rpi.imu.BnoRequest)

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