114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
import dotenv from "dotenv";
|
|
import { printJobModel } from "../../schemas/printjob.schema.js";
|
|
import { printSubJobModel } from "../../schemas/printsubjob.schema.js";
|
|
import log4js from "log4js";
|
|
import { printerModel } from "../../schemas/printer.schema.js";
|
|
import { filamentModel } from "../../schemas/filament.schema.js";
|
|
import { gcodeFileModel } from "../../schemas/gcodefile.schema.js";
|
|
|
|
dotenv.config();
|
|
|
|
const logger = log4js.getLogger("PrintJobs");
|
|
logger.level = process.env.LOG_LEVEL;
|
|
|
|
const formatPrintersResponse = (printers) => {
|
|
return printers.map((printer) => ({
|
|
id: printer.id,
|
|
name: printer.name,
|
|
link: `/production/printers/info?printerId=${printer.id}`,
|
|
printer: printer,
|
|
}));
|
|
};
|
|
|
|
const formatJobsResponse = (jobs) => {
|
|
return jobs.map((job) => ({
|
|
id: job.id,
|
|
name: job.gcodeFile.name,
|
|
link: `/production/printjobs/info?printJobId=${job.id}`,
|
|
job: job,
|
|
}));
|
|
};
|
|
|
|
const formatFilamentsResponse = (filaments) => {
|
|
return filaments.map((filament) => ({
|
|
id: filament.id,
|
|
name: filament.name,
|
|
link: `/management/filaments/info?filamentId=${filament.id}`,
|
|
filament: filament,
|
|
}));
|
|
};
|
|
|
|
const formatGCodeFilesResponse = (gcodeFiles) => {
|
|
return gcodeFiles.map((gcodeFile) => ({
|
|
id: gcodeFile.id,
|
|
name: gcodeFile.name,
|
|
link: `/management/gcodefiles/info?gcodeFileId=${gcodeFile.id}`,
|
|
gcodeFile: gcodeFile,
|
|
}));
|
|
};
|
|
|
|
export const getSpotlightRouteHandler = async (req, res) => {
|
|
try {
|
|
const query = req.params.query;
|
|
if (query.length <= 4) {
|
|
res.status(200).send([]);
|
|
return;
|
|
}
|
|
const prefix = query.substring(0, 3);
|
|
const delimiter = query.substring(3, 4);
|
|
const suffix = query.substring(4);
|
|
|
|
if (delimiter == ":") {
|
|
switch (prefix) {
|
|
case "PRN":
|
|
const printer = await printerModel.findOne({ id: suffix });
|
|
if (!printer) {
|
|
res.status(404).send({ error: "Job not found" });
|
|
} else {
|
|
res.status(200).send(formatPrintersResponse([printer]));
|
|
}
|
|
break;
|
|
case "JOB":
|
|
const job = await printJobModel
|
|
.findOne({ _id: suffix })
|
|
.populate("gcodeFile", "name");
|
|
if (!job) {
|
|
res.status(404).send({ error: "Job not found" });
|
|
} else {
|
|
res.status(200).send(formatJobsResponse([job]));
|
|
}
|
|
break;
|
|
case "FIL":
|
|
const filament = await filamentModel.findOne({ _id: suffix });
|
|
if (!filament) {
|
|
res.status(404).send({ error: "Filament not found" });
|
|
} else {
|
|
res.status(200).send(formatFilamentsResponse([filament]));
|
|
}
|
|
break;
|
|
case "GCF":
|
|
const gcodeFile = await gcodeFileModel.findOne({ _id: suffix });
|
|
if (!gcodeFile) {
|
|
res.status(404).send({ error: "Filament not found" });
|
|
} else {
|
|
res.status(200).send(formatGCodeFilesResponse([gcodeFile]));
|
|
}
|
|
break;
|
|
case "SBJ":
|
|
const subJob = await printSubJobModel.findOne({ id: suffix });
|
|
if (!subJob) {
|
|
res.status(404).send({ error: "SubJob not found" });
|
|
} else {
|
|
res.status(200).send([subJob]);
|
|
}
|
|
break;
|
|
default:
|
|
res.status(400).send({ error: "Invalid prefix" });
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error("Error listing print jobs:", error);
|
|
res.status(500).send({ error: error });
|
|
}
|
|
};
|