pull/834/head
williamwoldum 2 years ago
parent e20a0e90b2
commit 9f008fc04e

8518
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -418,5 +418,13 @@
"print_status": "Print Status",
"print_progress": "Progress",
"layers": "Layers"
},
"octoPrint": {
"printer_state": "Status",
"temp_tool": "Tool temp",
"temp_bed": "Bed temp",
"job_time_elapsed": "Elpased",
"job_time_left": "Left",
"job_completion": "Completion"
}
}

@ -28,6 +28,7 @@ const components = {
nextdns: dynamic(() => import("./nextdns/component")),
npm: dynamic(() => import("./npm/component")),
nzbget: dynamic(() => import("./nzbget/component")),
octoPrint: dynamic(() => import("./octoPrint/component")),
omada: dynamic(() => import("./omada/component")),
ombi: dynamic(() => import("./ombi/component")),
opnsense: dynamic(() => import("./opnsense/component")),

@ -0,0 +1,93 @@
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
const formatSecs = (totalSecs) => {
let secs = totalSecs;
const days = Math.floor(secs / 86400);
secs -= days * 86400;
let hours = Math.floor(secs / 3600);
secs -= hours * 3600;
let mins = Math.floor(secs / 60);
secs -= mins * 60;
if (hours < 10) hours = `0${hours}`;
if (mins < 10) mins = `0${mins}`;
if (secs < 10) secs = `0${secs}`;
return days === 0 ? `${hours}:${mins}:${secs}` : `${days}:${hours}:${mins}:${secs}`;
};
export default function Component({ service }) {
const { widget } = service;
const { data: printerStats, error: printerStatsError } = useWidgetAPI(widget, "printer_stats", {
refreshInterval: 1500,
});
const { data: jobStats, error: jobStatsError } = useWidgetAPI(widget, "job_stats", {
refreshInterval: 1500,
});
if (printerStatsError) {
return <Container error={printerStatsError} />;
}
if (!printerStats) {
return (
<Container service={service}>
<Block label="octoPrint.printer_state" />
</Container>
);
}
if (printerStats === 500) {
return (
<Container service={service}>
<Block label="octoPrint.printer_state" value={printerStats.state.text} />
</Container>
);
}
const state = printerStats.state.text;
if (state === "Printing" || state === "Paused") {
if (jobStatsError) {
return <Container error={jobStatsError} />;
}
if (!jobStats) {
return (
<Container service={service}>
<Block label="octoPrint.job_time_elapsed" />
<Block label="octoPrint.job_time_left" />
<Block label="octoPrint.job_completion" />
</Container>
);
}
const { printTimeLeft, printTime, completion } = jobStats.progress;
return (
<>
<Container service={service}>
<Block label="octoPrint.printer_state" value={printerStats.state.text} />
<Block label="octoPrint.temp_tool" value={`${printerStats.temperature.tool0.actual}°`} />
<Block label="octoPrint.temp_bed" value={`${printerStats.temperature.bed.actual}°`} />
</Container>
<Container service={service}>
<Block label="octoPrint.job_completion" value={`${completion.toFixed(2)}%`} />
<Block label="octoPrint.job_time_elapsed" value={formatSecs(printTime)} />
<Block label="octoPrint.job_time_left" value={formatSecs(printTimeLeft)} />
</Container>
</>
);
}
return (
<Container service={service}>
<Block label="octoPrint.printer_state" value={printerStats.state.text} />
<Block label="octoPrint.temp_tool" value={`${printerStats.temperature.tool0.actual}°`} />
<Block label="octoPrint.temp_bed" value={`${printerStats.temperature.bed.actual}°`} />
</Container>
);
}

@ -0,0 +1,58 @@
import getServiceWidget from "utils/config/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers";
import validateWidgetData from "utils/proxy/validate-widget-data";
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";
const logger = createLogger("octoPrintProxyHandler");
export default async function octoPrintProxyHandler(req, res, map) {
const { group, service, endpoint } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (!widgets?.[widget.type]?.api) {
return res.status(403).json({ error: "Service does not support API calls" });
}
if (widget) {
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const [status, contentType, data] = await httpProxy(url, {
method: req.method,
});
let resultData = data;
if (!validateWidgetData(widget, endpoint, resultData)) {
if (status === 500 && widget.silencePrinterNotFound) {
resultData = 500;
return res.status(status).send(resultData);
}
return res.status(status).json({ error: { message: "Invalid data", url, data: resultData } });
}
if (status === 200 && map) {
resultData = map(data);
}
if (contentType) res.setHeader("Content-Type", contentType);
if (status === 204 || status === 304) {
return res.status(status).end();
}
if (status >= 400) {
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
return res.status(status).json({ error: { message: "HTTP Error", url, data } });
}
return res.status(status).send(resultData);
}
}
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
return res.status(400).json({ error: "Invalid proxy service type" });
}

@ -0,0 +1,17 @@
import octoPrintProxyHandler from "./proxy";
const widget = {
api: "{url}/api/{endpoint}?apikey={key}",
proxyHandler: octoPrintProxyHandler,
mappings: {
printer_stats: {
endpoint: "printer",
},
job_stats: {
endpoint: "job",
},
},
};
export default widget;

@ -23,6 +23,7 @@ import navidrome from "./navidrome/widget";
import nextdns from "./nextdns/widget";
import npm from "./npm/widget";
import nzbget from "./nzbget/widget";
import octoPrint from "./octoPrint/widget";
import omada from "./omada/widget";
import ombi from "./ombi/widget";
import opnsense from "./opnsense/widget";
@ -50,8 +51,8 @@ import transmission from "./transmission/widget";
import tubearchivist from "./tubearchivist/widget";
import truenas from "./truenas/widget";
import unifi from "./unifi/widget";
import watchtower from './watchtower/widget'
import xteve from './xteve/widget'
import watchtower from "./watchtower/widget";
import xteve from "./xteve/widget";
const widgets = {
adguard,
@ -81,6 +82,7 @@ const widgets = {
nextdns,
npm,
nzbget,
octoPrint,
omada,
ombi,
opnsense,

Loading…
Cancel
Save