-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhealthBit.js
More file actions
278 lines (257 loc) · 10.5 KB
/
healthBit.js
File metadata and controls
278 lines (257 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
console.log('healthBit.js base library loaded')
// ini
hb = function(){
// ini
if((location.href.slice(0,16)=="http://localhost")||(location.href.slice(0,5)=="https")){
hb.connect()
if(sessionStorage.logingIntoFitbit){
hb.fitbit()
}
//hb.fitbit() // starting with fitbits
}else{ // force ssl
document.location.protocol='https:'
}
}
hb.fitbit=function(scopes){
if(!scopes){scopes='profile'}
else{scopes=scopes.join('%20')} // scopes expected as an array
// login fitbit
if(!sessionStorage.logingIntoFitbit){ // if not returning from login then this is about loggin in
var href=location.href
if(href.slice(-1)=='#'){href=href.slice(0,-1)}
sessionStorage.logingIntoFitbit=new Date()
location.href='https://www.fitbit.com/oauth2/authorize?client_id=228KD7&redirect_uri='+href+'&device=fitbit&response_type=token&scope='+scopes
}else{ // logged in fitbit
sessionStorage.removeItem('logingIntoFitbit')
hb.fitbit.login={}
location.hash.slice(1).split('&').map(
function(pp){
var p = pp.split('=');
hb.fitbit.login[p[0]]=p[1]
//console.log(p)
}
)
location.hash=""
// assemble fitbit UI
// 1. Prepare div
if($('#healthBitFitbitConnected').length==0){ // prepare div
var div = document.createElement('div')
div.id = "healthBitFitbitConnected"
healthBitFitbit.appendChild(div)
}else{
var div = document.getElementById("healthBitFitbitConnected")
}
// display connected
div.innerHTML='Connected:'
// scan each of the scopes
hb.fitbit.login.scope=hb.fitbit.login.scope.split('+')
// match checkboxes
$('#healthBitFitbit > input').each(function(i,el){
if(hb.fitbit.login.scope.indexOf(el.value)<0){
el.checked=false
}
})
hb.fitbit.scope={} // data about each scope will go here
hb.fitbit.login.scope.forEach(function(sc){
var divsc = document.createElement('div')
divsc.id='fitbitScope_'+sc
divsc.innerHTML='<h4 style="color:navy">'+sc+'</h4>'
div.appendChild(divsc)
// get some data for the given scope
if(sc=='activity'){
hb.fitbit.getActivity(divsc,'1y.json')
.then(function(){
hb.fitbit.getActivity(divsc,'1m.json')
})
/*
.then(function(){
hb.fitbit.getActivity(divsc,'1w.json')
})
.then(function(){
hb.fitbit.getActivity(divsc,'1d.json')
})
*/
}
if(sc=='heartrate'){
hb.fitbit.getHeartrate(divsc,'1m.json')
.then(function(){
hb.fitbit.getHeartrate(divsc,'1w.json')
})
}
// get the data
//hb.fitbit.get(sc)
})
//console.log('login',hb.fitbit.login)
// old stuff, remove later
if(false){
$.ajax({
url : "https://api.fitbit.com/1/user/"+hb.fitbit.login.user_id+"/profile.json",
headers: {'Authorization' : hb.fitbit.login.token_type+' '+hb.fitbit.login.access_token}
}).then(function(x){
var sp=hb.fitbit.login.scope.split('+').map(function(s){
return '<li>'+s+'</li>'
})
hb.fitbit.info=x
hb.fitbit.info.date=new Date
var spn=hb.fitbit.login.scope.split('+').length
sp='<ol>'+sp.join('')+'</ol>'
$('<h2 style="color:maroon">Using OAUTH 2.0</h2><div id="healthBitScope" style="color:blue"> You gave me OAUTH to engage the following services:'+sp+'</div>').appendTo(healthBitDiv)
$('<h3 style="color:maroon">Reading from your <i style="color:blue">profile</i> service</h3>').appendTo(healthBitDiv)
$('<div style="color:navy"><p>Could instead be reading from any of the <span style="color:red">other '+(spn-1)+' services</span> listed above ...</p><p>Have a look at the <a href="https://dev.fitbit.com/docs/activity/" target=_blank>fitbit API documentation</a> to find out what type of data may be available.</p></div>').appendTo(healthBitDiv)
var ol='<ol style="color:blue">'
Object.getOwnPropertyNames(x.user).map(function(p){
ol+='<li>'+p+': <span style="color:red">'+x.user[p]+'</span></li>'
})
ol+='</ol>'
$(ol).appendTo(healthBitDiv)
// full App
hb.app(healthBitDiv)
})
}
}
}
hb.fitbit.get=function(url,cb){
$.ajax({
url : url,
headers: {'Authorization' : hb.fitbit.login.token_type+' '+hb.fitbit.login.access_token}
}).then(function(x){
cb(x)
})
}
hb.fitbit.getActivity=function(div,range,cb){
if(!range){range='1m.json'}
// https://dev.fitbit.com/docs/activity
//var url = "https://api.fitbit.com/1/user/"+hb.fitbit.login.user_id+"/activities/date/today.json"
var url = "https://api.fitbit.com/1/user/"+hb.fitbit.login.user_id+"/activities/steps/date/today/"+range
var divPlot=document.createElement('div')
div.appendChild(divPlot)
hb.fitbit.get(url,function(x){
var time=[],steps=[]
x["activities-steps"].forEach(function(xi,i){
time[i]=new Date(xi.dateTime)
steps[i]=parseFloat(xi.value)
})
Plotly.plot( divPlot,
[
{
x: time,
y: steps
}
],
{
margin: { t: 0 },
yaxis: {title: 'steps / day'},
xaxis: {title: 'time'}
}
)
})
return $(divPlot).promise() // so we get promises
}
hb.fitbit.getHeartrate=function(div,range){
if(!range){range='1m.json'}
// https://dev.fitbit.com/docs/heart-rate
//var url = "https://api.fitbit.com/1/user/"+hb.fitbit.login.user_id+"/activities/date/today.json"
var url = "https://api.fitbit.com/1/user/"+hb.fitbit.login.user_id+"/activities/heart/date/today/"+range
var divPlot=document.createElement('div')
div.appendChild(divPlot)
hb.fitbit.get(url,function(x){
var time=[],heart={}
x["activities-heart"].forEach(function(xi,i){
time[i]=new Date(xi.dateTime)
xi.value.heartRateZones.forEach(function(v){
if(!heart[v.name]){
heart[v.name]={
values:[],
range:[v.min,v.max]
}
}else{
heart[v.name].values[i]=v.minutes
}
})
4
})
Plotly.plot( divPlot,
[
{
x: time,
y: heart["Out of Range"].values,
mode: 'markers',
type: 'scatter',
name: "Out of Range ("+heart["Out of Range"].range.join('-')+")"
},
{
x: time,
y: heart["Fat Burn"].values,
mode: 'markers',
type: 'scatter',
name: "Fat Burn ("+heart["Fat Burn"].range.join('-')+")"
}
],
{
margin: { t: 0 },
yaxis: {title: 'minutes / day'},
xaxis: {title: 'time'}
}
)
})
return $(divPlot).promise() // so we get promises
}
hb.app=function(div){ // full App
console.log('full app being assembled ')
$('<h3 style="color:maroon">Tentative App #1 <hr></h3>').appendTo(div)
// app1 configured as a table within hosting div
$('<div id="app1"></div>').appendTo(div)
$('<table id="app1Table"><tr id="app1Head"><td><img src="helilogo.png" width=60><br><i style="color:maroon">Helicopter<hr></i></td><td style="vertical-align:top" align="right">Logged in as <span style="color:blue">'+hb.fitbit.info.user.displayName+'</span> at <span style="color:blue">'+hb.fitbit.info.date.toString().slice(4,24)+'</span><p id="app1Msg" style="color:red">processing ...</p></td></tr><td id="app1Options"></td><td><tr></tr></table>').appendTo(app1)
// filing options
hb.fitbit.login.scopes=hb.fitbit.login.scope.split('+')
hb.fitbit.login.scopes.forEach(function(s){
$('<p id="app1_'+s+'" style="color:gray">'+s[0].toUpperCase()+s.slice(1)+'</p>').appendTo(app1Options)
})
}
// connect
hb.connect=function(){
var h = '<h3 style="color:maroon">Connect your devices</h3>'
// FitBit
h+='<div id="healthBitFitbit">'
h+='<h4 style="color:maroon"healthBitConnectFitbit><li>FitBit <a href="https://www.fitbit.com/" target="_blank"><i class="fa fa-external-link"></i></a></li></h4>'
var scp = ['activity','heartrate','location','nutrition','settings','sleep','social','weight']
for(var i=0;i<scp.length;i++){
var p=scp[i]
h+=p+':<input id="'+p+'Checkbox" type="checkbox" checked="true" value='+p+'> '
}
h+='<br><button id="connectFitBit" class="btn-primary" onclick="hb.connectFitBit(this)">Connect your fitbit</button> for the selected scopes.'
h+='</div>'
// connect your own
/*
h+='<div id="healthBitChoseYourOwn">'
h+='<h4 style="color:maroon"><li>Chose your own, say for <input id="choseYourOwnInput" type="text" value="heart" size=5 style="color:blue;text-align:center"> monitoring <a id="choseYourOwnLink" href="https://www.wearable-technologies.com/?s=heart" target="_blank"><i class="fa fa-external-link"></i></a></li></h4>'
h+='<a href="mailto:jonas.almeida@stonybrookmedicine.edu" target="_blank">Tell us</a> about a device you want to use at Stony Brook Univ, get us one, and we will try to add it here.'
h+='</div>'
*/
//https://www.wearable-technologies.com/2015/04/wearables-in-healthcare/
hbAction.innerHTML=h
// even listeners
//choseYourOwnInput.onkeyup=function(evt){
// this.size=Math.max(4,this.value.length) // adjust size
// choseYourOwnLink.href=choseYourOwnLink.href.match(/[^?]*/)[0]+'?s='+this.value
// if(evt.keyCode==13){
// choseYourOwnLink.click()
// }
// 4
//}
}
hb.connectFitBit=function(that){
// get scopes
var scopes=['profile'] // also profile by default]
$('#healthBitFitbit > input').each(function(i,el){
//console.log(i,el)
if(el.checked){
scopes.push(el.value)
}
})
hb.fitbit(scopes)
}
// ini
$(document).ready(function(){
hb()
})