mcp-3d-printer-server 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +0 -0
- package/LICENSE +339 -0
- package/README.md +313 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +313 -0
- package/dist/index.js.map +1 -0
- package/dist/printers/bambu.d.ts +25 -0
- package/dist/printers/bambu.js +111 -0
- package/dist/printers/creality.d.ts +10 -0
- package/dist/printers/creality.js +90 -0
- package/dist/printers/duet.d.ts +10 -0
- package/dist/printers/duet.js +66 -0
- package/dist/printers/klipper.d.ts +10 -0
- package/dist/printers/klipper.js +62 -0
- package/dist/printers/octoprint.d.ts +10 -0
- package/dist/printers/octoprint.js +97 -0
- package/dist/printers/printer-factory.d.ts +9 -0
- package/dist/printers/printer-factory.js +33 -0
- package/dist/printers/prusa.d.ts +10 -0
- package/dist/printers/prusa.js +93 -0
- package/dist/printers/repetier.d.ts +10 -0
- package/dist/printers/repetier.js +59 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.js +26 -0
- package/package.json +40 -0
- package/scripts/mcp-inspector.js +49 -0
- package/src/index.ts +453 -0
- package/src/printers/bambu.ts +146 -0
- package/src/printers/creality.ts +106 -0
- package/src/printers/duet.ts +82 -0
- package/src/printers/klipper.ts +78 -0
- package/src/printers/octoprint.ts +113 -0
- package/src/printers/printer-factory.ts +37 -0
- package/src/printers/prusa.ts +109 -0
- package/src/printers/repetier.ts +68 -0
- package/src/types.ts +46 -0
- package/tsconfig.json +16 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import {
|
|
6
|
+
ListResourcesRequestSchema,
|
|
7
|
+
ReadResourceRequestSchema,
|
|
8
|
+
ListToolsRequestSchema,
|
|
9
|
+
CallToolRequestSchema,
|
|
10
|
+
ErrorCode,
|
|
11
|
+
McpError
|
|
12
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
import axios from "axios";
|
|
14
|
+
import dotenv from "dotenv";
|
|
15
|
+
import fs from "fs";
|
|
16
|
+
import path from "path";
|
|
17
|
+
import { PrinterFactory } from "./printers/printer-factory.js";
|
|
18
|
+
|
|
19
|
+
// Load environment variables from .env file
|
|
20
|
+
dotenv.config();
|
|
21
|
+
|
|
22
|
+
// Default values
|
|
23
|
+
const DEFAULT_HOST = process.env.PRINTER_HOST || "localhost";
|
|
24
|
+
const DEFAULT_PORT = process.env.PRINTER_PORT || "80";
|
|
25
|
+
const DEFAULT_API_KEY = process.env.API_KEY || "";
|
|
26
|
+
const DEFAULT_TYPE = process.env.PRINTER_TYPE || "octoprint"; // Default to OctoPrint
|
|
27
|
+
const TEMP_DIR = process.env.TEMP_DIR || path.join(process.cwd(), "temp");
|
|
28
|
+
|
|
29
|
+
// Bambu-specific default values
|
|
30
|
+
const DEFAULT_BAMBU_SERIAL = process.env.BAMBU_SERIAL || "";
|
|
31
|
+
const DEFAULT_BAMBU_TOKEN = process.env.BAMBU_TOKEN || "";
|
|
32
|
+
|
|
33
|
+
// Ensure temp directory exists
|
|
34
|
+
if (!fs.existsSync(TEMP_DIR)) {
|
|
35
|
+
fs.mkdirSync(TEMP_DIR, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class ThreeDPrinterMCPServer {
|
|
39
|
+
private server: Server;
|
|
40
|
+
private printerFactory: PrinterFactory;
|
|
41
|
+
|
|
42
|
+
constructor() {
|
|
43
|
+
this.server = new Server(
|
|
44
|
+
{
|
|
45
|
+
name: "mcp-3d-printer-server",
|
|
46
|
+
version: "1.0.0"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
capabilities: {
|
|
50
|
+
resources: {},
|
|
51
|
+
tools: {}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
this.printerFactory = new PrinterFactory();
|
|
57
|
+
|
|
58
|
+
this.setupHandlers();
|
|
59
|
+
this.setupErrorHandling();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
setupErrorHandling() {
|
|
63
|
+
this.server.onerror = (error) => {
|
|
64
|
+
console.error("[MCP Error]", error);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
process.on("SIGINT", async () => {
|
|
68
|
+
// Disconnect all printers
|
|
69
|
+
await this.printerFactory.disconnectAll();
|
|
70
|
+
await this.server.close();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setupHandlers() {
|
|
76
|
+
this.setupResourceHandlers();
|
|
77
|
+
this.setupToolHandlers();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setupResourceHandlers() {
|
|
81
|
+
// List available resources
|
|
82
|
+
this.server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
83
|
+
return {
|
|
84
|
+
resources: [
|
|
85
|
+
{
|
|
86
|
+
uri: `printer://${DEFAULT_HOST}/status`,
|
|
87
|
+
name: "3D Printer Status",
|
|
88
|
+
mimeType: "application/json",
|
|
89
|
+
description: "Current status of the 3D printer including temperatures, print progress, and more"
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
uri: `printer://${DEFAULT_HOST}/files`,
|
|
93
|
+
name: "3D Printer Files",
|
|
94
|
+
mimeType: "application/json",
|
|
95
|
+
description: "List of files available on the 3D printer"
|
|
96
|
+
}
|
|
97
|
+
],
|
|
98
|
+
templates: [
|
|
99
|
+
{
|
|
100
|
+
uriTemplate: "printer://{host}/status",
|
|
101
|
+
name: "3D Printer Status",
|
|
102
|
+
mimeType: "application/json"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
uriTemplate: "printer://{host}/files",
|
|
106
|
+
name: "3D Printer Files",
|
|
107
|
+
mimeType: "application/json"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
uriTemplate: "printer://{host}/file/{filename}",
|
|
111
|
+
name: "3D Printer File Content",
|
|
112
|
+
mimeType: "application/gcode"
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Read resource
|
|
119
|
+
this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
120
|
+
const uri = request.params.uri;
|
|
121
|
+
const match = uri.match(/^printer:\/\/([^\/]+)\/(.+)$/);
|
|
122
|
+
|
|
123
|
+
if (!match) {
|
|
124
|
+
throw new McpError(ErrorCode.InvalidRequest, `Invalid resource URI: ${uri}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const [, host, resource] = match;
|
|
128
|
+
let content;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
if (resource === "status") {
|
|
132
|
+
content = await this.getPrinterStatus(host);
|
|
133
|
+
} else if (resource === "files") {
|
|
134
|
+
content = await this.getPrinterFiles(host);
|
|
135
|
+
} else if (resource.startsWith("file/")) {
|
|
136
|
+
const filename = resource.substring(5);
|
|
137
|
+
content = await this.getPrinterFile(host, filename);
|
|
138
|
+
} else {
|
|
139
|
+
throw new McpError(ErrorCode.InvalidRequest, `Unknown resource: ${resource}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
contents: [
|
|
144
|
+
{
|
|
145
|
+
uri,
|
|
146
|
+
mimeType: resource.startsWith("file/") ? "application/gcode" : "application/json",
|
|
147
|
+
text: typeof content === "string" ? content : JSON.stringify(content, null, 2)
|
|
148
|
+
}
|
|
149
|
+
]
|
|
150
|
+
};
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (axios.isAxiosError(error)) {
|
|
153
|
+
throw new McpError(
|
|
154
|
+
ErrorCode.InternalError,
|
|
155
|
+
`API error: ${error.response?.data?.error || error.message}`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
setupToolHandlers() {
|
|
164
|
+
// List available tools
|
|
165
|
+
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
166
|
+
return {
|
|
167
|
+
tools: [
|
|
168
|
+
{
|
|
169
|
+
name: "get_printer_status",
|
|
170
|
+
description: "Get the current status of the 3D printer",
|
|
171
|
+
inputSchema: {
|
|
172
|
+
type: "object",
|
|
173
|
+
properties: {
|
|
174
|
+
host: {
|
|
175
|
+
type: "string",
|
|
176
|
+
description: "Hostname or IP address of the printer (default: value from env)"
|
|
177
|
+
},
|
|
178
|
+
port: {
|
|
179
|
+
type: "string",
|
|
180
|
+
description: "Port of the printer API (default: value from env)"
|
|
181
|
+
},
|
|
182
|
+
type: {
|
|
183
|
+
type: "string",
|
|
184
|
+
description: "Type of printer management system (octoprint, klipper, duet, repetier, bambu, prusa, creality) (default: value from env)"
|
|
185
|
+
},
|
|
186
|
+
api_key: {
|
|
187
|
+
type: "string",
|
|
188
|
+
description: "API key for authentication (default: value from env)"
|
|
189
|
+
},
|
|
190
|
+
bambu_serial: {
|
|
191
|
+
type: "string",
|
|
192
|
+
description: "Serial number for Bambu Lab printers (default: value from env)"
|
|
193
|
+
},
|
|
194
|
+
bambu_token: {
|
|
195
|
+
type: "string",
|
|
196
|
+
description: "Access token for Bambu Lab printers (default: value from env)"
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
// ...other tools with similar structure
|
|
202
|
+
// Abbreviated for clarity, would include all other tools
|
|
203
|
+
]
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Handle tool calls
|
|
208
|
+
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
209
|
+
const { name, arguments: args } = request.params;
|
|
210
|
+
|
|
211
|
+
// Set default values for common parameters
|
|
212
|
+
const host = String(args?.host || DEFAULT_HOST);
|
|
213
|
+
const port = String(args?.port || DEFAULT_PORT);
|
|
214
|
+
const type = String(args?.type || DEFAULT_TYPE);
|
|
215
|
+
const apiKey = String(args?.api_key || DEFAULT_API_KEY);
|
|
216
|
+
const bambuSerial = String(args?.bambu_serial || DEFAULT_BAMBU_SERIAL);
|
|
217
|
+
const bambuToken = String(args?.bambu_token || DEFAULT_BAMBU_TOKEN);
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
let result;
|
|
221
|
+
|
|
222
|
+
switch (name) {
|
|
223
|
+
case "get_printer_status":
|
|
224
|
+
result = await this.getPrinterStatus(host, port, type, apiKey, bambuSerial, bambuToken);
|
|
225
|
+
break;
|
|
226
|
+
|
|
227
|
+
case "list_printer_files":
|
|
228
|
+
result = await this.getPrinterFiles(host, port, type, apiKey, bambuSerial, bambuToken);
|
|
229
|
+
break;
|
|
230
|
+
|
|
231
|
+
case "upload_gcode":
|
|
232
|
+
if (!args?.filename || !args?.gcode) {
|
|
233
|
+
throw new Error("Missing required parameters: filename and gcode");
|
|
234
|
+
}
|
|
235
|
+
result = await this.uploadGcode(
|
|
236
|
+
host, port, type, apiKey, bambuSerial, bambuToken,
|
|
237
|
+
String(args.filename),
|
|
238
|
+
String(args.gcode),
|
|
239
|
+
Boolean(args.print || false)
|
|
240
|
+
);
|
|
241
|
+
break;
|
|
242
|
+
|
|
243
|
+
case "start_print":
|
|
244
|
+
if (!args?.filename) {
|
|
245
|
+
throw new Error("Missing required parameter: filename");
|
|
246
|
+
}
|
|
247
|
+
result = await this.startPrint(host, port, type, apiKey, bambuSerial, bambuToken, String(args.filename));
|
|
248
|
+
break;
|
|
249
|
+
|
|
250
|
+
case "cancel_print":
|
|
251
|
+
result = await this.cancelPrint(host, port, type, apiKey, bambuSerial, bambuToken);
|
|
252
|
+
break;
|
|
253
|
+
|
|
254
|
+
case "set_printer_temperature":
|
|
255
|
+
if (!args?.component || args?.temperature === undefined) {
|
|
256
|
+
throw new Error("Missing required parameters: component and temperature");
|
|
257
|
+
}
|
|
258
|
+
result = await this.setPrinterTemperature(
|
|
259
|
+
host, port, type, apiKey, bambuSerial, bambuToken,
|
|
260
|
+
String(args.component),
|
|
261
|
+
Number(args.temperature)
|
|
262
|
+
);
|
|
263
|
+
break;
|
|
264
|
+
|
|
265
|
+
default:
|
|
266
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
content: [
|
|
271
|
+
{
|
|
272
|
+
type: "text",
|
|
273
|
+
text: typeof result === "string" ? result : JSON.stringify(result, null, 2)
|
|
274
|
+
}
|
|
275
|
+
]
|
|
276
|
+
};
|
|
277
|
+
} catch (error: unknown) {
|
|
278
|
+
console.error(`Error calling tool ${name}:`, error);
|
|
279
|
+
|
|
280
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
content: [
|
|
284
|
+
{
|
|
285
|
+
type: "text",
|
|
286
|
+
text: `Error: ${errorMessage}`
|
|
287
|
+
}
|
|
288
|
+
],
|
|
289
|
+
isError: true
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Delegating methods to printer implementations
|
|
296
|
+
|
|
297
|
+
async getPrinterStatus(
|
|
298
|
+
host: string,
|
|
299
|
+
port = DEFAULT_PORT,
|
|
300
|
+
type = DEFAULT_TYPE,
|
|
301
|
+
apiKey = DEFAULT_API_KEY,
|
|
302
|
+
bambuSerial = DEFAULT_BAMBU_SERIAL,
|
|
303
|
+
bambuToken = DEFAULT_BAMBU_TOKEN
|
|
304
|
+
) {
|
|
305
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
306
|
+
|
|
307
|
+
if (type.toLowerCase() === "bambu") {
|
|
308
|
+
const bambuApiKey = `${bambuSerial}:${bambuToken}`;
|
|
309
|
+
return implementation.getStatus(host, port, bambuApiKey);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return implementation.getStatus(host, port, apiKey);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async getPrinterFiles(
|
|
316
|
+
host: string,
|
|
317
|
+
port = DEFAULT_PORT,
|
|
318
|
+
type = DEFAULT_TYPE,
|
|
319
|
+
apiKey = DEFAULT_API_KEY,
|
|
320
|
+
bambuSerial = DEFAULT_BAMBU_SERIAL,
|
|
321
|
+
bambuToken = DEFAULT_BAMBU_TOKEN
|
|
322
|
+
) {
|
|
323
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
324
|
+
|
|
325
|
+
if (type.toLowerCase() === "bambu") {
|
|
326
|
+
const bambuApiKey = `${bambuSerial}:${bambuToken}`;
|
|
327
|
+
return implementation.getFiles(host, port, bambuApiKey);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return implementation.getFiles(host, port, apiKey);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async getPrinterFile(
|
|
334
|
+
host: string,
|
|
335
|
+
filename: string,
|
|
336
|
+
port = DEFAULT_PORT,
|
|
337
|
+
type = DEFAULT_TYPE,
|
|
338
|
+
apiKey = DEFAULT_API_KEY,
|
|
339
|
+
bambuSerial = DEFAULT_BAMBU_SERIAL,
|
|
340
|
+
bambuToken = DEFAULT_BAMBU_TOKEN
|
|
341
|
+
) {
|
|
342
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
343
|
+
|
|
344
|
+
if (type.toLowerCase() === "bambu") {
|
|
345
|
+
return (implementation as any).getFile(host, port, apiKey, bambuSerial, bambuToken, filename);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return implementation.getFile(host, port, apiKey, filename);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async uploadGcode(
|
|
352
|
+
host: string,
|
|
353
|
+
port: string,
|
|
354
|
+
type: string,
|
|
355
|
+
apiKey: string,
|
|
356
|
+
bambuSerial: string,
|
|
357
|
+
bambuToken: string,
|
|
358
|
+
filename: string,
|
|
359
|
+
gcode: string,
|
|
360
|
+
print: boolean
|
|
361
|
+
) {
|
|
362
|
+
const tempFilePath = path.join(TEMP_DIR, filename);
|
|
363
|
+
|
|
364
|
+
// Write gcode to temporary file
|
|
365
|
+
fs.writeFileSync(tempFilePath, gcode);
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
369
|
+
|
|
370
|
+
if (type.toLowerCase() === "bambu") {
|
|
371
|
+
return await (implementation as any).uploadFile(
|
|
372
|
+
host, port, apiKey, bambuSerial, bambuToken, tempFilePath, filename, print
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return await implementation.uploadFile(host, port, apiKey, tempFilePath, filename, print);
|
|
377
|
+
} finally {
|
|
378
|
+
// Clean up temporary file
|
|
379
|
+
if (fs.existsSync(tempFilePath)) {
|
|
380
|
+
fs.unlinkSync(tempFilePath);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async startPrint(
|
|
386
|
+
host: string,
|
|
387
|
+
port: string,
|
|
388
|
+
type: string,
|
|
389
|
+
apiKey: string,
|
|
390
|
+
bambuSerial: string,
|
|
391
|
+
bambuToken: string,
|
|
392
|
+
filename: string
|
|
393
|
+
) {
|
|
394
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
395
|
+
|
|
396
|
+
if (type.toLowerCase() === "bambu") {
|
|
397
|
+
return await (implementation as any).startJob(
|
|
398
|
+
host, port, apiKey, bambuSerial, bambuToken, filename
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return await implementation.startJob(host, port, apiKey, filename);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async cancelPrint(
|
|
406
|
+
host: string,
|
|
407
|
+
port: string,
|
|
408
|
+
type: string,
|
|
409
|
+
apiKey: string,
|
|
410
|
+
bambuSerial: string,
|
|
411
|
+
bambuToken: string
|
|
412
|
+
) {
|
|
413
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
414
|
+
|
|
415
|
+
if (type.toLowerCase() === "bambu") {
|
|
416
|
+
return await (implementation as any).cancelJob(
|
|
417
|
+
host, port, apiKey, bambuSerial, bambuToken
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return await implementation.cancelJob(host, port, apiKey);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async setPrinterTemperature(
|
|
425
|
+
host: string,
|
|
426
|
+
port: string,
|
|
427
|
+
type: string,
|
|
428
|
+
apiKey: string,
|
|
429
|
+
bambuSerial: string,
|
|
430
|
+
bambuToken: string,
|
|
431
|
+
component: string,
|
|
432
|
+
temperature: number
|
|
433
|
+
) {
|
|
434
|
+
const implementation = this.printerFactory.getImplementation(type);
|
|
435
|
+
|
|
436
|
+
if (type.toLowerCase() === "bambu") {
|
|
437
|
+
return await (implementation as any).setTemperature(
|
|
438
|
+
host, port, apiKey, bambuSerial, bambuToken, component, temperature
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return await implementation.setTemperature(host, port, apiKey, component, temperature);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async run() {
|
|
446
|
+
const transport = new StdioServerTransport();
|
|
447
|
+
await this.server.connect(transport);
|
|
448
|
+
console.error("3D Printer MCP server running on stdio transport");
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const server = new ThreeDPrinterMCPServer();
|
|
453
|
+
server.run().catch(console.error);
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { PrinterImplementation, BambuPrinterStore, BambuFTP } from "../types.js";
|
|
2
|
+
import { BambuPrinter } from "bambu-js";
|
|
3
|
+
|
|
4
|
+
export class BambuImplementation extends PrinterImplementation {
|
|
5
|
+
private bambuPrinterStore: BambuPrinterStore;
|
|
6
|
+
|
|
7
|
+
constructor(apiClient: any, bambuPrinterStore: BambuPrinterStore) {
|
|
8
|
+
super(apiClient);
|
|
9
|
+
this.bambuPrinterStore = bambuPrinterStore;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private getBambuPrinter(host: string, serial: string, token: string): InstanceType<typeof BambuPrinter> {
|
|
13
|
+
return this.bambuPrinterStore.get(host, serial, token);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getStatus(host: string, port: string, apiKey: string) {
|
|
17
|
+
// Extracting Bambu-specific parameters from apiKey
|
|
18
|
+
// Format should be "serial:token"
|
|
19
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
20
|
+
|
|
21
|
+
const printer = this.getBambuPrinter(host, serial, token);
|
|
22
|
+
|
|
23
|
+
// Connect if not already connected
|
|
24
|
+
if (!printer.isConnected) {
|
|
25
|
+
await printer.connect();
|
|
26
|
+
// Wait for initial state
|
|
27
|
+
await printer.awaitInitialState(10000); // 10 second timeout
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return printer.getState();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async getFiles(host: string, port: string, apiKey: string) {
|
|
34
|
+
// Extracting Bambu-specific parameters
|
|
35
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
36
|
+
|
|
37
|
+
const printer = this.getBambuPrinter(host, serial, token);
|
|
38
|
+
|
|
39
|
+
// Connect if not already connected
|
|
40
|
+
if (!printer.isConnected) {
|
|
41
|
+
await printer.connect();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Using the manipulateFiles API to list files
|
|
45
|
+
const fileList: string[] = [];
|
|
46
|
+
await printer.manipulateFiles(async (context: BambuFTP) => {
|
|
47
|
+
const files = await context.readDir("gcodes");
|
|
48
|
+
fileList.push(...files);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return { files: fileList };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async getFile(host: string, port: string, apiKey: string, filename: string) {
|
|
55
|
+
// Extracting Bambu-specific parameters
|
|
56
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
57
|
+
|
|
58
|
+
// Bambu doesn't have a direct API to get file content
|
|
59
|
+
// Instead, this returns metadata about the file by confirming it exists
|
|
60
|
+
const printer = this.getBambuPrinter(host, serial, token);
|
|
61
|
+
|
|
62
|
+
// Connect if not already connected
|
|
63
|
+
if (!printer.isConnected) {
|
|
64
|
+
await printer.connect();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let fileExists = false;
|
|
68
|
+
await printer.manipulateFiles(async (context: BambuFTP) => {
|
|
69
|
+
const files = await context.readDir("gcodes");
|
|
70
|
+
fileExists = files.includes(filename);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (!fileExists) {
|
|
74
|
+
throw new Error(`File not found: ${filename}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { name: filename, exists: true };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async uploadFile(host: string, port: string, apiKey: string, filePath: string, filename: string, print: boolean) {
|
|
81
|
+
// Extracting Bambu-specific parameters
|
|
82
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
83
|
+
|
|
84
|
+
const printer = this.getBambuPrinter(host, serial, token);
|
|
85
|
+
|
|
86
|
+
// Connect if not already connected
|
|
87
|
+
if (!printer.isConnected) {
|
|
88
|
+
await printer.connect();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Upload file via FTP
|
|
92
|
+
await printer.manipulateFiles(async (context: BambuFTP) => {
|
|
93
|
+
await context.sendFile(filePath, `gcodes/${filename}`);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (print) {
|
|
97
|
+
// To start a print directly, we would need more info
|
|
98
|
+
// This is a placeholder - starting a print needs more details
|
|
99
|
+
throw new Error("Direct printing of uploaded files is not implemented yet");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { status: "success", message: `File ${filename} uploaded successfully` };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async startJob(host: string, port: string, apiKey: string, filename: string) {
|
|
106
|
+
// Extracting Bambu-specific parameters
|
|
107
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
108
|
+
|
|
109
|
+
// Starting a job requires more information for Bambu printers
|
|
110
|
+
// This is a simplified implementation - in reality, we need
|
|
111
|
+
// more details about the 3MF project file structure
|
|
112
|
+
throw new Error("Starting a print job on Bambu printers requires more information about the file structure");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async cancelJob(host: string, port: string, apiKey: string) {
|
|
116
|
+
// Extracting Bambu-specific parameters
|
|
117
|
+
const [serial, token] = this.extractBambuCredentials(apiKey);
|
|
118
|
+
|
|
119
|
+
const printer = this.getBambuPrinter(host, serial, token);
|
|
120
|
+
|
|
121
|
+
// Connect if not already connected
|
|
122
|
+
if (!printer.isConnected) {
|
|
123
|
+
await printer.connect();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Cancel print
|
|
127
|
+
printer.stop();
|
|
128
|
+
|
|
129
|
+
return { status: "success", message: "Print job cancelled" };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async setTemperature(host: string, port: string, apiKey: string, component: string, temperature: number) {
|
|
133
|
+
// Bambu API doesn't have direct temperature controls
|
|
134
|
+
// We would need to send custom G-code commands
|
|
135
|
+
throw new Error("Setting temperatures directly is not supported via the Bambu API");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Helper method to extract Bambu-specific credentials from apiKey
|
|
139
|
+
private extractBambuCredentials(apiKey: string): [string, string] {
|
|
140
|
+
const parts = apiKey.split(':');
|
|
141
|
+
if (parts.length !== 2) {
|
|
142
|
+
throw new Error("Invalid Bambu credentials format. Expected 'serial:token'");
|
|
143
|
+
}
|
|
144
|
+
return [parts[0], parts[1]];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { PrinterImplementation } from "../types.js";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import FormData from "form-data";
|
|
4
|
+
|
|
5
|
+
export class CrealityImplementation extends PrinterImplementation {
|
|
6
|
+
async getStatus(host: string, port: string, apiKey: string) {
|
|
7
|
+
const url = `http://${host}:${port}/api/device/status`;
|
|
8
|
+
const response = await this.apiClient.get(url, {
|
|
9
|
+
headers: {
|
|
10
|
+
"Authorization": `Bearer ${apiKey}`
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
return response.data;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getFiles(host: string, port: string, apiKey: string) {
|
|
17
|
+
const url = `http://${host}:${port}/api/storage/list`;
|
|
18
|
+
const response = await this.apiClient.get(url, {
|
|
19
|
+
headers: {
|
|
20
|
+
"Authorization": `Bearer ${apiKey}`
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return response.data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async getFile(host: string, port: string, apiKey: string, filename: string) {
|
|
27
|
+
const url = `http://${host}:${port}/api/storage/info?filename=${encodeURIComponent(filename)}`;
|
|
28
|
+
const response = await this.apiClient.get(url, {
|
|
29
|
+
headers: {
|
|
30
|
+
"Authorization": `Bearer ${apiKey}`
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return response.data;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async uploadFile(host: string, port: string, apiKey: string, filePath: string, filename: string, print: boolean) {
|
|
37
|
+
const url = `http://${host}:${port}/api/storage/upload`;
|
|
38
|
+
|
|
39
|
+
const formData = new FormData();
|
|
40
|
+
formData.append("file", fs.createReadStream(filePath));
|
|
41
|
+
formData.append("filename", filename);
|
|
42
|
+
|
|
43
|
+
const response = await this.apiClient.post(url, formData as any, {
|
|
44
|
+
headers: {
|
|
45
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
46
|
+
...formData.getHeaders()
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (print && response.data.success) {
|
|
51
|
+
await this.startJob(host, port, apiKey, filename);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return response.data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async startJob(host: string, port: string, apiKey: string, filename: string) {
|
|
58
|
+
const url = `http://${host}:${port}/api/job/start`;
|
|
59
|
+
|
|
60
|
+
const response = await this.apiClient.post(url, {
|
|
61
|
+
filename: filename
|
|
62
|
+
} as any, {
|
|
63
|
+
headers: {
|
|
64
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
65
|
+
"Content-Type": "application/json"
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return response.data;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async cancelJob(host: string, port: string, apiKey: string) {
|
|
73
|
+
const url = `http://${host}:${port}/api/job/cancel`;
|
|
74
|
+
|
|
75
|
+
const response = await this.apiClient.post(url, {} as any, {
|
|
76
|
+
headers: {
|
|
77
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
78
|
+
"Content-Type": "application/json"
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return response.data;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async setTemperature(host: string, port: string, apiKey: string, component: string, temperature: number) {
|
|
86
|
+
const url = `http://${host}:${port}/api/printer/temperature`;
|
|
87
|
+
|
|
88
|
+
let data: Record<string, any> = {};
|
|
89
|
+
if (component === "bed") {
|
|
90
|
+
data.bed = temperature;
|
|
91
|
+
} else if (component === "extruder") {
|
|
92
|
+
data.hotend = temperature;
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error(`Unsupported component: ${component}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const response = await this.apiClient.post(url, data as any, {
|
|
98
|
+
headers: {
|
|
99
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
100
|
+
"Content-Type": "application/json"
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return response.data;
|
|
105
|
+
}
|
|
106
|
+
}
|