org.bidib.jbidibc.GetNodeList Maven / Gradle / Ivy
package org.bidib.jbidibc;
import org.bidib.jbidibc.core.node.BidibNode;
import org.bidib.jbidibc.messages.Node;
import org.bidib.jbidibc.messages.exception.PortNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.Command;
/**
* This commands gets the list of nodes from the specified port.
*
*/
@Command
public class GetNodeList extends BidibCommand {
private static final Logger LOGGER_NODE = LoggerFactory.getLogger(Node.class);
private final org.bidib.jbidibc.messages.logger.Logger nodeLogger;
public static void main(String[] args) {
run(new GetNodeList(), args);
}
protected GetNodeList() {
this.nodeLogger = new org.bidib.jbidibc.messages.logger.Logger() {
@Override
public void debug(String format, Object... arguments) {
LOGGER_NODE.debug(format, arguments);
}
@Override
public void info(String format, Object... arguments) {
LOGGER_NODE.info(format, arguments);
}
@Override
public void warn(String format, Object... arguments) {
LOGGER_NODE.warn(format, arguments);
}
@Override
public void error(String format, Object... arguments) {
LOGGER_NODE.error(format, arguments);
}
};
}
@Override
public Integer call() {
int result = 20;
try {
openPort(getPortName(), null);
BidibNode rootNode = getBidib().getRootNode();
int count = rootNode.getNodeCount();
System.out.println("Number of nodes: " + count); // NOSONAR
for (int index = 1; index <= count; index++) {
Node node = rootNode.getNextNode(this.nodeLogger);
System.out.println("Found node: " + node); // NOSONAR
}
result = 0;
releaseBidib();
}
catch (PortNotFoundException ex) {
System.err
.println("The provided port was not found: " + ex.getMessage() // NOSONAR
+ ". Verify that the BiDiB device is connected.");
}
catch (Exception ex) {
System.err.println("Get list of nodes failed: " + ex); // NOSONAR
ex.printStackTrace();
}
return result;
}
}