-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
138 lines (122 loc) · 3.88 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
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
const path = require("node:path");
const fs = require("node:fs");
const { Console } = require("node:console");
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}`);
setInterval(() => {
const memoryUsage = process.memoryUsage();
this.sendSocketNotification("LOG", `meory usage: ${JSON.stringify(memoryUsage)}`);
}, 10000);
},
async getAccessToken (payload) {
try {
const url = `${payload.tokenUrl}client_id=${payload.clientId}&client_secret=${payload.clientSecret}&refresh_token=${payload.refreshToken}&grant_type=refresh_token`;
const response = await axios.post(url);
const filePath = path.join(__dirname, "..", "strava_access_token.json");
try {
fs.writeFileSync(filePath, JSON.stringify(response.data));
} catch (error) {
this.sendSocketNotification(
"LOG",
`Error writing to file access_token.json: ${error}`
);
}
this.accessTokenData = response.data;
} catch (error) {
this.sendSocketNotification(
"LOG",
`Error fetching access token from API: ${error}`
);
this.sendSocketNotification("ACCESS_TOKEN_ERROR", error);
}
},
processData (data) {
let name,
activityDate,
distance,
minutes,
latitude,
longitude,
summaryPolyLine;
if (Array.isArray(data) && data.length > 0) {
this.sendSocketNotification("LOG", `response data count: ${data.length}`);
const activity = data[0];
const date = new Date(activity.start_date);
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
const day = String(date.getUTCDate()).padStart(2, "0");
const year = date.getUTCFullYear();
(name = activity.name), (activityDate = `${month}/${day}/${year}`);
distance = Math.floor(activity.distance * 0.000621371); // Convert meters to miles
minutes = Math.floor(activity.moving_time / 60);
latitude = activity.start_latlng[0];
longitude = activity.start_latlng[1];
summaryPolyLine = activity.map.summary_polyline;
}
return {
name,
activityDate,
distance,
minutes: minutes % 60,
hours: Math.floor(minutes / 60),
latitude,
longitude,
summaryPolyLine
};
},
async getStravaData (payload) {
const filePath = path.join(__dirname, "..", "strava_access_token.json");
try {
if (fs.existsSync(filePath)) {
const localAccessTokenFileData = await fs.promises.readFile(filePath);
const 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
});
}
} else {
await this.getAccessToken(payload);
}
const url = `${payload.url}athlete/activities?before=${payload.before}&after=${payload.after}`;
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${this.accessTokenData.access_token}`
}
});
const processedData = this.processData(response.data);
this.sendSocketNotification("STRAVA_DATA_RESULT", processedData);
} catch (error) {
if (error.response && error.response.status === 401) {
// Unauthorized error, refresh the access token and retry
this.sendSocketNotification(
"LOG",
"Access token expired, fetching new token."
);
await this.getAccessToken(payload);
// Retry fetching Strava data after refreshing token
await this.getStravaData(payload);
} else {
this.sendSocketNotification(
"LOG",
`Error fetching data from Strava API: ${error}`
);
}
}
},
socketNotificationReceived (notification, payload) {
if (notification === "GET_STRAVA_DATA") {
this.getStravaData(payload);
}
}
});