-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeensyDiscovery.java
124 lines (111 loc) · 3.01 KB
/
TeensyDiscovery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cc.arduino.packages.discoverers;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import processing.app.BaseNoGui;
import java.util.LinkedList;
import java.util.List;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
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.createParser(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);
}
}