-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-GetShellScript.js
72 lines (57 loc) · 2.17 KB
/
MMM-GetShellScript.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
/**
* Magic Mirror Module: MMM-GetShellScript
* This module adds an API endpoint to your MagicMirror installation.
*/
Module.register("MMM-GetShellScript", {
defaults: {
route: "/night",
authToken: "change-this-token",
scriptPath: "modules/MMM-GetShellScript/scripts/example.sh",
requireAuth: true,
showLogs: true,
maxLogEntries: 10,
},
logs: [],
getDom: function() {
const wrapper = document.createElement("div");
if (!this.config.showLogs) return wrapper;
wrapper.className = "small";
if (this.logs.length === 0) {
wrapper.innerHTML = "No executions yet.";
return wrapper;
}
const table = document.createElement("table");
table.className = "small";
this.logs.forEach(log => {
const row = document.createElement("tr");
const timeCell = document.createElement("td");
timeCell.innerHTML = log.time;
timeCell.className = "dimmed";
row.appendChild(timeCell);
const statusCell = document.createElement("td");
statusCell.innerHTML = log.success ? "✓" : "✗";
statusCell.className = log.success ? "bright" : "dimmed";
row.appendChild(statusCell);
table.appendChild(row);
});
wrapper.appendChild(table);
return wrapper;
},
start: function() {
Log.info("Starting module: " + this.name);
this.sendSocketNotification("SETUP_ENDPOINT", this.config);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "SCRIPT_EXECUTED") {
this.logs.unshift({
time: new Date().toLocaleTimeString(),
success: payload.success
});
if (this.logs.length > this.config.maxLogEntries) {
this.logs = this.logs.slice(0, this.config.maxLogEntries);
}
this.updateDom();
this.sendNotification("SHELL_SCRIPT_EXECUTED", payload);
}
}
});