|
| 1 | +package io.sloeber.core.api; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.eclipse.core.runtime.CoreException; |
| 9 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 10 | + |
| 11 | +import it.baeyens.arduino.common.InstancePreferences; |
| 12 | +import it.baeyens.arduino.managers.ArduinoPlatform; |
| 13 | +import it.baeyens.arduino.managers.Board; |
| 14 | +import it.baeyens.arduino.managers.Manager; |
| 15 | +import it.baeyens.arduino.managers.Package; |
| 16 | +import it.baeyens.arduino.tools.TxtFile; |
| 17 | + |
| 18 | +/** |
| 19 | + * This class groups both boards installed by the hardware manager and boards |
| 20 | + * installed locally. |
| 21 | + * |
| 22 | + * @author jan |
| 23 | + * |
| 24 | + */ |
| 25 | +public class BoardsManager { |
| 26 | + /** |
| 27 | + * Gets the board id based on the information provided. If |
| 28 | + * jsonFileName="local" the board is assumend not to be installed by the |
| 29 | + * boards manager. Otherwise the boardsmanager is queried to find the board |
| 30 | + * ID. In this case the latest installed board will be returned |
| 31 | + * |
| 32 | + * @param jsonFileName |
| 33 | + * equals to "local" or the name of the json file used by the |
| 34 | + * boards manager to install the boards |
| 35 | + * @param packageName |
| 36 | + * if jsonFileName equals "local" the filename of the boards.txt |
| 37 | + * containing the boards. otherwise the name of the package |
| 38 | + * containing the board |
| 39 | + * @param platformName |
| 40 | + * ignored if jsonFileName equals "local" otherwise the name of |
| 41 | + * the platform containing the board |
| 42 | + * @param boardID |
| 43 | + * the id of the board in the boards.txt file |
| 44 | + * @param options |
| 45 | + * the options to specify the board (the menu named on the |
| 46 | + * boards.txt file) |
| 47 | + * @return The class BoardDescriptor or null |
| 48 | + */ |
| 49 | + static public BoardDescriptor getBoardID(String jsonFileName, String packageName, String platformName, |
| 50 | + String boardID, Map<String, String> options) { |
| 51 | + if (jsonFileName.equals("local")) { //$NON-NLS-1$ |
| 52 | + return new BoardDescriptor(new TxtFile(new File(packageName)), boardID, options); |
| 53 | + } |
| 54 | + return getNewestBoardIDFromBoardsManager(jsonFileName, packageName, platformName, boardID, options); |
| 55 | + } |
| 56 | + |
| 57 | + static private BoardDescriptor getNewestBoardIDFromBoardsManager(String jsonFileName, String packageName, |
| 58 | + String platformName, String boardID, Map<String, String> options) { |
| 59 | + |
| 60 | + List<Board> boards = null; |
| 61 | + try { |
| 62 | + Package thePackage = Manager.getPackage(jsonFileName, packageName); |
| 63 | + if (thePackage == null) { |
| 64 | + // fail("failed to find package:" + this.mPackageName); |
| 65 | + return null; |
| 66 | + } |
| 67 | + ArduinoPlatform platform = thePackage.getLatestPlatform(platformName); |
| 68 | + if (platform == null) { |
| 69 | + // fail("failed to find platform " + this.mPlatform + " in |
| 70 | + // package:" + this.mPackageName); |
| 71 | + return null; |
| 72 | + } |
| 73 | + boards = platform.getBoards(); |
| 74 | + } catch (CoreException e1) { |
| 75 | + e1.printStackTrace(); |
| 76 | + } |
| 77 | + if (boards == null) { |
| 78 | + // fail("No boards found"); |
| 79 | + return null; |
| 80 | + } |
| 81 | + for (Board curBoard : boards) { |
| 82 | + if (curBoard.getId().equals(boardID)) { |
| 83 | + java.io.File boardsFile = curBoard.getPlatform().getBoardsFile(); |
| 84 | + TxtFile boardsTxtFile = new TxtFile(boardsFile); |
| 85 | + System.out.println("Testing board: " + curBoard.getName()); //$NON-NLS-1$ |
| 86 | + BoardDescriptor boardid = new BoardDescriptor(boardsTxtFile, curBoard.getId(), options); |
| 87 | + |
| 88 | + return boardid; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + public static void addPackageURLs(String[] packageUrlsToAdd) { |
| 96 | + Manager.addPackageURLs(packageUrlsToAdd); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + public static void installAllLatestPlatforms() { |
| 101 | + NullProgressMonitor monitor = new NullProgressMonitor(); |
| 102 | + List<Package> allPackages = Manager.getPackages(); |
| 103 | + for (Package curPackage : allPackages) { |
| 104 | + Collection<ArduinoPlatform> latestPlatforms = curPackage.getLatestPlatforms(); |
| 105 | + for (ArduinoPlatform curPlatform : latestPlatforms) { |
| 106 | + curPlatform.install(monitor); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static void referenceLocallInstallation(String newHardwarePath) { |
| 112 | + // TODO Auto-generated method stub |
| 113 | + |
| 114 | + String currentPaths[] = InstancePreferences.getPrivateHardwarePaths(); |
| 115 | + String newPaths[] = new String[currentPaths.length + 1]; |
| 116 | + for (int i = 0; i < currentPaths.length; i++) { |
| 117 | + if (currentPaths[i].equals(newHardwarePath)) { |
| 118 | + return; |
| 119 | + } |
| 120 | + newPaths[i] = currentPaths[i]; |
| 121 | + } |
| 122 | + newPaths[currentPaths.length] = newHardwarePath; |
| 123 | + InstancePreferences.setPrivateHardwarePaths(newPaths); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments