-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
79 lines (72 loc) · 2.84 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
const NodeHelper = require("node_helper");
const Log = require("logger");
const qrcode = require("qrcode"); // Import the qrcode library
const uuidv4 = require("uuid").v4; // Import the uuid library
module.exports = NodeHelper.create({
start () {
Log.log("next-episode, Node Helper started!");
this.timer = null; // Declare a variable to hold the interval timer
},
stop () {
// Clear the interval when the module is stopped
if (this.timer !== null) {
clearInterval(this.timer);
this.timer = null;
}
},
socketNotificationReceived (notification, payload) {
Log.debug("next-episode, Received socket notification: ", notification, " with payload: ", payload);
if (notification === "GET_DATA") {
this.getData(payload);
// Clear existing timer if any
if (this.timer !== null) {
clearInterval(this.timer);
}
// Enforce minimum update interval of 180 minutes
const updateInterval = payload.updateInterval < 180
? 180
: payload.updateInterval;
// Set interval to fetch data as per the configuration
const self = this;
this.timer = setInterval(() => {
self.getData(payload);
}, updateInterval * 60 * 1000); // updateInterval in minutes
}
},
async getData (config) {
const self = this;
// Check if id or hash_key is empty
if (config.id === '' || config.hash_key === '') {
// Generate unique device ID
var deviceId = uuidv4();
// Create URL for QR code
var url = `https://next-episode.net/api/magicmirror/v1/services.php?service=link&device_id=${deviceId}&username=${config.username}&password=${config.password}`;
// Generate QR code
qrcode.toDataURL(url, (err, url) => {
if (err) {
Log.log("next-episode, Error generating QR Code: ", err);
self.sendSocketNotification("ERROR", "Error generating QR Code");
} else {
Log.log("next-episode, QR Code: ", url);
// Send the QR code URL to the frontend so it can be displayed
self.sendSocketNotification("QR_CODE", url);
}
});
} else {
const url = `https://next-episode.net/api/magicmirror/v1/services.php?service=next&user_id=${config.id}&hash_key=${config.hash_key}`;
Log.log("next-episode, Requesting data from: ", url);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Error fetching data from API.");
}
const body = await response.text();
Log.log("next-episode, Successfully received data from the API");
self.sendSocketNotification("DATA", body);
} catch (error) {
Log.error("next-episode, Error in getData: ", error);
self.sendSocketNotification("ERROR", "Error fetching data from API.");
}
}
}
});