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