1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

PluggableDiscovery: Factored out method to umarshal BoardPort from JSON

This commit is contained in:
Cristian Maglie 2018-11-30 12:03:15 +01:00
parent 8e9f0cfd76
commit 4c188c9374
2 changed files with 21 additions and 20 deletions

View File

@ -126,7 +126,7 @@ public class BoardPort {
// If found, boardName is set to the name from boards.txt
// and the board is returned. If not found, null is returned.
public TargetBoard searchMatchingBoard() {
if (identificationPrefs.isEmpty()) return null;
if (identificationPrefs == null || identificationPrefs.isEmpty()) return null;
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
for (TargetBoard board : targetPlatform.getBoards().values()) {

View File

@ -141,13 +141,9 @@ public class PluggableDiscovery implements Discovery {
portList.clear();
portsNode.forEach(portNode -> {
try {
BoardPort port = mapper.treeToValue(portNode, BoardPort.class);
port.searchMatchingBoard();
BoardPort port = mapJsonNodeToBoardPort(mapper, node);
if (port != null) {
addOrUpdate(port);
} catch (JsonProcessingException e) {
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
e.printStackTrace();
}
});
return;
@ -155,23 +151,16 @@ public class PluggableDiscovery implements Discovery {
// Messages for SYNC updates
case "add":
try {
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
port.searchMatchingBoard();
addOrUpdate(port);
} catch (JsonProcessingException e) {
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
e.printStackTrace();
BoardPort addedPort = mapJsonNodeToBoardPort(mapper, node);
if (addedPort != null) {
addOrUpdate(addedPort);
}
return;
case "remove":
try {
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
remove(port);
} catch (JsonProcessingException e) {
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
e.printStackTrace();
BoardPort removedPort = mapJsonNodeToBoardPort(mapper, node);
if (removedPort != null) {
remove(removedPort);
}
return;
@ -181,6 +170,18 @@ public class PluggableDiscovery implements Discovery {
}
}
private BoardPort mapJsonNodeToBoardPort(ObjectMapper mapper, JsonNode node) {
try {
BoardPort port = mapper.treeToValue(node.get("port"), BoardPort.class);
port.searchMatchingBoard();
return port;
} catch (JsonProcessingException e) {
System.err.println(format("{0}: Invalid BoardPort message", discoveryName));
e.printStackTrace();
return null;
}
}
@Override
public void start() throws Exception {
try {