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();
}
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);
}
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();
}
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();
}
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();
}
Aggregations