feat(scanner): more work on the scanner and can now scan to prod no lst right now
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 2m41s
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 2m41s
This commit is contained in:
72
lstMobile/src/lib/tcpScan.ts
Normal file
72
lstMobile/src/lib/tcpScan.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import TcpSocket from "react-native-tcp-socket";
|
||||
|
||||
const STX = "\x02";
|
||||
const ETX = "\x03";
|
||||
|
||||
type TcpResponse = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
data: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends a Zebra-style TCP message:
|
||||
* <STX>98@{scanned}<ETX>
|
||||
*/
|
||||
export async function sendTcpMessage(
|
||||
scanned: string,
|
||||
host: string,
|
||||
port: number,
|
||||
timeoutMs = 5000,
|
||||
): Promise<TcpResponse> {
|
||||
return new Promise((resolve) => {
|
||||
const responses: string[] = [];
|
||||
|
||||
const client = TcpSocket.createConnection({ host, port }, () => {
|
||||
const payload = `${STX}98@${scanned}${ETX}`;
|
||||
|
||||
console.log("Sending TCP (raw):", payload);
|
||||
console.log("Sending TCP (visible):", `<stx>98@${scanned}<etx>`);
|
||||
|
||||
client.write(payload);
|
||||
});
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
client.destroy();
|
||||
|
||||
resolve({
|
||||
success: false,
|
||||
message: "TCP timeout",
|
||||
data: responses,
|
||||
});
|
||||
}, timeoutMs);
|
||||
|
||||
client.on("data", (data) => {
|
||||
const text = data.toString();
|
||||
console.log("TCP received:", text);
|
||||
|
||||
responses.push(text);
|
||||
});
|
||||
|
||||
client.on("error", (err) => {
|
||||
clearTimeout(timeout);
|
||||
client.destroy();
|
||||
|
||||
resolve({
|
||||
success: false,
|
||||
message: err.message,
|
||||
data: responses,
|
||||
});
|
||||
});
|
||||
|
||||
client.on("close", () => {
|
||||
clearTimeout(timeout);
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
message: "TCP complete",
|
||||
data: responses,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user