-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-StravaWeekInBike.js
114 lines (102 loc) · 2.65 KB
/
MMM-StravaWeekInBike.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
/* MagicMirror²
* Module: MMM-StravaWeekInBike
*
* By Tyler Stambaugh
*/
Module.register("MMM-StravaWeekInBike", {
baseUrl: "https://www.strava.com/api/v3/",
tokenUrl: "https://www.strava.com/oauth/token?",
accessTokenError: {},
stravaStats: {
numberOfRides: 0,
totalDistance: 0,
totalMinutes: 0,
minutes: 0,
hours: 0,
totalElevation: 0
},
// Module config defaults.
defaults: {
clientId: "",
clientSecret: "",
refreshToken: "",
header: "Strava Week in Bike",
numberOfDaysToQuery: 7,
maxWidth: "250px",
initialLoadDelay: 4250,
retryDelay: 2500,
updateInterval: 60 * 15 * 1000,
loading: true
},
init () {
this.stravaStats = {};
},
getHeader () {
return this.config.header || "Strava Week In Bike";
},
start () {
Log.info(`Starting module: ${this.name}`);
this.stravaStats = {};
this.scheduleUpdate();
},
scheduleUpdate () {
setInterval(() => {
this.getStravaStats();
}, this.config.updateInterval);
this.getStravaStats(this.config.initialLoadDelay);
var self = this;
},
notificationReceived () {},
getStravaStats () {
let payload = {
url: this.baseUrl,
tokenUrl: this.tokenUrl,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
refreshToken: this.config.refreshToken,
numberOfDaysToQuery: this.config.numberOfDaysToQuery,
after: Math.floor(
new Date(Date.now() - this.config.numberOfDaysToQuery * 24 * 60 * 60 * 1000).getTime() / 1000
),
before: Math.floor(
new Date(Date.now() - 1 * 2 * 60 * 60 * 1000).getTime() / 1000
)
};
this.sendSocketNotification("GET_STRAVA_STATS", payload);
},
// this gets data from node_helper
socketNotificationReceived (notification, payload) {
if (notification === "LOG") {
Log.info("NodeHelper log:", payload);
}
if (notification === "ACCESS_TOKEN_ERROR") {
this.accessTokenError = payload;
this.updateDom();
}
if (notification === "STRAVA_STATS_RESULT") {
this.loading = true;
this.stravaStats = payload;
this.loading = false;
this.updateDom();
}
},
getStyles () {
return ["font-awesome.css", "MMM-StravaWeekInBike.css"];
},
getTemplate () {
return "MMM-StravaWeekInBike.njk";
},
getTemplateData () {
return {
numberOfDaysToQuery: this.config.numberOfDaysToQuery,
numberOfRides: this.stravaStats.numberOfRides,
distance: this.stravaStats.totalDistance,
totalTime: `${Math.floor(this.stravaStats.totalMinutes / 60)} hours ${this.stravaStats.totalMinutes % 60} minutes`,
minutes: this.stravaStats.minutes,
hours: this.stravaStats.hours,
elevation: this.stravaStats.totalElevation,
accessTokenError: this.accessTokenError,
loading: this.loading
};
}
});