Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
144 lines (111 loc) · 3.74 KB
/
Copy pathnode_helper.js
File metadata and controls
144 lines (111 loc) · 3.74 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Magic Mirror
* Node Helper: MMM-octoprint
*
* 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,
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) {
return;
}
const job_status = await this.fetchPrinterJob();
let thumbnail = null;
let layer_information = null;
if (this.config.showThumbnail) {
thumbnail = await this.fetchThumbnail(job_status);
}
if (this.config.showLayerProgress) {
layer_information = await this.fetchLayerInformation();
}
const eta = moment.utc(1000 * (job_status.progress.printTimeLeft)).format('HH[h] mm[m] ss[s]');
this.sendSocketNotification("PRINTER_STATUS", { printer_status, job_status, eta, layer_information, thumbnail });
this.fetchTimerId = setTimeout(async function () {
await self.fetchData();
}, this.config.updateInterval);
},
async fetchPrinterStatus() {
const endpoint = this.config.endpoint + "/api/printer";
try {
const response = await fetch(endpoint, { headers: this.getHeaders() });
const json = await response.json();
return json;
} catch (error) {
Log.error(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
async fetchPrinterJob() {
const endpoint = this.config.endpoint + "/api/job";
try {
const response = await fetch(endpoint, { headers: this.getHeaders() });
const json = await response.json();
return json;
} catch (error) {
Log.error(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
async fetchLayerInformation() {
const endpoint = this.config.endpoint + "/plugin/DisplayLayerProgress/values";
try {
const response = await fetch(endpoint, { headers: this.getHeaders() });
const text = await response.text();
// Überprüfen, ob die Antwort gültiges JSON ist
if (response.headers.get("content-type").includes("application/json")) {
const json = JSON.parse(text);
return json;
} else {
Log.error(`${this.name} received an error: Couldn't fetch layer information. Maybe the DisplayLayerProgress plugin is not installed or activated?`);
return null;
}
} catch (error) {
Log.error(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
async fetchThumbnail(job_status) {
if (!job_status.job.file.name) {
return;
}
const endpoint = this.config.endpoint + "/api/files/" + job_status.job.file.origin + "/" + job_status.job.file.name;
try {
const response = await fetch(endpoint, { headers: this.getHeaders() });
const json = await response.json();
if (!json.thumbnail) {
json.thumbnail = "./modules/MMM-octoprint/img/no_thumbnail.png";
} else {
json.thumbnail = this.config.endpoint + "/" + json.thumbnail;
}
return json.thumbnail;
} catch (error) {
Log.error(`${this.name} received an error: ${error}`);
this.sendSocketNotification("HTTP_ERROR", {});
return null;
}
},
getHeaders() {
return {
"Authorization": `Bearer ${this.config.apiKey}`
};
},
});