Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates to fitbit connector. added fitbit battery level notifier util…

…iy. :)
  • Loading branch information...
commit 6c726dccf4e7fb2f9e051a73f6eff37c352ff467 1 parent 98f1e14
@smurthas smurthas authored
View
126 Apps/FitBitBatteryNotifier/app.js
@@ -0,0 +1,126 @@
+var fs = require('fs');
+var express = require('express');
+var connect = require('connect');
+
+var request = require('request');
+var locker = require('locker');
+
+var twClient;
+
+var app = express.createServer(connect.bodyParser());
+
+var info = {};
+
+app.get('/', function(req, res) {
+ res.writeHead(200, {'Content-Type':'text/html'});
+
+ res.write('Current Battery Level: ');
+
+ res.write('My Phone Number:<br><form method="POST" action="updatePhoneNumber">');
+ res.write('Phone #: <input name="number"');
+ if(info.phoneNumber) res.write(' value="' + info.phoneNumber + '"');
+ res.write('><br>');
+ res.write('<input type="submit">');
+ res.write('</form>');
+
+ res.write('<br>');
+ if(!info.twilio) {
+ res.write('Twilio:<br><form method="POST" action="updateTwilio">');
+ res.write('Account SID: <input name="sid"><br>');
+ res.write('Auth Token: <input name="token"><br>');
+ res.write('SMS URL: <input name="url"><br>');
+ res.write('Twilio CallerID Number: <input name="number"><br>');
+ res.write('<input type="submit">');
+ res.write('</form>');
+ }
+
+ res.end();
+});
+
+app.post('/updateTwilio', function(req, res) {
+ info.twilio = {};
+ info.twilio.sid = req.body.sid;
+ info.twilio.token = req.body.token;
+ info.twilio.url = req.body.url;
+ info.twilio.number = req.body.number;
+ writeInfo();
+ res.redirect('/');
+});
+
+app.post('/updatePhoneNumber', function(req, res) {
+ info.phoneNumber = req.body.number;
+ writeInfo();
+ res.redirect('/');
+});
+
+app.get('/updateDevices', function(req, res) {
+ if(!info.devices)
+ info.devices = {};
+ locker.providers('device/fitbit', function(err, providers) {
+ for(var i in providers) {
+ if(!info.devices[providers[i].id])
+ info.devices[providers[i].id] = {};
+ var providerInfo = info.devices[providers[i].id];
+ request.get({url:providers[i].uri + 'getDevices'}, function(err, resp, body) {
+ var json = JSON.parse(body);
+ for(var j in json) {
+ var device = json[j];
+ if(providerInfo[device.id]) { //already seen device
+ if(providerInfo[device.id].battery !== device.battery) {
+ sendNotification("Battery level changed to \"" + device.battery +
+ "\" for FitBit device with ID " + device.id);
+ }
+ } else {
+ //added device
+ sendNotification("Added FitBit device with ID " + device.id);
+ }
+ providerInfo[device.id] = device;
+ writeInfo();
+ }
+ });
+ }
+ });
+
+ locker.at('/updateDevices', 3600);
+ res.writeHead(200);
+ res.end();
+});
+
+
+function sendNotification(message) {
+ if(!twClient) {
+ var TwilioClient = require('twilio').Client;
+ twClient = new TwilioClient(info.twilio.sid, info.twilio.token, info.twilio.url);
+ }
+ twClient.sendSms(info.twilio.number, info.phoneNumber, message, null, function(body) {
+ }, function(body) {
+ console.error('err sending SMS via Twilio: ', body);
+ });
+}
+
+var stdin = process.openStdin();
+stdin.setEncoding('utf8');
+stdin.on('data', function (chunk) {
+ processInfo = JSON.parse(chunk);
+ locker.initClient(processInfo);
+ process.chdir(processInfo.workingDirectory);
+ readInfo();
+ app.listen(processInfo.port,function() {
+ var returnedInfo = {port: processInfo.port};
+ process.stdout.write(JSON.stringify(returnedInfo));
+ });
+});
+stdin.resume();
+
+
+function readInfo() {
+ try {
+ info = JSON.parse(fs.readFileSync('info.json', null, 4));
+ } catch(err) {
+
+ }
+}
+
+function writeInfo() {
+ fs.writeFileSync('info.json', JSON.stringify(info));
+}
View
7 Apps/FitBitBatteryNotifier/fitbitbatterynotifier.app
@@ -0,0 +1,7 @@
+{
+ "title":"FitBit Battery Notifier",
+ "desc":"Get notified by email when your FitBit Device battery drops to \"Low\".",
+ "status":"unstable",
+ "run":"node app.js",
+ "handle":"fitbitbatterynotifier"
+}
View
3  Apps/FitBitBatteryNotifier/package.json
@@ -0,0 +1,3 @@
+{
+
+}
View
2  Common/node/connector/client.js
@@ -59,7 +59,7 @@ exports.init = function (processOptions, callback) {
function startWebServer() {
started = true;
// Start the core web server
- if(callback) callback(app);
+ if(callback) callback(app, mongo);
app.listen(0, function() {
// Tell the locker core that we're done
var returnedInfo = {port: app.address().port};
View
26 Connectors/FitBit/api.js
@@ -0,0 +1,26 @@
+/*
+*
+* Copyright (C) 2011, The Locker Project
+* All rights reserved.
+*
+* Please see the LICENSE file for more information.
+*
+*/
+
+var dataStore = require('connector/dataStore');
+
+exports.init = function(mongo) {
+ dataStore.init('id', mongo);
+}
+
+exports.getDevices = function(req, res) {
+ dataStore.getAllCurrent('devices', function(err, array) {
+ if(err || !array) {
+ res.writeHead(500)
+ res.end(JSON.stringify(err));
+ } else {
+ res.writeHead(200, 'application/json');
+ res.end(JSON.stringify(array));
+ }
+ });
+}
View
3  Connectors/FitBit/fitbit.connector
@@ -5,5 +5,6 @@
"run":"node init.js",
"status":"unstable",
"mongoCollections": ["sleep", "devices", "profile"],
- "handle":"fitbit"
+ "handle":"fitbit",
+ "provides":["device/fitbit"]
}
View
15 Connectors/FitBit/init.js
@@ -1,3 +1,16 @@
+/*
+*
+* Copyright (C) 2011, The Locker Project
+* All rights reserved.
+*
+* Please see the LICENSE file for more information.
+*
+*/
+
// this is a common pattern many connectors use, it processes the startup data and eventfully loads the auth.js, sync-api.js, etc
require.paths.push(__dirname);
-require('connector/client').init({"enableCookies":true});
+require('connector/client').init({"enableCookies":true}, function(app, mongo) {
+ var api = require('./api');
+ api.init(mongo);
+ app.get('/getDevices', api.getDevices);
+});
View
13 Connectors/FitBit/migrations/1311006153638.js
@@ -0,0 +1,13 @@
+module.exports = function(dir) {
+ process.chdir(dir);
+ var path = require('path');
+ var fs = require('fs');
+
+ var me = JSON.parse(fs.readFileSync('me.json'));
+
+ me.provides = ["device/fitbit"];
+
+ fs.writeFileSync('me.json', JSON.stringify(me, null, 4));
+
+ return true;
+};
View
4 Connectors/FitBit/sync.js
@@ -81,7 +81,7 @@ function updateDevices(callback) {
makeApiCall('/user/-/devices.json', {}, function(err, resp) {
if(err) {
console.error(err);
- callback(err, resp);
+ callback(err, 3600, resp);
} else {
for(var i in resp) {
var device = resp[i];
@@ -95,7 +95,7 @@ function updateDevices(callback) {
}
});
}
- callback(null, resp);
+ callback(null, 3600, resp);
}
});
}
View
3  Connectors/skeleton/sync-api.js
@@ -8,7 +8,8 @@
*/
var locker = require('../../Common/node/locker.js');
-
+var sync = require('./sync.js');
+
var app, auth;
module.exports = function(theapp) {
Please sign in to comment.
Something went wrong with that request. Please try again.