In [1]:
import datetime, math, time
In [2]:
def exec_ipynb(url):
import json, re, urllib2
nb = (urllib2.urlopen(url) if re.match(r'https?:', url) else open(url)).read()
exec '\n'.join([''.join(cell['input']) for cell in json.loads(nb)['worksheets'][0]['cells'] if cell['cell_type'] == 'code']) in globals()
exec_ipynb('Fluxtream-Library.ipynb')
fluxtream_login()
Out[2]:
https://api.hexoskin.com/docs/resource/data/
Can return in JSON, CSV, binary (WAV)
In [3]:
def hexoskin_authenticate(username, password):
global hexoskin_username, hexoskin_password
hexoskin_username = username
hexoskin_password = password
# TODO: catch exception here and complain about auth
hexoskin_get_datatype()
return True
def hexoskin_headers():
auth = base64.encodestring('%s:%s' % (hexoskin_username, hexoskin_password)).replace('\n', '')
return {'Authorization': 'Basic ' + auth}
def hexoskin_get_datatype():
req = urllib2.Request('https://api.hexoskin.com/api/v1/datatype/?limit=1000', headers = hexoskin_headers())
return json.loads(urllib2.urlopen(req).read())
In [4]:
%%HTML
Hexoskin email: <input id="hexoskin_username" type="text"></input><br>
Hexoskin password: <input id="hexoskin_password" type="password"></input><br>
<button id = "hexoskin_authenticate">Authenticate</button> <span id="hexoskin_auth_result"></span>
<script>
// Send username and password to python
function hexoskin_authenticate(username, password, callback) {
cmd = 'hexoskin_authenticate(' + JSON.stringify(username) + ',' +
JSON.stringify(password) + ')'
console.log(cmd);
function cb(msg) {
console.log(msg);
console.log(msg.content.data['text/plain'] == 'True');
callback(msg.content.data['text/plain'] == 'True')
}
IPython.notebook.kernel.execute(cmd,
{iopub: {output: cb}}, {silent: false});
}
$('#hexoskin_authenticate').click(function() {
$('#hexoskin_auth_result').text('...');
hexoskin_authenticate($('#hexoskin_username').val(),
$('#hexoskin_password').val(),
function(success) {
$('#hexoskin_auth_result').text(success ? 'Success' : 'Failed');
});
});
</script>
In [6]:
def sync_hexoskin_to_fluxtream(source, device, dest, sources_list):
info = fluxtream_get_channel_info(device, dest, sources_list)
if info == None:
start = 1
else:
start = info['max_time']
end = time.time()
params = {
'datatype': source,
'user': hexoskin_username,
'start': int(start * 256),
'end': int(math.ceil(end * 256))
}
url = 'https://api.hexoskin.com/api/v1/data/?' + urllib.urlencode(params)
req = urllib2.Request(url, headers = hexoskin_headers())
data = json.loads(urllib2.urlopen(req).read())
if len(data) != 1:
raise Exception('unexpected format')
samples = data[0]['data'][str(source)]
print '%s: read %d samples since time %s' % (dest, len(samples), datetime.datetime.fromtimestamp(start).strftime('%Y-%m-%d %H:%M:%S'))
for sample in samples:
sample[0] /= 256.0
fluxtream_upload(device, [dest], samples)
device = 'Hexoskin'
hexoskin_to_fluxtream = {
'18': 'BeatSpacing',
'19': 'HeartRate',
'37': 'TidalVolume',
'4129': 'RespChest',
'4130': 'RespAbdomen',
'4145': 'AccelX',
'4146': 'AccelY',
'4147': 'AccelZ'
}
sources_list = fluxtream_get_sources_list(fluxtream_guest_id)
for source in sorted(hexoskin_to_fluxtream.keys()):
sync_hexoskin_to_fluxtream(source, device, hexoskin_to_fluxtream[source], sources_list)
print 'Done'
In [19]:
print json.dumps(hexoskin_get_datatype(), sort_keys=True, indent=4)