mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-16 11:21:18 +03:00
Network and Serial board ports discovery is now asynchronous, hence it does not block "tools" menu any more.
Fixes #2788
This commit is contained in:
@ -29,36 +29,27 @@
|
|||||||
|
|
||||||
package cc.arduino.packages;
|
package cc.arduino.packages;
|
||||||
|
|
||||||
import processing.app.helpers.PreferencesMap;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface Discovery {
|
public interface Discovery {
|
||||||
|
|
||||||
/**
|
|
||||||
* Set discovery preferences
|
|
||||||
*
|
|
||||||
* @param options
|
|
||||||
*/
|
|
||||||
public void setPreferences(PreferencesMap options);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start discovery service
|
* Start discovery service
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public void start() throws Exception;
|
void start() throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop discovery service
|
* Stop discovery service
|
||||||
*/
|
*/
|
||||||
public void stop() throws Exception;
|
void stop() throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the list of discovered ports.
|
* Return the list of discovered ports.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<BoardPort> discovery();
|
List<BoardPort> listDiscoveredBoards();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ public class DiscoveryManager {
|
|||||||
try {
|
try {
|
||||||
d.stop();
|
d.stop();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
|
e.printStackTrace(); //just printing as the JVM is terminating
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ public class DiscoveryManager {
|
|||||||
public List<BoardPort> discovery() {
|
public List<BoardPort> discovery() {
|
||||||
List<BoardPort> res = new ArrayList<BoardPort>();
|
List<BoardPort> res = new ArrayList<BoardPort>();
|
||||||
for (Discovery d : discoverers) {
|
for (Discovery d : discoverers) {
|
||||||
res.addAll(d.discovery());
|
res.addAll(d.listDiscoveredBoards());
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,9 @@ package cc.arduino.packages.discoverers;
|
|||||||
|
|
||||||
import cc.arduino.packages.BoardPort;
|
import cc.arduino.packages.BoardPort;
|
||||||
import cc.arduino.packages.Discovery;
|
import cc.arduino.packages.Discovery;
|
||||||
|
import cc.arduino.packages.discoverers.network.BoardReachabilityFilter;
|
||||||
import cc.arduino.packages.discoverers.network.NetworkChecker;
|
import cc.arduino.packages.discoverers.network.NetworkChecker;
|
||||||
import processing.app.BaseNoGui;
|
import processing.app.BaseNoGui;
|
||||||
import processing.app.helpers.NetUtils;
|
|
||||||
import processing.app.helpers.PreferencesMap;
|
import processing.app.helpers.PreferencesMap;
|
||||||
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
|
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
|
||||||
|
|
||||||
@ -41,70 +41,55 @@ import javax.jmdns.*;
|
|||||||
import javax.jmdns.impl.DNSTaskStarter;
|
import javax.jmdns.impl.DNSTaskStarter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.packages.discoverers.network.NetworkTopologyListener {
|
public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.packages.discoverers.network.NetworkTopologyListener {
|
||||||
|
|
||||||
private Timer timer;
|
private final List<BoardPort> boardPortsDiscoveredWithJmDNS;
|
||||||
private final List<BoardPort> ports;
|
|
||||||
private final Map<InetAddress, JmDNS> mappedJmDNSs;
|
private final Map<InetAddress, JmDNS> mappedJmDNSs;
|
||||||
|
private Timer networkCheckerTimer;
|
||||||
|
private Timer boardReachabilityFilterTimer;
|
||||||
|
private final List<BoardPort> reachableBoardPorts;
|
||||||
|
|
||||||
public NetworkDiscovery() {
|
public NetworkDiscovery() {
|
||||||
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
|
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
|
||||||
this.ports = new ArrayList<BoardPort>();
|
this.boardPortsDiscoveredWithJmDNS = new LinkedList<BoardPort>();
|
||||||
this.mappedJmDNSs = new Hashtable<InetAddress, JmDNS>();
|
this.mappedJmDNSs = new Hashtable<InetAddress, JmDNS>();
|
||||||
|
this.reachableBoardPorts = new LinkedList<BoardPort>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BoardPort> discovery() {
|
public List<BoardPort> listDiscoveredBoards() {
|
||||||
List<BoardPort> boardPorts = clonePortsList();
|
synchronized (reachableBoardPorts) {
|
||||||
Iterator<BoardPort> boardPortIterator = boardPorts.iterator();
|
return new LinkedList<BoardPort>(reachableBoardPorts);
|
||||||
while (boardPortIterator.hasNext()) {
|
|
||||||
try {
|
|
||||||
BoardPort board = boardPortIterator.next();
|
|
||||||
|
|
||||||
InetAddress inetAddress = InetAddress.getByName(board.getAddress());
|
|
||||||
int broadcastedPort = Integer.valueOf(board.getPrefs().get("port"));
|
|
||||||
|
|
||||||
List<Integer> ports = new LinkedList<Integer>();
|
|
||||||
ports.add(broadcastedPort);
|
|
||||||
|
|
||||||
//dirty code: allows non up to date yuns to be discovered. Newer yuns will broadcast port 22
|
|
||||||
if (broadcastedPort == 80) {
|
|
||||||
ports.add(0, 22);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean reachable = NetUtils.isReachable(inetAddress, ports);
|
|
||||||
if (!reachable) {
|
|
||||||
boardPortIterator.remove();
|
|
||||||
}
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
boardPortIterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return boardPorts;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<BoardPort> clonePortsList() {
|
|
||||||
synchronized (this) {
|
|
||||||
return new ArrayList<BoardPort>(this.ports);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void setReachableBoardPorts(List<BoardPort> newReachableBoardPorts) {
|
||||||
public void setPreferences(PreferencesMap options) {
|
synchronized (reachableBoardPorts) {
|
||||||
|
this.reachableBoardPorts.clear();
|
||||||
|
this.reachableBoardPorts.addAll(newReachableBoardPorts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BoardPort> getBoardPortsDiscoveredWithJmDNS() {
|
||||||
|
synchronized (boardPortsDiscoveredWithJmDNS) {
|
||||||
|
return new LinkedList<BoardPort>(boardPortsDiscoveredWithJmDNS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
this.timer = new Timer(this.getClass().getName() + " timer");
|
this.networkCheckerTimer = new Timer(NetworkChecker.class.getName());
|
||||||
new NetworkChecker(this, NetworkTopologyDiscovery.Factory.getInstance()).start(timer);
|
new NetworkChecker(this, NetworkTopologyDiscovery.Factory.getInstance()).start(networkCheckerTimer);
|
||||||
|
this.boardReachabilityFilterTimer = new Timer(BoardReachabilityFilter.class.getName());
|
||||||
|
new BoardReachabilityFilter(this).start(boardReachabilityFilterTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() throws IOException {
|
public void stop() throws IOException {
|
||||||
timer.purge();
|
this.networkCheckerTimer.purge();
|
||||||
|
this.boardReachabilityFilterTimer.purge();
|
||||||
// we don't close each JmDNS instance as it's too slow
|
// we don't close each JmDNS instance as it's too slow
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,10 +110,11 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.
|
|||||||
@Override
|
@Override
|
||||||
public void serviceRemoved(ServiceEvent serviceEvent) {
|
public void serviceRemoved(ServiceEvent serviceEvent) {
|
||||||
String name = serviceEvent.getName();
|
String name = serviceEvent.getName();
|
||||||
synchronized (this) {
|
synchronized (boardPortsDiscoveredWithJmDNS) {
|
||||||
for (BoardPort port : ports) {
|
for (BoardPort port : boardPortsDiscoveredWithJmDNS) {
|
||||||
if (port.getBoardName().equals(name))
|
if (port.getBoardName().equals(name)) {
|
||||||
ports.remove(port);
|
boardPortsDiscoveredWithJmDNS.remove(port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,10 +133,9 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.
|
|||||||
board = info.getPropertyString("board");
|
board = info.getPropertyString("board");
|
||||||
prefs.put("board", board);
|
prefs.put("board", board);
|
||||||
prefs.put("distro_version", info.getPropertyString("distro_version"));
|
prefs.put("distro_version", info.getPropertyString("distro_version"));
|
||||||
|
prefs.put("port", "" + info.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
prefs.put("port", "" + info.getPort());
|
|
||||||
|
|
||||||
String label = name + " at " + address;
|
String label = name + " at " + address;
|
||||||
if (board != null) {
|
if (board != null) {
|
||||||
String boardName = BaseNoGui.getPlatform().resolveDeviceByBoardID(BaseNoGui.packages, board);
|
String boardName = BaseNoGui.getPlatform().resolveDeviceByBoardID(BaseNoGui.packages, board);
|
||||||
@ -166,19 +151,21 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.
|
|||||||
port.setPrefs(prefs);
|
port.setPrefs(prefs);
|
||||||
port.setLabel(label);
|
port.setLabel(label);
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (boardPortsDiscoveredWithJmDNS) {
|
||||||
removeDuplicateBoards(port);
|
removeDuplicateBoards(port);
|
||||||
ports.add(port);
|
boardPortsDiscoveredWithJmDNS.add(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDuplicateBoards(BoardPort newBoard) {
|
private void removeDuplicateBoards(BoardPort newBoard) {
|
||||||
Iterator<BoardPort> iterator = ports.iterator();
|
synchronized (boardPortsDiscoveredWithJmDNS) {
|
||||||
while (iterator.hasNext()) {
|
Iterator<BoardPort> iterator = boardPortsDiscoveredWithJmDNS.iterator();
|
||||||
BoardPort board = iterator.next();
|
while (iterator.hasNext()) {
|
||||||
if (newBoard.getAddress().equals(board.getAddress())) {
|
BoardPort board = iterator.next();
|
||||||
iterator.remove();
|
if (newBoard.getAddress().equals(board.getAddress())) {
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,16 +31,11 @@ package cc.arduino.packages.discoverers;
|
|||||||
|
|
||||||
import cc.arduino.packages.BoardPort;
|
import cc.arduino.packages.BoardPort;
|
||||||
import cc.arduino.packages.Discovery;
|
import cc.arduino.packages.Discovery;
|
||||||
import processing.app.BaseNoGui;
|
import cc.arduino.packages.discoverers.serial.SerialBoardsLister;
|
||||||
import processing.app.Platform;
|
|
||||||
import processing.app.Serial;
|
|
||||||
import processing.app.debug.TargetBoard;
|
|
||||||
import processing.app.helpers.PreferencesMap;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Timer;
|
||||||
|
|
||||||
import static processing.app.I18n._;
|
import static processing.app.I18n._;
|
||||||
|
|
||||||
@ -51,66 +46,39 @@ public class SerialDiscovery implements Discovery {
|
|||||||
_("Uncertified");
|
_("Uncertified");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private Timer serialBoardsListerTimer;
|
||||||
public List<BoardPort> discovery() {
|
private final List<BoardPort> serialBoardPorts;
|
||||||
Platform os = BaseNoGui.getPlatform();
|
|
||||||
String devicesListOutput = os.preListAllCandidateDevices();
|
|
||||||
|
|
||||||
List<BoardPort> res = new ArrayList<BoardPort>();
|
public SerialDiscovery() {
|
||||||
|
this.serialBoardPorts = new LinkedList<BoardPort>();
|
||||||
List<String> ports = Serial.list();
|
|
||||||
|
|
||||||
for (String port : ports) {
|
|
||||||
Map<String, Object> boardData = os.resolveDeviceAttachedTo(port, BaseNoGui.packages, devicesListOutput);
|
|
||||||
|
|
||||||
BoardPort boardPort = new BoardPort();
|
|
||||||
boardPort.setAddress(port);
|
|
||||||
boardPort.setProtocol("serial");
|
|
||||||
|
|
||||||
String label = port;
|
|
||||||
|
|
||||||
PreferencesMap prefs = new PreferencesMap();
|
|
||||||
|
|
||||||
if (boardData != null) {
|
|
||||||
prefs.put("vid", boardData.get("vid").toString());
|
|
||||||
prefs.put("pid", boardData.get("pid").toString());
|
|
||||||
|
|
||||||
TargetBoard board = (TargetBoard) boardData.get("board");
|
|
||||||
if (board != null) {
|
|
||||||
String warningKey = "vid." + boardData.get("vid").toString() + ".warning";
|
|
||||||
String warning = board.getPreferences().get(warningKey);
|
|
||||||
prefs.put("warning", warning);
|
|
||||||
|
|
||||||
String boardName = board.getName();
|
|
||||||
if (boardName != null) {
|
|
||||||
if (warning != null) {
|
|
||||||
label += " (" + boardName + " - " + _(warning) + ")";
|
|
||||||
} else {
|
|
||||||
label += " (" + boardName + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boardPort.setBoardName(boardName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boardPort.setLabel(label);
|
|
||||||
boardPort.setPrefs(prefs);
|
|
||||||
|
|
||||||
res.add(boardPort);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPreferences(PreferencesMap options) {
|
public List<BoardPort> listDiscoveredBoards() {
|
||||||
|
return getSerialBoardPorts();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BoardPort> getSerialBoardPorts() {
|
||||||
|
synchronized (serialBoardPorts) {
|
||||||
|
return new LinkedList<BoardPort>(serialBoardPorts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
|
||||||
|
synchronized (serialBoardPorts) {
|
||||||
|
serialBoardPorts.clear();
|
||||||
|
serialBoardPorts.addAll(newSerialBoardPorts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
|
this.serialBoardsListerTimer = new Timer(SerialBoardsLister.class.getName());
|
||||||
|
new SerialBoardsLister(this).start(serialBoardsListerTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
this.serialBoardsListerTimer.purge();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Arduino.
|
||||||
|
*
|
||||||
|
* Arduino is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* As a special exception, you may use this file as part of a free software
|
||||||
|
* library without restriction. Specifically, if other files instantiate
|
||||||
|
* templates or use macros or inline functions from this file, or you compile
|
||||||
|
* this file and link it with other files to produce an executable, this
|
||||||
|
* file does not by itself cause the resulting executable to be covered by
|
||||||
|
* the GNU General Public License. This exception does not however
|
||||||
|
* invalidate any other reasons why the executable file might be covered by
|
||||||
|
* the GNU General Public License.
|
||||||
|
*
|
||||||
|
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cc.arduino.packages.discoverers.network;
|
||||||
|
|
||||||
|
import cc.arduino.packages.BoardPort;
|
||||||
|
import cc.arduino.packages.discoverers.NetworkDiscovery;
|
||||||
|
import processing.app.helpers.NetUtils;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class BoardReachabilityFilter extends TimerTask {
|
||||||
|
|
||||||
|
private final NetworkDiscovery networkDiscovery;
|
||||||
|
|
||||||
|
public BoardReachabilityFilter(NetworkDiscovery networkDiscovery) {
|
||||||
|
this.networkDiscovery = networkDiscovery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(Timer timer) {
|
||||||
|
timer.schedule(this, 0, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
List<BoardPort> boardPorts = networkDiscovery.getBoardPortsDiscoveredWithJmDNS();
|
||||||
|
|
||||||
|
Iterator<BoardPort> boardPortIterator = boardPorts.iterator();
|
||||||
|
while (boardPortIterator.hasNext()) {
|
||||||
|
try {
|
||||||
|
BoardPort board = boardPortIterator.next();
|
||||||
|
|
||||||
|
InetAddress inetAddress = InetAddress.getByName(board.getAddress());
|
||||||
|
int broadcastedPort = Integer.valueOf(board.getPrefs().get("port"));
|
||||||
|
|
||||||
|
List<Integer> ports = new LinkedList<Integer>();
|
||||||
|
ports.add(broadcastedPort);
|
||||||
|
|
||||||
|
//dirty code: allows non up to date yuns to be discovered. Newer yuns will broadcast port 22
|
||||||
|
if (broadcastedPort == 80) {
|
||||||
|
ports.add(0, 22);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean reachable = NetUtils.isReachable(inetAddress, ports);
|
||||||
|
if (!reachable) {
|
||||||
|
boardPortIterator.remove();
|
||||||
|
}
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
boardPortIterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
networkDiscovery.setReachableBoardPorts(boardPorts);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Arduino.
|
||||||
|
*
|
||||||
|
* Arduino is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* As a special exception, you may use this file as part of a free software
|
||||||
|
* library without restriction. Specifically, if other files instantiate
|
||||||
|
* templates or use macros or inline functions from this file, or you compile
|
||||||
|
* this file and link it with other files to produce an executable, this
|
||||||
|
* file does not by itself cause the resulting executable to be covered by
|
||||||
|
* the GNU General Public License. This exception does not however
|
||||||
|
* invalidate any other reasons why the executable file might be covered by
|
||||||
|
* the GNU General Public License.
|
||||||
|
*
|
||||||
|
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cc.arduino.packages.discoverers.serial;
|
||||||
|
|
||||||
|
import cc.arduino.packages.BoardPort;
|
||||||
|
import cc.arduino.packages.discoverers.SerialDiscovery;
|
||||||
|
import processing.app.BaseNoGui;
|
||||||
|
import processing.app.Platform;
|
||||||
|
import processing.app.Serial;
|
||||||
|
import processing.app.debug.TargetBoard;
|
||||||
|
import processing.app.helpers.PreferencesMap;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static processing.app.I18n._;
|
||||||
|
|
||||||
|
public class SerialBoardsLister extends TimerTask {
|
||||||
|
|
||||||
|
private final SerialDiscovery serialDiscovery;
|
||||||
|
|
||||||
|
public SerialBoardsLister(SerialDiscovery serialDiscovery) {
|
||||||
|
this.serialDiscovery = serialDiscovery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(Timer timer) {
|
||||||
|
timer.schedule(this, 0, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Platform platform = BaseNoGui.getPlatform();
|
||||||
|
if (platform == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<BoardPort> boardPorts = new LinkedList<BoardPort>();
|
||||||
|
|
||||||
|
List<String> ports = Serial.list();
|
||||||
|
|
||||||
|
String devicesListOutput = null;
|
||||||
|
if (!ports.isEmpty()) {
|
||||||
|
devicesListOutput = platform.preListAllCandidateDevices();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String port : ports) {
|
||||||
|
Map<String, Object> boardData = platform.resolveDeviceAttachedTo(port, BaseNoGui.packages, devicesListOutput);
|
||||||
|
|
||||||
|
BoardPort boardPort = new BoardPort();
|
||||||
|
boardPort.setAddress(port);
|
||||||
|
boardPort.setProtocol("serial");
|
||||||
|
|
||||||
|
String label = port;
|
||||||
|
|
||||||
|
PreferencesMap prefs = new PreferencesMap();
|
||||||
|
|
||||||
|
if (boardData != null) {
|
||||||
|
prefs.put("vid", boardData.get("vid").toString());
|
||||||
|
prefs.put("pid", boardData.get("pid").toString());
|
||||||
|
|
||||||
|
TargetBoard board = (TargetBoard) boardData.get("board");
|
||||||
|
if (board != null) {
|
||||||
|
String warningKey = "vid." + boardData.get("vid").toString() + ".warning";
|
||||||
|
String warning = board.getPreferences().get(warningKey);
|
||||||
|
prefs.put("warning", warning);
|
||||||
|
|
||||||
|
String boardName = board.getName();
|
||||||
|
if (boardName != null) {
|
||||||
|
if (warning != null) {
|
||||||
|
label += " (" + boardName + " - " + _(warning) + ")";
|
||||||
|
} else {
|
||||||
|
label += " (" + boardName + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boardPort.setBoardName(boardName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boardPort.setLabel(label);
|
||||||
|
boardPort.setPrefs(prefs);
|
||||||
|
|
||||||
|
boardPorts.add(boardPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
serialDiscovery.setSerialBoardPorts(boardPorts);
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ public abstract class NetUtils {
|
|||||||
|
|
||||||
private static boolean isReachableByEcho(InetAddress address) {
|
private static boolean isReachableByEcho(InetAddress address) {
|
||||||
try {
|
try {
|
||||||
return address.isReachable(100);
|
return address.isReachable(300);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ public abstract class NetUtils {
|
|||||||
Socket socket = null;
|
Socket socket = null;
|
||||||
try {
|
try {
|
||||||
socket = new Socket();
|
socket = new Socket();
|
||||||
socket.connect(new InetSocketAddress(address, port), 300);
|
socket.connect(new InetSocketAddress(address, port), 1000);
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user