2013-06-02 19:02:57 +02:00
|
|
|
package cc.arduino.packages;
|
|
|
|
|
2013-06-25 16:13:14 +02:00
|
|
|
import cc.arduino.packages.discoverers.NetworkDiscovery;
|
|
|
|
import cc.arduino.packages.discoverers.SerialDiscovery;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-06-25 16:13:14 +02:00
|
|
|
import static processing.app.I18n._;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
public class DiscoveryManager {
|
|
|
|
|
2013-07-10 09:42:27 +02:00
|
|
|
private final List<Discovery> discoverers;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
public DiscoveryManager() {
|
2013-07-10 09:42:27 +02:00
|
|
|
discoverers = new ArrayList<Discovery>();
|
2013-06-02 19:02:57 +02:00
|
|
|
discoverers.add(new SerialDiscovery());
|
|
|
|
discoverers.add(new NetworkDiscovery());
|
|
|
|
|
|
|
|
// Start all discoverers
|
|
|
|
for (Discovery d : discoverers) {
|
|
|
|
try {
|
|
|
|
d.start();
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.err.println(_("Error starting discovery method: ") + d.getClass());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 16:13:14 +02:00
|
|
|
|
2013-07-10 09:42:27 +02:00
|
|
|
Thread closeHook = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (Discovery d : discoverers) {
|
|
|
|
try {
|
|
|
|
d.stop();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-06-02 19:02:57 +02:00
|
|
|
Runtime.getRuntime().addShutdownHook(closeHook);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BoardPort> discovery() {
|
|
|
|
List<BoardPort> res = new ArrayList<BoardPort>();
|
|
|
|
for (Discovery d : discoverers)
|
|
|
|
res.addAll(d.discovery());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|