-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move unpack_from_jar to its own class (#656)
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
- Loading branch information
1 parent
194cdfc
commit 10a762b
Showing
5 changed files
with
193 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
java_package/brainflow/src/main/java/brainflow/JarHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package brainflow; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import com.sun.jna.Platform; | ||
|
||
public class JarHelper | ||
{ | ||
public static Path unpack_from_jar (String lib_name) | ||
{ | ||
File file = new File (lib_name); | ||
try | ||
{ | ||
File temp_dir = get_temp_dir (); | ||
file = new File (temp_dir, lib_name); | ||
} catch (IOException io) | ||
{ | ||
io.printStackTrace (); | ||
} | ||
try | ||
{ | ||
System.err.println ("Unpacking to: " + file.getAbsolutePath ().toString ()); | ||
if (file.exists ()) | ||
file.delete (); | ||
InputStream link = (JarHelper.class.getResourceAsStream (lib_name)); | ||
Files.copy (link, file.getAbsoluteFile ().toPath ()); | ||
return file.getAbsoluteFile ().toPath (); | ||
} catch (Exception io) | ||
{ | ||
io.printStackTrace (); | ||
System.err.println ("file: " + lib_name + " is not found in jar file"); | ||
return null; | ||
} | ||
} | ||
|
||
static File get_temp_dir () throws IOException | ||
{ | ||
File jna_tmp; | ||
String prop = System.getProperty ("jna.tmpdir"); | ||
if (prop != null) | ||
{ | ||
jna_tmp = new File (prop); | ||
jna_tmp.mkdirs (); | ||
} else | ||
{ | ||
File tmp = new File (System.getProperty ("java.io.tmpdir")); | ||
if (Platform.isMac ()) | ||
{ | ||
// https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html | ||
jna_tmp = new File (System.getProperty ("user.home"), "Library/Caches/JNA/temp"); | ||
} else if ( | ||
Platform.isLinux () || Platform.isSolaris () || Platform.isAIX () || Platform.isFreeBSD () | ||
|| Platform.isNetBSD () || Platform.isOpenBSD () || Platform.iskFreeBSD () | ||
) | ||
{ | ||
// https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | ||
// The XDG_CACHE_DIR is expected to be per user | ||
String xdg_cache_environment = System.getenv ("XDG_CACHE_HOME"); | ||
File xdg_cache_file; | ||
if (xdg_cache_environment == null || xdg_cache_environment.trim ().isEmpty ()) | ||
{ | ||
xdg_cache_file = new File (System.getProperty ("user.home"), ".cache"); | ||
} else | ||
{ | ||
xdg_cache_file = new File (xdg_cache_environment); | ||
} | ||
jna_tmp = new File (xdg_cache_file, "JNA/temp"); | ||
} else | ||
{ | ||
// Loading DLLs via System.load() under a directory with a unicode | ||
// name will fail on windows, so use a hash code of the user's | ||
// name in case the user's name contains non-ASCII characters | ||
jna_tmp = new File (tmp, "jna-" + System.getProperty ("user.name").hashCode ()); | ||
} | ||
|
||
jna_tmp.mkdirs (); | ||
if (!jna_tmp.exists () || !jna_tmp.canWrite ()) | ||
{ | ||
jna_tmp = tmp; | ||
} | ||
} | ||
if (!jna_tmp.exists ()) | ||
{ | ||
throw new IOException ("JNA temporary directory '" + jna_tmp + "' does not exist"); | ||
} | ||
if (!jna_tmp.canWrite ()) | ||
{ | ||
throw new IOException ("JNA temporary directory '" + jna_tmp + "' is not writable"); | ||
} | ||
return jna_tmp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import argparse | ||
import time | ||
|
||
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds, BrainFlowPresets | ||
|
||
|
||
def main(): | ||
BoardShim.enable_dev_board_logger() | ||
parser = argparse.ArgumentParser() | ||
# use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port | ||
parser.add_argument('--timeout', type=int, help='timeout for device discovery or connection', required=False, | ||
default=0) | ||
parser.add_argument('--ip-port', type=int, help='ip port', required=False, default=0) | ||
parser.add_argument('--ip-protocol', type=int, help='ip protocol, check IpProtocolType enum', required=False, | ||
default=0) | ||
parser.add_argument('--ip-address', type=str, help='ip address', required=False, default='') | ||
parser.add_argument('--serial-port', type=str, help='serial port', required=False, default='') | ||
parser.add_argument('--mac-address', type=str, help='mac address', required=False, default='') | ||
parser.add_argument('--other-info', type=str, help='other info', required=False, default='') | ||
parser.add_argument('--serial-number', type=str, help='serial number', required=False, default='') | ||
parser.add_argument('--board-id', type=int, help='board id, check docs to get a list of supported boards', | ||
required=True) | ||
parser.add_argument('--file', type=str, help='file', required=False, default='') | ||
parser.add_argument('--master-board', type=int, help='master board id for streaming and playback boards', | ||
required=False, default=BoardIds.NO_BOARD) | ||
args = parser.parse_args() | ||
|
||
params = BrainFlowInputParams() | ||
params.ip_port = args.ip_port | ||
params.serial_port = args.serial_port | ||
params.mac_address = args.mac_address | ||
params.other_info = args.other_info | ||
params.serial_number = args.serial_number | ||
params.ip_address = args.ip_address | ||
params.ip_protocol = args.ip_protocol | ||
params.timeout = args.timeout | ||
params.file = args.file | ||
params.master_board = args.master_board | ||
|
||
board = BoardShim(args.board_id, params) | ||
board_id = board.get_board_id() | ||
presets = BoardShim.get_board_presets(board_id) | ||
board.prepare_session() | ||
for preset in presets: | ||
# to stream to plotjuggler | ||
board.add_streamer( | ||
f'plotjuggler_udp://127.0.0.1:9870', preset) | ||
# to store data in a file | ||
board.add_streamer(f'file://data_{preset}.csv:w', preset) | ||
board.start_stream () | ||
|
||
while True: | ||
try: | ||
time.sleep(1) | ||
except KeyboardInterrupt: | ||
break | ||
except Exception as e: | ||
print(e) | ||
board.release_session() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |