-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
75 lines (68 loc) · 2.54 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
var NodeHelper = require("node_helper");
var fetch = require("node-fetch");
var fs = require("fs");
var path = require("path");
module.exports = NodeHelper.create({
start: function() {
console.log("MMM-FearAndGreedIndex helper started...");
this.scheduleUpdate();
this.scheduleCryptoUpdate();
},
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
console.log("[MMM-FearAndGreedIndex] Configuration received.", this.config);
this.scheduleUpdate(); // Update traditional index
this.scheduleCryptoUpdate(); // Update crypto index
}
},
scheduleUpdate: function() {
var self = this;
self.sendIndexData();
setInterval(function() {
self.sendIndexData();
}, this.config.updateInterval);
},
sendIndexData: function() {
var filepath = path.resolve(__dirname, this.config.filename);
console.log("Attempting to send INDEX_DATA from:", filepath);
if (fs.existsSync(filepath)) {
fs.readFile(filepath, (err, data) => {
if (err) {
console.error("Error reading data.json:", err);
return;
}
console.log("Sending INDEX_DATA:", JSON.parse(data));
this.sendSocketNotification("INDEX_DATA", JSON.parse(data));
});
} else {
console.log("data.json not found at:", filepath);
}
},
scheduleCryptoUpdate: function() {
var self = this;
self.fetchCryptoIndex();
setInterval(function() {
self.fetchCryptoIndex();
}, this.config.updateInterval);
},
fetchCryptoIndex: function() {
var self = this;
console.log("Attempting to fetch crypto index data.");
var url = "https://api.alternative.me/fng/";
fetch(url)
.then(response => response.json())
.then(data => {
if (data && data.data && data.data.length > 0) {
var indexData = data.data[0];
console.log("Sending CRYPTO_INDEX_DATA:", indexData);
self.sendSocketNotification("CRYPTO_INDEX_DATA", indexData);
} else {
console.error("Unexpected crypto index data format:", data);
}
})
.catch(error => {
console.error("Error fetching crypto index:", error);
});
},
});