import React, { useState, useEffect } from 'react'; import { RotateCw } from 'lucide-react'; import { IOPoint } from '../types'; import { comms } from '../communication'; interface IOMonitorPageProps { onToggle: (id: number, type: 'input' | 'output') => void; } interface InterlockData { axisIndex: number; axisName: string; nonAxis: boolean; locks: { id: number; name: string; state: boolean }[]; hexValue: string; } export const IOMonitorPage: React.FC = ({ onToggle }) => { const [ioPoints, setIoPoints] = useState([]); const [interlocks, setInterlocks] = useState([]); const [isLoading, setIsLoading] = useState(true); const [activeIOTab, setActiveIOTab] = useState<'in' | 'out' | 'interlock'>('in'); // Fetch initial IO list when page mounts useEffect(() => { const fetchIOList = async () => { setIsLoading(true); try { const ioStr = await comms.getIOList(); const ioData = JSON.parse(ioStr); // Handle new structured format: { inputs: [...], outputs: [...], interlocks: [...] } if (ioData.inputs && ioData.outputs) { // New format const flatIoList: IOPoint[] = [ ...ioData.outputs.map((io: any) => ({ id: io.id, name: io.name, type: 'output' as const, state: io.state })), ...ioData.inputs.map((io: any) => ({ id: io.id, name: io.name, type: 'input' as const, state: io.state })) ]; setIoPoints(flatIoList); setInterlocks(ioData.interlocks || []); } else if (Array.isArray(ioData)) { // Old format - already flat array setIoPoints(ioData); } } catch (e) { console.error('Failed to fetch IO list:', e); } setIsLoading(false); }; fetchIOList(); // Subscribe to real-time IO updates const unsubscribe = comms.subscribe((msg: any) => { if (msg?.type === 'STATUS_UPDATE' && msg.ioState) { setIoPoints(prev => { const newIO = [...prev]; msg.ioState.forEach((update: { id: number, type: string, state: boolean }) => { const idx = newIO.findIndex(p => p.id === update.id && p.type === update.type); if (idx >= 0) newIO[idx] = { ...newIO[idx], state: update.state }; }); return newIO; }); } }); return () => { unsubscribe(); }; }, []); const points = activeIOTab === 'interlock' ? [] : ioPoints.filter(p => p.type === (activeIOTab === 'in' ? 'input' : 'output')); return (
{/* Local Header / Controls */}

SYSTEM I/O MONITOR

{activeIOTab === 'interlock' ? `TOTAL AXES: ${interlocks.length}` : `TOTAL POINTS: ${ioPoints.length}`}
{isLoading ? (
LOADING IO POINTS...
) : activeIOTab === 'interlock' ? (
{interlocks.map(axis => (
{axis.axisName} ({axis.hexValue})
{axis.locks.map(lock => (
{lock.name}
))}
))}
) : (
{points.map(p => (
onToggle(p.id, p.type)} className={` flex items-center gap-4 px-4 py-3 cursor-pointer transition-all border clip-tech-sm group hover:translate-x-1 ${p.state ? (p.type === 'output' ? 'bg-neon-green/10 border-neon-green text-neon-green shadow-[0_0_15px_rgba(10,255,0,0.2)]' : 'bg-neon-yellow/10 border-neon-yellow text-neon-yellow shadow-[0_0_15px_rgba(255,230,0,0.2)]') : 'bg-slate-900/40 border-slate-800 text-slate-500 hover:border-slate-600 hover:bg-slate-800/40'} `} > {/* LED Indicator */}
{/* ID Badge */}
{p.type === 'input' ? 'I' : 'Q'}{p.id.toString().padStart(2, '0')}
{/* Name */}
{p.name}
))}
)}
); };