Skip to content

Commit 8208376

Browse files
committed
SerialDiscovery: refactor search loop
1 parent bb629d1 commit 8208376

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

arduino-core/src/cc/arduino/packages/discoverers/serial/SerialDiscovery.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,20 @@ public synchronized void forceRefresh() {
148148

149149
Map<String, Object> boardData = platform.resolveDeviceByVendorIdProductId(port, BaseNoGui.packages);
150150

151-
BoardPort boardPort = null;
152-
int i = 0;
153-
154-
// create new board if in ports but not in boardPorts
155-
for (BoardPort board : boardPorts) {
156-
if (board.toCompleteString().equalsIgnoreCase(newPort)) {
157-
boardPort = boardPorts.get(i);
158-
boardPorts.get(i).setOnlineStatus(true);
159-
break;
160-
}
161-
i++;
162-
}
163-
164-
if (boardPort != null) {
151+
// if port has been already discovered bring it back online
152+
BoardPort oldBoardPort = boardPorts.stream() //
153+
.filter(bp -> bp.toCompleteString().equalsIgnoreCase(newPort)) //
154+
.findAny().orElse(null);
155+
if (oldBoardPort != null) {
156+
oldBoardPort.setOnlineStatus(true);
165157
continue;
166158
}
167159

168-
boardPort = new BoardPort();
160+
BoardPort boardPort = new BoardPort();
169161
boardPorts.add(boardPort);
170162
boardPort.setAddress(port);
171163
boardPort.setProtocol("serial");
172164
boardPort.setOnlineStatus(true);
173-
174165
boardPort.setLabel(port);
175166

176167
if (boardData != null) {

0 commit comments

Comments
 (0)