feat(mobile): update notifications and more error handling added
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s
This commit is contained in:
@@ -9,136 +9,217 @@ type TcpResponse = {
|
||||
data: string[];
|
||||
};
|
||||
|
||||
function parseErpResponse(buffer: Buffer) {
|
||||
const text = buffer
|
||||
.toString("utf8")
|
||||
.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|#[0-9A-Za-z])/g, "")
|
||||
.replace(/\x02/g, "")
|
||||
.replace(/\x03/g, "")
|
||||
.trim();
|
||||
type ScannerEvent = {
|
||||
scannerId?: string;
|
||||
commandDescription?: string;
|
||||
prompt?: string;
|
||||
message?: string;
|
||||
status: "success" | "error" | "location" | "unknown" | "scan";
|
||||
lines?: string[];
|
||||
};
|
||||
|
||||
const noHeader = text.replace(/^\d+@/, "");
|
||||
console.log(text);
|
||||
if (!noHeader.includes("Scan:")) {
|
||||
return {
|
||||
raw: text,
|
||||
type: "error",
|
||||
message: noHeader.trim(),
|
||||
lines: [noHeader.trim()],
|
||||
};
|
||||
}
|
||||
// const ERROR_MESSAGES = [
|
||||
// "Invalid barcode",
|
||||
// "Already scanned",
|
||||
// "Not on stock",
|
||||
// "Article tolerance for consolidation not satisfied.",
|
||||
// ];
|
||||
|
||||
const [actionPart, scanPart = ""] = noHeader.split("Scan:");
|
||||
const action = actionPart.trim();
|
||||
const scanClean = scanPart.trim();
|
||||
const ERROR_KEYWORDS = [
|
||||
"invalid barcode",
|
||||
"already",
|
||||
"not on stock",
|
||||
"article tolerance",
|
||||
"unloaded",
|
||||
"delivered",
|
||||
"blocked",
|
||||
];
|
||||
|
||||
const successMatch = scanClean.match(/^(.*?)\s+V$/);
|
||||
// function parseErpResponse(buffer: Buffer) {
|
||||
// const text = buffer
|
||||
// .toString("utf8")
|
||||
// .replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|#[0-9A-Za-z])/g, "")
|
||||
// .replace(/\x02/g, "")
|
||||
// .replace(/\x03/g, "")
|
||||
// .trim();
|
||||
|
||||
if (successMatch) {
|
||||
const prompt = successMatch[1].trim();
|
||||
// const noHeader = text.replace(/^\d+@/, "");
|
||||
// console.log(text);
|
||||
// if (!noHeader.includes("Scan:")) {
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "error",
|
||||
// message: noHeader.trim(),
|
||||
// lines: [noHeader.trim()],
|
||||
// };
|
||||
// }
|
||||
|
||||
return {
|
||||
raw: text,
|
||||
type: "success",
|
||||
action,
|
||||
prompt,
|
||||
status: "V",
|
||||
lines: [action, prompt, "V"],
|
||||
};
|
||||
}
|
||||
// const [actionPart, scanPart = ""] = noHeader.split("Scan:");
|
||||
// const action = actionPart.trim();
|
||||
// const scanClean = scanPart.trim();
|
||||
|
||||
// // Handles: "Production lotInvalid barcode"
|
||||
// const knownErrors = [
|
||||
// "Invalid barcode",
|
||||
// "Invalid machine",
|
||||
// "Not on stock",
|
||||
// "Article tolerance for consolidation not satisfied",
|
||||
// ].sort((a, b) => b.length - a.length);
|
||||
// const successMatch = scanClean.match(/^(.*?)\s+V$/);
|
||||
|
||||
// const foundError = knownErrors.find((err) => scanClean.includes(err));
|
||||
// if (successMatch) {
|
||||
// const prompt = successMatch[1].trim();
|
||||
|
||||
// if (foundError) {
|
||||
// const prompt = scanClean.replace(foundError, "").trim();
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "success",
|
||||
// action,
|
||||
// prompt,
|
||||
// status: "V",
|
||||
// lines: [action, prompt, "V"],
|
||||
// };
|
||||
// }
|
||||
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "error",
|
||||
// action,
|
||||
// prompt,
|
||||
// message: foundError,
|
||||
// lines: [action, prompt, foundError].filter(Boolean),
|
||||
// };
|
||||
// }
|
||||
// // // Handles: "Production lotInvalid barcode"
|
||||
// // const knownErrors = [
|
||||
// // "Invalid barcode",
|
||||
// // "Invalid machine",
|
||||
// // "Not on stock",
|
||||
// // "Article tolerance for consolidation not satisfied",
|
||||
// // ].sort((a, b) => b.length - a.length);
|
||||
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "pending",
|
||||
// action,
|
||||
// prompt: scanClean,
|
||||
// lines: [action, scanClean].filter(Boolean),
|
||||
// };
|
||||
// // const foundError = knownErrors.find((err) => scanClean.includes(err));
|
||||
|
||||
const unitMatch = scanClean.match(/^(Unit\s+\d+\/\d+)(.*)$/);
|
||||
// // if (foundError) {
|
||||
// // const prompt = scanClean.replace(foundError, "").trim();
|
||||
|
||||
if (unitMatch) {
|
||||
const prompt = unitMatch[1].trim(); // "Unit 1/4"
|
||||
const remainder = unitMatch[2].trim(); // everything after
|
||||
// // return {
|
||||
// // raw: text,
|
||||
// // type: "error",
|
||||
// // action,
|
||||
// // prompt,
|
||||
// // message: foundError,
|
||||
// // lines: [action, prompt, foundError].filter(Boolean),
|
||||
// // };
|
||||
// // }
|
||||
|
||||
// SUCCESS
|
||||
if (remainder === "V") {
|
||||
return {
|
||||
raw: text,
|
||||
type: "success",
|
||||
action,
|
||||
prompt,
|
||||
status: "V",
|
||||
lines: [action, prompt, "V"],
|
||||
};
|
||||
}
|
||||
// // return {
|
||||
// // raw: text,
|
||||
// // type: "pending",
|
||||
// // action,
|
||||
// // prompt: scanClean,
|
||||
// // lines: [action, scanClean].filter(Boolean),
|
||||
// // };
|
||||
|
||||
// Known ERP errors
|
||||
const knownErrors = [
|
||||
"Invalid barcode",
|
||||
"Invalid machine",
|
||||
"Not on stock",
|
||||
"Article tolerance for consolidation not satisfied",
|
||||
];
|
||||
// const unitMatch = scanClean.match(/^(Unit\s+\d+\/\d+)(.*)$/);
|
||||
|
||||
const foundError = knownErrors.find((err) =>
|
||||
remainder.toLowerCase().includes(err.toLowerCase()),
|
||||
);
|
||||
// if (unitMatch) {
|
||||
// const prompt = unitMatch[1].trim(); // "Unit 1/4"
|
||||
// const remainder = unitMatch[2].trim(); // everything after
|
||||
|
||||
if (foundError) {
|
||||
return {
|
||||
raw: text,
|
||||
type: "error",
|
||||
action,
|
||||
prompt,
|
||||
message: foundError,
|
||||
lines: [action, prompt, foundError],
|
||||
};
|
||||
}
|
||||
// // SUCCESS
|
||||
// if (remainder === "V") {
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "success",
|
||||
// action,
|
||||
// prompt,
|
||||
// status: "V",
|
||||
// lines: [action, prompt, "V"],
|
||||
// };
|
||||
// }
|
||||
|
||||
if (remainder) {
|
||||
return {
|
||||
raw: text,
|
||||
type: "prompt",
|
||||
action,
|
||||
prompt,
|
||||
message: remainder,
|
||||
lines: [action, prompt, remainder],
|
||||
};
|
||||
}
|
||||
// // Known ERP errors
|
||||
// const knownErrors = [
|
||||
// "Invalid barcode",
|
||||
// "Invalid machine",
|
||||
// "Not on stock",
|
||||
// "Article tolerance for consolidation not satisfied",
|
||||
// ];
|
||||
|
||||
return {
|
||||
raw: text,
|
||||
type: "pending",
|
||||
action,
|
||||
prompt,
|
||||
lines: [action, prompt],
|
||||
};
|
||||
}
|
||||
}
|
||||
// const foundError = knownErrors.find((err) =>
|
||||
// remainder.toLowerCase().includes(err.toLowerCase()),
|
||||
// );
|
||||
|
||||
// if (foundError) {
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "error",
|
||||
// action,
|
||||
// prompt,
|
||||
// message: foundError,
|
||||
// lines: [action, prompt, foundError],
|
||||
// };
|
||||
// }
|
||||
|
||||
// if (remainder) {
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "prompt",
|
||||
// action,
|
||||
// prompt,
|
||||
// message: remainder,
|
||||
// lines: [action, prompt, remainder],
|
||||
// };
|
||||
// }
|
||||
|
||||
// return {
|
||||
// raw: text,
|
||||
// type: "pending",
|
||||
// action,
|
||||
// prompt,
|
||||
// lines: [action, prompt],
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
|
||||
const parseScannerText = (buffer: Buffer) => {
|
||||
const text = buffer.toString("utf8");
|
||||
|
||||
return (
|
||||
text
|
||||
// remove cursor movement like ESC[122C, ESC[2;1H, ESC[8q
|
||||
.replace(/\x1B\[[0-9;]*[A-Za-z]/g, "\n")
|
||||
|
||||
// remove other ANSI sequences like ESC#5
|
||||
.replace(/\x1B#[0-9]/g, "\n")
|
||||
|
||||
// normalize carriage returns
|
||||
.replace(/\r/g, "\n")
|
||||
|
||||
// split into clean lines
|
||||
.split(/\n+/)
|
||||
|
||||
// clean each line
|
||||
.map((line) => line.trim())
|
||||
|
||||
// remove blanks
|
||||
.filter(Boolean)
|
||||
);
|
||||
};
|
||||
|
||||
const parseScannerEvent = (lines: string[]): ScannerEvent => {
|
||||
const scannerId = lines[0];
|
||||
const messageLines = lines.slice(1);
|
||||
const message = messageLines.at(-1);
|
||||
|
||||
const commandDescription = messageLines.find((x) => /^\d+\s+/.test(x));
|
||||
const prompt = messageLines.find((x) => /^Scan:/i.test(x));
|
||||
|
||||
let status: ScannerEvent["status"] = "unknown";
|
||||
|
||||
const msg = message?.toLowerCase() ?? "";
|
||||
|
||||
if (msg === "v") status = "success";
|
||||
else if (msg && ERROR_KEYWORDS.some((keyword) => msg.includes(keyword)))
|
||||
status = "error";
|
||||
else if (msg?.includes("scan")) status = "success";
|
||||
// everything else will just be a location
|
||||
else if (commandDescription?.includes("Relocate")) status = "location";
|
||||
|
||||
// TODO: split command description and use the command id next to description for sorting.
|
||||
return {
|
||||
scannerId,
|
||||
commandDescription,
|
||||
prompt,
|
||||
message,
|
||||
status,
|
||||
lines,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends a Zebra-style TCP message:
|
||||
@@ -154,7 +235,7 @@ export async function sendTcpMessage(
|
||||
const responses: any = [];
|
||||
|
||||
const client = TcpSocket.createConnection({ host, port }, () => {
|
||||
console.log("Sending TCP (visible):", `${command}`);
|
||||
//console.log("Sending TCP (visible):", `${command}`);
|
||||
|
||||
client.write(command);
|
||||
});
|
||||
@@ -170,16 +251,20 @@ export async function sendTcpMessage(
|
||||
}, timeoutMs);
|
||||
|
||||
client.on("data", (data) => {
|
||||
//const text = data.toString();
|
||||
//console.log("TCP received:", text);
|
||||
const parsed = parseErpResponse(data);
|
||||
const parsed = parseScannerText(data);
|
||||
//console.log("scanned:", parsed);
|
||||
|
||||
responses.push(parsed);
|
||||
//responses.push(parsed);
|
||||
|
||||
const cleaned = parseScannerEvent(parsed);
|
||||
|
||||
//console.log(responses);
|
||||
clearTimeout(timeout);
|
||||
resolve({
|
||||
success: true,
|
||||
message: "TCP Response",
|
||||
data: responses,
|
||||
data: cleaned as any,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -190,7 +275,7 @@ export async function sendTcpMessage(
|
||||
resolve({
|
||||
success: false,
|
||||
message: err.message,
|
||||
data: responses,
|
||||
data: ["Error", "Please try again"],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -200,7 +285,7 @@ export async function sendTcpMessage(
|
||||
resolve({
|
||||
success: true,
|
||||
message: "TCP complete",
|
||||
data: responses,
|
||||
data: ["Error", "Please try again"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user