forked from emilecantin/octo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
221 lines (216 loc) · 10.2 KB
/
index.js
File metadata and controls
221 lines (216 loc) · 10.2 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var config = require('./config');
const dns = require('dns');
var http = require('http');
// --------------------------------------------------------------------------
// This is the section for helper function(s).
// --------------------------------------------------------------------------
// Returns the response from the printer, given the path
function queryPrinter(method, path, body, callback) {
if (config.apiKey == '') {
return callback('{"status": "Config error: No apiKey set in node_modules/octo-client/config.js file"}');
}
var options;
if (body) {
var postData = JSON.stringify(body);
options = {
hostname: config.hostName, port: config.port, path: path, method: method, timeout: 5000,
headers: {
'X-Api-Key': config.apiKey,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
}; // options = ...
} else {
options = {
hostname: config.hostName, port: config.port, path: path, method: method, timeout: 5000,
headers: {
'X-Api-Key': config.apiKey,
'Content-Type': 'application/json'
}
}; // options = ...
} // if (body)
dns.lookup(config.hostName, function(err, address) {
if (err) {callback('{"status": "' + err + '"}'); return;}
if (method == "GET") {
http.get(options, function(resp) {
resp.setEncoding('utf8');
var data = '';
resp.on('data', function(chunk) {data += chunk;});
resp.on('error', function(err) {console.log('queryPrinter(): ' + err);});
resp.on('end', function() {return callback(data);});
});
} else if (method == "POST") {
var req = http.request(options, function(resp) {
resp.setEncoding('utf8');
var data = '';
resp.on('data', function(chunk) {data += chunk;});
resp.on('end', function() {
if (resp.statusCode == 204) return callback('{"status": "Success"}');
return callback(data);
}); // resp.on('end')
}); // var req = ...
req.on('error', function(err) {console.log('queryPrinter(): ' + err);});
req.write(postData);
req.end();
} else {
// Unsupported method sent in
callback({"status": "Unknown method sent to queryPrinter(): " + method});
} // if (method...)
}); // dns.lookup()
} // function queryPrinter()
// --------------------------------------------------------------------------
// The exported interface consists of several methods for querying the
// printer directly. The try/catch behavior is necessary since some of the
// OctoPrint error conditions come back as plain text.
// --------------------------------------------------------------------------
module.exports = {
// Version information - http://docs.octoprint.org/en/master/api/version.html
version: function(callback) {
queryPrinter('GET', '/api/version', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('version(): ' + e.message); return callback({"status": response});}
});
},
// Connection handling - http://docs.octoprint.org/en/master/api/connection.html
connection: function(callback) {
queryPrinter('GET', '/api/connection', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('connection(): ' + e.message); return callback({"status": response});}
});
},
connect: function(callback) {
var body = {"command": "connect"};
queryPrinter('POST', '/api/connection', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.log('connect(): ' + e.message); return callback({"status": response});}
});
},
disconnect: function(callback) {
var body = {"command": "disconnect"};
queryPrinter('POST', '/api/connection', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.log('disconnect(): ' + e.message); return callback({"status": response});}
});
},
// File operations - http://docs.octoprint.org/en/master/api/files.html
files: function(bRecursive, callback) {
queryPrinter('GET', '/api/files' + (bRecursive ? '?recursive=true' : ''), null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('files(): ' + e.message); return callback({"status": response});}
});
},
file: function(filePath, callback) {
queryPrinter('GET', '/api/files/local/' + filePath, null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('file(): ' + e.message); return callback({"status": response});}
});
},
selectFile: function(filePath, bPrint, callback) {
var body = {"command": "select", "print": bPrint};
queryPrinter('POST', '/api/files/local/' + filePath, body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('selectFile(): ' + e.message); return callback({"status": response});}
});
},
// Job operations - http://docs.octoprint.org/en/master/api/job.html
job: function(callback) {
queryPrinter('GET', '/api/job', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('job(): ' + e.message); return callback({"status": response});}
});
},
jobStart: function(callback) {
var body = {"command": "start"};
queryPrinter('POST', '/api/job', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('jobStart(): ' + e.message); return callback({"status": response});}
});
},
jobCancel: function(callback) {
var body = {"command": "cancel"};
queryPrinter('POST', '/api/job', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('jobCancel(): ' + e.message); return callback({"status": response});}
});
},
jobPause: function(callback) {
var body = {"command": "pause", "action": "pause"};
queryPrinter('POST', '/api/job', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('jobPause(): ' + e.message); return callback({"status": response});}
});
},
jobResume: function(callback) {
var body = {"command": "pause", "action": "resume"};
queryPrinter('POST', '/api/job', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('jobResume(): ' + e.message); return callback({"status": response});}
});
},
// Languages - http://docs.octoprint.org/en/master/api/languages.html
languages: function(callback) {
queryPrinter('GET', '/api/languages', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('languages(): ' + e.message); return callback({"status": response});}
});
},
// Settings - http://docs.octoprint.org/en/master/api/settings.html
settings: function(callback) {
queryPrinter('GET', '/api/settings', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('settings(): ' + e.message); return callback({"status": response});}
});
},
// System - http://docs.octoprint.org/en/master/api/system.html
systemCommands: function(bCustom, callback) {
queryPrinter('GET', '/api/system/commands' + (bCustom ? '/custom' : ''), null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('systemCommands(): ' + e.message); return callback({"status": response});}
});
},
systemReboot: function(callback) {
var body = {};
queryPrinter('POST', '/api/system/commands/core/reboot', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('systemReboot(): ' + e.message); return callback({"status": response});}
});
},
systemRestart: function(callback) {
var body = {};
queryPrinter('POST', '/api/system/commands/core/restart', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('systemRestart(): ' + e.message); return callback({"status": response});}
});
},
systemShutdown: function(callback) {
var body = {};
queryPrinter('POST', '/api/system/commands/core/shutdown', body, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('systemShutdown(): ' + e.message); return callback({"status": response});}
});
},
// Timelapse operations - http://docs.octoprint.org/en/master/api/timelapse.html
timelapse: function(callback) {
queryPrinter('GET', '/api/timelapse', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.error('timelapse(): ' + e.message); return callback({"status": response});}
});
},
// Printer operations - http://docs.octoprint.org/en/master/api/printer.html
printerState: function(callback) {
queryPrinter('GET', '/api/printer', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.log('printerState(): ' + e.message); return callback({"status": response});}
});
},
// Printer profile operations - http://docs.octoprint.org/en/master/api/printerprofiles.html
printerProfiles: function(callback) {
queryPrinter('GET', '/api/printerprofiles', null, function(response) {
try { return callback(JSON.parse(response));}
catch (e) { console.log('printerProfiles(): ' + e.message); return callback({"status": response});}
});
}
// ------------------------------------------------------------------------
// End of exported function list
// ------------------------------------------------------------------------
};