|
| 1 | +/* |
| 2 | + * |
| 3 | + * * This file is part of Arduino. |
| 4 | + * * |
| 5 | + * * Copyright 2015 Arduino LLC (http://www.arduino.cc/) |
| 6 | + * * |
| 7 | + * * Arduino is free software; you can redistribute it and/or modify |
| 8 | + * * it under the terms of the GNU General Public License as published by |
| 9 | + * * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * * (at your option) any later version. |
| 11 | + * * |
| 12 | + * * This program is distributed in the hope that it will be useful, |
| 13 | + * * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * * GNU General Public License for more details. |
| 16 | + * * |
| 17 | + * * You should have received a copy of the GNU General Public License |
| 18 | + * * along with this program; if not, write to the Free Software |
| 19 | + * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + * * |
| 21 | + * * As a special exception, you may use this file as part of a free software |
| 22 | + * * library without restriction. Specifically, if other files instantiate |
| 23 | + * * templates or use macros or inline functions from this file, or you compile |
| 24 | + * * this file and link it with other files to produce an executable, this |
| 25 | + * * file does not by itself cause the resulting executable to be covered by |
| 26 | + * * the GNU General Public License. This exception does not however |
| 27 | + * * invalidate any other reasons why the executable file might be covered by |
| 28 | + * * the GNU General Public License. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +package processing.app.helpers; |
| 33 | + |
| 34 | +import cc.arduino.utils.network.HttpConnectionManager; |
| 35 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 36 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 37 | +import org.apache.logging.log4j.LogManager; |
| 38 | +import org.apache.logging.log4j.Logger; |
| 39 | +import processing.app.BaseNoGui; |
| 40 | +import processing.app.I18n; |
| 41 | +import processing.app.debug.TargetBoard; |
| 42 | +import processing.app.debug.TargetPackage; |
| 43 | +import processing.app.debug.TargetPlatform; |
| 44 | + |
| 45 | +import java.io.InputStream; |
| 46 | +import java.net.HttpURLConnection; |
| 47 | +import java.net.URL; |
| 48 | +import java.util.Map; |
| 49 | + |
| 50 | +import static processing.app.I18n.tr; |
| 51 | + |
| 52 | +public class BoardCloudResolver { |
| 53 | + private static Logger log = LogManager.getLogger(BoardCloudResolver.class); |
| 54 | + |
| 55 | + public synchronized void getBoardBy(String vid, String pid) { |
| 56 | + // this method is less useful in Windows < WIN10 since you need drivers to be already installed |
| 57 | + ObjectMapper mapper = new ObjectMapper(); |
| 58 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 59 | + try { |
| 60 | + URL jsonUrl = new URL(String.format("https://builder.arduino.cc/builder/v1/boards/0x%s/0x%s", vid, pid)); |
| 61 | + |
| 62 | + final HttpURLConnection httpConnection = new HttpConnectionManager(jsonUrl) |
| 63 | + .makeConnection(); |
| 64 | + int code = httpConnection.getResponseCode(); |
| 65 | + if (code == 404) { |
| 66 | + log.warn("Fail to get the Vid Pid information from the builder response code={}", code); |
| 67 | + return; |
| 68 | + } |
| 69 | + InputStream is = httpConnection.getInputStream(); |
| 70 | + BoardCloudAPIid board = mapper.readValue(is, BoardCloudAPIid.class); |
| 71 | + log.info("Board info from the cloud {}", board); |
| 72 | + // Launch a popup with a link to boardmanager#board.getName() |
| 73 | + // replace spaces with & |
| 74 | + String realBoardName = board.getName().replaceAll("\\(.*?\\)", "").trim(); |
| 75 | + String boardNameReplaced = realBoardName.replaceAll(" ", "&"); |
| 76 | + String message = I18n.format(tr("{0}Install this package{1} to use your {2} board"), "<a href=\"http://boardsmanager/all#" + boardNameReplaced + "\">", "</a>", realBoardName); |
| 77 | + BaseNoGui.setBoardManagerLink(message); |
| 78 | + } catch (Exception e) { |
| 79 | + // No connection no problem, fail silently |
| 80 | + //e.printStackTrace(); |
| 81 | + log.warn("Error during get board information by vid, pid", e); |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) { |
| 87 | + assert packages != null; |
| 88 | + assert boardId != null; |
| 89 | + for (TargetPackage targetPackage : packages.values()) { |
| 90 | + for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) { |
| 91 | + for (TargetBoard board : targetPlatform.getBoards().values()) { |
| 92 | + if (boardId.equals(board.getId())) { |
| 93 | + return board.getName(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + private static class BoardCloudAPIid { |
| 102 | + |
| 103 | + private String fqbn; |
| 104 | + private String name; |
| 105 | + private String architecture; |
| 106 | + private String id; |
| 107 | + |
| 108 | + public String getName() { |
| 109 | + return name; |
| 110 | + } |
| 111 | + |
| 112 | + public void setName(String tmp) { |
| 113 | + name = tmp; |
| 114 | + } |
| 115 | + |
| 116 | + public String getFqbn() { |
| 117 | + return fqbn; |
| 118 | + } |
| 119 | + |
| 120 | + public void setFqbn(String fqbn) { |
| 121 | + this.fqbn = fqbn; |
| 122 | + } |
| 123 | + |
| 124 | + public String getArchitecture() { |
| 125 | + return architecture; |
| 126 | + } |
| 127 | + |
| 128 | + public void setArchitecture(String tmp) { |
| 129 | + architecture = tmp; |
| 130 | + } |
| 131 | + |
| 132 | + public String getId() { |
| 133 | + return id; |
| 134 | + } |
| 135 | + |
| 136 | + public void setId(String tmp) { |
| 137 | + id = tmp; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String toString() { |
| 142 | + return "BoardCloudAPIid{" + |
| 143 | + "name='" + name + '\'' + |
| 144 | + ", fqbn='" + fqbn + '\'' + |
| 145 | + ", architecture='" + architecture + '\'' + |
| 146 | + ", id='" + id + '\'' + |
| 147 | + '}'; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments