import { useState } from 'react'; import { useChatStore } from '@/store/chatStore'; import type { IoTDevice } from '@/types/chat'; const deviceIcons: Record = { light: '💡', ac: '❄️', curtain: '🪟', sensor: '🌡️', lock: '🔒', }; const deviceTypeLabels: Record = { light: '灯光', ac: '空调', curtain: '窗帘', sensor: '传感器', lock: '门锁', }; function getStatusText(device: IoTDevice): string { switch (device.type) { case 'light': return device.status === 'on' ? `亮度 ${device.brightness}%` : '已关闭'; case 'ac': return device.status === 'on' ? `${device.temperature}°C` : '已关闭'; case 'curtain': return device.status === 'open' ? '已打开' : '已关闭'; case 'sensor': return `${device.value}${device.unit === 'celsius' ? '°C' : '%'}`; case 'lock': return `${device.status === 'locked' ? '已锁定' : '已解锁'} · 🔋${device.battery}%`; default: return device.status; } } function getStatusColor(device: IoTDevice): string { if (device.type === 'lock') { return device.status === 'locked' ? 'text-green-500' : 'text-yellow-500'; } if (device.type === 'sensor') { return 'text-blue-400'; } return device.status === 'on' || device.status === 'open' ? 'text-green-400' : 'text-gray-400'; } export function IoTStatusBar() { const [expanded, setExpanded] = useState(false); const devices = useChatStore((s) => s.iotDevices); const lastUpdated = useChatStore((s) => s.iotDevicesLastUpdated); // 对所有用户显示 IoT 状态栏(生产环境也可用) const isEnabled = import.meta.env.VITE_DISABLE_IOT_PANEL !== 'true'; if (!isEnabled) return null; // 没有设备数据时显示空状态 if (devices.length === 0) { return (
🔌 IoT 设备未连接
); } // 按类型排序:灯光、空调、窗帘、传感器、门锁 const sortedDevices = [...devices].sort((a, b) => { const order: Record = { light: 1, ac: 2, curtain: 3, sensor: 4, lock: 5 }; return (order[a.type] || 99) - (order[b.type] || 99); }); // 紧凑模式下显示的关键设备(前4个) const previewDevices = sortedDevices.slice(0, 4); return (
{/* 紧凑状态栏 */} {/* 展开的设备详情 */} {expanded && (
{sortedDevices.map((device) => (
{deviceIcons[device.type] || '📦'}
{device.name}
{getStatusText(device)}
))}
{devices.length} 台设备 · 模拟调试模式
)}
); }