forked from tderflinger/MMM-GConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
52 lines (49 loc) · 1.81 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const { GarminConnect } = require("garmin-connect");
const getLastActivityDate = (activities) => {
const lastActivity = activities[0];
const lastStartTimeLocal = lastActivity.startTimeLocal;
return lastStartTimeLocal;
};
const getDiffActivityDate = (activityDate) => {
const activityDateDate = new Date(activityDate);
const today = new Date();
const diff = Math.round((today - activityDateDate) / (1000 * 60 * 60 * 24));
return diff;
};
module.exports = NodeHelper.create({
start: function () {
console.info("MMM-GConnect started!");
},
socketNotificationReceived: async function (notification, payload) {
const self = this;
switch (notification) {
case "GET_GARMIN_DATA":
const GCClient = new GarminConnect();
await GCClient.login(payload.loginName, payload.password);
const activities = await GCClient.getActivities(0, 1);
const lastActivityDate = getLastActivityDate(activities);
const lastActivityDistance =
Math.round((activities[0].distance / 1000) * 100) / 100;
const lastActivityTime =
Math.round((activities[0].duration / 60) * 100) / 100;
const lastActivityAvgSpeed =
Math.round(activities[0].averageSpeed * 100) / 100;
const lastActivityAvgHR =
Math.round(activities[0].averageHR * 100) / 100;
const diff = getDiffActivityDate(lastActivityDate);
const activityType = activities[0].activityName;
self.sendSocketNotification("UPDATE_GARMIN_DATA", {
diff,
lastActivityDistance,
lastActivityTime,
lastActivityAvgSpeed,
lastActivityAvgHR,
activityType,
});
break;
default:
console.error("Switch item {} is missing", notification);
}
},
});