Skip to content

Commit 4c188c9

Browse files
committed
PluggableDiscovery: Factored out method to umarshal BoardPort from JSON
1 parent 8e9f0cf commit 4c188c9

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

arduino-core/src/cc/arduino/packages/BoardPort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public String toString() {
126126
// If found, boardName is set to the name from boards.txt
127127
// and the board is returned. If not found, null is returned.
128128
public TargetBoard searchMatchingBoard() {
129-
if (identificationPrefs.isEmpty()) return null;
129+
if (identificationPrefs == null || identificationPrefs.isEmpty()) return null;
130130
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
131131
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
132132
for (TargetBoard board : targetPlatform.getBoards().values()) {

arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java

+20-19
Original file line numberDiff line numberDiff line change
@@ -141,37 +141,26 @@ private void processJsonNode(ObjectMapper mapper, JsonNode node) {
141141

142142
portList.clear();
143143
portsNode.forEach(portNode -> {
144-
try {
145-
BoardPort port = mapper.treeToValue(portNode, BoardPort.class);
146-
port.searchMatchingBoard();
144+
BoardPort port = mapJsonNodeToBoardPort(mapper, node);
145+
if (port != null) {
147146
addOrUpdate(port);
148-
} catch (JsonProcessingException e) {
149-
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
150-
e.printStackTrace();
151147
}
152148
});
153149
return;
154150

155151
// Messages for SYNC updates
156152

157153
case "add":
158-
try {
159-
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
160-
port.searchMatchingBoard();
161-
addOrUpdate(port);
162-
} catch (JsonProcessingException e) {
163-
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
164-
e.printStackTrace();
154+
BoardPort addedPort = mapJsonNodeToBoardPort(mapper, node);
155+
if (addedPort != null) {
156+
addOrUpdate(addedPort);
165157
}
166158
return;
167159

168160
case "remove":
169-
try {
170-
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
171-
remove(port);
172-
} catch (JsonProcessingException e) {
173-
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
174-
e.printStackTrace();
161+
BoardPort removedPort = mapJsonNodeToBoardPort(mapper, node);
162+
if (removedPort != null) {
163+
remove(removedPort);
175164
}
176165
return;
177166

@@ -181,6 +170,18 @@ private void processJsonNode(ObjectMapper mapper, JsonNode node) {
181170
}
182171
}
183172

173+
private BoardPort mapJsonNodeToBoardPort(ObjectMapper mapper, JsonNode node) {
174+
try {
175+
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
176+
port.searchMatchingBoard();
177+
return port;
178+
} catch (JsonProcessingException e) {
179+
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
180+
e.printStackTrace();
181+
return null;
182+
}
183+
}
184+
184185
@Override
185186
public void start() throws Exception {
186187
try {

0 commit comments

Comments
 (0)