-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
105 lines (96 loc) · 3.21 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
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
const path = require("node:path");
const fs = require("node:fs");
const NodeHelper = require("node_helper");
const axios = require("axios");
const Log = require("logger");
module.exports = NodeHelper.create({
accessTokenData: {},
start () {
console.log(`Starting node_helper for: ${this.name}`);
},
async getAccessToken (payload) {
try {
let url = `${payload.tokenUrl}client_id=${payload.clientId}&client_secret=${payload.clientSecret}&refresh_token=${payload.refreshToken}&grant_type=refresh_token`;
await axios
.post(url)
.then((response) => {
try {
const filePath = path.join(__dirname, "..", "strava_access_token.json");
fs.writeFileSync(filePath, JSON.stringify(response.data));
} catch (error) {
this.xsendSocketNotification("LOG", `"MMM-StravaWeekInBike - Error writing to file strava_access_token.json: ${error}`);
}
this.accessTokenData = response.data;
});
} catch (error) {
this.sendSocketNotification("LOG", `"MMM-StravaWeekInBike - GetAccessToken error: ${error}`);
this.sendSocketNotification("ACCESS_TOKEN_ERROR", error);
}
},
processData (data) {
let totalDistance = 0;
let totalElevation = 0;
let totalMinutes = 0;
let numberOfRides = 0;
data.forEach((activity) => {
if (activity.type === "Ride") {
totalDistance += Math.floor(activity.distance * 0.000621371);
totalElevation += Math.floor(activity.total_elevation_gain * 3.28084);
totalMinutes += Math.floor((activity.moving_time / 60));
numberOfRides++;
}
});
return {
totalDistance: totalDistance,
totalElevation: totalElevation,
minutes: totalMinutes % 60,
hours: Math.floor(totalMinutes / 60),
totalMinutes: totalMinutes,
numberOfRides: numberOfRides
};
},
async getStravaStats (payload) {
const filePath = path.join(__dirname, "..", "strava_access_token.json");
let localAccessTokenData = {};
try {
if (fs.existsSync(filePath)) {
let localAccessTokenFileData = await fs.promises.readFile(filePath);
try {
localAccessTokenData = JSON.parse(localAccessTokenFileData);
if (localAccessTokenData.access_token && localAccessTokenData.expires_at < Math.floor(Date.now() / 1000)) {
this.accessTokenData = localAccessTokenData;
} else {
await this.getAccessToken({ ...payload, refreshToken: localAccessTokenData.refresh_token });
}
} catch (parseError) {
await this.getAccessToken(payload);
}
} else {
await this.getAccessToken(payload);
}
let url
= `${payload.url}athlete/activities?before=${payload.before}&after=${payload.after}`;
await axios
.get(url, {
headers: {
Authorization: `Bearer ${this.accessTokenData.access_token}`
}
})
.then((response) => {
const processedData = this.processData(response.data);
return processedData;
})
.then((data) => {
this.sendSocketNotification("STRAVA_STATS_RESULT", data);
});
} catch (error) {
this.sendSocketNotification("LOG", `"MMM-StravaWeekInBike - Node helper getStravaStats - Error fetching data from API: ${error}`);
return null;
}
},
socketNotificationReceived (notification, payload) {
if (notification === "GET_STRAVA_STATS") {
this.getStravaStats(payload);
}
}
});