|
| 1 | +package processing.app.macosx; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.StringReader; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +public class SystemProfilerParser { |
| 12 | + |
| 13 | + private final Pattern vidRegex; |
| 14 | + private final Pattern serialNumberRegex; |
| 15 | + private final Pattern locationRegex; |
| 16 | + private final Pattern pidRegex; |
| 17 | + |
| 18 | + public SystemProfilerParser() { |
| 19 | + serialNumberRegex = Pattern.compile("^Serial Number: (.+)$"); |
| 20 | + locationRegex = Pattern.compile("^Location ID: (.+)$"); |
| 21 | + pidRegex = Pattern.compile("^Product ID: (.+)$"); |
| 22 | + vidRegex = Pattern.compile("^Vendor ID: (.+)$"); |
| 23 | + } |
| 24 | + |
| 25 | + public String extractVIDAndPID(String output, String serial) throws IOException { |
| 26 | + BufferedReader reader = new BufferedReader(new StringReader(output)); |
| 27 | + |
| 28 | + String devicePrefix; |
| 29 | + if (serial.startsWith("/dev/tty.")) { |
| 30 | + devicePrefix = "/dev/tty.usbmodem"; |
| 31 | + } else { |
| 32 | + devicePrefix = "/dev/cu.usbmodem"; |
| 33 | + } |
| 34 | + |
| 35 | + Map<String, String> device = new HashMap<String, String>(); |
| 36 | + |
| 37 | + String line; |
| 38 | + Matcher matcher; |
| 39 | + while ((line = reader.readLine()) != null) { |
| 40 | + line = line.trim(); |
| 41 | + line = line.replaceAll("\\s+", " "); |
| 42 | + |
| 43 | + if ((matcher = serialNumberRegex.matcher(line)).matches()) { |
| 44 | + device.put("serial_number", matcher.group(1)); |
| 45 | + } else if ((matcher = locationRegex.matcher(line)).matches()) { |
| 46 | + device.put("device_path", devicePrefix + matcher.group(1).substring(2, 6) + "1"); |
| 47 | + } else if ((matcher = pidRegex.matcher(line)).matches()) { |
| 48 | + device.put("pid", matcher.group(1)); |
| 49 | + } else if ((matcher = vidRegex.matcher(line)).matches()) { |
| 50 | + device.put("vid", matcher.group(1)); |
| 51 | + } else if (line.equals("")) { |
| 52 | + if (device.containsKey("serial_number") && device.get("device_path").equals(serial)) { |
| 53 | + return device.get("vid").substring(2).toUpperCase() + "_" + device.get("pid").substring(2).toUpperCase(); |
| 54 | + } |
| 55 | + device = new HashMap<String, String>(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments