Permalink
Browse files

Use streaming JSON for TeensyDiscovery

  • Loading branch information...
PaulStoffregen committed Sep 7, 2018
1 parent aad635e commit 752298d0c4e97cf43ccafcbaf4a29f15152bc358
@@ -47,6 +47,18 @@ public BoardPort() {
this.prefs = new PreferencesMap();
}
public BoardPort(BoardPort bp) {
prefs = new PreferencesMap();
// TODO: copy bp.prefs to prefs
address = bp.address;
protocol = bp.protocol;
boardName = bp.boardName;
vid = bp.vid;
pid = bp.pid;
iserial = bp.iserial;
label = bp.label;
}
public String getAddress() {
return address;
}
@@ -13,7 +13,117 @@
import java.net.Socket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
public class TeensyDiscovery implements Discovery {
private final List<BoardPort> portlist;
private Process program=null;
private JsonParser parser;
private ObjectMapper mapper;
public TeensyDiscovery() {
portlist = new LinkedList<>();
}
@Override
public void run() {
String cmdline = BaseNoGui.getHardwarePath() + File.separator
+ "tools" + File.separator + "teensy_ports";
try {
program = Runtime.getRuntime().exec(new String[] {cmdline, "-J"});
} catch (Exception e1) {
program = null;
}
if (program == null) {
print("unable to run teensy_ports -J");
return;
}
InputStream input = program.getInputStream();
print("teensy_ports -J started");
try {
// https://sohlich.github.io/post/jackson/
// https://www.baeldung.com/jackson-object-mapper-tutorial
JsonFactory factory = new JsonFactory();
parser = factory.createJsonParser(input);
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
while (true) {
BoardPort port = mapper.readValue(parser, BoardPort.class);
if (port != null) {
incoming(port);
}
}
} catch (Exception e) {
print("ended with exception");
}
print("end");
}
private synchronized void incoming(BoardPort port) {
String address = port.getAddress();
if (address == null) {
return; // address is required
}
for (BoardPort bp : portlist) {
if (address.equals(bp.getAddress())) {
// if address already on the list, discard old info
portlist.remove(bp);
}
}
if (port.isOnline()) {
if (port.getLabel() == null) {
// if no label, use address
port.setLabel(address);
}
if (port.getProtocol() == null) {
// if no protocol, assume serial
port.setProtocol("serial");
}
portlist.add(new BoardPort(port));
}
}
@Override
public void start() {
print("start");
run();
}
@Override
public void stop() {
print("stop");
if (program != null) {
program.destroy();
program = null;
}
}
@Override
public List<BoardPort> listDiscoveredBoards() {
return listDiscoveredBoards(false);
}
@Override
public synchronized List<BoardPort> listDiscoveredBoards(boolean complete) {
print("listDiscoveredBoards");
List<BoardPort> portlistCopy = new LinkedList<>();
for (BoardPort bp : portlist) {
portlistCopy.add(new BoardPort(bp));
}
return portlistCopy;
}
void print(String str) {
System.err.println("TeensyDiscovery: " + str);
}
}
/*
public class TeensyDiscovery implements Discovery {
Socket sock=null;
@@ -220,3 +330,4 @@ synchronized boolean run_program() {
}
}
*/

0 comments on commit 752298d

Please sign in to comment.