import { normalizeIsbn } from '../shared/isbn.ts'; export const REPEAT_MS = 5000; /** * A barcode read is only believed once the same full code has been decoded on this many separate frames, each * within AGREE_GAP_MS of the one before. The EAN check digit alone is not enough: a blurred frame sometimes * misreads two digits so that the check still passes (about one misread in ten), and the same wrong code can * come up again on the next frame, so two agreeing reads are not safe. Three still land in about 0.3 s on a * clean barcode. */ export const AGREE_READS = 3; export const AGREE_GAP_MS = 1500; /** Remembers when each code was last reported, so a barcode held in view is announced once per 5 seconds. */ export interface ReadState { emittedAt: Record; rejectedAt: Record; /** The ISBN being confirmed: how many frames in a row have read it, and when the latest one did. */ seen: { isbn: string; count: number; at: number } | null; } export function newReadState(): ReadState { return { emittedAt: {}, rejectedAt: {}, seen: null }; } /** * Count one frame's read of a valid ISBN. The count restarts when a different valid ISBN was read in between * or the last agreeing read is older than AGREE_GAP_MS. `agreed` is true once `need` frames in a row agree. */ export function agree(state: ReadState, isbn: string, now: number, need: number = AGREE_READS): { state: ReadState; agreed: boolean } { const s = state.seen; const count = s && s.isbn === isbn && now - s.at <= AGREE_GAP_MS ? s.count + 1 : 1; if (count >= need) return { state: { ...state, seen: null }, agreed: true }; return { state: { ...state, seen: { isbn, count, at: now } }, agreed: false }; } /** * Judge one decoded barcode, one call per frame. A 13-digit code with a correct check digit and a 978 or 979 * prefix counts toward AGREE_READS agreeing frames and is emitted on the last of them; the check digit is only * ever verified, never computed. Anything else (a UPC, a price code, a bad check digit) is rejected. Neither is * repeated within REPEAT_MS. */ export function acceptRead( state: ReadState, code: string, now: number, ): { state: ReadState; emit: string | null; reject: boolean } { const isbn = /^\d{13}$/.test(code) ? normalizeIsbn(code) : null; if (isbn === null) { const last = state.rejectedAt[code]; if (last !== undefined && now - last < REPEAT_MS) return { state, emit: null, reject: false }; return { state: { ...state, rejectedAt: { ...state.rejectedAt, [code]: now } }, emit: null, reject: true }; } const last = state.emittedAt[isbn]; if (last !== undefined && now - last < REPEAT_MS) return { state: { ...state, seen: null }, emit: null, reject: false }; const a = agree(state, isbn, now); if (!a.agreed) return { state: a.state, emit: null, reject: false }; return { state: { ...a.state, emittedAt: { ...a.state.emittedAt, [isbn]: now } }, emit: isbn, reject: false }; } /** Record that an ISBN was just reported by some other path (the Capture button), so the loop stays quiet. */ export function markEmitted(state: ReadState, isbn: string, now: number): ReadState { return { ...state, emittedAt: { ...state.emittedAt, [isbn]: now } }; }