forked from fahrPool/fahrPool
fix: FinScanner Abbruch-Race, Fehlversuch-Limit, Escape/Fokus, min-w-0, nativer Probe-Fallback
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
70fa8e27b7
commit
ae74b730f0
|
|
@ -38,7 +38,7 @@ export function FinFeld({
|
||||||
autoCapitalize="characters"
|
autoCapitalize="characters"
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
spellCheck={false}
|
spellCheck={false}
|
||||||
className={`${FIELD_CLASSES} font-mono`}
|
className={`${FIELD_CLASSES} min-w-0 font-mono`}
|
||||||
/>
|
/>
|
||||||
<Button type="button" variant="secondary" fullWidth={false} onClick={() => setScannerOffen(true)}>
|
<Button type="button" variant="secondary" fullWidth={false} onClick={() => setScannerOffen(true)}>
|
||||||
Scannen
|
Scannen
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,11 @@ import { useEffect, useRef, useState } from "react";
|
||||||
import { Alert } from "@/components/ui/Alert";
|
import { Alert } from "@/components/ui/Alert";
|
||||||
import { Button } from "@/components/ui/Button";
|
import { Button } from "@/components/ui/Button";
|
||||||
import { finAusScan } from "@/lib/orte-fahrzeuge/fin";
|
import { finAusScan } from "@/lib/orte-fahrzeuge/fin";
|
||||||
import { kameraFehlerText, UNSICHERER_KONTEXT_TEXT } from "@/lib/orte-fahrzeuge/kamera-fehler";
|
import { kameraFehlerText, SCANNER_LADEFEHLER_TEXT, UNSICHERER_KONTEXT_TEXT } from "@/lib/orte-fahrzeuge/kamera-fehler";
|
||||||
import type { Detector } from "@/lib/orte-fahrzeuge/barcode-detector";
|
import type { Detector } from "@/lib/orte-fahrzeuge/barcode-detector";
|
||||||
|
|
||||||
const SCAN_INTERVALL_MS = 250;
|
const SCAN_INTERVALL_MS = 250;
|
||||||
|
const MAX_FEHLVERSUCHE = 5;
|
||||||
|
|
||||||
export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: string) => void; onSchliessen: () => void }) {
|
export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: string) => void; onSchliessen: () => void }) {
|
||||||
const videoRef = useRef<HTMLVideoElement>(null);
|
const videoRef = useRef<HTMLVideoElement>(null);
|
||||||
|
|
@ -15,9 +16,22 @@ export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: strin
|
||||||
const [hinweis, setHinweis] = useState<string | null>(null);
|
const [hinweis, setHinweis] = useState<string | null>(null);
|
||||||
const [fehler, setFehler] = useState<string | null>(null);
|
const [fehler, setFehler] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const onSchliessenRef = useRef(onSchliessen);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onErkanntRef.current = onErkannt;
|
onErkanntRef.current = onErkannt;
|
||||||
}, [onErkannt]);
|
onSchliessenRef.current = onSchliessen;
|
||||||
|
}, [onErkannt, onSchliessen]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const beiTaste = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === "Escape") {
|
||||||
|
onSchliessenRef.current();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener("keydown", beiTaste);
|
||||||
|
return () => document.removeEventListener("keydown", beiTaste);
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let beendet = false;
|
let beendet = false;
|
||||||
|
|
@ -51,10 +65,12 @@ export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: strin
|
||||||
const { erzeugeBarcodeDetector } = await import("@/lib/orte-fahrzeuge/barcode-detector");
|
const { erzeugeBarcodeDetector } = await import("@/lib/orte-fahrzeuge/barcode-detector");
|
||||||
detector = await erzeugeBarcodeDetector();
|
detector = await erzeugeBarcodeDetector();
|
||||||
} catch {
|
} catch {
|
||||||
setFehler("Der Scanner konnte nicht geladen werden. Bitte die FIN manuell eingeben.");
|
setFehler(SCANNER_LADEFEHLER_TEXT);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let fehlversuche = 0;
|
||||||
|
|
||||||
const pruefe = async () => {
|
const pruefe = async () => {
|
||||||
if (beendet) {
|
if (beendet) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -62,6 +78,10 @@ export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: strin
|
||||||
if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
|
if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
|
||||||
try {
|
try {
|
||||||
const codes = await detector.detect(video);
|
const codes = await detector.detect(video);
|
||||||
|
if (beendet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fehlversuche = 0;
|
||||||
for (const code of codes) {
|
for (const code of codes) {
|
||||||
const fin = finAusScan(code.rawValue);
|
const fin = finAusScan(code.rawValue);
|
||||||
if (fin) {
|
if (fin) {
|
||||||
|
|
@ -73,10 +93,20 @@ export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: strin
|
||||||
setHinweis("Code erkannt, enthält aber keine gültige FIN.");
|
setHinweis("Code erkannt, enthält aber keine gültige FIN.");
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Einzelnes Bild nicht auswertbar -- naechster Versuch.
|
// Einzelnes Bild nicht auswertbar -- naechster Versuch. Bei dauerhaftem
|
||||||
|
// Fehlschlag (z. B. WASM nicht ladbar) aufgeben statt endlos zu haengen.
|
||||||
|
fehlversuche += 1;
|
||||||
|
if (fehlversuche >= MAX_FEHLVERSUCHE) {
|
||||||
|
if (!beendet) {
|
||||||
|
setFehler(SCANNER_LADEFEHLER_TEXT);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer = window.setTimeout(pruefe, SCAN_INTERVALL_MS);
|
if (!beendet) {
|
||||||
|
timer = window.setTimeout(pruefe, SCAN_INTERVALL_MS);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
pruefe();
|
pruefe();
|
||||||
}
|
}
|
||||||
|
|
@ -101,7 +131,7 @@ export function FinScanner({ onErkannt, onSchliessen }: { onErkannt: (fin: strin
|
||||||
) : (
|
) : (
|
||||||
<p className="text-sm text-asphalt-grey">{hinweis ?? "Barcode mit der FIN in den Bildausschnitt halten."}</p>
|
<p className="text-sm text-asphalt-grey">{hinweis ?? "Barcode mit der FIN in den Bildausschnitt halten."}</p>
|
||||||
)}
|
)}
|
||||||
<Button type="button" variant="secondary" onClick={onSchliessen}>
|
<Button type="button" variant="secondary" autoFocus onClick={onSchliessen}>
|
||||||
Abbrechen
|
Abbrechen
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,13 @@ export async function erzeugeBarcodeDetector(): Promise<Detector> {
|
||||||
// stillschweigend nie erkannt.
|
// stillschweigend nie erkannt.
|
||||||
const nativ = (globalThis as { BarcodeDetector?: NativerDetector }).BarcodeDetector;
|
const nativ = (globalThis as { BarcodeDetector?: NativerDetector }).BarcodeDetector;
|
||||||
if (nativ) {
|
if (nativ) {
|
||||||
const unterstuetzt = await nativ.getSupportedFormats();
|
try {
|
||||||
if (FORMATE.every((format) => unterstuetzt.includes(format))) {
|
const unterstuetzt = await nativ.getSupportedFormats();
|
||||||
return new nativ({ formats: [...FORMATE] });
|
if (FORMATE.every((format) => unterstuetzt.includes(format))) {
|
||||||
|
return new nativ({ formats: [...FORMATE] });
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Nativer Detector nicht nutzbar -- weiter mit dem Ponyfill.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
export const UNSICHERER_KONTEXT_TEXT =
|
export const UNSICHERER_KONTEXT_TEXT =
|
||||||
"Die Kamera ist nur über eine sichere Verbindung (HTTPS) verfügbar. Bitte die FIN manuell eingeben.";
|
"Die Kamera ist nur über eine sichere Verbindung (HTTPS) verfügbar. Bitte die FIN manuell eingeben.";
|
||||||
|
|
||||||
|
export const SCANNER_LADEFEHLER_TEXT = "Der Scanner konnte nicht geladen werden. Bitte die FIN manuell eingeben.";
|
||||||
|
|
||||||
export function kameraFehlerText(fehler: unknown): string {
|
export function kameraFehlerText(fehler: unknown): string {
|
||||||
const name =
|
const name =
|
||||||
typeof fehler === "object" && fehler !== null && "name" in fehler ? String((fehler as { name: unknown }).name) : "";
|
typeof fehler === "object" && fehler !== null && "name" in fehler ? String((fehler as { name: unknown }).name) : "";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue