"use client"; import React, { useEffect, useState } from "react"; import { RefreshCw, Cpu, Activity, ChevronDown, ChevronRight } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; import { useToast } from "@/hooks/use-toast"; import { EquipmentStateAPI, ConnectionStatusSummary, EquipmentTagState, } from "@/lib/api/equipmentState"; export default function EquipmentStatePage() { const { toast } = useToast(); const [summary, setSummary] = useState([]); const [expanded, setExpanded] = useState>({}); const [loadingId, setLoadingId] = useState(null); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(""); const load = async () => { setLoading(true); try { const data = await EquipmentStateAPI.summary(); setSummary(data); } catch (err) { toast({ title: "조회 실패", description: (err as Error).message, variant: "destructive", }); } finally { setLoading(false); } }; useEffect(() => { load(); const t = setInterval(load, 15000); return () => clearInterval(t); }, []); const toggleExpand = async (connectionId: number) => { if (expanded[connectionId]) { const next = { ...expanded }; delete next[connectionId]; setExpanded(next); return; } setLoadingId(connectionId); try { const tags = await EquipmentStateAPI.tagsByConnection(connectionId); setExpanded(prev => ({ ...prev, [connectionId]: tags })); } catch (err) { toast({ title: "태그 조회 실패", description: (err as Error).message, variant: "destructive", }); } finally { setLoadingId(null); } }; const filtered = summary.filter( s => !search || s.connection_name?.toLowerCase().includes(search.toLowerCase()) || s.host?.toLowerCase().includes(search.toLowerCase()) ); return (

장비 현재 상태

각 장비 연결의 최신 수집값과 연결 상태를 확인합니다 (15초마다 자동 새로고침)

setSearch(e.target.value)} />
{loading ? (
{Array.from({ length: 5 }).map((_, i) => (
))}
) : filtered.length === 0 ? (

등록된 장비 연결이 없습니다

) : (
{filtered.map(s => { const isOpen = !!expanded[s.connection_id]; const isHealthy = s.connection_status === "active" || s.connection_status === "connected"; return (
{isOpen && (
{loadingId === s.connection_id ? (

로딩 중...

) : (expanded[s.connection_id] || []).length === 0 ? (

수집된 태그가 없습니다

) : ( {(expanded[s.connection_id] || []).map(t => { const v = t.value_numeric ?? (t.value_boolean !== null ? String(t.value_boolean) : t.value_text) ?? "—"; return ( ); })}
Tag Value Unit Quality Last Collected
{t.tag_name} {String(v)} {t.tag_unit || ""} {t.quality} {new Date(t.last_collected_at).toLocaleString()}
)}
)}
); })}
)}
); }