-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
121 lines (93 loc) · 3.6 KB
/
Copy pathnode_helper.js
File metadata and controls
121 lines (93 loc) · 3.6 KB
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
/* Magic Mirror
* Node Helper: MMM-moonraker
*
* By Ben Konsemüller
* MIT Licensed.
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
const moment = require("moment");
module.exports = NodeHelper.create({
config: {},
currentFile: null,
metadata: null,
async socketNotificationReceived(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
if (this.fetchTimerId) {
clearTimeout(this.fetchTimerId);
}
await this.fetchData();
}
},
async fetchData() {
const self = this;
const printer_status = await this.fetchPrinterStatus();
if (!printer_status) {
this.fetchTimerId = setTimeout(async function () {
await self.fetchData();
}, this.config.updateInterval);
return;
}
if (this.currentFile !== printer_status.print_stats.filename) {
this.currentFile = printer_status.print_stats.filename;
this.metadata = await this.fetchMetadata(printer_status);
}
const thumbnail = this.metadata?.thumbnails[0].relative_path;
const eta = await this.calculateEta(printer_status, this.metadata);
this.sendSocketNotification("PRINTER_STATUS", { printer_status, metadata: this.metadata, eta, thumbnail });
this.fetchTimerId = setTimeout(async function () {
await self.fetchData();
}, this.config.updateInterval);
},
async fetchPrinterStatus() {
const endpoint = this.config.endpoint + "/printer/objects/query?print_stats&virtual_sdcard&extruder&heater_bed";
try {
const response = await fetch(endpoint);
const json = await response.json();
return json.result.status;
} catch (error) {
Log.log(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
async fetchMetadata(printer_status) {
if (!printer_status.print_stats.filename) {
return;
}
const endpoint = this.config.endpoint + "/server/files/metadata?filename=" + printer_status.print_stats.filename;
try {
const response = await fetch(endpoint);
const json = await response.json();
// join up the final thumbnail url for convenience
if (json.result.thumbnails) {
for (let index = 0; index < json.result.thumbnails.length; index++) {
const element = json.result.thumbnails[index];
element.relative_path = this.config.endpoint + "/server/files/gcodes/" + element.relative_path;
}
// sort thumbnails by width so the first one is the larger one
json.result.thumbnails.sort(function (a, b) { return b.width - a.width });
}
if (!json.result.thumbnails || json.result.thumbnails.length === 0) {
json.result.thumbnails = [{ relative_path: "./modules/MMM-moonraker/img/no_thumbnail.png" }];
}
return json.result;
} catch (error) {
Log.log(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
calculateEta(printer_status, metadata) {
if (printer_status.print_stats.state !== 'printing') {
return null;
}
if (metadata && metadata.estimated_time) {
const progressTime = printer_status.virtual_sdcard.progress * metadata.estimated_time;
return moment.utc(1000 * (metadata.estimated_time - progressTime)).format('HH[h] mm[m] ss[s]');;
}
const total_time = printer_status.print_stats.print_duration / printer_status.virtual_sdcard.progress;
return moment.utc(1000 * (total_time - printer_status.print_stats.print_duration)).format('HH[h] mm[m] ss[s]');
},
});