-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMMM-fitbit.js
More file actions
198 lines (170 loc) · 4.65 KB
/
MMM-fitbit.js
File metadata and controls
198 lines (170 loc) · 4.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* global Module */
/* Magic Mirror
* Module: MMM-fitbit
*
* By Sam Vendittelli
* MIT Licensed.
*/
Module.register('MMM-fitbit',{
userData: {
steps: 0,
floors: 0,
caloriesOut: 0,
distance: 0,
activeMinutes: 0,
sleep: 0,
heart: 0
},
goals: {
steps: 10000,
floors: 10,
caloriesOut: 2000,
distance: 5,
activeMinutes: 30,
sleep: 480,
heart: 0
},
// Default module config.
defaults: {
credentials: {
client_id: '',
client_secret: ''
},
resources: [
'steps',
'floors',
'caloriesOut',
'distance',
'activeMinutes',
'sleep',
'heart'
],
update_interval: 60
},
// Define required scripts.
getStyles: function() {
return ["MMM-fitbit.css"];
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "DATA"){
resource = payload['resource'];
if (this.inResources(resource)) {
this.userData[resource] = payload['values']['data'];
this.goals[resource] = payload['values']['goal'];
Log.log("Writing " + resource + " (data/goal): " + this.userData[resource] + "/" + this.goals[resource]);
}
}
if (notification === "UPDATE") {
Log.log('Updating Dom');
this.updateDom(this.fadeSpeed);
}
},
// Initialisation
start: function() {
Log.info('Starting module: ' + this.name);
this.sendSocketNotification('SET CREDS',this.config.credentials)
this.sendSocketNotification('GET DATA', 'intial');
this.fadeSpeed = 500;
// Schedule update interval.
var self = this;
setInterval(function() {
self.updateData();
}, this.config.update_interval*60*1000);
},
// Updates the data from fitbit
updateData: function() {
this.sendSocketNotification('GET DATA', 'Update');
},
// Checks whether the user wants to lookup a resourse type
inResources: function(resource) {
return this.config.resources.indexOf(resource) > -1;
},
// To add commas to the step and calorie count
numberWithCommas: function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
// Converts minutes into HH:MM
minsToHourMin: function(number) {
hours = Math.floor(number / 60);
minutes = number % 60;
return ("00" + hours.toString()).slice(-2) + ":" + ("00" + minutes.toString()).slice(-2);
},
// WIdth of the progress bar
progressBar: function(resource) {
if (this.userData[resource] >= this.goals[resource]) {
return 100;
} else {
return Math.round(Number(this.userData[resource]) / this.goals[resource] * 100)
}
},
// Make each resource element for the UI
UIElement: function(resource) {
iconPath = '/img/' + resource + 'White.png';
// Create wrappers
var wrapper = document.createElement("div");
var icon = document.createElement("img");
var text = document.createElement("div");
var userData = document.createElement("div")
var suffix = document.createElement("div");
var progress = document.createElement("div");
var bar = document.createElement("div");
// Icon
icon.className = 'fitbiticon';
icon.src = 'modules/' + this.name + iconPath;
// Text to display
userData.className = 'normal medium';
suffix.className = "dimmed small"
if (resource == 'steps' || resource == 'caloriesOut') {
userData.innerHTML = this.numberWithCommas(this.userData[resource]);
} else if (resource == 'sleep') {
userData.innerHTML = this.minsToHourMin(this.userData[resource]);
} else {
userData.innerHTML = this.userData[resource];
};
switch(resource) {
case 'distance':
suffix.innerHTML = 'mi';
break;
case 'activeMinutes':
suffix.innerHTML = 'mins';
break;
case 'heart':
suffix.innerHTML = 'bpm';
break;
default:
suffix.innerHTML = '';
}
// Make text on the same line
userData.style.display = 'inline-block';
suffix.style.display = 'inline-block';
// Progress bar
progress.className = 'progbarbkg';
bar.className = 'progbar';
bar.style.width = this.progressBar(resource) + '%';
if (resource !== 'heart') {
progress.appendChild(bar);
}
// Put them all together
wrapper.appendChild(icon);
text.appendChild(userData);
if (['distance','activeMinutes','heart'].indexOf(resource) > -1) {
text.appendChild(suffix);
}
wrapper.appendChild(text);
wrapper.appendChild(progress);
wrapper.style.display = 'inline-block';
wrapper.style.paddingLeft = '5px';
wrapper.style.paddingRight = '5px';
return wrapper;
},
// Override dom generator.
getDom: function() {
// Create Wrappers
var wrapper = document.createElement("div");
for (resource in this.config.resources) {
wrapper.appendChild(this.UIElement(this.config.resources[resource]));
}
return wrapper;
},
});