1 | 'use strict'
|
2 |
|
3 | var request = require('request');
|
4 |
|
5 | function OP(url, key) {
|
6 | this.URL = url;
|
7 | this.AUTH_KEY = key;
|
8 | }
|
9 |
|
10 |
|
11 | function octoprintRequest(OP, method, apiPath, commandBody, onError, onSuccess) {
|
12 | console.log("OCTO_ENTERED_octoprintRequest");
|
13 | console.log('OP.URL', OP.URL);
|
14 | console.log('OP.AUTH_KEY', OP.AUTH_KEY);
|
15 |
|
16 |
|
17 | var requestSettings = octoprintRequestSettings(OP.URL, OP.AUTH_KEY, method, apiPath, commandBody);
|
18 |
|
19 | console.log("OCTO_REQUEST_SETTINGS", requestSettings);
|
20 |
|
21 |
|
22 | request(requestSettings,
|
23 | function (error, response, body) {
|
24 | if (!error && response.statusCode >= 200 && response.statusCode < 300) {
|
25 | if (body) {
|
26 | var receivedData = JSON.parse(body);
|
27 |
|
28 | console.log("OCTO_RECEIVED => ", receivedData);
|
29 | onSuccess(receivedData);
|
30 | }
|
31 | else {
|
32 | console.log("OCTO_COMPLETED => EMPTY RESPONSE");
|
33 | onSuccess();
|
34 | }
|
35 | }
|
36 | else if (response.statusCode >= 400 && response.statusCode < 500) {
|
37 | console.log(`OCTO_${response.statusCode} => `, error, body);
|
38 | var errMsg = '';
|
39 |
|
40 | if (response.statusCode == 401) errMsg = "Invalid OctoPrint key";
|
41 | else if (response.statusCode == 409) errMsg = "OctoPrint prevented this action, because of a conflict";
|
42 |
|
43 | onError({
|
44 | statusCode: response.statusCode,
|
45 | data: body,
|
46 | error: error,
|
47 | errMsg: errMsg
|
48 | });
|
49 | }
|
50 | else {
|
51 | console.log(`OCTO_FAILED_${response.statusCode} => `, error);
|
52 | console.log("OCTO_FAILED_USER_DATA => ", OP.USER_DATA);
|
53 | console.log("OCTO_FAILED_REQUEST - ", requestSettings);
|
54 |
|
55 | onError({
|
56 | statusCode: response.statusCode,
|
57 | data: body,
|
58 | error: error,
|
59 | errMsg: errMsg
|
60 | });
|
61 | }
|
62 | }
|
63 | );
|
64 | }
|
65 |
|
66 |
|
67 | OP.prototype.getOctoprintVersion = function (onError, onSuccess) {
|
68 | octoprintRequest(this, "GET", "/api/version", null, onError,
|
69 | (data) => { onSuccess(data); }
|
70 | );
|
71 | };
|
72 |
|
73 | OP.prototype.getCurrentJobState = function (onError, onSuccess) {
|
74 | octoprintRequest(this, "GET", "/api/job", null, onError,
|
75 | (data) => { onSuccess(data); }
|
76 | );
|
77 | };
|
78 |
|
79 | OP.prototype.getCurrentTemperatures = function (onError, onSuccess) {
|
80 | octoprintRequest(this, "GET", "/api/printer?exclude=sd,state", null, onError,
|
81 | (data) => { onSuccess(data); }
|
82 | );
|
83 | };
|
84 |
|
85 |
|
86 | OP.prototype.checkIfPrinterIsPrinting = function (onError, onSuccess) {
|
87 | octoprintRequest(this, "GET", "/api/printer?exclude=temperature,sd", null, onError,
|
88 | (data) => {
|
89 | onSuccess(data.state.flags.printing);
|
90 | });
|
91 | };
|
92 |
|
93 |
|
94 | OP.prototype.startPrint = function (onError, onSuccess) {
|
95 | var startCmd = {
|
96 | "command": "start"
|
97 | };
|
98 |
|
99 | octoprintRequest(this, "POST", "/api/job", startCmd, onError, onSuccess);
|
100 | };
|
101 |
|
102 | OP.prototype.stopPrint = function (onError, onSuccess) {
|
103 | var cancelCmd = {
|
104 | "command": "cancel"
|
105 | };
|
106 |
|
107 | octoprintRequest(this, "POST", "/api/job", cancelCmd, onError, onSuccess);
|
108 | };
|
109 |
|
110 | OP.prototype.pausePrint = function (onError, onSuccess) {
|
111 | var pauseCmd = {
|
112 | "command": "pause",
|
113 | "action": "pause"
|
114 | };
|
115 |
|
116 | octoprintRequest(this, "POST", "/api/job", pauseCmd, onError, onSuccess);
|
117 | };
|
118 |
|
119 | OP.prototype.resumePrint = function (onError, onSuccess) {
|
120 | var pauseCmd = {
|
121 | "command": "pause",
|
122 | "action": "resume"
|
123 | };
|
124 |
|
125 | octoprintRequest(this, "POST", "/api/job", pauseCmd, onError, onSuccess);
|
126 | };
|
127 |
|
128 | OP.prototype.autoHome = function (onError, onSuccess) {
|
129 | var homeCmd = {
|
130 | "command": "home",
|
131 | "axes": ["x", "y", "z"]
|
132 | };
|
133 |
|
134 | octoprintRequest(this, "POST", "/api/printer/printhead", homeCmd, onError, onSuccess);
|
135 | };
|
136 |
|
137 | OP.prototype.disableSteppers = function (onError, onSuccess) {
|
138 | var disableStepCmd = {
|
139 | "command": "M18"
|
140 | };
|
141 |
|
142 | octoprintRequest(this, "POST", "/api/printer/command", disableStepCmd, onError, onSuccess);
|
143 | };
|
144 |
|
145 | OP.prototype.hotendsCooldown = function (onError, onSuccess) {
|
146 | var hotendCooldownCmd = {
|
147 | "command": "target",
|
148 | "targets": {
|
149 | "tool0": 0,
|
150 | "tool1": 0
|
151 | }
|
152 | };
|
153 |
|
154 | octoprintRequest(this, "POST", "/api/printer/tool", hotendCooldownCmd, onError, onSuccess);
|
155 | };
|
156 |
|
157 | OP.prototype.bedCooldown = function (onError, onSuccess) {
|
158 | var bedCooldownCmd = {
|
159 | "command": "target",
|
160 | "target": 0
|
161 | };
|
162 |
|
163 | octoprintRequest(this, "POST", "/api/printer/bed", bedCooldownCmd, onError, onSuccess);
|
164 | };
|
165 |
|
166 | OP.prototype.cooldown = function (onError, onSuccess) {
|
167 | this.hotendsCooldown(onError, () => {
|
168 | this.bedCooldown(onError, onSuccess);
|
169 | });
|
170 | };
|
171 |
|
172 | OP.prototype.setTemperature = function (temp, toolNum, onError, onSuccess) {
|
173 | var toolId = "tool" + (toolNum - 1);
|
174 |
|
175 | var hotendCooldownCmd = {
|
176 | "command": "target",
|
177 | "targets": {
|
178 | toolId: temp
|
179 | }
|
180 | };
|
181 |
|
182 | octoprintRequest(this, "POST", "/api/printer/tool", hotendCooldownCmd, onError, onSuccess);
|
183 | };
|
184 |
|
185 |
|
186 | module.exports = OP;
|
187 |
|
188 |
|
189 | function octoprintRequestSettings(url, key, method, path, data) {
|
190 | var fullUrl = url + '/' + path;
|
191 |
|
192 | return {
|
193 | uri: fullUrl,
|
194 | method: method,
|
195 | headers: {
|
196 | 'X-Api-Key': key
|
197 | },
|
198 | json: data
|
199 | };
|
200 | } |
\ | No newline at end of file |