refactor(mobile): scanner response

ref #16
This commit is contained in:
2026-05-12 08:53:12 -05:00
parent d61be61f44
commit a9c69250bd

View File

@@ -6,7 +6,7 @@ import TcpSocket from "react-native-tcp-socket";
type TcpResponse = { type TcpResponse = {
success: boolean; success: boolean;
message: string; message: string;
data: string[]; data: ScannerEvent;
}; };
type ScannerEvent = { type ScannerEvent = {
@@ -232,7 +232,7 @@ export async function sendTcpMessage(
timeoutMs = 5000, timeoutMs = 5000,
): Promise<TcpResponse> { ): Promise<TcpResponse> {
return new Promise((resolve) => { return new Promise((resolve) => {
const responses: any = []; //const responses: any = [];
const client = TcpSocket.createConnection({ host, port }, () => { const client = TcpSocket.createConnection({ host, port }, () => {
//console.log("Sending TCP (visible):", `${command}`); //console.log("Sending TCP (visible):", `${command}`);
@@ -240,17 +240,53 @@ export async function sendTcpMessage(
client.write(command); client.write(command);
}); });
const timeout = setTimeout(() => { let settled = false;
client.destroy();
resolve({ const done = (response: TcpResponse) => {
if (settled) return;
settled = true;
clearTimeout(timeout);
try {
client.destroy();
} catch {}
resolve(response);
};
// const timeout = setTimeout(() => {
// client.destroy();
// resolve({
// success: false,
// message: "TCP timeout",
// data: responses,
// });
// }, timeoutMs);
const timeout = setTimeout(() => {
done({
success: false, success: false,
message: "TCP timeout", message: "No response from scanner server",
data: responses, data: {
scannerId: "999",
commandDescription: "TCP Command",
prompt: command,
message: "Invalid command",
status: "error",
lines: [
"SYSTEM",
"TCP Command",
`Scan: ${command}`,
"Invalid command",
],
},
}); });
}, timeoutMs); }, timeoutMs);
client.on("data", (data) => { client.on("data", (data: any) => {
//console.log("TCP received:", text); //console.log("TCP received:", text);
const parsed = parseScannerText(data); const parsed = parseScannerText(data);
//console.log("scanned:", parsed); //console.log("scanned:", parsed);
@@ -260,32 +296,69 @@ export async function sendTcpMessage(
const cleaned = parseScannerEvent(parsed); const cleaned = parseScannerEvent(parsed);
//console.log(responses); //console.log(responses);
clearTimeout(timeout); // clearTimeout(timeout);
resolve({ // resolve({
success: true, // success: true,
message: "TCP Response", // message: "TCP Response",
data: cleaned as any, // data: cleaned as any,
// });
done({
success: cleaned.status !== "error",
message:
cleaned.status === "error"
? (cleaned.message ?? "TCP Error")
: "TCP Response",
data: cleaned,
}); });
}); });
client.on("error", (err) => { client.on("error", (err) => {
clearTimeout(timeout); // resolve({
client.destroy(); // success: false,
// message: err.message,
// data: ["Error", "Please try again"] as any,
// });
resolve({ done({
success: false, success: false,
message: err.message, message: err.message,
data: ["Error", "Please try again"], data: {
scannerId: "SYSTEM",
commandDescription: "TCP Error",
prompt: command,
message: err.message,
status: "error",
lines: ["SYSTEM", "TCP Error", `Scan: ${command}`, err.message],
},
}); });
}); });
client.on("close", () => { client.on("close", () => {
clearTimeout(timeout); if (settled) return;
resolve({ // resolve({
success: true, // success: true,
message: "TCP complete", // message: "TCP complete",
data: ["Error", "Please try again"], // data: ["Error", "Please try again"] as any,
// });
done({
success: false,
message: "TCP connection closed",
data: {
scannerId: "SYSTEM",
commandDescription: "TCP Closed",
prompt: command,
message: "Connection closed before response",
status: "error",
lines: [
"SYSTEM",
"TCP Closed",
`Scan: ${command}`,
"Connection closed before response",
],
},
}); });
}); });
}); });