Skip to content

Commit bcb8e90

Browse files
Mattia Bertorellocmaglie
Mattia Bertorello
authored andcommitted
Remove getBoardWithMatchingVidPidFromCloud and make a new class BoardCloudResolver
1 parent 824567d commit bcb8e90

File tree

2 files changed

+152
-90
lines changed

2 files changed

+152
-90
lines changed

arduino-core/src/processing/app/Platform.java

+1-90
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
package processing.app;
2424

2525
import cc.arduino.packages.BoardPort;
26-
import cc.arduino.utils.network.HttpConnectionManager;
27-
import com.fasterxml.jackson.databind.DeserializationFeature;
28-
import com.fasterxml.jackson.databind.ObjectMapper;
29-
import org.apache.logging.log4j.LogManager;
30-
import org.apache.logging.log4j.Logger;
3126
import processing.app.debug.TargetBoard;
3227
import processing.app.debug.TargetPackage;
3328
import processing.app.debug.TargetPlatform;
@@ -36,9 +31,6 @@
3631
import javax.swing.*;
3732
import java.io.File;
3833
import java.io.IOException;
39-
import java.io.InputStream;
40-
import java.net.HttpURLConnection;
41-
import java.net.URL;
4234
import java.util.*;
4335

4436
import static processing.app.I18n.tr;
@@ -60,8 +52,7 @@
6052
* know if name is proper Java package syntax.)
6153
*/
6254
public class Platform {
63-
private static Logger log = LogManager.getLogger(Platform.class);
64-
55+
// DO NOT USE log4j here otherwise the root path of the logs will no be initialize correctly
6556
/**
6657
* Set the default L & F. While I enjoy the bounty of the sixteen possible
6758
* exception types that this UIManager method might throw, I feel that in
@@ -185,36 +176,6 @@ public List<String> listSerialsNames() {
185176
return list;
186177
}
187178

188-
public synchronized void getBoardWithMatchingVidPidFromCloud(String vid, String pid) {
189-
// this method is less useful in Windows < WIN10 since you need drivers to be already installed
190-
ObjectMapper mapper = new ObjectMapper();
191-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
192-
try {
193-
URL jsonUrl = new URL(String.format("https://builder.arduino.cc/builder/v1/boards/0x%s/0x%s", vid, pid));
194-
195-
final HttpURLConnection httpConnection = new HttpConnectionManager(jsonUrl)
196-
.makeConnection();
197-
int code = httpConnection.getResponseCode();
198-
if (code == 404) {
199-
log.warn("Fail to get the Vid Pid information from the builder response code={}", code);
200-
return;
201-
}
202-
InputStream is = httpConnection.getInputStream();
203-
BoardCloudAPIid board = mapper.readValue(is, BoardCloudAPIid.class);
204-
log.info("Board info from the cloud {}", board);
205-
// Launch a popup with a link to boardmanager#board.getName()
206-
// replace spaces with &
207-
String realBoardName = board.getName().replaceAll("\\(.*?\\)", "").trim();
208-
String boardNameReplaced = realBoardName.replaceAll(" ", "&");
209-
String message = I18n.format(tr("{0}Install this package{1} to use your {2} board"), "<a href=\"http://boardsmanager/all#" + boardNameReplaced + "\">", "</a>", realBoardName);
210-
BaseNoGui.setBoardManagerLink(message);
211-
} catch (Exception e) {
212-
// No connection no problem, fail silently
213-
//e.printStackTrace();
214-
215-
}
216-
}
217-
218179
public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String serial, Map<String, TargetPackage> packages) {
219180
String vid_pid_iSerial = resolveDeviceAttachedToNative(serial);
220181
for (TargetPackage targetPackage : packages.values()) {
@@ -256,56 +217,6 @@ public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String
256217
return null;
257218
}
258219

259-
public static class BoardCloudAPIid {
260-
261-
private String fqbn;
262-
private String name;
263-
private String architecture;
264-
private String id;
265-
266-
public String getName() {
267-
return name;
268-
}
269-
270-
public String getFqbn() {
271-
return fqbn;
272-
}
273-
274-
public String getArchitecture() {
275-
return architecture;
276-
}
277-
278-
public String getId() {
279-
return id;
280-
}
281-
282-
public void setName(String tmp) {
283-
name = tmp;
284-
}
285-
286-
public void setFqbn(String fqbn) {
287-
this.fqbn = fqbn;
288-
}
289-
290-
public void setArchitecture(String tmp) {
291-
architecture = tmp;
292-
}
293-
294-
public void setId(String tmp) {
295-
id = tmp;
296-
}
297-
298-
@Override
299-
public String toString() {
300-
return "BoardCloudAPIid{" +
301-
"name='" + name + '\'' +
302-
", fqbn='" + fqbn + '\'' +
303-
", architecture='" + architecture + '\'' +
304-
", id='" + id + '\'' +
305-
'}';
306-
}
307-
}
308-
309220
public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) {
310221
assert packages != null;
311222
assert boardId != null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)