1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Pluggable discovery: search in platform.txt (WIP)

This commit is contained in:
Cristian Maglie 2018-09-10 14:04:10 +02:00
parent 3ba85835bf
commit f81798badf
3 changed files with 142 additions and 7 deletions

View File

@ -29,13 +29,20 @@
package cc.arduino.packages;
import cc.arduino.packages.discoverers.NetworkDiscovery;
import cc.arduino.packages.discoverers.SerialDiscovery;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static processing.app.I18n.tr;
import cc.arduino.packages.discoverers.PluggableDiscovery;
import cc.arduino.packages.discoverers.NetworkDiscovery;
import cc.arduino.packages.discoverers.SerialDiscovery;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.StringReplacer;
public class DiscoveryManager {
@ -43,17 +50,46 @@ public class DiscoveryManager {
private final SerialDiscovery serialDiscoverer = new SerialDiscovery();
private final NetworkDiscovery networkDiscoverer = new NetworkDiscovery();
public DiscoveryManager() {
// private final Map<String, TargetPackage> packages;
public DiscoveryManager(Map<String, TargetPackage> packages) {
// this.packages = packages;
discoverers = new ArrayList<>();
discoverers.add(serialDiscoverer);
discoverers.add(networkDiscoverer);
// Search for discoveries in installed packages
for (TargetPackage targetPackage : packages.values()) {
for (TargetPlatform platform: targetPackage.getPlatforms().values()) {
//System.out.println("installed: "+platform);
PreferencesMap prefs = platform.getPreferences().subTree("discovery");
for (String discoveryName : prefs.firstLevelMap().keySet()) {
PreferencesMap discoveryPrefs = prefs.subTree(discoveryName);
String pattern = discoveryPrefs.get("pattern");
if (pattern == null) {
System.out.println(format(tr("No recipes defined for discovery '{0}'"),discoveryName));
continue;
}
try {
System.out.println("found discovery: " + discoveryName + " -> " + pattern);
System.out.println("with preferencess -> " + discoveryPrefs);
String[] cmd = StringReplacer.formatAndSplit(pattern, discoveryPrefs);
discoverers.add(new PluggableDiscovery(discoveryName, cmd));
} catch (Exception e) {
System.out.println(format(tr("Could not start discovery '{0}': {1}"), discoveryName, e.getMessage()));
}
}
}
}
// Start all discoverers
for (Discovery d : discoverers) {
try {
new Thread(d).start();
} catch (Exception e) {
System.err.println(tr("Error starting discovery method: ") + d.getClass());
System.err.println(tr("Error starting discovery method: ") + d.toString());
e.printStackTrace();
}
}

View File

@ -0,0 +1,99 @@
/*
* 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 2018 Arduino SA (http://www.arduino.cc/)
*/
package cc.arduino.packages.discoverers;
import java.util.ArrayList;
import java.util.List;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import processing.app.legacy.PApplet;
public class PluggableDiscovery implements Discovery {
private String discoveryName;
public PluggableDiscovery(String discoveryName, String[] cmd) {
this.discoveryName = discoveryName;
System.out.println("Starting: " + PApplet.join(cmd, " "));
}
@Override
public void run() {
// TODO this method is started as a new thread, it will constantly
// communicate with the discovery tool and keep track of the discovered
// port to be returned from listDiscoveredBoard()
try {
start();
while (true) { // TODO: Find a better way to terminate discovery
System.out.println(discoveryName + ": looping...");
Thread.sleep(500);
}
// stop();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void start() throws Exception {
// TODO send a START_SYNC command to the discovery tool
// or fallback to START if not available
}
@Override
public void stop() throws Exception {
// TODO send a STOP to the discovery
}
@Override
public List<BoardPort> listDiscoveredBoards() {
// TODO return the ports discovered so far
final List<BoardPort> empty = new ArrayList<>();
return empty;
}
@Override
public List<BoardPort> listDiscoveredBoards(boolean complete) {
// XXX: parameter "complete "is really needed?
// should be checked on all existing discoveries
// TODO
final List<BoardPort> empty = new ArrayList<>();
return empty;
}
@Override
public String toString() {
return discoveryName;
}
}

View File

@ -223,7 +223,7 @@ public class BaseNoGui {
public static DiscoveryManager getDiscoveryManager() {
if (discoveryManager == null) {
discoveryManager = new DiscoveryManager();
discoveryManager = new DiscoveryManager(packages);
}
return discoveryManager;
}
@ -506,7 +506,7 @@ public class BaseNoGui {
}
if (discoveryManager == null) {
discoveryManager = new DiscoveryManager();
discoveryManager = new DiscoveryManager(packages);
}
}