Debian Codegraph @

arduino-core/src/processing/app/helpers/BoardCloudResolver.java

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