-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-AACPSBus.js
103 lines (87 loc) · 2.74 KB
/
MMM-AACPSBus.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
/* Magic Mirror
* Module: AACPS Bus
*
* By James Wood
*/
var busData = "";
Module.register("MMM-AACPSBus", {
defaults: {
url: "", // The web page URL to check
bus: "", // The string to look for
image: "", // The image to display if the string is found
updateInterval: 60 * 1000 // How often to check the web page (in milliseconds)
},
// Define the start function to initialize the module
start: function () {
// Log the module start
Log.info(`Starting module: ${this.name}`);
// Set the initial value of the boolean flag
this.found = false;
// Schedule the first update
this.scheduleUpdate();
},
// Define the getDom function to create the module display
getDom: function () {
// Create a wrapper element
var wrapper = document.createElement("div");
// If the boolean flag is true, display the image
if (this.found) {
// Create an image element
var image = document.createElement("img");
// Set the image source and style
image.src = this.config.image;
image.style.width = "50%";
image.style.height = "auto";
var message = document.createElement("div");
message.innerHTML = busData;
message.id = 'busdata';
// Append the image to the wrapper
wrapper.appendChild(message);
wrapper.appendChild(image);
}
// Return the wrapper
return wrapper;
},
// Define the update function to check the web page
update: function () {
// Log the update
Log.info(this.name + " is updating");
this.sendSocketNotification("AACPS_REQUEST", this.config);
},
// Define the scheduleUpdate function to set a timer for the next update
scheduleUpdate: function () {
// Log the schedule
Log.info(`${this.name} is scheduling update`);
// Set a timeout for the update function
setTimeout(() => {
this.update();
// Schedule the next update
this.scheduleUpdate();
}, this.config.updateInterval);
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
var re = new RegExp ('\\[\'' + this.config.bus + '.+?(?= \'<a)', 'm');
if (notification === "AACPS_RESPONSE") {
// The node helper sent the website string
if (payload.includes(`['${this.config.bus}'`)) {
this.found = true;
const data = payload.match(re)[0].match(/'[^']*'/g);
busData = `Bus ${data[0].replace('[','')} is not running </br>at Schools ${data[2]} </br>`
if (data[1] !== "''"){
busData = busData + `Backup Bus: ${data[1]} </br>`;
}
busData = busData + `in ${data[3]}`;
} else {
this.found = false;
busData = '';
}
// Update the dom
this.updateDom();
} else if (notification === "AACPS_ERROR") {
// The node helper sent an error message
// Log it to the console
Log.error(this.name + ": " + payload);
}
},
});