-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOctoPrint.js
187 lines (171 loc) · 5.81 KB
/
OctoPrint.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const fetch = require('node-fetch');
const credentials = require("./credentials-cal.json")
const { exec } = require("child_process");
const { FormData } = require('formdata-node');
const fs = require('fs')
exports.OctoPrint = class OctoPrint {
constructor() {
this.link = `http://${credentials.octoIP}/`
this.ApiKey = credentials['X-Api-Key'];
this.options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': this.ApiKey
},
}
}
/**
* Prints a file in the local storage in 2Print folder.
* @param {String} entry the filename to print.
* @returns {Boolean} true if mission successful, false otherwise.
*/
print(entry) {
console.log("Printing");
if (!entry.includes(".gcode")) {
entry += ".gcode"
}
fetch(`${this.link}api/files/local/2Print/${entry}`, {
method: "POST",
headers: {
"X-Api-Key": this.ApiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
command: "select",
print: true
})
}).then(res => console.log(res))
//IF THE ABOVE IS NOT WORKING, UNCOMMENT THIS PART AND RUN ON LINUX ONLY
// exec(`curl -d '{"command": "select", "print": true}' -H "X-Api-Key: ${this.ApiKey}" -H "Content-Type: application/json" -X POST http://137.22.30.138/api/files/local/2Print/${entry}`, (error, stdout, stderr) => {
// if (error) {
// console.log(`error: ${error.message}`);
// return;
// }
// if (stderr) {
// console.log(`stderr: ${stderr}`);
// return;
// }
// console.log(`stdout: ${stdout}`);
// });
// editEntry("./confirmation.json", "phy.ysf1629297000000", "admin", true)
// // TO DO => remove file form local storage
// return true;
// return false;
}
/**
*
* @returns boolean true if the printer is ready to print, false otherwise.
*/
getPrinterStatus() {
return fetch(this.link + "/api/printer", this.options)
.then(res => {
return res.json()
}).then(resfinal => {
return !!resfinal.sd.ready && !(!!resfinal.state.flags.printing) && !!resfinal.state.flags.ready;
})
}
/**
* Uploads a file from the folder Gcodes to OctoPrint server.
* @param {String} file The string of the file name. Accepts both ending with .gcode or not.
* @param {Boolean} print Boolean Value to specify if true uploads and prints file if false uploads only, defaults to false.
*/
uploadFile(file, print = false) {
console.log({ filename: file });
if (!file.includes(".gcode")) {
file += ".gcode"
}
exec(`curl -k -H "X-Api-Key: ${this.ApiKey}" -F "print=${print}" -F "file=@${__dirname}/Gcodes/${file}" -F path="2Print" "http://137.22.30.138/api/files/local"`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
}
/**
* Returns a lisk of users with their permissions
*/
getUsersList() {
console.log(`${this.link}api/access/users`);
fetch(`${this.link}api/access/users`, this.options)
.then(res => {
return res.json()
}).then(resfinal => {
console.log(resfinal)
})
}
/**
*
* @param {String} filename The filename to delete inside local/2Print including/not including the .gcode at the end.
*/
deleteFile(filename) {
if (!file.includes(".gcode")) {
file += ".gcode"
}
fetch(`${this.link}api/files/local/2Print/${filename}`, {
headers: {
'X-Api-Key': this.ApiKey
},
method: "delete",
}).then(res => {
console.log(res)
})
}
getInfo(filename) {
return fetch(`${this.link}api/files/local/2Print/${filename}`, {
headers: {
'X-Api-Key': this.ApiKey
},
method: "GET",
}).then(res => {
return res.json();
}).then(res => {
console.log(res);
return {
name: res.display,
printDuration: res.gcodeAnalysis.estimatedPrintTime
}
})
}
/**
*
* @param {String} printer The printer name (Balin, Dwalin, Prusa, Taz4, Taz6)
* @param {String} command The String of the job command (start - cancel - resume - pause - restart)
*/
jobCommand(printer, command) {
let ip = credentials[printer];
fetch(`http://${ip}/api/job`, {
mathod: "POST",
headers: {
"X-Api-Key": this.ApiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
command: command,
action: command
})
}).then(res => res.json()).then(resfinal => {
console.log(resfinal);
})
}
/**
*
* @returns response body containing current state of printer
*/
getJob() {
return fetch(`http://${this.octoIP}/api/job`, {
mathod: "GET",
headers: {
"X-Api-Key": this.ApiKey,
"Content-Type": "application/json"
},
}).then(res => res.body.json()).then(resfinal => {
return resfinal
})
}
}