|
1 | 1 | package openbci_gui_helpers; |
2 | 2 |
|
3 | | -import java.io.BufferedReader; |
4 | 3 | import java.io.File; |
5 | 4 | import java.io.InputStream; |
6 | | -import java.io.InputStreamReader; |
7 | | -import java.lang.reflect.Type; |
8 | | -import java.nio.charset.StandardCharsets; |
9 | 5 | import java.nio.file.Files; |
10 | | -import java.nio.file.Paths; |
11 | | -import java.nio.file.Path; |
12 | | -import java.util.Map; |
13 | | -import java.util.stream.Collectors; |
| 6 | +import java.util.ArrayList; |
14 | 7 |
|
15 | 8 | import org.apache.commons.lang3.SystemUtils; |
16 | 9 |
|
17 | 10 | import com.google.gson.Gson; |
18 | | -import com.google.gson.reflect.TypeToken; |
19 | 11 | import com.sun.jna.Library; |
20 | 12 | import com.sun.jna.Native; |
21 | 13 |
|
22 | | -public class GUIHelper |
23 | | -{ |
24 | | - private interface DllInterface extends Library |
25 | | - { |
26 | | - int scan_for_ganglions (String serial_port, int timeout_sec, byte[] output, int[] output_len); |
| 14 | +public class GUIHelper { |
| 15 | + public class GanglionDevice { |
| 16 | + public int firmware_version; |
| 17 | + public String identifier; |
| 18 | + public String mac_address; |
27 | 19 | } |
28 | 20 |
|
29 | | - private interface DllNativeInterface extends Library |
30 | | - { |
31 | | - int scan_for_ganglions (int timeout_sec, byte[] output, int[] output_len); |
| 21 | + private interface DllInterface extends Library { |
| 22 | + int scan_for_ganglions(String serial_port, int timeout_sec, byte[] output, int[] output_len); |
| 23 | + } |
| 24 | + |
| 25 | + private interface DllNativeInterface extends Library { |
| 26 | + int scan_for_ganglions(int timeout_sec, byte[] output, int[] output_len); |
32 | 27 | } |
33 | 28 |
|
34 | 29 | private static DllInterface instance; |
35 | 30 | private static DllNativeInterface instance_native; |
36 | | - private static final String VERSION = "2.0.1"; |
| 31 | + private static final String VERSION = "3.0.0"; |
37 | 32 |
|
38 | | - static |
39 | | - { |
| 33 | + static { |
40 | 34 | System.out.println("OpenBCI_GUI_Helpers Version: " + VERSION); |
41 | 35 |
|
42 | 36 | String lib_name = "libGanglionScan.so"; |
43 | 37 | String lib_native_name = "libGanglionNativeScan.so"; |
44 | | - if (SystemUtils.IS_OS_WINDOWS) |
45 | | - { |
| 38 | + if (SystemUtils.IS_OS_WINDOWS) { |
46 | 39 | lib_name = "GanglionScan.dll"; |
47 | 40 | lib_native_name = "GanglionNativeScan.dll"; |
48 | 41 |
|
49 | | - } else if (SystemUtils.IS_OS_MAC) |
50 | | - { |
| 42 | + } else if (SystemUtils.IS_OS_MAC) { |
51 | 43 | lib_name = "libGanglionScan.dylib"; |
52 | 44 | lib_native_name = "libGanglionNativeScan.dylib"; |
53 | 45 | } |
54 | 46 |
|
55 | 47 | // need to extract libraries from jar |
56 | | - String lib_path = unpack_from_jar (lib_name); |
57 | | - String lib_native_path = unpack_from_jar (lib_native_name); |
| 48 | + String lib_path = unpack_from_jar(lib_name); |
| 49 | + String lib_native_path = unpack_from_jar(lib_native_name); |
58 | 50 |
|
59 | | - instance = (DllInterface) Native.load (lib_path, DllInterface.class); |
60 | | - instance_native = (DllNativeInterface) Native.load (lib_native_path, DllNativeInterface.class); |
| 51 | + instance = (DllInterface) Native.load(lib_path, DllInterface.class); |
| 52 | + instance_native = (DllNativeInterface) Native.load(lib_native_path, DllNativeInterface.class); |
61 | 53 | } |
62 | 54 |
|
63 | | - private static String unpack_from_jar (String lib_name) |
64 | | - { |
| 55 | + private static String unpack_from_jar(String lib_name) { |
65 | 56 | File file = null; |
66 | 57 | InputStream link = null; |
67 | | - try |
68 | | - { |
69 | | - file = new File (lib_name); |
70 | | - if (file.exists ()) |
71 | | - file.delete (); |
72 | | - link = (GUIHelper.class.getResourceAsStream (lib_name)); |
| 58 | + try { |
| 59 | + file = new File(lib_name); |
| 60 | + if (file.exists()) |
| 61 | + file.delete(); |
| 62 | + link = (GUIHelper.class.getResourceAsStream(lib_name)); |
73 | 63 | if (SystemUtils.IS_OS_MAC) { |
74 | 64 | String jarPath = GUIHelper.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
75 | 65 | File jarFile = new File(jarPath); |
76 | 66 | String libPathString = jarFile.getParentFile().getAbsolutePath() + File.separator + lib_name; |
77 | 67 | return libPathString; |
78 | 68 | } else { |
79 | | - Files.copy (link, file.getAbsoluteFile ().toPath ()); |
| 69 | + Files.copy(link, file.getAbsoluteFile().toPath()); |
80 | 70 | } |
81 | 71 | return file.getAbsolutePath(); |
82 | | - } catch (Exception io) |
83 | | - { |
84 | | - io.printStackTrace (); |
| 72 | + } catch (Exception io) { |
| 73 | + io.printStackTrace(); |
85 | 74 | System.err.println("native library: " + lib_name + " is not found in jar file"); |
86 | 75 | System.err.println("file absolute to path: " + file.getAbsoluteFile().toPath()); |
87 | 76 | System.err.println("file get absolute path: " + file.getAbsolutePath()); |
88 | 77 | return ""; |
89 | 78 | } |
90 | 79 | } |
91 | 80 |
|
92 | | - public static Map<String, String> scan_for_ganglions (String port_name, int timeout_sec) throws GanglionError |
93 | | - { |
| 81 | + public static GanglionDevice[] scan_for_ganglions(String port_name, int timeout_sec) throws GanglionError { |
94 | 82 | int[] len = new int[1]; |
95 | 83 | byte[] output_json = new byte[10240]; |
96 | | - int ec = instance.scan_for_ganglions (port_name, timeout_sec, output_json, len); |
97 | | - if (ec != GanglionExitCodes.STATUS_OK.get_code ()) |
98 | | - { |
99 | | - throw new GanglionError ("Error in scan for ganglions", ec); |
| 84 | + |
| 85 | + int ec = instance.scan_for_ganglions(port_name, timeout_sec, output_json, len); |
| 86 | + if (ec != GanglionExitCodes.STATUS_OK.get_code()) { |
| 87 | + throw new GanglionError("Error in scan for ganglions", ec); |
100 | 88 | } |
101 | | - String json = new String (output_json, 0, len[0]); |
102 | | - Gson gson = new Gson (); |
103 | | - Type type = new TypeToken<Map<String, String>> () |
104 | | - { |
105 | | - }.getType (); |
106 | | - Map<String, String> map = gson.fromJson (json, type); |
107 | | - return map; |
| 89 | + |
| 90 | + String json = new String(output_json, 0, len[0]); |
| 91 | + Gson gson = new Gson(); |
| 92 | + |
| 93 | + GanglionDevice[] devices = gson.fromJson(json, GanglionDevice[].class); |
| 94 | + |
| 95 | + ArrayList<GanglionDevice> uniqueDevices = new ArrayList<GanglionDevice>(); |
| 96 | + for (GanglionDevice device : devices) { |
| 97 | + if (!uniqueDevices.stream().anyMatch(entry -> entry.mac_address.equals(device.mac_address))) { |
| 98 | + uniqueDevices.add(device); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return uniqueDevices.toArray(new GanglionDevice[uniqueDevices.size()]); |
108 | 103 | } |
109 | 104 |
|
110 | | - public static Map<String, String> scan_for_ganglions (int timeout_sec) throws GanglionError |
111 | | - { |
| 105 | + public static GanglionDevice[] scan_for_ganglions(int timeout_sec) throws GanglionError { |
112 | 106 | int[] len = new int[1]; |
113 | 107 | byte[] output_json = new byte[10240]; |
114 | | - int ec = instance_native.scan_for_ganglions (timeout_sec, output_json, len); |
115 | | - if (ec != GanglionExitCodes.STATUS_OK.get_code ()) |
116 | | - { |
117 | | - throw new GanglionError ("Error in scan for ganglions", ec); |
| 108 | + |
| 109 | + int ec = instance_native.scan_for_ganglions(timeout_sec, output_json, len); |
| 110 | + if (ec != GanglionExitCodes.STATUS_OK.get_code()) { |
| 111 | + throw new GanglionError("Error in scan for ganglions", ec); |
118 | 112 | } |
119 | | - String json = new String (output_json, 0, len[0]); |
120 | | - Gson gson = new Gson (); |
121 | | - Type type = new TypeToken<Map<String, String>> () |
122 | | - { |
123 | | - }.getType (); |
124 | | - Map<String, String> map = gson.fromJson (json, type); |
125 | | - return map; |
126 | | - } |
127 | 113 |
|
| 114 | + String json = new String(output_json, 0, len[0]); |
| 115 | + Gson gson = new Gson(); |
| 116 | + |
| 117 | + GanglionDevice[] devices = gson.fromJson(json, GanglionDevice[].class); |
| 118 | + |
| 119 | + ArrayList<GanglionDevice> uniqueDevices = new ArrayList<GanglionDevice>(); |
| 120 | + for (GanglionDevice device : devices) { |
| 121 | + if (!uniqueDevices.stream().anyMatch(entry -> entry.mac_address.equals(device.mac_address))) { |
| 122 | + uniqueDevices.add(device); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return uniqueDevices.toArray(new GanglionDevice[uniqueDevices.size()]); |
| 127 | + } |
128 | 128 | } |
0 commit comments