mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-02 13:33:40 +01:00
current
This commit is contained in:
parent
c439661a93
commit
5ba3bd046f
@ -8,24 +8,29 @@ export default function Component({ service }) {
|
|||||||
const { widget } = service;
|
const { widget } = service;
|
||||||
|
|
||||||
const { data: printerStats, error: printerStatsError } = useWidgetAPI(widget, "printer_stats");
|
const { data: printerStats, error: printerStatsError } = useWidgetAPI(widget, "printer_stats");
|
||||||
const { data: jobStats, error: jobStatsError } = useWidgetAPI(widget, "job_stats");
|
// const { data: jobStats, error: jobStatsError } = useWidgetAPI(widget, "job_stats");
|
||||||
|
|
||||||
if (printerStatsError) {
|
if (printerStatsError) {
|
||||||
const msg = JSON.parse(Buffer.from(printerStatsError.resultData.data).toString());
|
let msg
|
||||||
|
try {
|
||||||
|
msg = JSON.parse(Buffer.from(printerStatsError.resultData.data).toString()).error;
|
||||||
|
} catch (error) {
|
||||||
|
msg = 'Octoprint Not Found'
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="OFFLINE" value={msg.error} />
|
<Block label="Error" value={msg} />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jobStatsError) {
|
// if (jobStatsError) {
|
||||||
return <Container service={service} error={jobStatsError} />;
|
// return <Container service={service} error={jobStatsError} />;
|
||||||
}
|
// }
|
||||||
|
|
||||||
const state = printerStats?.state?.text;
|
const state = printerStats[1].Status;
|
||||||
const tempTool = printerStats?.temperature?.tool0?.actual;
|
const tempTool = printerStats[1].Temp.Tool;
|
||||||
const tempBed = printerStats?.temperature?.bed?.actual;
|
const tempBed = printerStats[1].Temp.Bed;
|
||||||
|
|
||||||
if (!printerStats || !state || !tempTool || !tempBed) {
|
if (!printerStats || !state || !tempTool || !tempBed) {
|
||||||
return (
|
return (
|
||||||
|
68
src/widgets/octoprint/proxy.js
Normal file
68
src/widgets/octoprint/proxy.js
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
|
||||||
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
|
const proxyName = "octoprintProxyHandler";
|
||||||
|
const logger = createLogger(proxyName);
|
||||||
|
|
||||||
|
async function getWidget(req) {
|
||||||
|
const { group, service } = req.query;
|
||||||
|
if (!group || !service) {
|
||||||
|
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
if (!widget) {
|
||||||
|
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return widget;
|
||||||
|
}
|
||||||
|
//http://192.168.4.200/api/printer?apikey=97F8AA6805FD428E8395C8E5E805D01A
|
||||||
|
async function printer_stats(params) {
|
||||||
|
const path = `/api/printer?apikey=${params.key}`;
|
||||||
|
const url = `${new URL(`${params.url}${path}`)}`
|
||||||
|
|
||||||
|
const [status, , data] = await httpProxy(url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.error("HTTP %d communicating with jdownloader. Data: %s", status, data.toString());
|
||||||
|
return [status, data];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const decryptedData = JSON.parse(data)
|
||||||
|
|
||||||
|
return [status, {
|
||||||
|
"Status": decryptedData.state.text,
|
||||||
|
"Flags": decryptedData.state.flags,
|
||||||
|
"Temp":{
|
||||||
|
"Tool": decryptedData.temperature.tool0.actual,
|
||||||
|
"Bed": decryptedData.temperature.bed.actual
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Error decoding jdownloader API data. Data: %s", data.toString());
|
||||||
|
return [status, null];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function octoprintProxyHandler(req, res) {
|
||||||
|
const widget = await getWidget(req);
|
||||||
|
|
||||||
|
if (!widget) {
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
logger.debug("Getting data from JDRss API");
|
||||||
|
const d = await printer_stats(widget)
|
||||||
|
return res.send(d);
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
import octoprintProxyHandler from "./proxy";
|
||||||
|
|
||||||
const widget = {
|
const widget = {
|
||||||
api: "{url}/api/{endpoint}?apikey={key}",
|
api: "{url}/api/{endpoint}?apikey={key}",
|
||||||
proxyHandler: genericProxyHandler,
|
proxyHandler: octoprintProxyHandler,
|
||||||
|
|
||||||
mappings: {
|
mappings: {
|
||||||
printer_stats: {
|
printer_stats: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user