diff --git a/src/widgets/octoprint/component.jsx b/src/widgets/octoprint/component.jsx index 894bd887..2483e5bf 100644 --- a/src/widgets/octoprint/component.jsx +++ b/src/widgets/octoprint/component.jsx @@ -8,24 +8,29 @@ export default function Component({ service }) { const { widget } = service; 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) { - 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 ( - + ); } - if (jobStatsError) { - return ; - } + // if (jobStatsError) { + // return ; + // } - const state = printerStats?.state?.text; - const tempTool = printerStats?.temperature?.tool0?.actual; - const tempBed = printerStats?.temperature?.bed?.actual; + const state = printerStats[1].Status; + const tempTool = printerStats[1].Temp.Tool; + const tempBed = printerStats[1].Temp.Bed; if (!printerStats || !state || !tempTool || !tempBed) { return ( diff --git a/src/widgets/octoprint/proxy.js b/src/widgets/octoprint/proxy.js new file mode 100644 index 00000000..c7461148 --- /dev/null +++ b/src/widgets/octoprint/proxy.js @@ -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); + +} \ No newline at end of file diff --git a/src/widgets/octoprint/widget.js b/src/widgets/octoprint/widget.js index 591c1dae..e5aaf631 100644 --- a/src/widgets/octoprint/widget.js +++ b/src/widgets/octoprint/widget.js @@ -1,8 +1,8 @@ -import genericProxyHandler from "utils/proxy/handlers/generic"; +import octoprintProxyHandler from "./proxy"; const widget = { api: "{url}/api/{endpoint}?apikey={key}", - proxyHandler: genericProxyHandler, + proxyHandler: octoprintProxyHandler, mappings: { printer_stats: {