import { useCallback, useEffect, useState } from "react"; import { Text, View } from "react-native"; import { useAppStore } from "../hooks/useAppStore"; import { sendTcpMessage } from "../lib/tcpScan"; import { type ZebraScanResult, zebraScanner } from "../lib/ZebraScanner"; import { ScannedLabelBox } from "./ScannedLabels"; const STX = "\x02"; const ETX = "\x03"; export default function ProdScanner() { const [lastScan, setLastScan] = useState(null); const [tagScans, setTagScans] = useState([]); const scannerIdFromStore = useAppStore((s) => s.scannerId); const serverIp = useAppStore((s) => s.serverIp); const serverPort = useAppStore((s) => s.serverPort); const handleScan = useCallback( async (scan: ZebraScanResult) => { const scanned = scan.data; let commandToSend = `${STX}${scannerIdFromStore}@${scanned}${ETX}`; // if we are sscc we need to scan like this .... 98@]C100090087710038712256 if (scan.data.startsWith("000")) { commandToSend = `${STX}${scannerIdFromStore}@]C1${scanned}${ETX}`; setTagScans((prev: any) => [ parseInt(scanned.slice(10, -1) || "000", 10).toString(), ...prev, ]); } // if we change commands we want to zero out the last scanned labels if (/^[a-zA-Z]/.test(scan.data)) { setTagScans([]); } const something = await sendTcpMessage( commandToSend, serverIp, parseInt(serverPort || "0", 10), ); // Later this is where your TCP send goes. // const response = await sendTcpMessage(tcpMessage); setLastScan(something.data[0]); //console.log("TCP response:", something); }, [scannerIdFromStore, serverIp, serverPort], ); console.log(lastScan); useEffect(() => { zebraScanner.ensureProfile(); zebraScanner.startListening(); const sub = zebraScanner.addScanListener((scan) => { //console.log("SCAN:", scan); handleScan(scan); }); return () => { sub.remove(); zebraScanner.stopListening(); }; }, [handleScan]); return ( Scanner ID: {parseInt(scannerIdFromStore || "0", 10)} {!lastScan ? ( Waiting on scan.... ) : ( {lastScan?.action} {lastScan?.type === "error" ? ( {lastScan?.message} ) : ( {lastScan?.prompt} {lastScan?.message} )} )} ); }