Display TpLink Tapo power usage values
This commit is contained in:
parent
e6b5fed399
commit
a0487c0b59
5 changed files with 187 additions and 0 deletions
101
app/components/PowerCard.tsx
Normal file
101
app/components/PowerCard.tsx
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import { type TapoDevice } from "../lib/getPower";
|
||||
|
||||
interface PowerCardProps {
|
||||
device: TapoDevice | null;
|
||||
label: string;
|
||||
delay?: number;
|
||||
}
|
||||
|
||||
function powerColor(watts: number): string {
|
||||
if (watts > 400) return "#ef4444";
|
||||
if (watts > 200) return "#f59e0b";
|
||||
return "#3b82f6";
|
||||
}
|
||||
|
||||
export default function PowerCard({ device, label, delay = 0 }: PowerCardProps) {
|
||||
const pct = device ? Math.min(100, (device.current_power_w / 500) * 100) : 0;
|
||||
const runtimeHours = device ? Math.floor(device.today_runtime_min / 60) : 0;
|
||||
const runtimeMins = device ? device.today_runtime_min % 60 : 0;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="bg-white border border-gray-200 rounded-2xl p-5 flex flex-col shadow-sm hover:shadow-md hover:-translate-y-0.5 transition-all duration-200 animate-fade-up"
|
||||
style={{ animationDelay: `${delay}ms` }}
|
||||
>
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<span className="text-[0.68rem] font-medium tracking-widest uppercase text-gray-400">
|
||||
{label}
|
||||
</span>
|
||||
{device ? (
|
||||
<span
|
||||
className={`flex items-center gap-1.5 text-[0.62rem] font-medium uppercase tracking-widest ${
|
||||
device.on ? "text-emerald-500" : "text-gray-400"
|
||||
}`}
|
||||
>
|
||||
<span
|
||||
className={`w-1.5 h-1.5 rounded-full ${
|
||||
device.on ? "bg-emerald-400" : "bg-gray-300"
|
||||
}`}
|
||||
/>
|
||||
{device.on ? "On" : "Off"}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{device ? (
|
||||
<>
|
||||
<div className="flex items-baseline gap-1.5 mt-0.5">
|
||||
<span className="text-3xl font-medium tracking-tight text-gray-900 leading-none">
|
||||
{device.current_power_w.toFixed(1)}
|
||||
</span>
|
||||
<span className="text-base text-gray-400 font-medium">W</span>
|
||||
</div>
|
||||
<span className="text-[0.7rem] text-gray-400 mt-1 truncate">
|
||||
{device.alias} · {device.model}
|
||||
</span>
|
||||
|
||||
<div className="h-[3px] bg-gray-100 rounded-full mt-4 overflow-hidden">
|
||||
<div
|
||||
className="h-full rounded-full transition-all duration-700"
|
||||
style={{
|
||||
width: `${pct}%`,
|
||||
background: powerColor(device.current_power_w),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-3 gap-2 mt-4 pt-4 border-t border-gray-100">
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<span className="text-sm font-medium text-gray-900">
|
||||
{(device.today_energy_wh / 1000).toFixed(3)}
|
||||
<span className="text-gray-400 text-xs ml-0.5">kWh</span>
|
||||
</span>
|
||||
<span className="text-[0.62rem] uppercase tracking-widest text-gray-400">
|
||||
Today
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<span className="text-sm font-medium text-gray-900">
|
||||
{(device.month_energy_wh / 1000).toFixed(2)}
|
||||
<span className="text-gray-400 text-xs ml-0.5">kWh</span>
|
||||
</span>
|
||||
<span className="text-[0.62rem] uppercase tracking-widest text-gray-400">
|
||||
Month
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<span className="text-sm font-medium text-gray-900">
|
||||
{runtimeHours}h {runtimeMins}m
|
||||
</span>
|
||||
<span className="text-[0.62rem] uppercase tracking-widest text-gray-400">
|
||||
Runtime
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="skeleton h-24 mt-2" />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
18
app/components/PowerGrid.tsx
Normal file
18
app/components/PowerGrid.tsx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { type PowerData } from "../lib/getPower";
|
||||
import PowerCard from "./PowerCard";
|
||||
|
||||
interface PowerGridProps {
|
||||
power: PowerData | null;
|
||||
}
|
||||
|
||||
export default function PowerGrid({ power }: PowerGridProps) {
|
||||
const server = power?.devices.find((d) => d.name === "server") ?? null;
|
||||
const desktop = power?.devices.find((d) => d.name === "desktop") ?? null;
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-3.5 mb-11">
|
||||
<PowerCard device={server} label="Server" delay={0} />
|
||||
<PowerCard device={desktop} label="Desktop" delay={60} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue