Skip to content

added Fitbit weight #134

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions fitbit/fitbit.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<select id="node-input-dataType" style="width:125px !important">
<option value="goals" data-i18n="fitbit.label.goals"></option>
<option value="sleep" data-i18n="fitbit.label.sleep"></option>
<option value="weight" data-i18n="fitbit.label.weight"></option>
<option value="badges" data-i18n="fitbit.label.badges"></option>
</select>
</div>
Expand All @@ -169,15 +170,19 @@
as follows:</p>
<dl>
<dt>goals<dt>
<dd>Messages are sent when a daily goal is reached. The message payload contains an achievement message and the message <b>data</b> property contains the current <a href="https://wiki.fitbit.com/display/API/API-Get-Activities"
<dd>Messages are sent when a daily goal is reached. The message payload contains an achievement message and the message <b>data</b> property contains the current <a href="https://dev.fitbit.com/docs/activity/#get-daily-activity-summary"
>activities</a> data.</dd>
<dt>sleep</dt>
<dd>Messages are sent when a new daily sleep record becomes available. The message payload contains <a
href="https://wiki.fitbit.com/display/API/API-Get-Sleep"
href="https://dev.fitbit.com/docs/sleep/#get-sleep-logs"
>sleep log</a> data.</dd>
<dt>Weight</dt>
<dd>Messages are sent when a new weight record becomes available. The message payload contains the latest entry in the days <a
href="https://dev.fitbit.com/docs/body/#get-weight-logs"
>weight log</a>.</dd>
<dt>badges</dt>
<dd>Messages are sent when a new badge is earned. The message payload contains the badge message and the message <b>data</b> property contains a single badge entry from the list returned by the <a
href="https://wiki.fitbit.com/display/API/API-Get-Badges"
href="https://dev.fitbit.com/docs/user/#get-badges"
>badges API call</a>.</dd>
</dl>
</script>
Expand Down Expand Up @@ -213,6 +218,7 @@
<select id="node-input-dataType" style="width:125px !important">
<option value="activities" data-i18n="fitbit.label.activities"></option>
<option value="sleep" data-i18n="fitbit.label.sleep"></option>
<option value="weight" data-i18n="fitbit.label.weight"></option>
<option value="badges" data-i18n="fitbit.label.badges"></option>
</select>
</div>
Expand All @@ -229,16 +235,21 @@
<dl>
<dt>activities<dt>
<dd>the message payload contains <a
href="https://wiki.fitbit.com/display/API/API-Get-Activities"
href="https://dev.fitbit.com/docs/activity/#get-daily-activity-summary"
>daily activities</a> data.</dd>
<dt>sleep</dt>
<dd>the message payload contains <a
href="https://wiki.fitbit.com/display/API/API-Get-Sleep"
href="https://dev.fitbit.com/docs/sleep/#get-sleep-logs"
>sleep log</a> data for the main sleep and the message
<b>data</b> property contains the complete sleep data result.</dd>
<dt>weight</dt>
<dd>the message payload contains the latest entry in the days <a
href="https://dev.fitbit.com/docs/body/#get-weight-logs"
>weight log</a> and the message
<b>data</b> property contains the complete days weight entry.</dd>
<dt>badges</dt>
<dd>the message payload contains data about <a
href="https://wiki.fitbit.com/display/API/API-Get-Badges"
href="https://dev.fitbit.com/docs/user/#get-badges"
>badges</a> awarded.</dd>
</dl>
<p>The <b>msg.date</b> property may be set to an ISO 8601 format
Expand Down
69 changes: 65 additions & 4 deletions fitbit/fitbit.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,55 @@ module.exports = function(RED) {
});
node.setInterval();
});
} else if (node.dataType === 'goals') {
} else if (node.dataType === 'weight') {
oa.get('https://api.fitbit.com/1/user/-/body/log/weight/date/'+day+'.json',
credentials.access_token,
credentials.access_token_secret,
function(err, body, response) {
if (err) {
node.error(formatError(err));
node.status({fill:"red",shape:"ring",text:"fitbit.status.failed"});
return;
}
var data = JSON.parse(body);
if( data.weight.length > 0){
node.state = {
logId: data.weight[data.weight.length-1].logId,
};
}
else {
node.state = {
logId: null,
};
}
node.status({});
node.on('input', function(msg) {
node.status({fill:"blue",shape:"dot",text:"fitbit.status.querying"});
var day = today();
oa.get('https://api.fitbit.com/1/user/-/body/log/weight/date/'+day+'.json',
credentials.access_token,
credentials.access_token_secret,
function(err, body, response) {
if (err) {
node.error(formatError(err),msg);
node.status({fill:"red",shape:"ring",text:"fitbit.status.failed"});
return;
}
var data = JSON.parse(body);
if( data.weight.length > 0){
if(data.weight[data.weight.length-1].logId != node.state.logId){
node.send({ payload: data.weight[data.weight.length-1] });
node.state = {
logId: data.weight[data.weight.length-1].logId,
};
}
}
node.status({});
});
});
node.setInterval();
});
}else if (node.dataType === 'goals') {
oa.get('https://api.fitbit.com/1/user/-/activities/date/'+day+'.json',
credentials.access_token,
credentials.access_token_secret,
Expand Down Expand Up @@ -271,7 +319,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.fitbitConfig = RED.nodes.getNode(n.fitbit);
this.dataType = n.dataType || 'activities';
var supportedTypes = ["activities","sleep","badges"];
var supportedTypes = ["activities","sleep","weight","badges"];
if (supportedTypes.indexOf(this.dataType) == -1) {
this.error(RED._("fitbit.error.unsupported-data-type"));
return;
Expand All @@ -281,8 +329,7 @@ module.exports = function(RED) {
return;
}
var credentials = this.fitbitConfig.credentials;
if (credentials &&
credentials.access_token && credentials.access_token_secret) {
if (credentials && credentials.access_token && credentials.access_token_secret) {
var oa = getOAuth(credentials.client_key,credentials.client_secret);
var node = this;
this.on('input', function(msg) {
Expand All @@ -295,6 +342,9 @@ module.exports = function(RED) {
node.dataType + '/date/' + day + '.json';
} else if (node.dataType === 'badges') {
url = 'https://api.fitbit.com/1/user/-/badges.json';
} else if (node.dataType === 'weight') {
var day = msg.date || today();
url = 'https://api.fitbit.com/1/user/-/body/log/weight/date/' + day + '.json';
}
oa.get(url,
credentials.access_token,
Expand All @@ -311,11 +361,22 @@ module.exports = function(RED) {
var sleep = mainSleep(data.sleep);
if (!sleep) {
node.warn(RED._("fitbit.warn.no-sleep-record"));
node.status({});
return;
} else {
msg.payload = sleep;
delete msg.error;
}
} else if (node.dataType === 'weight') {
if( data.weight.length > 0) {
msg.payload = data.weight.reverse();
msg.data = data.weight[data.weight.length-1];
delete msg.error;
} else {
node.warn(RED._("fitbit.warn.no-weight-record")+" "+day);
node.status({});
return;
}
} else {
msg.payload = data;
}
Expand Down
4 changes: 3 additions & 1 deletion fitbit/locales/en-US/fitbit.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "Type",
"goals": "goals",
"sleep": "sleep",
"weight": "weight",
"badges": "badges",
"activities": "activities",
"name": "Name",
Expand All @@ -22,7 +23,8 @@
},
"warn": {
"missing-credentials": "Missing fitbit credentials",
"no-sleep-record": "no main sleep record found"
"no-sleep-record": "No main sleep record found",
"no-weight-record": "No weight record returned for"
},
"error": {
"unsupported-data-type": "Unsupported data type",
Expand Down