forked from Reportr/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
var _ = require("underscore"); | ||
var moment = require('moment'); | ||
var FitbitStrategy = require('passport-fitbit').Strategy; | ||
var Fitbit = require("fitbit-js"); | ||
|
||
function setup(options, imports, register) { | ||
var logger = imports.logger.namespace("tracker.fitbit"); | ||
var trackers = imports.trackers; | ||
var tasks = imports.tasks; | ||
|
||
var tracker = trackers.register({ | ||
'id': "fitbit", | ||
'name': "Fitbit", | ||
'description': "Track your running activity from Runkeeper.", | ||
|
||
'initConfig': function() { | ||
_.defaults(this.config, { | ||
'interval': 60*60 | ||
}); | ||
}, | ||
|
||
'initWeb': function() { | ||
this.oauth(FitbitStrategy, { | ||
'strategyOptions': { | ||
'passReqToCallback': true, | ||
'consumerKey': this.config.clientId, | ||
'consumerSecret': this.config.clientSecret, | ||
'callbackURL': "/auth/"+this.id+"/callback" | ||
} | ||
}); | ||
}, | ||
|
||
'initWorker': function() { | ||
var that = this; | ||
|
||
tasks.addTrackerTask(this, function(user) { | ||
var userConfig = user.getTrackerSettings(that.id); | ||
if (userConfig == null | ||
|| userConfig.accessToken == null | ||
|| userConfig.refreshToken == null) return; | ||
|
||
var client = new Fitbit(that.config.clientId, that.config.clientSecret); | ||
var clientMethod = function(method, url, callback) { | ||
client.apiCall(method, url, { | ||
'token': { | ||
'oauth_token_secret': userConfig.refreshToken, | ||
'oauth_token': userConfig.accessToken | ||
} | ||
}, callback); | ||
}; | ||
|
||
that.trackActivities(clientMethod, user, userConfig); | ||
}, this.config.interval); | ||
}, | ||
|
||
// Track user activities | ||
trackActivities: function(client, user, userConfig) { | ||
var that = this; | ||
|
||
// Define model | ||
user.setModel({ | ||
'eventNamespace': that.id, | ||
'eventName': 'distance', | ||
|
||
'name': "Fitbit Distance", | ||
'icon': '$fitbit', | ||
'description': "Distance calculate on Fitbit." | ||
}); | ||
|
||
client("GET", "/user/-/activities/distance/date/today/1m.json", function(err, res, json) { | ||
if (err) { | ||
logger.exception(err, false); | ||
return; | ||
} | ||
if (!json | ||
|| !json["activities-distance"]) { | ||
logger.error("Invalid data for fitbit activities", err, res, json); | ||
return; | ||
} | ||
|
||
_.each(json["activities-distance"], function(activity) { | ||
if (!activity.dateTime | ||
|| !activity.value) { | ||
logger.error("Invalid data for fitbit activities"); | ||
return; | ||
} | ||
|
||
var timestamp = moment(activity.dateTime).unix() * 1000; | ||
|
||
// Track as an event | ||
user.track({ | ||
'eventId': timestamp, | ||
'namespace': that.id, | ||
'name': 'distance', | ||
'properties': { | ||
'distance': activity.value | ||
}, | ||
'timestamp': timestamp | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
|
||
register(null, { | ||
'tracker.fitbit': tracker | ||
}); | ||
}; | ||
|
||
// Exports | ||
module.exports = setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "tracker.fitbit", | ||
"version": "0.0.1", | ||
|
||
"main": "./main.js", | ||
"private": true, | ||
|
||
"plugin": { | ||
"consumes": [ | ||
"logger", "trackers", "tasks" | ||
], | ||
"provides": [ | ||
"tracker.fitbit" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.