import React, { useState, useEffect, useCallback } from 'react'; import { X, Move, Square, ChevronLeft, ChevronRight, ChevronUp, ChevronDown, Home, Printer, XCircle, Settings } from 'lucide-react'; import { comms } from '../communication'; import { useAlert } from '../contexts/AlertContext'; interface PickerMoveDialogProps { isOpen: boolean; onClose: () => void; } interface PickerStatus { xEnabled: boolean; zEnabled: boolean; pickerSafe: boolean; managementEnabled: boolean; manPosL: boolean; manPosR: boolean; } export const PickerMoveDialog: React.FC = ({ isOpen, onClose }) => { const { showAlert, showConfirm } = useAlert(); const [status, setStatus] = useState({ xEnabled: false, zEnabled: false, pickerSafe: false, managementEnabled: false, manPosL: false, manPosR: false }); const [isJogging, setIsJogging] = useState(false); // Subscribe to picker status updates useEffect(() => { if (!isOpen) return; const unsubscribe = comms.subscribe((data: any) => { if (data.type === 'PICKER_STATUS') { setStatus(data.data); } }); // Request initial status comms.getPickerStatus(); // Set up polling interval const intervalId = setInterval(() => { comms.getPickerStatus(); }, 200); return () => { unsubscribe(); clearInterval(intervalId); }; }, [isOpen]); const handleClose = async () => { try { const result = await comms.canCloseManage(); if (!result.canClose) { showAlert({ type: 'error', title: 'Cannot Close', message: result.message || 'Printer motion is in management position.\nReturn to position and try again.' }); return; } onClose(); } catch (error) { // If check fails, still allow close onClose(); } }; // === Position Move Buttons === const handleMoveLeft = async () => { const result = await comms.pickerMoveLeft(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleMoveLeftWait = async () => { const result = await comms.pickerMoveLeftWait(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleMoveCenter = async () => { const result = await comms.pickerMoveCenter(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleMoveRightWait = async () => { const result = await comms.pickerMoveRightWait(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleMoveRight = async () => { const result = await comms.pickerMoveRight(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; // === Jog Control === const handleJogStart = useCallback(async (direction: 'up' | 'down' | 'left' | 'right') => { setIsJogging(true); await comms.pickerJogStart(direction); }, []); const handleJogStop = useCallback(async () => { setIsJogging(false); await comms.pickerJogStop(); }, []); const handleStop = async () => { await comms.pickerStop(); }; // === Vision Validation Cancel === const handleCancelVisionL = async () => { const confirmed = await showConfirm({ title: 'Cancel Vision Validation', message: 'Do you want to cancel LEFT-QR code verification?' }); if (confirmed) { const result = await comms.cancelVisionValidation('left'); if (result.success) { showAlert({ type: 'success', title: 'Cancelled', message: 'LEFT-QR verification cancelled' }); } else { showAlert({ type: 'error', title: 'Cancel Failed', message: result.message }); } } }; const handleCancelVisionR = async () => { const confirmed = await showConfirm({ title: 'Cancel Vision Validation', message: 'Do you want to cancel RIGHT-QR code verification?' }); if (confirmed) { const result = await comms.cancelVisionValidation('right'); if (result.success) { showAlert({ type: 'success', title: 'Cancelled', message: 'RIGHT-QR verification cancelled' }); } else { showAlert({ type: 'error', title: 'Cancel Failed', message: result.message }); } } }; // === Management Position === const handleManagePosL = async () => { const result = await comms.pickerManagePosition('left'); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleManagePosR = async () => { const result = await comms.pickerManagePosition('right'); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } }; const handleManagePosReturn = async () => { const result = await comms.pickerManageReturn(); if (!result.success) { showAlert({ type: 'error', title: 'Return Failed', message: result.message }); } }; // === Z-Axis Control === const handleZHome = async () => { const confirmed = await showConfirm({ title: 'Z-Axis Home', message: 'Do you want to proceed with picker Z-axis home search?' }); if (confirmed) { const result = await comms.pickerZHome(); if (!result.success) { showAlert({ type: 'error', title: 'Home Failed', message: result.message }); } } }; const handleZZero = async () => { const confirmed = await showConfirm({ title: 'Z-Axis Zero', message: 'Do you want to move picker Z-axis to coordinate:0?' }); if (confirmed) { const result = await comms.pickerZZero(); if (!result.success) { showAlert({ type: 'error', title: 'Move Failed', message: result.message }); } } }; // === Print Test === const handlePrintL = async () => { const result = await comms.pickerTestPrint('left'); if (!result.success) { showAlert({ type: 'error', title: 'Print Failed', message: result.message }); } }; const handlePrintR = async () => { const result = await comms.pickerTestPrint('right'); if (!result.success) { showAlert({ type: 'error', title: 'Print Failed', message: result.message }); } }; if (!isOpen) return null; return (
{/* Backdrop */}
{/* Dialog */}
{/* Header */}

Picker(X) Movement and Management

{/* Content */}
{/* Row 1: Jog Controls */}
{/* Z Up */} {/* X Left Jog */} {/* Stop */} {/* X Right Jog */} {/* Z Down */}
{/* Row 2: Position Buttons */}
{/* Row 3: Vision Cancel & Management Position */}
{/* Row 4: Z-Home, Print, Z-Zero */}
{/* Footer */}
); };