feat(mobile): update notifications and more error handling added
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s

This commit is contained in:
2026-04-30 17:02:21 -05:00
parent bb6155c969
commit 30ffd843c7
24 changed files with 2784 additions and 200 deletions

View File

@@ -15,6 +15,15 @@ export default function TabsLayout() {
options={{
title: "Scan",
tabBarIcon: ({ color, size }) => <Home size={size} color={color} />,
// header: ({ route }) => {
// const version = serverVersion?.versionCode;
// const hasUpdate = version && version > build;
// if (!hasUpdate) return null; // 👈 hides header completely
// return <GlobalHeader title={route.name} />;
// },
}}
/>
<Tabs.Screen

View File

@@ -2,6 +2,7 @@ import { Stack } from "expo-router";
import { StatusBar } from "expo-status-bar";
import "../../global.css";
import { PortalHost } from "@rn-primitives/portal";
import { View } from "react-native";
export default function RootLayout() {
return (
@@ -9,7 +10,17 @@ export default function RootLayout() {
<StatusBar style="dark" />
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen name="index" />
{/* <Stack.Screen name="(tabs)" /> */}
<View className="items-center">
<Stack.Screen
name="(tabs)"
options={{
title: "Pending update",
headerStyle: {
backgroundColor: "lightblue",
},
}}
/>
</View>
</Stack>
<PortalHost />
</>

View File

@@ -1,18 +1,22 @@
import axios from "axios";
import Constants from "expo-constants";
import { Redirect, useRouter } from "expo-router";
import { useEffect, useState } from "react";
import { ActivityIndicator, Text, View } from "react-native";
import { useAppStore } from "../hooks/useAppStore";
import { useServerStore } from "../hooks/useServerCheck";
import { devDelay } from "../lib/devMode";
export default function Index() {
const router = useRouter();
const [message, setMessage] = useState(<Text>Starting app...</Text>);
const [ready, setReady] = useState(false);
const setServerVersion = useServerStore((s) => s.setServerVersion);
const hasHydrated = useAppStore((s) => s.hasHydrated);
const serverPort = useAppStore((s) => s.serverPort);
const serverIp = useAppStore((s) => s.serverIp);
const build = Constants.expoConfig?.android?.versionCode ?? 1;
const hasValidSetup = useAppStore((s) => s.hasValidSetup);
useEffect(() => {
@@ -46,6 +50,18 @@ export default function Index() {
);
console.log(res.data);
// if the build version dose not match the latest server version force update
if (res.status === 200) {
setServerVersion(res.data);
}
// TODO: change the header to show orange and theres a new version
// console.log(build < res.data.minSupportedVersionCode);
// if (build < res.data.minSupportedVersionCode) {
// router.replace("/updateScreen");
// return;
// }
} catch (error) {
console.log("Error: ", error);
}
@@ -79,7 +95,14 @@ export default function Index() {
};
startup();
}, [hasHydrated, hasValidSetup, serverPort, serverIp, router]);
}, [
hasHydrated,
hasValidSetup,
serverPort,
serverIp,
router,
setServerVersion,
]);
if (ready) {
return <Redirect href="/(tabs)/scanner" />;

View File

@@ -3,6 +3,7 @@ import { useRouter } from "expo-router";
import { useState } from "react";
import { Alert, Button, Text, TextInput, View } from "react-native";
import { useAppStore } from "../hooks/useAppStore";
import { useServerStore } from "../hooks/useServerCheck";
export default function Setup() {
const router = useRouter();
@@ -22,6 +23,8 @@ export default function Setup() {
const [serverPort, setLocalServerPort] = useState(serverPortFromStore);
const [scannerId, setScannerId] = useState(scannerIdFromStore);
const server = useServerStore((s) => s.serverVersion);
const authCheck = () => {
if (pin === "6971") {
setAuth(true);
@@ -151,8 +154,11 @@ export default function Setup() {
marginBottom: 12,
}}
>
<Text style={{ fontSize: 12, color: "#666" }}>
LST Scanner v{version}-{build}
<Text className="text-[12] color-#666">
App v{version}-{build}
</Text>
<Text className="text-[12] color-#666">
Server version - v{server?.versionName}-{server?.versionCode}
</Text>
</View>
</View>

View File

@@ -1,9 +1,47 @@
import Constants from "expo-constants";
import { Link } from "expo-router";
import { Text, View } from "react-native";
import {
Card,
CardContent,
CardFooter,
CardHeader,
} from "../components/ui/card";
import { Separator } from "../components/ui/separator";
import { useServerStore } from "../hooks/useServerCheck";
export default function blocked() {
export default function Update() {
const version = Constants.expoConfig?.version;
const build = Constants.expoConfig?.android?.versionCode ?? 1;
const server = useServerStore((s) => s.serverVersion);
return (
<View>
<Text>Blocked</Text>
<View className="flex-1 mt-5 p-5">
<Card>
<CardHeader>
<Text className="text-center underline">Update Required</Text>
</CardHeader>
<CardContent>
<Text>Your app is out of date and needs to be updated</Text>
<Separator className="mt-5 mb-5" />
<Text>
App version - v{version}-{build}
</Text>
<Text>
Server version - v{server?.versionName}-{server?.versionCode}
</Text>
<Separator className="mt-5 mb-5" />
<Text>
To update the app please head go to a computer and open LST.
</Text>
<Text>Then head to Scan.</Text>
<Text>Click update Then follow the instructions on screen</Text>
</CardContent>
</Card>
{server && server?.versionCode >= build && (
<Link href={"/"}>
<Text className="text-center underline">Home</Text>
</Link>
)}
</View>
);
}

View File

@@ -1,3 +1,4 @@
import axios from "axios";
import { format } from "date-fns-tz";
import { useCallback, useEffect, useState } from "react";
import { Text, View } from "react-native";
@@ -7,6 +8,8 @@ import { scannerFeedback } from "../lib/feedbackScan";
import { sendTcpMessage } from "../lib/tcpScan";
import { type ZebraScanResult, zebraScanner } from "../lib/ZebraScanner";
import { ScannedLabelBox } from "./ScannedLabels";
import { GlobalFooter } from "./UpdateFooter";
import { Separator } from "./ui/separator";
const STX = "\x02";
const ETX = "\x03";
@@ -23,12 +26,6 @@ export default function ProdScanner() {
const handleScan = useCallback(
async (scan: ZebraScanResult) => {
let commandToSend = `${STX}${scannerIdFromStore}@${scan.data}${ETX}`;
await scannerFeedback({
type: "good",
sound: true,
vibrate: true,
led: true,
});
// if we are sscc we need to scan like this .... <STX>98@]C100090087710038712256<ETX>
if (scan.data.startsWith("000")) {
@@ -42,41 +39,54 @@ export default function ProdScanner() {
]);
}
// if we change commands we want to zero out the last scanned labels
if (/^[a-zA-Z]/.test(scan.data)) {
setTagScans([]);
}
const scanned = (await sendTcpMessage(
commandToSend,
serverIp,
parseInt(serverPort || "0", 10),
)) as any;
// Later this is where your TCP send goes.
// const response = await sendTcpMessage(tcpMessage);
console.log(scanned);
await scannerFeedback({
type: scanned.data[0]?.type === "error" ? "bad" : "good",
sound: true,
vibrate: true,
led: true,
});
// send the logs to lst but allow it to time out if it dose not exist just bc.
if (scanned.data[0]?.type !== "error") {
try {
await axios.post(
`http://${serverIp.trim()}:3000/lst/api/mobile/logs`,
scanned,
);
} catch (error) {
console.log(error);
}
// const response = await sendTcpMessage(tcpMessage);
console.log(scanned.data);
if (scanned.data.status !== "error") {
await scannerFeedback({
type: "good",
sound: true,
vibrate: true,
led: true,
});
setBGColor("bg-green-500");
setTimeout(() => {
setBGColor(null);
}, 1 * 1000);
}
if (scanned.data[0]?.type === "error") {
if (scanned.data.status === "error") {
await scannerFeedback({
type: scanned.data.status === "error" ? "bad" : "good",
sound: true,
vibrate: true,
led: true,
});
setBGColor("bg-red-500");
setTimeout(() => {
setBGColor(null);
}, 1 * 1000);
}
setLastScan(scanned.data[0]);
//console.log("TCP response:", something);
setLastScan(scanned.data);
// if we change commands we want to zero out the last scanned labels
if (/^[a-zA-Z]/.test(scan.data)) {
setTagScans([]);
}
},
[scannerIdFromStore, serverIp, serverPort, setLastScan],
);
@@ -85,7 +95,7 @@ export default function ProdScanner() {
setTagScans([]);
};
console.log(lastScan);
//console.log(lastScan);
useEffect(() => {
zebraScanner.ensureProfile();
@@ -109,6 +119,7 @@ export default function ProdScanner() {
Scanner ID: {parseInt(scannerIdFromStore || "0", 10)}
</Text>
</View>
<Separator />
{!lastScan ? (
<View style={{ marginTop: 10, alignItems: "center" }}>
<Text className="text-xl font-bold">Ready to scan</Text>
@@ -121,18 +132,19 @@ export default function ProdScanner() {
alignItems: "center",
}}
>
<View style={{ marginTop: 10, alignItems: "center" }}>
<Text style={{ fontSize: 20, fontWeight: "600" }}>
{lastScan.action}
</Text>
<Text style={{ fontSize: 20, fontWeight: "600" }}>
{lastScan.message}
</Text>
</View>
{lastScan.lines
?.filter((line) => !/^\d+@$/.test(line))
.map((i) => {
return (
<View style={{ marginTop: 10, alignItems: "center" }} key={i}>
<Text style={{ fontSize: 20, fontWeight: "600" }}>{i}</Text>
</View>
);
})}
</View>
)}
</View>
<Separator className="m-2" />
<View className="flex-1 w-full px-4">
<ScannedLabelBox
labels={tagScans}
@@ -140,6 +152,9 @@ export default function ProdScanner() {
clearScan={clearScans}
/>
</View>
<View>
<GlobalFooter />
</View>
</View>
);
}

View File

@@ -2,6 +2,7 @@ import { ScrollView, View } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Button } from "@/components/ui/button";
import { Text } from "@/components/ui/text";
import { Card } from "./ui/card";
type ScannedLabel = {
label: string;
@@ -29,27 +30,29 @@ export function ScannedLabelBox({
<ScrollView className="w-full flex-1">
{labels.length === 0 ? (
<Text className="text-center">No labels scanned yet</Text>
<Text className="text-center">
pending new labels to be scanned...
</Text>
) : (
<View className="flex items-center gap-2 w-full">
{labels.map((i, index) => (
<View
<Card
key={`${i.label}-${index}`}
className={`p-2 border rounded items-center ${color ?? ""}`}
className={`p-2 border rounded items-center ${color ?? ""} w-full`}
>
<Text style={{ fontSize: 18, fontWeight: "700" }}>
{i.label} - {i.date.toString()}
</Text>
</View>
</Card>
))}
</View>
)}
</ScrollView>
{labels.length !== 0 && (
{/* {labels.length !== 0 && (
<Button onPress={clearScan} variant="secondary">
<Text>Clear Scans</Text>
</Button>
)}
)} */}
</SafeAreaView>
);
}

View File

@@ -0,0 +1,40 @@
import Constants from "expo-constants";
import { Link } from "expo-router";
import { Text, View } from "react-native";
import { useServerStore } from "../hooks/useServerCheck";
export function GlobalFooter() {
const build = Constants.expoConfig?.android?.versionCode ?? 1;
const serverVersion = useServerStore((s) => s.serverVersion);
const hasUpdate =
serverVersion && serverVersion?.minSupportedVersionCode > build;
const shouldUpdate = serverVersion && serverVersion?.versionCode > build;
if (serverVersion && serverVersion?.versionCode <= build) return;
return (
<View>
<View>
{hasUpdate && (
<View className="items-center h-[75px] bg-[#EB091A]">
<Link href={"/updateScreen"}>
<Text className="h-[75px] font-medium text-base text-wrap text-center">
Critical updates pending, once you are completed with your task
please click me for instructions to update
</Text>
</Link>
</View>
)}
{!hasUpdate && shouldUpdate && (
<View className="bg-[#FDBA74]">
<Link href={"/updateScreen"}>
<Text className="h-[32] font-medium text-lg text-wrap text-center">
There is an update click me for instructions
</Text>
</Link>
</View>
)}
</View>
</View>
);
}

View File

@@ -0,0 +1,24 @@
import { cn } from '@/lib/utils';
import * as SeparatorPrimitive from '@rn-primitives/separator';
function Separator({
className,
orientation = 'horizontal',
decorative = true,
...props
}: React.ComponentProps<typeof SeparatorPrimitive.Root>) {
return (
<SeparatorPrimitive.Root
decorative={decorative}
orientation={orientation}
className={cn(
'bg-border shrink-0',
orientation === 'horizontal' ? 'h-[1px] w-full' : 'h-full w-[1px]',
className
)}
{...props}
/>
);
}
export { Separator };

View File

@@ -1,10 +1,12 @@
import { create } from "zustand";
type LastScan = {
action?: string;
type?: "success" | "error" | string;
terminalId?: string;
screen?: string;
prompt?: string;
message?: string;
status: "success" | "error" | "location" | "unknown";
lines?: string[];
timestamp?: number;
};

View File

@@ -0,0 +1,29 @@
import { create } from "zustand";
type ServerVersion = {
packageName: string;
versionName: string;
versionCode: number;
minSupportedVersionCode: number;
downloadUrl: string;
};
type AppState = {
serverVersion: ServerVersion | null;
setServerVersion: (data: ServerVersion) => void;
};
export const useServerStore = create<AppState>((set, get) => ({
serverVersion: null,
hasUpdate: () => {
const v = get().serverVersion;
if (!v) return false;
return v.versionCode < v.minSupportedVersionCode;
},
setServerVersion: (data) =>
set(() => ({
serverVersion: data,
})),
}));

View File

@@ -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"],
});
});
});