Skip to content

Commit

Permalink
Enhancement: fritzbox uptime display (gethomepage#2481)
Browse files Browse the repository at this point in the history
  • Loading branch information
evorg authored Dec 15, 2023
1 parent aeaf36e commit 24e25e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
28 changes: 20 additions & 8 deletions src/widgets/fritzbox/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ import useWidgetAPI from "utils/proxy/use-widget-api";

export const fritzboxDefaultFields = ["connectionStatus", "uptime", "maxDown", "maxUp"];

const formatUptime = (timestamp) => {
const hours = Math.floor(timestamp / 3600);
const minutes = Math.floor((timestamp % 3600) / 60);
const seconds = timestamp % 60;
const formatUptime = (uptimeInSeconds) => {
const days = Math.floor(uptimeInSeconds / (3600 * 24));
const hours = Math.floor((uptimeInSeconds % (3600 * 24)) / 3600);
const minutes = Math.floor((uptimeInSeconds % 3600) / 60);
const seconds = Math.floor(uptimeInSeconds) % 60;
const format = (num) => String(num).padStart(2, "0");

const hourDuration = hours > 0 ? `${hours}h` : "00h";
const minDuration = minutes > 0 ? `${minutes}m` : "00m";
const secDuration = seconds > 0 ? `${seconds}s` : "00s";
let uptimeStr = "";
if (days) {
uptimeStr += `${days}d`;
}
if (uptimeInSeconds >= 3600) {
uptimeStr += `${format(hours)}h`;
}
if (uptimeInSeconds >= 60) {
uptimeStr += `${format(minutes)}m`;
}
if (!days) {
uptimeStr += `${format(seconds)}s `;
}

return hourDuration + minDuration + secDuration;
return uptimeStr;
};

export default function Component({ service }) {
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/fritzbox/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export default async function fritzboxProxyHandler(req, res) {
const serviceWidget = await getServiceWidget(group, service);

if (!serviceWidget) {
res.status(500).json({ error: "Service widget not found" });
res.status(500).json({ error: { message: "Service widget not found" } });
return;
}

if (!serviceWidget.url) {
res.status(500).json({ error: "Service widget url not configured" });
res.status(500).json({ error: { message: "Service widget url not configured" } });
return;
}

Expand Down Expand Up @@ -91,6 +91,6 @@ export default async function fritzboxProxyHandler(req, res) {
});
})
.catch((error) => {
res.status(500).json({ error: error.message });
res.status(500).json({ error: { message: error.message } });
});
}

0 comments on commit 24e25e8

Please sign in to comment.