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