New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sleep, weight and body fat % to Fitbit connector #330

Merged
merged 4 commits into from May 25, 2012
Jump to file or symbol
Failed to load files and symbols.
+35 −104
Diff settings

Always

Just for now

Viewing a subset of changes. View all
Prev

Consolidate Fitbit daily syncs

Now all the logic is encapsulated in `fitbit/lib#dailySync`. The only things
that differ are:

* API endpoint
* Key to check in the response data
* Type of IDR object it is
* IDR path
  • Loading branch information...
kristjan committed May 24, 2012
commit a8c77e1b9f5b3a9f1deaa333e18ef767418a837a
@@ -7,29 +7,5 @@
*
*/
exports.sync = require('./lib').genericSync(function(pi){
if(!pi.config) pi.config = {};
if(!pi.config.memberSince || !pi.config.lastSyncTime) return false;
if(!pi.config.activeNext) pi.config.activeNext = (new Date(pi.config.memberSince).getTime()); // convert to epoch format
if((pi.config.activeNext > new Date(pi.config.lastSyncTime).getTime())) return false; // don't run ahead of last sync
return 'activities/date/'+format(pi.config.activeNext)+'.json';
}, function(pi, data, cb){
if(!data || !data.summary) return cb();
data.id = format(pi.config.activeNext); // stub in an id based on the date
data.at = pi.config.activeNext; // also fill in
var next = pi.config.activeNext + (3600*1000*24); // next run get next day
if(next < (new Date(pi.config.lastSyncTime).getTime())){
pi.config.activeNext = next; // don't move forward past last sync time!
if(pi.config.activeNext < Date.now()) pi.config.nextRun = -1; // force run again
}
var base = 'activity:'+pi.auth.pid+'/activities';
var ret = {};
ret[base] = [data];
cb(null, {config:pi.config, data:ret});
});
function format(epoch)
{
d = new Date(epoch);
return ""+d.getFullYear()+'-'+((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1))+'-'+((d.getDate() < 10 ? '0' : '') + d.getDate());
}
exports.sync =
require('./lib').dailySync('activities', 'summary', 'activity', 'activities');
View
@@ -7,29 +7,5 @@
*
*/
exports.sync = require('./lib').genericSync(function(pi){
if(!pi.config) pi.config = {};
if(!pi.config.memberSince || !pi.config.lastSyncTime) return false;
if(!pi.config.activeNext) pi.config.activeNext = (new Date(pi.config.memberSince).getTime()); // convert to epoch format
if((pi.config.activeNext > new Date(pi.config.lastSyncTime).getTime())) return false; // don't run ahead of last sync
return 'body/log/fat/date/'+format(pi.config.activeNext)+'.json';
}, function(pi, data, cb){
if(!data || !data.fat) return cb();
data.id = format(pi.config.activeNext); // stub in an id based on the date
data.at = pi.config.activeNext; // also fill in
var next = pi.config.activeNext + (3600*1000*24); // next run get next day
if(next < (new Date(pi.config.lastSyncTime).getTime())){
pi.config.activeNext = next; // don't move forward past last sync time!
if(pi.config.activeNext < Date.now()) pi.config.nextRun = -1; // force run again
}
var base = 'fat:'+pi.auth.pid+'/fat';
var ret = {};
ret[base] = [data];
cb(null, {config:pi.config, data:ret});
});
function format(epoch)
{
d = new Date(epoch);
return ""+d.getFullYear()+'-'+((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1))+'-'+((d.getDate() < 10 ? '0' : '') + d.getDate());
}
exports.sync =
require('./lib').dailySync('body/log/fat', 'fat', 'fat', 'fat');
View
@@ -24,3 +24,31 @@ exports.genericSync = function(pather, cbDone) {
});
};
};
exports.dailySync = function(apiPath, item, idrType, idrPath) {
return this.genericSync(function(pi) {
if(!pi.config) pi.config = {};
if(!pi.config.memberSince || !pi.config.lastSyncTime) return false;
if(!pi.config.activeNext) pi.config.activeNext = (new Date(pi.config.memberSince).getTime()); // convert to epoch format
if((pi.config.activeNext > new Date(pi.config.lastSyncTime).getTime())) return false; // don't run ahead of last sync
return apiPath + '/date/'+format(pi.config.activeNext)+'.json';
}, function(pi, data, cb) {
if(!data || !data[item]) return cb();
data.id = format(pi.config.activeNext); // stub in an id based on the date
data.at = pi.config.activeNext; // also fill in
var next = pi.config.activeNext + (3600*1000*24); // next run get next day
if(next < (new Date(pi.config.lastSyncTime).getTime())){
pi.config.activeNext = next; // don't move forward past last sync time!
if(pi.config.activeNext < Date.now()) pi.config.nextRun = -1; // force run again
}
var base = idrType + ':' + pi.auth.pid + '/' + idrPath;
var ret = {};
ret[base] = [data];
cb(null, {config:pi.config, data:ret});
});
};
function format(epoch) {
d = new Date(epoch);
return ""+d.getFullYear()+'-'+((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1))+'-'+((d.getDate() < 10 ? '0' : '') + d.getDate());
}
@@ -7,29 +7,4 @@
*
*/
exports.sync = require('./lib').genericSync(function(pi){
if(!pi.config) pi.config = {};
if(!pi.config.memberSince || !pi.config.lastSyncTime) return false;
if(!pi.config.activeNext) pi.config.activeNext = (new Date(pi.config.memberSince).getTime()); // convert to epoch format
if((pi.config.activeNext > new Date(pi.config.lastSyncTime).getTime())) return false; // don't run ahead of last sync
return 'sleep/date/'+format(pi.config.activeNext)+'.json';
}, function(pi, data, cb){
if(!data || !data.summary) return cb();
data.id = format(pi.config.activeNext); // stub in an id based on the date
data.at = pi.config.activeNext; // also fill in
var next = pi.config.activeNext + (3600*1000*24); // next run get next day
if(next < (new Date(pi.config.lastSyncTime).getTime())){
pi.config.activeNext = next; // don't move forward past last sync time!
if(pi.config.activeNext < Date.now()) pi.config.nextRun = -1; // force run again
}
var base = 'sleep:'+pi.auth.pid+'/sleep';
var ret = {};
ret[base] = [data];
cb(null, {config:pi.config, data:ret});
});
function format(epoch)
{
d = new Date(epoch);
return ""+d.getFullYear()+'-'+((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1))+'-'+((d.getDate() < 10 ? '0' : '') + d.getDate());
}
exports.sync = require('./lib').dailySync('sleep', 'summary', 'sleep', 'sleep');
@@ -7,29 +7,5 @@
*
*/
exports.sync = require('./lib').genericSync(function(pi){
if(!pi.config) pi.config = {};
if(!pi.config.memberSince || !pi.config.lastSyncTime) return false;
if(!pi.config.activeNext) pi.config.activeNext = (new Date(pi.config.memberSince).getTime()); // convert to epoch format
if((pi.config.activeNext > new Date(pi.config.lastSyncTime).getTime())) return false; // don't run ahead of last sync
return 'body/log/weight/date/'+format(pi.config.activeNext)+'.json';
}, function(pi, data, cb){
if(!data || !data.weight) return cb();
data.id = format(pi.config.activeNext); // stub in an id based on the date
data.at = pi.config.activeNext; // also fill in
var next = pi.config.activeNext + (3600*1000*24); // next run get next day
if(next < (new Date(pi.config.lastSyncTime).getTime())){
pi.config.activeNext = next; // don't move forward past last sync time!
if(pi.config.activeNext < Date.now()) pi.config.nextRun = -1; // force run again
}
var base = 'weight:'+pi.auth.pid+'/weight';
var ret = {};
ret[base] = [data];
cb(null, {config:pi.config, data:ret});
});
function format(epoch)
{
d = new Date(epoch);
return ""+d.getFullYear()+'-'+((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1))+'-'+((d.getDate() < 10 ? '0' : '') + d.getDate());
}
exports.sync =
require('./lib').dailySync('body/log/weight', 'weight', 'weight', 'weight');
ProTip! Use n and p to navigate between commits in a pull request.