homepage/src/widgets/octoprint/component.jsx

65 lines
2.2 KiB
React
Raw Normal View History

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");
const { data: jobStats, error: jobStatsError } = useWidgetAPI(widget, "job_stats");
2023-01-17 03:54:24 +01:00
if (printerStatsError) {
2023-04-30 19:09:37 -04:00
return <Container service={service} error={printerStatsError} />;
2023-01-17 03:54:24 +01:00
}
2023-01-17 23:08:58 +01:00
if (jobStatsError) {
2023-04-30 19:09:37 -04:00
return <Container service={service} error={jobStatsError} />;
2023-01-17 03:54:24 +01:00
}
2023-01-17 23:08:58 +01:00
const state = printerStats?.state?.text;
const tempTool = printerStats?.temperature?.tool0?.actual;
const tempBed = printerStats?.temperature?.bed?.actual;
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>
);
}