forked from SVendittelli/MMM-fitbit
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MMM-Fitbit2.js
301 lines (253 loc) · 7.23 KB
/
MMM-Fitbit2.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* global Module */
/* Magic Mirror
* Module: MMM-Fitbit2
*
* Forked from MMM-fitbit by Sam Vendittelli
* MMM-Fitbit2 modifications by Mike Roberts
* MIT Licensed.
*/
Module.register("MMM-Fitbit2", {
// Initial values
userData: {
steps: {
value: 0,
goal: 10000,
unit: "steps"
},
caloriesOut: {
value: 0,
goal: 2000,
unit: "cals"
},
distance: {
value: 0,
goal: 5,
unit: "km"
},
activeMinutes: {
value: 0,
goal: 30,
unit: "mins"
},
floors: {
value: 0,
goal: 10,
unit: "floors"
},
restingHeart: {
value: 0,
goal: 0,
unit: "bpm"
},
water: {
value: 0,
goal: 2000,
unit: "ml"
},
caloriesIn: {
value: 0,
goal: 2000,
unit: "cals"
},
sleep: {
value: 0,
goal: 480,
unit: "" // Formatted as HH:MM - no explicit unit
},
weight: {
value: 0,
goal: 0,
unit: "kg"
}
},
// Default module config.
defaults: {
credentials: {
clientId: "",
clientSecret: ""
},
resources: [
"steps",
"caloriesOut",
"distance",
"activeMinutes",
"floors",
"restingHeart",
"water",
"caloriesIn",
"sleep",
"weight"
],
debug: false,
test: false,
showLastSynced: false,
updateInterval: 10
},
// Define required scripts.
getStyles: function() {
return ["MMM-Fitbit2.css"];
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "API_DATA_RECEIVED") {
if (payload.clientId != this.config.credentials.clientId) {
// Not for this user
return
}
resource = payload.resource;
if (this.inResources(resource)) {
this.userData[resource]["value"] = payload.values.data;
this.userData[resource]["goal"] = payload.values.goal;
Log.log("Writing " + resource + " (data/goal): " + this.userData[resource]["value"] + "/" + this.userData[resource]["goal"]);
}
}
if (notification === "UPDATE_VIEW") {
Log.log("Updating DOM");
this.loaded = true;
this.updateDom(this.fadeSpeed);
}
},
getData: function(trigger) {
payload = {};
payload.config = this.config;
payload.trigger = trigger;
this.sendSocketNotification("GET DATA", payload);
},
// Initialisation
start: function() {
Log.info("Starting module: " + this.name);
this.getData("Initial");
this.loaded = false;
this.fadeSpeed = 500;
// Schedule update interval.
var self = this;
setInterval(function() {
self.updateData();
}, this.config.updateInterval*60*1000);
},
// Updates the data from fitbit
updateData: function() {
this.getData("Update");
},
// Checks whether the user wants to lookup a resource type
inResources: function(resource) {
return this.config.resources.indexOf(resource) > -1;
},
// Generate div for icon
iconDiv: function(resource) {
const iconPath = "/img/" + resource + ".png";
var iconDiv = document.createElement("img");
iconDiv.className = "widgeticon";
iconDiv.src = "modules/" + this.name + iconPath;
return iconDiv
},
// Add commas to step and calorie count
formatNumberWithCommas: function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
// Converts minutes into HH:MM
formatMinsToHourMin: function(number) {
hours = Math.floor(number / 60);
minutes = number % 60;
return ("00" + hours.toString()).slice(-2) + ":" + ("00" + minutes.toString()).slice(-2);
},
// Generate div for first part of text div
userDataValueDiv: function(resource) {
var userDataValueDiv = document.createElement("div");
userDataValueDiv.className = "normal medium";
if (["steps", "caloriesOut", "caloriesIn"].indexOf(resource) > -1) {
userDataValueDiv.innerHTML = this.formatNumberWithCommas(this.userData[resource]["value"]);
} else if (resource == "sleep") {
userDataValueDiv.innerHTML = this.formatMinsToHourMin(this.userData[resource]["value"]);
} else {
userDataValueDiv.innerHTML = this.userData[resource]["value"];
}
return userDataValueDiv;
},
// Generate div for second part of text div
userDataUnitDiv: function(resource) {
var userDataMeasurementUnit = document.createElement("div");
userDataMeasurementUnit.className = "dimmed small";
userDataMeasurementUnit.innerHTML = this.userData[resource]["unit"];
return userDataMeasurementUnit;
},
// Generate div for text (data + unit)
textDiv: function(resource) {
var textDiv = document.createElement("div");
textDiv.className = "widgettext";
textDiv.appendChild(this.userDataValueDiv(resource));
textDiv.appendChild(this.userDataUnitDiv(resource));
return textDiv
},
// Generate div for progress (grey background line and white overlay)
progressBarDiv: function(resource) {
// Start with background
var progressBarMasterDiv = document.createElement("div");
progressBarMasterDiv.className = "widgetprogbarbkg";
// Overlay actual progress
var progressBarChildDiv = document.createElement("div");
progressBarChildDiv.className = "widgetprogbar";
var width;
const exceededGoal = this.userData[resource]["value"] >= this.userData[resource]["goal"];
if (exceededGoal) {
width = 100;
} else {
width = Math.round(
Number(this.userData[resource]["value"]) / this.userData[resource]["goal"] * 100
)
}
progressBarChildDiv.style.width = width + "%";
progressBarMasterDiv.appendChild(progressBarChildDiv);
return progressBarMasterDiv;
},
// Make each resource element for the UI
UIElement: function(resource) {
var widgetDiv = document.createElement("div");
widgetDiv.className = "widget"
widgetDiv.appendChild(this.iconDiv(resource));
widgetDiv.appendChild(this.textDiv(resource));
widgetDiv.appendChild(this.progressBarDiv(resource));
return widgetDiv;
},
LastSyncedElement: function(resource) {
var lastSyncedWrapperDiv = document.createElement("div");
lastSyncedWrapperDiv.className = "lastsynced";
var textBottomWrapperDiv = document.createElement("div");
textBottomWrapperDiv.className = "textbottomwrapper";
var currentDate = new Date();
var date = ('0' + currentDate.getDate()).slice(-2) + "/"
+ ('0' + (currentDate.getMonth()+1)).slice(-2) + "/"
+ currentDate.getFullYear();
var lastSyncedDateDiv = document.createElement("div");
lastSyncedDateDiv.innerHTML = date;
lastSyncedDateDiv.className = "dimmed light xsmall textbottom";
var time = ('0' + currentDate.getHours()).slice(-2) + ":"
+ ('0' + currentDate.getMinutes()).slice(-2) + ":"
+ ('0' + currentDate.getSeconds()).slice(-2);
var lastSyncedTimeDiv = document.createElement("div");
lastSyncedTimeDiv.innerHTML = time;
lastSyncedTimeDiv.className = "dimmed light xsmall textbottom";
textBottomWrapperDiv.appendChild(lastSyncedDateDiv);
textBottomWrapperDiv.appendChild(lastSyncedTimeDiv);
lastSyncedWrapperDiv.appendChild(textBottomWrapperDiv);
return lastSyncedWrapperDiv;
},
// Override DOM generator
getDom: function() {
var wrapper = document.createElement("div");
if (this.loaded) {
wrapper.className = "wrapper"
for (resource in this.config.resources) {
wrapper.appendChild(this.UIElement(this.config.resources[resource]));
}
if (this.config.showLastSynced) {
wrapper.appendChild(this.LastSyncedElement());
}
}
else {
wrapper.innerHTML = "Loading...";
wrapper.className = "dimmed light small";
}
return wrapper;
},
});