-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitbit_server.js
More file actions
147 lines (127 loc) · 5.21 KB
/
fitbit_server.js
File metadata and controls
147 lines (127 loc) · 5.21 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
var express = require('express'),
connect = require('connect'),
app = express.createServer(connect.bodyParser(),
connect.cookieParser('session'),
connect.session());
var fs = require('fs');
var creds = require('./creds');
console.log(creds.PASS11);
var fitbitClient = require('./fitbit')(creds.PASS11, creds.PASS2);
var mongoDB=require('./mongolib');
var token;
app.get('/', function (req, res) {
fitbitClient.getAccessToken(req, res, function (error, newToken) {
if(newToken) {
token = newToken;
res.writeHead(200, {'Content-Type':'text/html'});
res.write('<html>Now ');
res.end('<a href="/getSteps">get steps</a><a href="/getSteps">get steps</a></html>');
}
});
});
//1/user/-/body/weight/date/2014-04-03/max.json
//user/-/activities/steps/date/2014-04-03/max.json
//1/user/-/sleep/minutesAsleep/date/2014-04-03/max.xml
//1/user/-/body/log/weight/date/2014-04-03/max.xml
app.get('/getOneActivity', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/activities/steps/date/2014-04-03.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/getActiveDays', function (req, res) {
mongoDB.queryData("step_data",{intValue: { $gt: 0 }},"intValue",-1, 1000,
function(docs){
res.send(JSON.stringify(docs));
});
});
app.get('/get10KSteps', function (req, res) {
mongoDB.queryData("step_data",{intValue: { $gt: 10000 }},"intValue",-1, 1000,
function(docs){
res.send(JSON.stringify(docs));
});
});
app.get('/get20KSteps', function (req, res) {
mongoDB.queryData("step_data",{intValue: { $gt: 20000 }},"intValue",-1, 1000,
function(docs){
res.send(JSON.stringify(docs));
});
});
app.get('/getOneSteps', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/activities/steps/date/2014-04-03.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/getAllSteps', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/activities/steps/date/2014-04-03/max.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
//activities-steps
var obj = JSON.parse(resp);
var step_array=obj['activities-steps'];
for (i=0;i<step_array.length;i++)
step_array[i].intValue=parseInt(step_array[i].value);
try{
mongoDB.insertData(step_array,'step_data',function(){ });
}
catch (err){ console.log("error is "+err); }
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/update', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/activities/steps/date/2014-04-03/max.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
var d = new Date()
console.log("Start Update: "+d.getTime());
var obj = JSON.parse(resp);
var step_array=obj['activities-steps'];
for (var i=0;i<step_array.length;i++){
step_array[i].intValue=parseInt(step_array[i].value);
try{
mongoDB.updateTimeSeriesData(step_array[i],'step_data',function(){ });
}
catch (err){ console.log("error is "+err); }
}
console.log("Stop Update: "+d.getTime());
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/getAllSleep', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/sleep/minutesAsleep/date/2014-04-03/max.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/getOneSleep', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/sleep/date/2014-04-03.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
app.get('/getWeight', function (req, res) {
fitbitClient.apiCall('GET', '/user/-/body/weight/date/2014-04-03/max.json',
{token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
function(err, resp) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
});
mongoDB.openDB(function(){
console.log("ready to set unique field");
//Steps, Sleep, Weight
mongoDB.createCollection("step_data", function() { });
});
app.listen(8553);
console.log('listening at http://localhost:8553/');