homepage/src/widgets/octoprint/component.jsx

77 lines
2.4 KiB
React
Raw Normal View History

2023-07-24 11:59:22 +01:00
import { Buffer } from 'buffer';
2023-01-17 03:54:24 +01:00
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
export default function Component({ service }) {
const { widget } = service;
2023-01-18 19:33:55 +01:00
const { data: printerStats, error: printerStatsError } = useWidgetAPI(widget, "printer_stats");
2023-09-20 17:39:58 +01:00
// const { data: jobStats, error: jobStatsError } = useWidgetAPI(widget, "job_stats");
2023-01-17 03:54:24 +01:00
if (printerStatsError) {
2023-09-20 17:39:58 +01:00
let msg
try {
msg = JSON.parse(Buffer.from(printerStatsError.resultData.data).toString()).error;
} catch (error) {
msg = 'Octoprint Not Found'
}
2023-07-24 11:39:42 +01:00
return (
<Container service={service}>
2023-09-20 17:39:58 +01:00
<Block label="Error" value={msg} />
2023-07-24 11:39:42 +01:00
</Container>
);
2023-01-17 03:54:24 +01:00
}
2023-09-20 17:39:58 +01:00
// if (jobStatsError) {
// return <Container service={service} error={jobStatsError} />;
// }
2023-01-17 03:54:24 +01:00
2023-09-20 17:39:58 +01:00
const state = printerStats[1].Status;
const tempTool = printerStats[1].Temp.Tool;
const tempBed = printerStats[1].Temp.Bed;
2023-01-17 23:08:58 +01:00
if (!printerStats || !state || !tempTool || !tempBed) {
2023-01-17 03:54:24 +01:00
return (
<Container service={service}>
<Block label="octoprint.printer_state" />
2023-01-17 03:54:24 +01:00
</Container>
);
}
const printingStateFalgs = ["Printing", "Paused", "Pausing", "Resuming"];
if (printingStateFalgs.includes(state)) {
2023-01-17 23:08:58 +01:00
const { completion } = jobStats.progress;
2023-01-17 23:08:58 +01:00
if (!jobStats || !completion) {
2023-01-17 03:54:24 +01:00
return (
<Container service={service}>
<Block label="octoprint.printer_state" />
<Block label="octoprint.temp_tool" />
<Block label="octoprint.temp_bed" />
<Block label="octoprint.job_completion" />
2023-01-17 03:54:24 +01:00
</Container>
);
}
return (
2023-01-17 23:08:58 +01:00
<Container service={service}>
<Block label="octoprint.printer_state" value={printerStats.state.text} />
<Block label="octoprint.temp_tool" value={`${printerStats.temperature.tool0.actual} °C`} />
<Block label="octoprint.temp_bed" value={`${printerStats.temperature.bed.actual} °C`} />
<Block label="octoprint.job_completion" value={`${completion.toFixed(2)}%`} />
2023-01-17 23:08:58 +01:00
</Container>
2023-01-17 03:54:24 +01:00
);
}
return (
<Container service={service}>
<Block label="octoprint.printer_state" value={printerStats.state.text} />
<Block label="octoprint.temp_tool" value={`${printerStats.temperature.tool0.actual} °C`} />
<Block label="octoprint.temp_bed" value={`${printerStats.temperature.bed.actual} °C`} />
2023-01-17 03:54:24 +01:00
</Container>
);
}