|
| 1 | +# usage: python3 log_download.py [mac] |
| 2 | +from __future__ import print_function |
| 3 | +from mbientlab.metawear import MetaWear, libmetawear, parse_value |
| 4 | +from mbientlab.metawear.cbindings import * |
| 5 | +from time import sleep, strftime |
| 6 | +from threading import Event |
| 7 | + |
| 8 | +import platform |
| 9 | +import sys |
| 10 | + |
| 11 | +if sys.version_info[0] == 2: |
| 12 | + range = xrange |
| 13 | + |
| 14 | +# connect |
| 15 | +metawear = MetaWear(sys.argv[1]) |
| 16 | +metawear.connect() |
| 17 | +print("Connected") |
| 18 | + |
| 19 | +# setup |
| 20 | +e = Event() |
| 21 | +result = {} |
| 22 | + |
| 23 | +# handler fxn |
| 24 | +def handler(ctx, board, signals, len): |
| 25 | + result['length'] = len |
| 26 | + result['signals'] = cast(signals, POINTER(c_void_p * len)) if signals is not None else None |
| 27 | + e.set() |
| 28 | +# handler fxn ptr |
| 29 | +handler_fn = FnVoid_VoidP_VoidP_VoidP_UInt(handler) |
| 30 | + |
| 31 | +# datahandler class |
| 32 | +class DataHandler: |
| 33 | + def __init__(self, signal): |
| 34 | + self.identifier = libmetawear.mbl_mw_anonymous_datasignal_get_identifier(signal) |
| 35 | + self.filename = self.identifier.decode() + '-' + strftime("%m-%d-%Y-%H-%M-%S") + '.csv' |
| 36 | + self.file = None |
| 37 | + #self.data_handler_fn = FnVoid_VoidP_DataP(lambda ctx, ptr: print({"identifier": self.identifier, "epoch": ptr.contents.epoch, "value": parse_value(ptr)})) |
| 38 | + self.data_handler_fn = FnVoid_VoidP_DataP(lambda ctx, ptr: self.write(ctx, ptr)) |
| 39 | + def write(self, ctx, ptr): |
| 40 | + d_struct = parse_value(ptr) |
| 41 | + if self.file is None: |
| 42 | + print("Opening %s for write." % (self.filename)) |
| 43 | + self.file = open(self.filename, 'w') |
| 44 | + self.file.write('epoch,' + ','.join([f[0] for f in d_struct._fields_]) + '\n') |
| 45 | + data = [str(getattr(d_struct, f[0])) for f in d_struct._fields_] |
| 46 | + self.file.write(str(ptr.contents.epoch) + ',' + ','.join(data) + '\n') |
| 47 | + def __del__(self): |
| 48 | + if self.file is not None: |
| 49 | + self.file.close() |
| 50 | + |
| 51 | +# setup ble |
| 52 | +libmetawear.mbl_mw_settings_set_connection_parameters(metawear.board, 7.5, 7.5, 0, 6000) |
| 53 | +sleep(1.0) |
| 54 | + |
| 55 | +# create anonymous signal |
| 56 | +print("Creating anonymous signals") |
| 57 | +libmetawear.mbl_mw_metawearboard_create_anonymous_datasignals(metawear.board, None, handler_fn) |
| 58 | +e.wait() |
| 59 | + |
| 60 | +# make sense of logging signals found |
| 61 | +if (result['signals'] == None): |
| 62 | + if (result['length'] != 0): |
| 63 | + print("Error creating anonymous signals, status = " + str(result['length'])) |
| 64 | + else: |
| 65 | + print("No active loggers detected") |
| 66 | +else: |
| 67 | + e.clear() |
| 68 | + # stop logging |
| 69 | + libmetawear.mbl_mw_logging_stop(metawear.board) |
| 70 | + # print results |
| 71 | + print(str(result['length']) + " active loggers discovered") |
| 72 | + handlers = [] |
| 73 | + # analyze results |
| 74 | + for x in range(0, result['length']): |
| 75 | + wrapper = DataHandler(result['signals'].contents[x]) |
| 76 | + # subscribe to download if good signal |
| 77 | + libmetawear.mbl_mw_anonymous_datasignal_subscribe(result['signals'].contents[x], None, wrapper.data_handler_fn) |
| 78 | + handlers.append(wrapper) |
| 79 | + # progress handler fxn |
| 80 | + def progress_update_handler(ctx, left, total): |
| 81 | + if (left == 0): |
| 82 | + e.set() |
| 83 | + # unknown entry handler fxn |
| 84 | + def unknown_entry_handler(ctx, id, epoch, data, length): |
| 85 | + print("unknown entry = " + str(id)) |
| 86 | + # download handlers |
| 87 | + print("Downloading log") |
| 88 | + progress_update_fn = FnVoid_VoidP_UInt_UInt(progress_update_handler) |
| 89 | + unknown_entry_fn = FnVoid_VoidP_UByte_Long_UByteP_UByte(unknown_entry_handler) |
| 90 | + download_handler= LogDownloadHandler(context = None, received_progress_update = progress_update_fn, |
| 91 | + received_unknown_entry = unknown_entry_fn, received_unhandled_entry = cast(None, FnVoid_VoidP_DataP)) |
| 92 | + # download |
| 93 | + libmetawear.mbl_mw_logging_download(metawear.board, 10, byref(download_handler)) |
| 94 | + # wait until done |
| 95 | + e.wait() |
| 96 | + # wait 1s |
| 97 | + sleep(1.0) |
| 98 | + print("Download completed") |
| 99 | + e.clear() |
| 100 | + metawear.on_disconnect = lambda status: e.set() |
| 101 | + # disconnect |
| 102 | + libmetawear.mbl_mw_debug_disconnect(metawear.board) |
| 103 | + e.wait() |
| 104 | + |
0 commit comments