UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.

342 lines (341 loc) 12.7 kB
import { __exportAll } from "../_virtual/_rolldown/runtime.js"; import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorateMetadata.js"; import { __decorate } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorate.js"; import { NotFoundException } from "../exceptions/runtime.exceptions.js"; import { AppConstants } from "../server.constants.js"; import { ROLES } from "../constants/authorization.constants.js"; import { authenticate, authorizeRoles } from "../middleware/authenticate.js"; import { ParamId } from "../middleware/param-converter.middleware.js"; import { FileStorageService } from "../services/file-storage.service.js"; import { PrintJobService } from "../services/orm/print-job.service.js"; import { PrintQueueService } from "../services/print-queue.service.js"; import { PrinterCache } from "../state/printer.cache.js"; import { DELETE, GET, POST, PUT, before, route } from "awilix-express"; //#region src/controllers/print-queue.controller.ts var print_queue_controller_exports = /* @__PURE__ */ __exportAll({ PrintQueueController: () => PrintQueueController }); var _ref, _ref2, _ref3, _ref4, _PrintQueueController; let PrintQueueController = _PrintQueueController = class PrintQueueController { logger; constructor(loggerFactory, printQueueService, printJobService, fileStorageService, printerCache) { this.printQueueService = printQueueService; this.printJobService = printJobService; this.fileStorageService = fileStorageService; this.printerCache = printerCache; this.logger = loggerFactory(_PrintQueueController.name); } async getGlobalQueue(req, res) { try { const page = Number.parseInt(req.query.page) || 1; const pageSize = Number.parseInt(req.query.pageSize) || 50; if (page < 1 || pageSize < 1 || pageSize > 200) { res.status(400).send({ error: "Invalid page or pageSize parameters" }); return; } const [jobs, totalCount] = await this.printQueueService.getGlobalQueuePaged(page, pageSize); const queueItems = jobs.map((job) => ({ jobId: job.id, fileName: job.fileName, printerId: job.printerId, printerName: job.printerName || job.printer?.name, queuePosition: job.queuePosition, status: job.status, createdAt: job.createdAt, estimatedTimeSeconds: job.metadata?.gcodePrintTimeSeconds, filamentGrams: job.metadata?.filamentUsedGrams })); res.send({ items: queueItems, page, pageSize, totalCount, totalPages: Math.ceil(totalCount / pageSize) }); } catch (error) { this.logger.error(`Failed to get global queue: ${error}`); res.status(500).send({ error: "Failed to get global queue" }); } } async getQueue(req, res) { const printerId = req.local.printerId; try { const queue = await this.printQueueService.getQueue(printerId); res.send({ printerId, queue, count: queue.length }); } catch (error) { this.logger.error(`Failed to get queue for printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to get queue" }); } } async addToQueue(req, res) { const printerId = req.local.printerId; const jobId = req.local.jobId; const position = req.body.position === void 0 ? void 0 : Number.parseInt(req.body.position); try { await this.printQueueService.addToQueue(printerId, jobId, position); const queue = await this.printQueueService.getQueue(printerId); res.send({ message: "Job added to queue", printerId, jobId, position, queue }); } catch (error) { this.logger.error(`Failed to add job ${jobId} to queue: ${error}`); res.status(500).send({ error: "Failed to add to queue", message: error instanceof Error ? error.message : "Unknown error" }); } } async reorderQueue(req, res) { const printerId = req.local.printerId; const jobIds = req.body.jobIds; if (!Array.isArray(jobIds)) { res.status(400).send({ error: "jobIds must be an array" }); return; } try { await this.printQueueService.reorderQueue(printerId, jobIds); const queue = await this.printQueueService.getQueue(printerId); res.send({ message: "Queue reordered", printerId, queue }); } catch (error) { this.logger.error(`Failed to reorder queue for printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to reorder queue", message: error instanceof Error ? error.message : "Unknown error" }); } } async clearQueue(req, res) { const printerId = req.local.printerId; try { await this.printQueueService.clearQueue(printerId); res.send({ message: "Queue cleared", printerId }); } catch (error) { this.logger.error(`Failed to clear queue for printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to clear queue", message: error instanceof Error ? error.message : "Unknown error" }); } } async removeFromQueue(req, res) { const printerId = req.local.printerId; const jobId = req.local.jobId; try { await this.printQueueService.removeFromQueue(jobId); const queue = await this.printQueueService.getQueue(printerId); res.send({ message: "Job removed from queue", printerId, jobId, queue }); } catch (error) { this.logger.error(`Failed to remove job ${jobId} from queue: ${error}`); res.status(500).send({ error: "Failed to remove from queue", message: error instanceof Error ? error.message : "Unknown error" }); } } async getNextInQueue(req, res) { const printerId = req.local.printerId; try { const nextJob = await this.printQueueService.getNextInQueue(printerId); res.send({ printerId, nextJob }); } catch (error) { this.logger.error(`Failed to get next job for printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to get next job" }); } } async processQueue(req, res) { const printerId = req.local.printerId; try { const nextJob = await this.printQueueService.processQueue(printerId); if (!nextJob) { res.send({ message: "Queue is empty", printerId, nextJob: null }); return; } res.send({ message: "Processing next job in queue", printerId, nextJob }); } catch (error) { this.logger.error(`Failed to process queue for printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to process queue", message: error instanceof Error ? error.message : "Unknown error" }); } } async createJobFromFile(req, res) { const printerId = req.local.printerId; const { fileStorageId, addToQueue = true, position } = req.body; if (!fileStorageId) { res.status(400).send({ error: "fileStorageId is required" }); return; } try { if (!await this.fileStorageService.fileExists(fileStorageId)) throw new NotFoundException("File not found in storage"); const metadata = await this.fileStorageService.loadMetadata(fileStorageId); if (!metadata) { res.status(400).send({ error: "File has no metadata. Please analyze the file first." }); return; } const printer = await this.printerCache.getCachedPrinterOrThrowAsync(printerId); const job = await this.printJobService.createPendingJob(printerId, metadata._originalFileName || metadata.fileName || "Unknown", metadata, printer.name); job.fileStorageId = fileStorageId; job.fileHash = metadata._fileHash; job.analysisState = "ANALYZED"; job.analyzedAt = /* @__PURE__ */ new Date(); if (metadata.fileFormat) job.fileFormat = metadata.fileFormat; await this.printJobService.updateJob(job); if (addToQueue) await this.printQueueService.addToQueue(printerId, job.id, position); this.logger.log(`Created job ${job.id} from file storage ${fileStorageId} for printer ${printerId}${addToQueue ? " and added to queue" : ""}`); res.send({ id: job.id, printerId: job.printerId, printerName: job.printerName, fileName: job.fileName, fileStorageId: job.fileStorageId, status: job.status, analysisState: job.analysisState, createdAt: job.createdAt, addedToQueue: addToQueue }); } catch (error) { this.logger.error(`Failed to create job from file ${fileStorageId}: ${error}`); res.status(500).send({ error: "Failed to create job from file" }); } } async submitToPrinter(req, res) { const printerId = req.local.printerId; const jobId = req.local.jobId; try { await this.printQueueService.submitToPrinter(printerId, jobId); res.send({ message: "Job submitted to printer for printing", printerId, jobId }); } catch (error) { this.logger.error(`Failed to submit job ${jobId} to printer ${printerId}: ${error}`); res.status(500).send({ error: "Failed to submit job to printer", message: error instanceof Error ? error.message : "Unknown error" }); } } }; __decorate([ GET(), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "getGlobalQueue", null); __decorate([ GET(), route("/:printerId"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "getQueue", null); __decorate([ POST(), route("/:printerId/add/:jobId"), before([ParamId("printerId"), ParamId("jobId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "addToQueue", null); __decorate([ PUT(), route("/:printerId/reorder"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "reorderQueue", null); __decorate([ DELETE(), route("/:printerId/clear"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "clearQueue", null); __decorate([ DELETE(), route("/:printerId/:jobId"), before([ParamId("printerId"), ParamId("jobId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "removeFromQueue", null); __decorate([ GET(), route("/:printerId/next"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "getNextInQueue", null); __decorate([ POST(), route("/:printerId/process"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "processQueue", null); __decorate([ POST(), route("/:printerId/from-file"), before([ParamId("printerId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "createJobFromFile", null); __decorate([ POST(), route("/:printerId/submit/:jobId"), before([ParamId("printerId"), ParamId("jobId")]), __decorateMetadata("design:type", Function), __decorateMetadata("design:paramtypes", [Object, Object]), __decorateMetadata("design:returntype", Promise) ], PrintQueueController.prototype, "submitToPrinter", null); PrintQueueController = _PrintQueueController = __decorate([ route(AppConstants.apiRoute + "/print-queue"), before([authenticate(), authorizeRoles([ROLES.ADMIN, ROLES.OPERATOR])]), __decorateMetadata("design:paramtypes", [ Object, typeof (_ref = typeof PrintQueueService !== "undefined" && PrintQueueService) === "function" ? _ref : Object, typeof (_ref2 = typeof PrintJobService !== "undefined" && PrintJobService) === "function" ? _ref2 : Object, typeof (_ref3 = typeof FileStorageService !== "undefined" && FileStorageService) === "function" ? _ref3 : Object, typeof (_ref4 = typeof PrinterCache !== "undefined" && PrinterCache) === "function" ? _ref4 : Object ]) ], PrintQueueController); //#endregion export { PrintQueueController, print_queue_controller_exports }; //# sourceMappingURL=print-queue.controller.js.map