Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6aff8f512 | ||
|
|
1c277d1944 | ||
|
|
4083c05b72 | ||
|
|
7b4d24a212 |
@@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import {computed, onMounted, ref, watch} from 'vue';
|
||||
import {computed, onMounted, onUnmounted, ref, watch} from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
|
||||
import {BSwitch} from "buefy";
|
||||
@@ -18,6 +18,39 @@ const selfServeEnabled = ref(false);
|
||||
const isLoadingSelfServeEnabled = ref(false);
|
||||
const isSavingSelfServeEnabled = ref(false);
|
||||
const bookingsCount = ref(0);
|
||||
const selfServeLoadingMinimumMs = import.meta.env.MODE === "test" ? 0 : import.meta.env.VITE_IS_PLAYWRIGHT ? 5000 : 250;
|
||||
const selfServeLoadingStartedAt = ref(0);
|
||||
let selfServeLoadingTimer = null;
|
||||
|
||||
const clearSelfServeLoadingTimer = () => {
|
||||
if (selfServeLoadingTimer !== null) {
|
||||
clearTimeout(selfServeLoadingTimer);
|
||||
selfServeLoadingTimer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const setSelfServeLoading = (isLoading) => {
|
||||
if (isLoading) {
|
||||
clearSelfServeLoadingTimer();
|
||||
selfServeLoadingStartedAt.value = Date.now();
|
||||
isLoadingSelfServeEnabled.value = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const elapsed = Date.now() - selfServeLoadingStartedAt.value;
|
||||
const remaining = Math.max(0, selfServeLoadingMinimumMs - elapsed);
|
||||
clearSelfServeLoadingTimer();
|
||||
|
||||
if (remaining === 0) {
|
||||
isLoadingSelfServeEnabled.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
selfServeLoadingTimer = setTimeout(() => {
|
||||
isLoadingSelfServeEnabled.value = false;
|
||||
selfServeLoadingTimer = null;
|
||||
}, remaining);
|
||||
};
|
||||
|
||||
const getDepartmentId = () => {
|
||||
return parseInt(router.currentRoute.value.params.departmentId);
|
||||
@@ -190,13 +223,13 @@ const getSelfServeStatus = async () => {
|
||||
const departmentId = getDepartmentId();
|
||||
if (!departmentId) return;
|
||||
|
||||
isLoadingSelfServeEnabled.value = true;
|
||||
setSelfServeLoading(true);
|
||||
try {
|
||||
selfServeEnabled.value = await getDepartmentSelfServeEnabled(departmentId);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch self-serve status", error);
|
||||
} finally {
|
||||
isLoadingSelfServeEnabled.value = false;
|
||||
setSelfServeLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -204,6 +237,10 @@ onMounted(() => {
|
||||
getSelfServeStatus();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
clearSelfServeLoadingTimer();
|
||||
});
|
||||
|
||||
// Watch the departmentId
|
||||
watch(() => router.currentRoute.value.params.departmentId, () => {
|
||||
getTodaysBookings();
|
||||
|
||||
@@ -24,71 +24,79 @@ import {
|
||||
} from "./objects/PosDepartmentStepMobileFlow.vue";
|
||||
import { PosSearchResult } from "./objects/PosSearchResult.vue";
|
||||
import RegistrationNumberSearchResult from "@/components/models/pos/step1/RegistrationNumberSearchResult.vue";
|
||||
import UnknownCustomer from "@/components/viewport/elements/icons/UnknownCustomer.vue";
|
||||
import VerifiedCustomer from "@/components/viewport/elements/icons/VerifiedCustomer.vue";
|
||||
import BookedCustomer from "@/components/viewport/elements/icons/BookedCustomer.vue";
|
||||
import KnownCustomer from "@/components/viewport/elements/icons/KnownCustomer.vue";
|
||||
import CardPaymentCustomer from "@/components/viewport/elements/icons/CardPaymentCustomer.vue";
|
||||
import PosDepartmentStepMobileFixedBottomControl from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobileFixedBottomControl.vue";
|
||||
import PosDepartmentStepMobile1Location from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobile1Location.vue";
|
||||
import PosDepartmentStepMobile1Debug from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobile1Debug.vue";
|
||||
import PosDepartmentStepMobileAttachments from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobileAttachments.vue";
|
||||
import PosDepartmentStep1MobileTransactionHistory from "@/components/displays/department/pos/steps/mobile/views/PosDepartmentStep1MobileTransactionHistory.vue";
|
||||
import { attachments } from "@/components/displays/department/pos/steps/mobile/objects/PosDepartmentStepMobileFlow.vue";
|
||||
import {
|
||||
LPR_FRAME_CLIENT_BYTES_FIELD,
|
||||
LPR_FRAME_CLIENT_CAPTURE_MS_FIELD,
|
||||
LPR_FRAME_CLIENT_DRAW_MS_FIELD,
|
||||
LPR_FRAME_CLIENT_ENCODE_MS_FIELD,
|
||||
LPR_FRAME_CLIENT_HEIGHT_FIELD,
|
||||
LPR_FRAME_CLIENT_PREFLIGHT_MS_FIELD,
|
||||
LPR_FRAME_CLIENT_VISUAL_FINGERPRINT_MS_FIELD,
|
||||
LPR_FRAME_CLIENT_WIDTH_FIELD,
|
||||
getVisualFingerprintDistance,
|
||||
type LPRFrameEncodeCandidate,
|
||||
type LPRFramePayload,
|
||||
type LPRFrameViewportRect,
|
||||
} from "@/components/viewport/page/templates/scanner/lprFrameCapture";
|
||||
// Debug mode flag
|
||||
const debug_mode = ref(false);
|
||||
// Debug array to store request results
|
||||
const debug_request_results = ref([]);
|
||||
const debug_request_results = ref<unknown[]>([]);
|
||||
|
||||
const lastParsedImage = ref(null);
|
||||
const setLastCapturedImage = (image: string) => {
|
||||
camera.latestImage.value = image;
|
||||
type ParsedFrameFingerprint = {
|
||||
content: string | null;
|
||||
contentFingerprintPromise: Promise<string> | null;
|
||||
getContentFingerprint: (() => Promise<string>) | null;
|
||||
getVisualFingerprint: (() => string | null) | null;
|
||||
outcome: "pending" | "miss" | "success";
|
||||
quick: string;
|
||||
visual: string | null;
|
||||
};
|
||||
|
||||
const lastParsedImage = ref<ParsedFrameFingerprint | null>(null);
|
||||
const scannerFocusRef = ref<HTMLElement | null>(null);
|
||||
|
||||
type LPRResponse = {
|
||||
success: boolean;
|
||||
license_plate_number: string;
|
||||
};
|
||||
|
||||
const latestLPRResponse = ref<LPRResponse | null>(null);
|
||||
const isLPRRequestInFlight = ref(false);
|
||||
const LPR_IMAGE_MAX_WIDTH = 1280;
|
||||
const LPR_IMAGE_MAX_HEIGHT = 720;
|
||||
const LPR_IMAGE_JPEG_QUALITY = 0.72;
|
||||
|
||||
const compressImageForLPR = (image: string): Promise<string> => {
|
||||
return new Promise((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const sourceWidth = img.naturalWidth || img.width;
|
||||
const sourceHeight = img.naturalHeight || img.height;
|
||||
if (!sourceWidth || !sourceHeight) {
|
||||
resolve(image);
|
||||
return;
|
||||
}
|
||||
|
||||
const scale = Math.min(1, LPR_IMAGE_MAX_WIDTH / sourceWidth, LPR_IMAGE_MAX_HEIGHT / sourceHeight);
|
||||
const targetWidth = Math.max(1, Math.round(sourceWidth * scale));
|
||||
const targetHeight = Math.max(1, Math.round(sourceHeight * scale));
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = targetWidth;
|
||||
canvas.height = targetHeight;
|
||||
|
||||
const context = canvas.getContext("2d");
|
||||
if (!context) {
|
||||
resolve(image);
|
||||
return;
|
||||
}
|
||||
|
||||
context.drawImage(img, 0, 0, targetWidth, targetHeight);
|
||||
resolve(canvas.toDataURL("image/jpeg", LPR_IMAGE_JPEG_QUALITY));
|
||||
};
|
||||
img.onerror = () => resolve(image);
|
||||
img.src = image;
|
||||
});
|
||||
type LPRScanContext = {
|
||||
activeVehicleIndex: number;
|
||||
attachmentView: boolean;
|
||||
manualInput: boolean;
|
||||
registrationNumbers: string[];
|
||||
transactionHistoryView: boolean;
|
||||
};
|
||||
|
||||
const latestLPRResponse = ref<LPRResponse | null>(null);
|
||||
const isLPRFrameProcessing = ref(false);
|
||||
const isLPRRequestInFlight = ref(false);
|
||||
const isNoPlateBackoffActive = ref(false);
|
||||
const isDuplicateFrameBackoffActive = ref(false);
|
||||
const isSuccessCooldownActive = ref(false);
|
||||
let lprRequestAbortController: AbortController | null = null;
|
||||
let noPlateBackoffTimerId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
let duplicateFrameBackoffTimerId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
let successCooldownTimerId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
|
||||
const NO_PLATE_BACKOFF_DELAYS_MS = [1500, 2500, 4000];
|
||||
const LPR_VISUAL_DUPLICATE_DISTANCE_THRESHOLD = 4;
|
||||
const LPR_VISUAL_DUPLICATE_RECHECK_DELAY_MS = 700;
|
||||
const LPR_ENDPOINT = "/modules/scanner/lpr";
|
||||
let consecutiveNoPlateResponses = 0;
|
||||
|
||||
const nowMs = (): number =>
|
||||
typeof performance !== "undefined" && typeof performance.now === "function"
|
||||
? performance.now()
|
||||
: Date.now();
|
||||
|
||||
const activeVehicleIndexNext = () => {
|
||||
// Increment the active vehicle index, wrapping around if necessary
|
||||
if (vehicles.activeVehicleIndex.value < 3) {
|
||||
@@ -116,48 +124,511 @@ const handleLPRResult = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const parseImage = async (image: string) => {
|
||||
if (isLPRRequestInFlight.value) {
|
||||
type LPRFrameInput = string | LPRFramePayload;
|
||||
|
||||
const isLPRFramePayload = (image: LPRFrameInput): image is LPRFramePayload =>
|
||||
typeof image === "object" && image !== null && image.blob instanceof Blob;
|
||||
|
||||
const getLPRFrameFingerprint = (image: LPRFrameInput): string =>
|
||||
isLPRFramePayload(image) ? image.fingerprint : image;
|
||||
|
||||
const getLPRFrameContentFingerprint = (image: LPRFrameInput): (() => Promise<string>) | null =>
|
||||
isLPRFramePayload(image) ? image.getContentFingerprint ?? null : null;
|
||||
|
||||
const getLPRFrameVisualFingerprint = (image: LPRFrameInput): string | null =>
|
||||
isLPRFramePayload(image) ? image.visualFingerprint ?? null : null;
|
||||
|
||||
const getLPRFrameVisualFingerprintGetter = (image: LPRFrameInput): (() => string | null) | null =>
|
||||
isLPRFramePayload(image) ? image.getVisualFingerprint ?? null : null;
|
||||
|
||||
const rememberParsedImageFingerprint = (image: LPRFrameInput) => {
|
||||
const quick = getLPRFrameFingerprint(image);
|
||||
const getContentFingerprint = getLPRFrameContentFingerprint(image);
|
||||
const entry: ParsedFrameFingerprint = {
|
||||
content: getContentFingerprint === null ? quick : null,
|
||||
contentFingerprintPromise: null,
|
||||
getContentFingerprint,
|
||||
getVisualFingerprint: getLPRFrameVisualFingerprintGetter(image),
|
||||
outcome: "pending",
|
||||
quick,
|
||||
visual: getLPRFrameVisualFingerprint(image),
|
||||
};
|
||||
|
||||
lastParsedImage.value = entry;
|
||||
};
|
||||
|
||||
const markLastParsedImageOutcome = (outcome: ParsedFrameFingerprint["outcome"]) => {
|
||||
if (lastParsedImage.value !== null) {
|
||||
if (outcome === "miss" && lastParsedImage.value.visual === null) {
|
||||
lastParsedImage.value.visual = lastParsedImage.value.getVisualFingerprint?.() ?? null;
|
||||
}
|
||||
|
||||
lastParsedImage.value.outcome = outcome;
|
||||
}
|
||||
};
|
||||
|
||||
const resetParsedImageFingerprint = () => {
|
||||
lastParsedImage.value = null;
|
||||
};
|
||||
|
||||
const resolveParsedFrameContentFingerprint = (entry: ParsedFrameFingerprint): Promise<string> | null => {
|
||||
if (entry.content !== null) {
|
||||
return Promise.resolve(entry.content);
|
||||
}
|
||||
|
||||
if (entry.getContentFingerprint === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
entry.contentFingerprintPromise ??= entry.getContentFingerprint().then((content) => {
|
||||
if (lastParsedImage.value === entry) {
|
||||
entry.content = content;
|
||||
}
|
||||
|
||||
return content;
|
||||
}).catch((error) => {
|
||||
if (lastParsedImage.value === entry) {
|
||||
entry.contentFingerprintPromise = null;
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
|
||||
return entry.contentFingerprintPromise;
|
||||
};
|
||||
|
||||
const isVisuallySimilarToLastMiss = (visualFingerprint: string | null | undefined): boolean => {
|
||||
const lastParsed = lastParsedImage.value;
|
||||
|
||||
return lastParsed !== null
|
||||
&& lastParsed.outcome === "miss"
|
||||
&& getVisualFingerprintDistance(lastParsed.visual, visualFingerprint) <= LPR_VISUAL_DUPLICATE_DISTANCE_THRESHOLD;
|
||||
};
|
||||
|
||||
const hasLastMissVisualFingerprint = (): boolean =>
|
||||
lastParsedImage.value !== null
|
||||
&& lastParsedImage.value.outcome === "miss"
|
||||
&& lastParsedImage.value.visual !== null;
|
||||
|
||||
const shouldBuildLPRVisualFingerprint = (): boolean =>
|
||||
hasLastMissVisualFingerprint();
|
||||
|
||||
const shouldSkipDuplicateFrame = async (image: LPRFrameInput): Promise<boolean> => {
|
||||
const quick = getLPRFrameFingerprint(image);
|
||||
const lastParsed = lastParsedImage.value;
|
||||
|
||||
if (lastParsed === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isVisuallySimilarToLastMiss(getLPRFrameVisualFingerprint(image))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lastParsed.quick !== quick) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentContentFingerprint = getLPRFrameContentFingerprint(image);
|
||||
const lastContentFingerprint = resolveParsedFrameContentFingerprint(lastParsed);
|
||||
if (lastContentFingerprint === null || currentContentFingerprint === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const [lastContent, currentContent] = await Promise.all([
|
||||
lastContentFingerprint,
|
||||
currentContentFingerprint(),
|
||||
]);
|
||||
|
||||
return lastContent === currentContent;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const appendFiniteTimingParam = (
|
||||
queryParts: string[],
|
||||
field: string,
|
||||
value: number | null | undefined
|
||||
) => {
|
||||
if (value === null || value === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const numericValue = Number(value);
|
||||
if (!Number.isFinite(numericValue) || numericValue < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const roundedValue = numericValue.toFixed(3);
|
||||
if (Number(roundedValue) > 0) {
|
||||
queryParts.push(`${field}=${roundedValue}`);
|
||||
}
|
||||
};
|
||||
|
||||
const appendPositiveIntegerParam = (
|
||||
queryParts: string[],
|
||||
field: string,
|
||||
value: number | null | undefined
|
||||
) => {
|
||||
const numericValue = Number(value);
|
||||
if (!Number.isFinite(numericValue) || numericValue <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
queryParts.push(`${field}=${Math.round(numericValue)}`);
|
||||
};
|
||||
|
||||
type LPRRequestPayload = Blob | { base64_image: string };
|
||||
type LPRRequestBuildResult = {
|
||||
headers?: Record<string, string>;
|
||||
payload: LPRRequestPayload;
|
||||
url: string;
|
||||
};
|
||||
|
||||
const buildLPRRequestPayload = (
|
||||
image: LPRFrameInput,
|
||||
clientPreflightDurationMs: number | null = null
|
||||
): LPRRequestBuildResult => {
|
||||
if (!isLPRFramePayload(image)) {
|
||||
return {
|
||||
payload: { base64_image: image },
|
||||
url: LPR_ENDPOINT,
|
||||
};
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": image.mimeType || image.blob.type || "image/jpeg",
|
||||
};
|
||||
const queryParts: string[] = [];
|
||||
appendFiniteTimingParam(queryParts, LPR_FRAME_CLIENT_CAPTURE_MS_FIELD, image.captureDurationMs);
|
||||
appendFiniteTimingParam(queryParts, LPR_FRAME_CLIENT_PREFLIGHT_MS_FIELD, clientPreflightDurationMs);
|
||||
appendFiniteTimingParam(queryParts, LPR_FRAME_CLIENT_DRAW_MS_FIELD, image.captureTimings?.drawMs);
|
||||
appendFiniteTimingParam(queryParts, LPR_FRAME_CLIENT_ENCODE_MS_FIELD, image.captureTimings?.encodeMs);
|
||||
appendFiniteTimingParam(
|
||||
queryParts,
|
||||
LPR_FRAME_CLIENT_VISUAL_FINGERPRINT_MS_FIELD,
|
||||
image.captureTimings?.visualFingerprintMs
|
||||
);
|
||||
appendPositiveIntegerParam(queryParts, LPR_FRAME_CLIENT_WIDTH_FIELD, image.width);
|
||||
appendPositiveIntegerParam(queryParts, LPR_FRAME_CLIENT_HEIGHT_FIELD, image.height);
|
||||
appendPositiveIntegerParam(queryParts, LPR_FRAME_CLIENT_BYTES_FIELD, image.blob.size);
|
||||
const queryString = queryParts.join("&");
|
||||
|
||||
return {
|
||||
headers,
|
||||
payload: image.blob,
|
||||
url: queryString ? `${LPR_ENDPOINT}?${queryString}` : LPR_ENDPOINT,
|
||||
};
|
||||
};
|
||||
|
||||
type LPRCurrentStateSkipOptions = {
|
||||
ignoreNoPlateBackoff?: boolean;
|
||||
};
|
||||
|
||||
const shouldEncodeLPRFrame = (candidate: LPRFrameEncodeCandidate): boolean => {
|
||||
if (views.attachmentView.value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
shouldSkipLPRForCurrentState({ ignoreNoPlateBackoff: true })
|
||||
|| isLPRFrameProcessing.value
|
||||
|| isLPRRequestInFlight.value
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isNoPlateBackoffActive.value) {
|
||||
if (!hasLastMissVisualFingerprint() || isVisuallySimilarToLastMiss(candidate.visualFingerprint)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
clearNoPlateBackoff();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!isVisuallySimilarToLastMiss(candidate.visualFingerprint)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
scheduleDuplicateFrameBackoff();
|
||||
return false;
|
||||
};
|
||||
|
||||
const rememberLatestCameraImage = (image: LPRFrameInput) => {
|
||||
if (isLPRFramePayload(image)) {
|
||||
camera.setLatestImageBlob(image.blob);
|
||||
return;
|
||||
}
|
||||
|
||||
camera.setLatestImage(image);
|
||||
};
|
||||
|
||||
const hasRegistrationNumber = (registrationNumber: string | null | undefined): boolean =>
|
||||
String(registrationNumber ?? "").trim().length > 0;
|
||||
|
||||
const isActiveRegistrationSlotFilled = (): boolean =>
|
||||
hasRegistrationNumber(vehicles.getActiveVehicle()?.reg);
|
||||
|
||||
const areAllRegistrationSlotsFilled = (): boolean =>
|
||||
[1, 2, 3].every((vehicleIndex) => hasRegistrationNumber(vehicles.get(vehicleIndex)?.reg));
|
||||
|
||||
const shouldSkipLPRForCurrentState = (options: LPRCurrentStateSkipOptions = {}): boolean =>
|
||||
views.attachmentView.value
|
||||
|| isActiveRegistrationSlotFilled()
|
||||
|| areAllRegistrationSlotsFilled()
|
||||
|| (!options.ignoreNoPlateBackoff && isNoPlateBackoffActive.value)
|
||||
|| isDuplicateFrameBackoffActive.value
|
||||
|| isSuccessCooldownActive.value;
|
||||
|
||||
const getScannerFocusViewportRect = (): LPRFrameViewportRect | null => {
|
||||
if (views.attachmentView.value || scannerFocusRef.value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rect = scannerFocusRef.value.getBoundingClientRect();
|
||||
if (
|
||||
!Number.isFinite(rect.width) ||
|
||||
!Number.isFinite(rect.height) ||
|
||||
rect.width <= 0 ||
|
||||
rect.height <= 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
height: rect.height,
|
||||
width: rect.width,
|
||||
x: rect.left,
|
||||
y: rect.top,
|
||||
};
|
||||
};
|
||||
|
||||
const isCameraFrameCaptureEnabled = computed(() => {
|
||||
if (views.attachmentView.value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !isLPRFrameProcessing.value
|
||||
&& !isLPRRequestInFlight.value
|
||||
&& (!isNoPlateBackoffActive.value || hasLastMissVisualFingerprint())
|
||||
&& !isDuplicateFrameBackoffActive.value
|
||||
&& !isSuccessCooldownActive.value
|
||||
&& !isActiveRegistrationSlotFilled()
|
||||
&& !areAllRegistrationSlotsFilled();
|
||||
});
|
||||
|
||||
const shouldPauseScannerPreview = computed(() =>
|
||||
!views.attachmentView.value
|
||||
&& (
|
||||
isLPRFrameProcessing.value
|
||||
|| isLPRRequestInFlight.value
|
||||
|| isDuplicateFrameBackoffActive.value
|
||||
|| isSuccessCooldownActive.value
|
||||
|| isActiveRegistrationSlotFilled()
|
||||
|| areAllRegistrationSlotsFilled()
|
||||
|| (isNoPlateBackoffActive.value && !hasLastMissVisualFingerprint())
|
||||
)
|
||||
);
|
||||
|
||||
const lprCameraCaptureIntervalMs = computed(() =>
|
||||
!views.attachmentView.value && isNoPlateBackoffActive.value && hasLastMissVisualFingerprint()
|
||||
? LPR_VISUAL_DUPLICATE_RECHECK_DELAY_MS
|
||||
: null
|
||||
);
|
||||
|
||||
const abortLPRRequest = () => {
|
||||
lprRequestAbortController?.abort();
|
||||
lprRequestAbortController = null;
|
||||
};
|
||||
|
||||
const isDocumentHidden = (): boolean =>
|
||||
typeof document !== "undefined" && document.visibilityState === "hidden";
|
||||
|
||||
const handleDocumentVisibilityChange = () => {
|
||||
if (!isDocumentHidden()) {
|
||||
return;
|
||||
}
|
||||
|
||||
abortLPRRequest();
|
||||
resetParsedImageFingerprint();
|
||||
resetNoPlateBackoff();
|
||||
};
|
||||
|
||||
const clearNoPlateBackoff = () => {
|
||||
if (noPlateBackoffTimerId !== null) {
|
||||
window.clearTimeout(noPlateBackoffTimerId);
|
||||
noPlateBackoffTimerId = null;
|
||||
}
|
||||
isNoPlateBackoffActive.value = false;
|
||||
};
|
||||
|
||||
const clearDuplicateFrameBackoff = () => {
|
||||
if (duplicateFrameBackoffTimerId !== null) {
|
||||
window.clearTimeout(duplicateFrameBackoffTimerId);
|
||||
duplicateFrameBackoffTimerId = null;
|
||||
}
|
||||
isDuplicateFrameBackoffActive.value = false;
|
||||
};
|
||||
|
||||
const clearSuccessCooldown = () => {
|
||||
if (successCooldownTimerId !== null) {
|
||||
window.clearTimeout(successCooldownTimerId);
|
||||
successCooldownTimerId = null;
|
||||
}
|
||||
isSuccessCooldownActive.value = false;
|
||||
};
|
||||
|
||||
const resetNoPlateBackoff = () => {
|
||||
consecutiveNoPlateResponses = 0;
|
||||
clearNoPlateBackoff();
|
||||
clearDuplicateFrameBackoff();
|
||||
clearSuccessCooldown();
|
||||
};
|
||||
|
||||
const scheduleNoPlateBackoff = () => {
|
||||
consecutiveNoPlateResponses += 1;
|
||||
const delay = NO_PLATE_BACKOFF_DELAYS_MS[
|
||||
Math.min(consecutiveNoPlateResponses - 1, NO_PLATE_BACKOFF_DELAYS_MS.length - 1)
|
||||
];
|
||||
|
||||
clearNoPlateBackoff();
|
||||
isNoPlateBackoffActive.value = true;
|
||||
noPlateBackoffTimerId = window.setTimeout(() => {
|
||||
noPlateBackoffTimerId = null;
|
||||
isNoPlateBackoffActive.value = false;
|
||||
}, delay);
|
||||
};
|
||||
|
||||
const scheduleDuplicateFrameBackoff = () => {
|
||||
clearDuplicateFrameBackoff();
|
||||
isDuplicateFrameBackoffActive.value = true;
|
||||
duplicateFrameBackoffTimerId = window.setTimeout(() => {
|
||||
duplicateFrameBackoffTimerId = null;
|
||||
isDuplicateFrameBackoffActive.value = false;
|
||||
}, Math.min(camera.getImageCaptureDelay(false), LPR_VISUAL_DUPLICATE_RECHECK_DELAY_MS));
|
||||
};
|
||||
|
||||
const scheduleSuccessCooldown = () => {
|
||||
clearSuccessCooldown();
|
||||
isSuccessCooldownActive.value = true;
|
||||
successCooldownTimerId = window.setTimeout(() => {
|
||||
successCooldownTimerId = null;
|
||||
isSuccessCooldownActive.value = false;
|
||||
}, camera.getImageCaptureDelayAfterSuccess());
|
||||
};
|
||||
|
||||
const isAbortError = (error: unknown): boolean => {
|
||||
if (error instanceof DOMException && error.name === "AbortError") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return typeof error === "object" && error !== null && (error as { name?: string; code?: string }).code === "ERR_CANCELED";
|
||||
};
|
||||
|
||||
const isSameLPRScanContext = (first: LPRScanContext, second: LPRScanContext): boolean =>
|
||||
first.activeVehicleIndex === second.activeVehicleIndex
|
||||
&& first.attachmentView === second.attachmentView
|
||||
&& first.manualInput === second.manualInput
|
||||
&& first.transactionHistoryView === second.transactionHistoryView
|
||||
&& first.registrationNumbers.length === second.registrationNumbers.length
|
||||
&& first.registrationNumbers.every((registrationNumber, index) => registrationNumber === second.registrationNumbers[index]);
|
||||
|
||||
const parseImage = async (image: LPRFrameInput) => {
|
||||
if (views.attachmentView.value) {
|
||||
rememberLatestCameraImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
if (shouldSkipLPRForCurrentState() || isLPRFrameProcessing.value || isLPRRequestInFlight.value) {
|
||||
return;
|
||||
}
|
||||
// Check if the time since the last successful parse is enough
|
||||
if (!camera.hasDelayAfterSuccessPassed()) {
|
||||
return;
|
||||
}
|
||||
if (lastParsedImage.value === image) {
|
||||
// If the image is the same as the last parsed one, skip parsing
|
||||
return;
|
||||
}
|
||||
lastParsedImage.value = image; // Update the last parsed image
|
||||
camera.setLatestImage(image); // Update the latest image in the camera object
|
||||
isLPRRequestInFlight.value = true;
|
||||
|
||||
isLPRFrameProcessing.value = true;
|
||||
try {
|
||||
// Function to parse the image data
|
||||
const compressedImage = await compressImageForLPR(image);
|
||||
const response = await SessionUser.request("/modules/scanner/lpr", "POST", {
|
||||
base64_image: compressedImage,
|
||||
});
|
||||
const clientPreflightStartedAt = nowMs();
|
||||
const isDuplicateFrame = await shouldSkipDuplicateFrame(image);
|
||||
const clientPreflightDurationMs = Math.max(0, nowMs() - clientPreflightStartedAt);
|
||||
|
||||
if (debug_mode.value) {
|
||||
debug_request_results.value.push(response);
|
||||
}
|
||||
// If the response is not successful, stop here.
|
||||
if (!response.data.success) {
|
||||
if (shouldSkipLPRForCurrentState()) {
|
||||
return;
|
||||
}
|
||||
latestLPRResponse.value = response.data.data as LPRResponse;
|
||||
// Set the last successful capture time
|
||||
camera.setLastSuccess();
|
||||
// Handle parsed result.
|
||||
handleLPRResult();
|
||||
} catch (error) {
|
||||
if (debug_mode.value) {
|
||||
debug_request_results.value.push(error);
|
||||
|
||||
if (isDuplicateFrame) {
|
||||
// If the image is the same as the last parsed one, skip parsing
|
||||
scheduleDuplicateFrameBackoff();
|
||||
return;
|
||||
}
|
||||
|
||||
rememberParsedImageFingerprint(image); // Update the last parsed image
|
||||
isLPRRequestInFlight.value = true;
|
||||
const abortController = new AbortController();
|
||||
lprRequestAbortController = abortController;
|
||||
const requestScanContext = getLPRScanContext();
|
||||
const lprRequest = buildLPRRequestPayload(image, clientPreflightDurationMs);
|
||||
|
||||
try {
|
||||
const response = await SessionUser.request(
|
||||
lprRequest.url,
|
||||
"POST",
|
||||
lprRequest.payload,
|
||||
null,
|
||||
null,
|
||||
{
|
||||
...(lprRequest.headers ? { headers: lprRequest.headers } : {}),
|
||||
signal: abortController.signal,
|
||||
transport: "fetch",
|
||||
}
|
||||
);
|
||||
|
||||
if (debug_mode.value) {
|
||||
debug_request_results.value.push(response);
|
||||
}
|
||||
|
||||
if (!isSameLPRScanContext(requestScanContext, getLPRScanContext())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the response is not successful, stop here.
|
||||
if (!response.data.success) {
|
||||
markLastParsedImageOutcome("miss");
|
||||
scheduleNoPlateBackoff();
|
||||
return;
|
||||
}
|
||||
resetNoPlateBackoff();
|
||||
markLastParsedImageOutcome("success");
|
||||
rememberLatestCameraImage(image);
|
||||
latestLPRResponse.value = response.data.data as LPRResponse;
|
||||
// Set the last successful capture time
|
||||
camera.setLastSuccess();
|
||||
scheduleSuccessCooldown();
|
||||
// Handle parsed result.
|
||||
handleLPRResult();
|
||||
} catch (error) {
|
||||
if (isAbortError(error)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (debug_mode.value) {
|
||||
debug_request_results.value.push(error);
|
||||
}
|
||||
markLastParsedImageOutcome("miss");
|
||||
scheduleNoPlateBackoff();
|
||||
//console.error("Error parsing image:", error);
|
||||
} finally {
|
||||
if (lprRequestAbortController === abortController) {
|
||||
lprRequestAbortController = null;
|
||||
}
|
||||
isLPRRequestInFlight.value = false;
|
||||
}
|
||||
//console.error("Error parsing image:", error);
|
||||
} finally {
|
||||
isLPRRequestInFlight.value = false;
|
||||
isLPRFrameProcessing.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -165,13 +636,6 @@ watch(manualInput, (newValue) => {
|
||||
// Update the header transparency when manualInput changes
|
||||
setTransparency(!newValue);
|
||||
});
|
||||
type statusIcon =
|
||||
| typeof VerifiedCustomer
|
||||
| typeof KnownCustomer
|
||||
| typeof UnknownCustomer
|
||||
| typeof CardPaymentCustomer
|
||||
| typeof BookedCustomer;
|
||||
|
||||
const registrationNumbers = computed(() => {
|
||||
// Return the registration numbers of all vehicles
|
||||
return [
|
||||
@@ -181,6 +645,14 @@ const registrationNumbers = computed(() => {
|
||||
];
|
||||
});
|
||||
|
||||
const getLPRScanContext = (): LPRScanContext => ({
|
||||
activeVehicleIndex: vehicles.activeVehicleIndex.value,
|
||||
attachmentView: views.attachmentView.value,
|
||||
manualInput: manualInput.value,
|
||||
registrationNumbers: [...registrationNumbers.value],
|
||||
transactionHistoryView: transactionHistoryView.value,
|
||||
});
|
||||
|
||||
function getSearchResultIndex(object: PosSearchResult) {
|
||||
let targetIndex = vehicles.activeVehicleIndex.value; // Default to the current active vehicle index
|
||||
// Check if the registration number already exists in the vehicles (And update the vehicle if it does)
|
||||
@@ -236,27 +708,49 @@ const getQuery = computed(() => {
|
||||
return activeVehicle ? activeVehicle.reg : "";
|
||||
});
|
||||
|
||||
const activeSelectionHasRegistrationNumber = computed(() => {
|
||||
return vehicles.getActiveVehicle()?.reg && vehicles.getActiveVehicle()?.reg.length > 0;
|
||||
});
|
||||
|
||||
const shouldCustomerBeModified = computed(() => {
|
||||
// Check if the customer should be modified based on the active vehicle index
|
||||
return vehicles.activeVehicleIndex.value === 1;
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener("visibilitychange", handleDocumentVisibilityChange);
|
||||
// Set the header to be transparent
|
||||
setTransparency(!views.isAnyActive.value);
|
||||
setBackgroundColor(backgroundColors.default); // Set the default background color
|
||||
setOverflow(false); // Prevent scrolling
|
||||
});
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener("visibilitychange", handleDocumentVisibilityChange);
|
||||
abortLPRRequest();
|
||||
clearNoPlateBackoff();
|
||||
clearDuplicateFrameBackoff();
|
||||
clearSuccessCooldown();
|
||||
// Reset the header settings when the component is unmounted
|
||||
setTransparency(false); // Reset transparency
|
||||
setBackgroundColor(backgroundColors.default); // Reset to default background color
|
||||
setOverflow(true); // Allow scrolling again
|
||||
});
|
||||
|
||||
watch(
|
||||
() => [manualInput.value, transactionHistoryView.value, views.attachmentView.value],
|
||||
([isManualInputActive, isTransactionHistoryActive, isAttachmentViewActive]) => {
|
||||
if (isManualInputActive || isTransactionHistoryActive || isAttachmentViewActive) {
|
||||
abortLPRRequest();
|
||||
resetParsedImageFingerprint();
|
||||
resetNoPlateBackoff();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
watch(
|
||||
() => [vehicles.activeVehicleIndex.value, ...registrationNumbers.value],
|
||||
() => {
|
||||
abortLPRRequest();
|
||||
resetParsedImageFingerprint();
|
||||
resetNoPlateBackoff();
|
||||
}
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -271,7 +765,16 @@ onUnmounted(() => {
|
||||
<!-- Default: Scanner view -->
|
||||
<template v-else>
|
||||
<div class="background-fixed">
|
||||
<ScannerCamera @update:frame="parseImage" />
|
||||
<ScannerCamera
|
||||
:capture-enabled="isCameraFrameCaptureEnabled"
|
||||
:capture-interval-ms="lprCameraCaptureIntervalMs"
|
||||
:capture-mode="views.attachmentView.value ? 'preview' : 'lpr'"
|
||||
:get-focus-viewport-rect="getScannerFocusViewportRect"
|
||||
:pause-preview="shouldPauseScannerPreview"
|
||||
:should-build-visual-fingerprint="shouldBuildLPRVisualFingerprint"
|
||||
:should-encode-frame="shouldEncodeLPRFrame"
|
||||
@update:frame="parseImage"
|
||||
/>
|
||||
</div>
|
||||
<div class="custom-content" data-testid="pos-mobile-step-1">
|
||||
<!-- Meta objects, registration number auto-lookup -->
|
||||
@@ -297,7 +800,13 @@ onUnmounted(() => {
|
||||
/>
|
||||
<!-- Scanner outline object -->
|
||||
<div class="is-align-content-center is-flex is-justify-content-center">
|
||||
<ScannerOutline :loading="false" v-if="!views.attachmentView.value" />
|
||||
<div
|
||||
v-if="!views.attachmentView.value"
|
||||
ref="scannerFocusRef"
|
||||
class="scanner-focus-target"
|
||||
>
|
||||
<ScannerOutline :loading="false" />
|
||||
</div>
|
||||
</div>
|
||||
<!-- Reg. 1, Reg. 2, Reg. 3 -->
|
||||
<div
|
||||
@@ -311,7 +820,10 @@ onUnmounted(() => {
|
||||
<!-- Location -->
|
||||
<PosDepartmentStepMobile1Location />
|
||||
<!-- Buttons -->
|
||||
<PosDepartmentStepMobileFixedBottomControl variant="pos-step">
|
||||
<PosDepartmentStepMobileFixedBottomControl
|
||||
variant="pos-step"
|
||||
:use-backdrop-blur="views.attachmentView.value"
|
||||
>
|
||||
<!-- Attachments -->
|
||||
<div class="is-flex is-justify-content-center">
|
||||
<PosDepartmentStepMobileAttachments :showDefaultControls="false" v-show="views.attachmentView.value" />
|
||||
@@ -437,4 +949,10 @@ onUnmounted(() => {
|
||||
.custom-content > * {
|
||||
width: min(100%, 48rem);
|
||||
}
|
||||
|
||||
.scanner-focus-target {
|
||||
display: inline-flex;
|
||||
max-width: 100%;
|
||||
width: fit-content;
|
||||
}
|
||||
</style>
|
||||
|
||||
+13
-1
@@ -11,6 +11,10 @@ const props = defineProps({
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
useBackdropBlur: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
variant: {
|
||||
type: String,
|
||||
default: 'default',
|
||||
@@ -25,6 +29,13 @@ const props = defineProps({
|
||||
const FIXED_BOTTOM_HEIGHT_CSS_VAR = '--pos-mobile-fixed-bottom-height';
|
||||
|
||||
const style = computed(() => {
|
||||
const background = `linear-gradient(to top, rgba(255, 255, 255, ${props.blurAmount * 0.8}) 0%, rgba(255, 255, 255, ${props.blurAmount * 0.4}) 100%)`;
|
||||
if (!props.useBackdropBlur) {
|
||||
return {
|
||||
background,
|
||||
};
|
||||
}
|
||||
|
||||
// Smooth blur transition
|
||||
if (props.smoothBlur) {
|
||||
return {
|
||||
@@ -32,12 +43,13 @@ const style = computed(() => {
|
||||
WebkitBackdropFilter: `blur(${props.blurAmount * 10}px)`,
|
||||
transition: 'backdrop-filter 0.3s ease, -webkit-backdrop-filter 0.3s ease',
|
||||
// Add gradient from bottom
|
||||
background: `linear-gradient(to top, rgba(255, 255, 255, ${props.blurAmount * 0.8}) 0%, rgba(255, 255, 255, ${props.blurAmount * 0.4}) 100%)`,
|
||||
background,
|
||||
// Add gradient from top
|
||||
|
||||
}
|
||||
}
|
||||
return {
|
||||
background,
|
||||
backdropFilter: `blur(${props.blurAmount * 10}px)`,
|
||||
WebkitBackdropFilter: `blur(${props.blurAmount * 10}px)`
|
||||
}
|
||||
|
||||
+27
-2
@@ -1065,9 +1065,9 @@ const getTotalAttachmentsCount = () => {
|
||||
);
|
||||
};
|
||||
// Function to take a picture as a base64 attachment
|
||||
const takePicture = () => {
|
||||
const takePicture = async () => {
|
||||
// Save the last picture to the base64 attachments
|
||||
const lastPicture = latestImage.value;
|
||||
const lastPicture = await getLatestImage();
|
||||
if (lastPicture) {
|
||||
addAttachmentBase64({
|
||||
filename: "last_picture.jpg",
|
||||
@@ -1118,6 +1118,7 @@ watch(
|
||||
/** Camera */
|
||||
// Define the reactive properties
|
||||
const latestImage = ref<string | null>(null);
|
||||
const latestImageBlob = ref<Blob | null>(null);
|
||||
const isCameraMounted = ref<boolean>(false);
|
||||
const cameraImageCaptureDelayInitial = ref<number>(500); // Initial delay for camera image capture in milliseconds (First capture)
|
||||
const cameraImageCaptureDelaySubsequent = ref<number>(1005); // Further captures delay in milliseconds (Every capture after the first one)
|
||||
@@ -1159,13 +1160,34 @@ const hasCameraImageCaptureDelayAfterSuccessPassed = (): boolean => {
|
||||
const currentTime = Date.now();
|
||||
return currentTime - cameraImageCaptureLastSuccess.value > cameraImageCaptureDelayAfterSuccess.value;
|
||||
};
|
||||
const blobToDataUrl = (blob: Blob): Promise<string | null> => new Promise((resolve) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
resolve(typeof reader.result === "string" ? reader.result : null);
|
||||
};
|
||||
reader.onerror = () => {
|
||||
resolve(null);
|
||||
};
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
|
||||
// Function to retrieve the latest image frame.
|
||||
const getLatestImage = async () => {
|
||||
if (!latestImage.value && latestImageBlob.value) {
|
||||
latestImage.value = await blobToDataUrl(latestImageBlob.value);
|
||||
}
|
||||
|
||||
return latestImage.value;
|
||||
};
|
||||
// Function to set the latest image frame.
|
||||
const setLatestImage = (image: string | null) => {
|
||||
latestImage.value = image;
|
||||
latestImageBlob.value = null;
|
||||
};
|
||||
// Function to set the latest image frame as a Blob.
|
||||
const setLatestImageBlob = (image: Blob | null) => {
|
||||
latestImage.value = null;
|
||||
latestImageBlob.value = image;
|
||||
};
|
||||
// Function to set the camera mounted state.
|
||||
const setCameraMounted = (mounted: boolean) => {
|
||||
@@ -1174,6 +1196,7 @@ const setCameraMounted = (mounted: boolean) => {
|
||||
// Function to clear the latest image.
|
||||
const clearLatestImage = () => {
|
||||
latestImage.value = null;
|
||||
latestImageBlob.value = null;
|
||||
};
|
||||
// Function to clear the camera mounted state.
|
||||
const clearCameraMounted = () => {
|
||||
@@ -1194,10 +1217,12 @@ const setCameraImageCaptureDelay = (isFirstCapture: boolean, delay: number) => {
|
||||
|
||||
const camera = {
|
||||
latestImage,
|
||||
latestImageBlob,
|
||||
get: getLatestImage,
|
||||
mounted: isCameraMounted,
|
||||
setMounted: setCameraMounted,
|
||||
setLatestImage,
|
||||
setLatestImageBlob,
|
||||
clearLatestImage,
|
||||
clearMounted: clearCameraMounted,
|
||||
getImageCaptureDelay: getCameraImageCaptureDelay,
|
||||
|
||||
@@ -41,6 +41,12 @@ const REQUEST_INSIGHT_DEFINITIONS = Object.freeze([
|
||||
iconClass: "fa-calendar-alt",
|
||||
matcher: (url) => /\/order-bookings(?:[/?#]|$)/i.test(url),
|
||||
},
|
||||
{
|
||||
key: "scanner",
|
||||
label: "Scanner",
|
||||
iconClass: "fa-camera",
|
||||
matcher: (url) => /\/modules\/scanner\/lpr(?:[/?#]|$)/i.test(url),
|
||||
},
|
||||
]);
|
||||
const SHIFT_MULTI_PRESS_WINDOW_MS = 700;
|
||||
const SYSTEM_SEARCH_CLOSE_EVENT = "system-search:close";
|
||||
@@ -62,6 +68,7 @@ const processedRequests = computed(() => requestQueueState.batchCompleted + requ
|
||||
const missingPermissions = computed(() => requestQueueState.missingPermissions || []);
|
||||
const activeRequests = computed(() => requestQueueState.activeRequests || []);
|
||||
const recentRequests = computed(() => requestQueueState.recentRequests || []);
|
||||
const queueRequestInsights = computed(() => requestQueueState.requestInsights || {});
|
||||
const errorRequests = computed(() => requestQueueState.errorRequests || []);
|
||||
const networkTotals = computed(() => requestQueueState.networkTotals || {
|
||||
outgoingRequests: 0,
|
||||
@@ -108,7 +115,7 @@ const userTypeLabel = computed(() => (SessionUser.isSubuser.value ? "Subuser" :
|
||||
const hasSuperuserToken = computed(() => {
|
||||
try {
|
||||
return Boolean(localStorage.getItem("superuser_token"));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
@@ -191,6 +198,152 @@ const formatBytes = (value) => {
|
||||
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
||||
};
|
||||
|
||||
const parseServerTimingDurations = (value) => {
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return value.split(",").reduce((durations, part) => {
|
||||
const [rawMetric, ...rawParams] = part.trim().split(";");
|
||||
const metric = rawMetric.trim();
|
||||
if (!metric) {
|
||||
return durations;
|
||||
}
|
||||
|
||||
const durationParam = rawParams.find((param) => param.trim().toLowerCase().startsWith("dur="));
|
||||
if (!durationParam) {
|
||||
return durations;
|
||||
}
|
||||
|
||||
const duration = Number.parseFloat(durationParam.split("=").slice(1).join("="));
|
||||
if (Number.isFinite(duration)) {
|
||||
durations[metric] = duration;
|
||||
}
|
||||
|
||||
return durations;
|
||||
}, {});
|
||||
};
|
||||
|
||||
const findServerTimingDuration = (durations, metricNames) => {
|
||||
const metricName = metricNames.find((name) => Number.isFinite(durations[name]));
|
||||
|
||||
return metricName ? durations[metricName] : null;
|
||||
};
|
||||
|
||||
const formatScannerFrameSize = (width, height) => {
|
||||
const roundedWidth = Math.round(Number(width) || 0);
|
||||
const roundedHeight = Math.round(Number(height) || 0);
|
||||
|
||||
if (roundedWidth <= 0 || roundedHeight <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return `img ${roundedWidth}x${roundedHeight}`;
|
||||
};
|
||||
|
||||
const formatScannerFrameBytes = (bytes) => {
|
||||
const roundedBytes = Math.round(Number(bytes) || 0);
|
||||
|
||||
if (roundedBytes <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return `bytes ${formatBytes(roundedBytes)}`;
|
||||
};
|
||||
|
||||
const getBrowserNetworkDuration = (requestDurationMs, serverDurationMs) => {
|
||||
if (serverDurationMs === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const durationMs = Number(requestDurationMs) - Number(serverDurationMs);
|
||||
|
||||
if (!Number.isFinite(durationMs) || durationMs < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return durationMs;
|
||||
};
|
||||
|
||||
const formatRequestLatency = (definition, request) => {
|
||||
const requestDurationMs = Math.max(0, Number(request?.requestDurationMs) || 0);
|
||||
if (definition.key !== "scanner") {
|
||||
return formatDuration(requestDurationMs);
|
||||
}
|
||||
|
||||
const queueDurationMs = Math.max(0, Number(request?.queueDurationMs) || 0);
|
||||
const durations = parseServerTimingDurations(request?.serverTiming);
|
||||
const captureDurationMs = findServerTimingDuration(durations, ["lpr_client_capture"]);
|
||||
const preflightDurationMs = findServerTimingDuration(durations, ["lpr_client_preflight"]);
|
||||
const visualFingerprintDurationMs = findServerTimingDuration(durations, ["lpr_client_visual_fingerprint"]);
|
||||
const drawDurationMs = findServerTimingDuration(durations, ["lpr_client_draw"]);
|
||||
const encodeDurationMs = findServerTimingDuration(durations, ["lpr_client_encode"]);
|
||||
const clientFrameWidth = findServerTimingDuration(durations, ["lpr_client_frame_width"]);
|
||||
const clientFrameHeight = findServerTimingDuration(durations, ["lpr_client_frame_height"]);
|
||||
const clientFrameBytes = findServerTimingDuration(durations, ["lpr_client_frame_bytes"]);
|
||||
const cacheDurationMs = findServerTimingDuration(durations, ["lpr_cache"]);
|
||||
const cacheHit = findServerTimingDuration(durations, ["lpr_cache_hit"]) !== null;
|
||||
const cacheMiss = findServerTimingDuration(durations, ["lpr_cache_miss"]) !== null;
|
||||
const localDurationMs = findServerTimingDuration(durations, ["lpr_local"]);
|
||||
const upstreamProcessingDurationMs = findServerTimingDuration(durations, ["lpr_upstream_processing"]);
|
||||
const upstreamDurationMs = findServerTimingDuration(durations, ["lpr_upstream_total", "lpr_upstream"]);
|
||||
const serverDurationMs = findServerTimingDuration(durations, ["lpr_total", "lpr_route_total", "lpr_request_total"]);
|
||||
const browserNetworkDurationMs = getBrowserNetworkDuration(requestDurationMs, serverDurationMs);
|
||||
const timingParts = [`browser ${formatDuration(requestDurationMs)}`];
|
||||
const frameSize = formatScannerFrameSize(clientFrameWidth, clientFrameHeight);
|
||||
const frameBytes = formatScannerFrameBytes(clientFrameBytes);
|
||||
|
||||
if (queueDurationMs > 0) {
|
||||
timingParts.push(`queue ${formatDuration(queueDurationMs)}`);
|
||||
}
|
||||
if (browserNetworkDurationMs !== null) {
|
||||
timingParts.push(`net ${formatDuration(browserNetworkDurationMs)}`);
|
||||
}
|
||||
if (captureDurationMs !== null) {
|
||||
timingParts.push(`cap ${formatDuration(captureDurationMs)}`);
|
||||
}
|
||||
if (preflightDurationMs !== null) {
|
||||
timingParts.push(`prep ${formatDuration(preflightDurationMs)}`);
|
||||
}
|
||||
if (visualFingerprintDurationMs !== null) {
|
||||
timingParts.push(`vf ${formatDuration(visualFingerprintDurationMs)}`);
|
||||
}
|
||||
if (drawDurationMs !== null) {
|
||||
timingParts.push(`draw ${formatDuration(drawDurationMs)}`);
|
||||
}
|
||||
if (encodeDurationMs !== null) {
|
||||
timingParts.push(`enc ${formatDuration(encodeDurationMs)}`);
|
||||
}
|
||||
if (frameSize !== null) {
|
||||
timingParts.push(frameSize);
|
||||
}
|
||||
if (frameBytes !== null) {
|
||||
timingParts.push(frameBytes);
|
||||
}
|
||||
if (cacheHit) {
|
||||
timingParts.push("cache hit");
|
||||
} else if (cacheMiss) {
|
||||
timingParts.push("cache miss");
|
||||
}
|
||||
if (cacheDurationMs !== null) {
|
||||
timingParts.push(`cache ${formatDuration(cacheDurationMs)}`);
|
||||
}
|
||||
if (localDurationMs !== null) {
|
||||
timingParts.push(`local ${formatDuration(localDurationMs)}`);
|
||||
}
|
||||
if (upstreamProcessingDurationMs !== null) {
|
||||
timingParts.push(`proc ${formatDuration(upstreamProcessingDurationMs)}`);
|
||||
}
|
||||
if (upstreamDurationMs !== null) {
|
||||
timingParts.push(`up ${formatDuration(upstreamDurationMs)}`);
|
||||
}
|
||||
if (serverDurationMs !== null) {
|
||||
timingParts.push(`srv ${formatDuration(serverDurationMs)}`);
|
||||
}
|
||||
|
||||
return timingParts.join(" / ");
|
||||
};
|
||||
|
||||
const formatTimeAgo = (timestamp) => {
|
||||
const value = Number(timestamp || 0);
|
||||
if (!value || Number.isNaN(value)) {
|
||||
@@ -219,43 +372,67 @@ const getActiveRequestElapsedMs = (request) => Math.max(0, nowMs.value - Number(
|
||||
|
||||
const hasInsightEntryChanged = (currentEntry, nextEntry) =>
|
||||
Number(currentEntry?.requestDurationMs || 0) !== Number(nextEntry?.requestDurationMs || 0)
|
||||
|| Number(currentEntry?.queueDurationMs || 0) !== Number(nextEntry?.queueDurationMs || 0)
|
||||
|| Number(currentEntry?.completedAt || 0) !== Number(nextEntry?.completedAt || 0)
|
||||
|| Number(currentEntry?.startedAt || 0) !== Number(nextEntry?.startedAt || 0)
|
||||
|| Number(currentEntry?.queuedAt || 0) !== Number(nextEntry?.queuedAt || 0);
|
||||
|| Number(currentEntry?.queuedAt || 0) !== Number(nextEntry?.queuedAt || 0)
|
||||
|| String(currentEntry?.serverTiming || "") !== String(nextEntry?.serverTiming || "");
|
||||
|
||||
const getInsightEntryTimestamp = (entry) =>
|
||||
Number(entry?.completedAt || entry?.startedAt || entry?.queuedAt || 0);
|
||||
|
||||
const selectNewestInsightEntry = (...entries) =>
|
||||
entries
|
||||
.filter((entry) => entry !== null && entry !== undefined)
|
||||
.sort((first, second) => getInsightEntryTimestamp(second) - getInsightEntryTimestamp(first))[0] || null;
|
||||
|
||||
const requestInsights = computed(() => REQUEST_INSIGHT_DEFINITIONS.map((definition) => {
|
||||
const matchedRequestFromRecent = recentRequests.value.find((request) =>
|
||||
definition.matcher(String(request?.url || ""))
|
||||
) || null;
|
||||
const matchedRequest = matchedRequestFromRecent || requestInsightHistory.value[definition.key] || null;
|
||||
const matchedRequestFromQueueInsight = queueRequestInsights.value[definition.key] || null;
|
||||
const matchedRequest = selectNewestInsightEntry(
|
||||
matchedRequestFromRecent,
|
||||
matchedRequestFromQueueInsight,
|
||||
requestInsightHistory.value[definition.key],
|
||||
);
|
||||
const hasData = matchedRequest !== null;
|
||||
|
||||
return {
|
||||
...definition,
|
||||
latencyText: hasData ? `${Math.max(0, Number(matchedRequest.requestDurationMs) || 0)} ms` : " ",
|
||||
latencyText: hasData ? formatRequestLatency(definition, matchedRequest) : " ",
|
||||
serverTiming: matchedRequest?.serverTiming || "",
|
||||
timeAgoText: hasData
|
||||
? formatTimeAgo(matchedRequest.completedAt || matchedRequest.startedAt || matchedRequest.queuedAt)
|
||||
: "",
|
||||
};
|
||||
}));
|
||||
|
||||
watch(recentRequests, (requests) => {
|
||||
if (!Array.isArray(requests) || requests.length === 0) {
|
||||
watch([recentRequests, queueRequestInsights], ([requests, insights]) => {
|
||||
const recentRequestList = Array.isArray(requests) ? requests : [];
|
||||
const insightEntries = insights && typeof insights === "object" ? insights : {};
|
||||
|
||||
if (recentRequestList.length === 0 && Object.keys(insightEntries).length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextHistory = { ...requestInsightHistory.value };
|
||||
let hasChanges = false;
|
||||
REQUEST_INSIGHT_DEFINITIONS.forEach((definition) => {
|
||||
const matchedRequest = requests.find((request) =>
|
||||
definition.matcher(String(request?.url || ""))
|
||||
const matchedRequest = selectNewestInsightEntry(
|
||||
recentRequestList.find((request) =>
|
||||
definition.matcher(String(request?.url || ""))
|
||||
),
|
||||
insightEntries[definition.key],
|
||||
);
|
||||
if (!matchedRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextEntry = {
|
||||
serverTiming: matchedRequest.serverTiming || null,
|
||||
requestDurationMs: Math.max(0, Number(matchedRequest.requestDurationMs) || 0),
|
||||
queueDurationMs: Math.max(0, Number(matchedRequest.queueDurationMs) || 0),
|
||||
completedAt: matchedRequest.completedAt || null,
|
||||
startedAt: matchedRequest.startedAt || null,
|
||||
queuedAt: matchedRequest.queuedAt || null,
|
||||
@@ -329,7 +506,7 @@ const measurePingLatency = async () => {
|
||||
queuedAt: startedAt,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
pingLatencyMs.value = null;
|
||||
pingIsUnavailable.value = true;
|
||||
} finally {
|
||||
@@ -574,7 +751,7 @@ onBeforeUnmount(() => {
|
||||
{{ request.method }}
|
||||
</span>
|
||||
<span class="request-queue-progress__endpoint" :title="request.url">{{ request.url }}</span>
|
||||
<span class="request-queue-progress__time">
|
||||
<span class="request-queue-progress__time" :title="request.serverTiming || ''">
|
||||
{{ formatDuration(request.requestDurationMs) }}
|
||||
</span>
|
||||
</li>
|
||||
@@ -595,7 +772,9 @@ onBeforeUnmount(() => {
|
||||
{{ insight.label }}
|
||||
</span>
|
||||
<span class="request-queue-progress__bottom-request-time">{{ insight.timeAgoText }}</span>
|
||||
<span class="request-queue-progress__bottom-request-latency">{{ insight.latencyText }}</span>
|
||||
<span class="request-queue-progress__bottom-request-latency" :title="insight.serverTiming">
|
||||
{{ insight.latencyText }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1481,6 +1660,10 @@ onBeforeUnmount(() => {
|
||||
.request-queue-progress__bottom-request-latency {
|
||||
font-size: 11px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
max-width: 190px;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ const ACTIVE_WASH_STARTED_STATUSES = new Set(['MACHINE_RELAY_ENABLED', 'MACHINE_
|
||||
const SELF_SERVE_HARDWARE_QUEUE_GROUP = 'SELF_SERVE_HARDWARE';
|
||||
const POS_SCANNER_QUEUE_GROUP = 'POS_SCANNER';
|
||||
const POS_STRIPE_QUEUE_GROUP = 'POS_STRIPE';
|
||||
const FETCH_TRANSPORT = 'fetch';
|
||||
const SELF_SERVE_HARDWARE_ENDPOINTS = [
|
||||
'/modules/self-serve/lane/command',
|
||||
'/modules/self-serve/lane/relay/',
|
||||
@@ -28,8 +29,15 @@ const POS_LATENCY_QUEUE_RULES = [
|
||||
{
|
||||
endpoints: ['/modules/scanner/lpr'],
|
||||
queueGroup: POS_SCANNER_QUEUE_GROUP,
|
||||
concurrencyLimit: 2,
|
||||
concurrencyLimit: 1,
|
||||
retryByStatusCode: {},
|
||||
skipRequestByteAccounting: true,
|
||||
skipResponseByteAccounting: true,
|
||||
skipNetworkTotals: true,
|
||||
insightKey: 'scanner',
|
||||
recordRecentOnSuccess: false,
|
||||
trackActiveRequest: false,
|
||||
trackProgressCounters: false,
|
||||
},
|
||||
{
|
||||
endpoints: ['/modules/stripe/invoice'],
|
||||
@@ -110,12 +118,106 @@ const findPosLatencyQueueRule = (url, method) => {
|
||||
) || null;
|
||||
};
|
||||
|
||||
const hasHeader = (headers, name) => {
|
||||
const normalizedName = String(name || '').trim().toLowerCase();
|
||||
if (!normalizedName || !headers || typeof headers !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Object.keys(headers).some((headerName) => String(headerName).toLowerCase() === normalizedName);
|
||||
};
|
||||
|
||||
const parseFetchResponseData = async (response) => {
|
||||
const text = await response.text();
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentType = response.headers?.get?.('content-type') || '';
|
||||
if (
|
||||
contentType.toLowerCase().includes('application/json') ||
|
||||
text.trim().startsWith('{') ||
|
||||
text.trim().startsWith('[')
|
||||
) {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (_error) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
};
|
||||
|
||||
const buildFetchBody = (method, data, headers) => {
|
||||
if (String(method || '').trim().toUpperCase() === 'GET' || data === undefined || data === null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof Blob !== 'undefined' && data instanceof Blob ||
|
||||
typeof FormData !== 'undefined' && data instanceof FormData ||
|
||||
typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams ||
|
||||
typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer ||
|
||||
typeof ReadableStream !== 'undefined' && data instanceof ReadableStream ||
|
||||
typeof data === 'string'
|
||||
) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if (!hasHeader(headers, 'Content-Type')) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
return JSON.stringify(data);
|
||||
};
|
||||
|
||||
const executeFetchRequest = async ({ method, url, data, signal, headers }) => {
|
||||
const fetchHeaders = { ...headers };
|
||||
const body = buildFetchBody(method, data, fetchHeaders);
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
headers: fetchHeaders,
|
||||
...(body !== undefined ? { body } : {}),
|
||||
...(signal ? { signal } : {}),
|
||||
});
|
||||
const responseData = await parseFetchResponseData(response);
|
||||
const axiosLikeResponse = {
|
||||
data: responseData,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: response.headers,
|
||||
config: {
|
||||
data,
|
||||
headers: fetchHeaders,
|
||||
method,
|
||||
url,
|
||||
},
|
||||
request: null,
|
||||
};
|
||||
|
||||
if (response.ok) {
|
||||
return axiosLikeResponse;
|
||||
}
|
||||
|
||||
const error = new Error(`Request failed with status code ${response.status}`);
|
||||
error.name = 'AxiosError';
|
||||
error.response = axiosLikeResponse;
|
||||
throw error;
|
||||
};
|
||||
|
||||
const buildRequestQueueOptions = (url, method, options = {}) => {
|
||||
const queueOptions = {
|
||||
retryByStatusCode: options?.retryByStatusCode,
|
||||
shouldRetry: options?.shouldRetry,
|
||||
queueGroup: options?.queueGroup,
|
||||
concurrencyLimit: options?.concurrencyLimit,
|
||||
skipRequestByteAccounting: options?.skipRequestByteAccounting,
|
||||
skipResponseByteAccounting: options?.skipResponseByteAccounting,
|
||||
skipNetworkTotals: options?.skipNetworkTotals,
|
||||
insightKey: options?.insightKey,
|
||||
recordRecentOnSuccess: options?.recordRecentOnSuccess,
|
||||
trackActiveRequest: options?.trackActiveRequest,
|
||||
trackProgressCounters: options?.trackProgressCounters,
|
||||
};
|
||||
|
||||
if (isSelfServeHardwareMutation(url, method)) {
|
||||
@@ -129,6 +231,13 @@ const buildRequestQueueOptions = (url, method, options = {}) => {
|
||||
queueOptions.retryByStatusCode ??= posQueueRule.retryByStatusCode;
|
||||
queueOptions.queueGroup ??= posQueueRule.queueGroup;
|
||||
queueOptions.concurrencyLimit ??= posQueueRule.concurrencyLimit;
|
||||
queueOptions.skipRequestByteAccounting ??= posQueueRule.skipRequestByteAccounting;
|
||||
queueOptions.skipResponseByteAccounting ??= posQueueRule.skipResponseByteAccounting;
|
||||
queueOptions.skipNetworkTotals ??= posQueueRule.skipNetworkTotals;
|
||||
queueOptions.insightKey ??= posQueueRule.insightKey;
|
||||
queueOptions.recordRecentOnSuccess ??= posQueueRule.recordRecentOnSuccess;
|
||||
queueOptions.trackActiveRequest ??= posQueueRule.trackActiveRequest;
|
||||
queueOptions.trackProgressCounters ??= posQueueRule.trackProgressCounters;
|
||||
}
|
||||
|
||||
return queueOptions;
|
||||
@@ -146,6 +255,7 @@ export const authenticatedRequest = (url, method, data, catchCallable = null, th
|
||||
// Build headers
|
||||
const headers = {
|
||||
...buildCurrentReleaseHeaders(),
|
||||
...(options?.headers || {}),
|
||||
};
|
||||
if (canSendCredentials && token && token.length > 0) {
|
||||
headers.Authorization = `Bearer ${token}`;
|
||||
@@ -158,8 +268,18 @@ export const authenticatedRequest = (url, method, data, catchCallable = null, th
|
||||
headers['X-Customer-Number'] = selectedCustomerNumber;
|
||||
}
|
||||
|
||||
const useFetchTransport = options?.transport === FETCH_TRANSPORT;
|
||||
|
||||
return enqueueRequest(
|
||||
() => axios({
|
||||
() => useFetchTransport
|
||||
? executeFetchRequest({
|
||||
method,
|
||||
url: requestUrl,
|
||||
data,
|
||||
signal: options?.signal,
|
||||
headers,
|
||||
})
|
||||
: axios({
|
||||
method,
|
||||
url: requestUrl,
|
||||
...(method === 'GET' ? { params: data } : { data }),
|
||||
@@ -175,6 +295,7 @@ export const authenticatedRequest = (url, method, data, catchCallable = null, th
|
||||
data: method === 'GET' ? null : data,
|
||||
headers,
|
||||
},
|
||||
signal: options?.signal,
|
||||
...buildRequestQueueOptions(requestUrl, method, options),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -4,7 +4,6 @@ import {ObjectsGlobal} from "@/components/session/token/SessionUser/Objects/Obje
|
||||
import {SessionUser} from "@/components/session/token/SessionUser.vue";
|
||||
import AssignDraftOrderCustomerModal from "@/components/displays/modals/AssignDraftOrderCustomerModal.vue";
|
||||
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
|
||||
import { editOrderItem, getOrderItems } from "@/components/shop/OrdersItems.vue";
|
||||
import {createApp} from "vue";
|
||||
import i18n from '@/i18n';
|
||||
import { dispatchNavigationCountRefresh } from "@/components/models/navigation/items/navigationCountEvents.js";
|
||||
@@ -265,6 +264,19 @@ const refreshDraftNavigationCount = () => {
|
||||
dispatchNavigationCountRefresh();
|
||||
};
|
||||
|
||||
|
||||
const getOrderItemsForRepricing = (orderId) => authenticatedRequest('/order/items', 'GET', {
|
||||
order_id: orderId,
|
||||
});
|
||||
|
||||
const editOrderItemForRepricing = ({ id, price, notes, reference, quantity }) => authenticatedRequest('/order/items', 'PUT', {
|
||||
id,
|
||||
price,
|
||||
notes,
|
||||
reference,
|
||||
quantity,
|
||||
});
|
||||
|
||||
const getFinalProductPriceForCustomer = async (productId, departmentId, customerId) => {
|
||||
const normalizedProductId = normalizePositiveInteger(productId);
|
||||
const normalizedDepartmentId = normalizePositiveInteger(departmentId);
|
||||
@@ -298,7 +310,7 @@ const recalculateOrderItemPricesForCustomer = async ({ order_id, department_id,
|
||||
throw new Error("Invalid order repricing context");
|
||||
}
|
||||
|
||||
const response = await getOrderItems(normalizedOrderId);
|
||||
const response = await getOrderItemsForRepricing(normalizedOrderId);
|
||||
const orderItems = Array.isArray(response?.data?.data) ? response.data.data : [];
|
||||
const uniqueProductIds = [...new Set(
|
||||
orderItems
|
||||
@@ -329,13 +341,13 @@ const recalculateOrderItemPricesForCustomer = async ({ order_id, department_id,
|
||||
return null;
|
||||
}
|
||||
|
||||
return editOrderItem(
|
||||
normalizedItemId,
|
||||
finalPriceMap.get(normalizedProductId),
|
||||
item?.notes ?? "",
|
||||
item?.reference ?? "",
|
||||
normalizePositiveInteger(item?.quantity) ?? 1
|
||||
);
|
||||
return editOrderItemForRepricing({
|
||||
id: normalizedItemId,
|
||||
price: finalPriceMap.get(normalizedProductId),
|
||||
notes: item?.notes ?? "",
|
||||
reference: item?.reference ?? "",
|
||||
quantity: normalizePositiveInteger(item?.quantity) ?? 1,
|
||||
});
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
@@ -370,11 +382,6 @@ const assignDraftOrderCustomer = async ({
|
||||
normalizedCustomerId
|
||||
);
|
||||
|
||||
const invoiceCollectionResponse = await SessionUser.objects.orders.set.invoice_collection_id(
|
||||
normalizedOrderId,
|
||||
normalizedInvoiceCollectionId
|
||||
);
|
||||
|
||||
let repricingResponse = null;
|
||||
if (recalculate_prices) {
|
||||
repricingResponse = await recalculateOrderItemPricesForCustomer({
|
||||
@@ -384,6 +391,11 @@ const assignDraftOrderCustomer = async ({
|
||||
});
|
||||
}
|
||||
|
||||
const invoiceCollectionResponse = await SessionUser.objects.orders.set.invoice_collection_id(
|
||||
normalizedOrderId,
|
||||
normalizedInvoiceCollectionId
|
||||
);
|
||||
|
||||
return {
|
||||
customerResponse,
|
||||
invoiceCollectionResponse,
|
||||
|
||||
@@ -1,20 +1,81 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted, watch } from 'vue';
|
||||
import { nextTick, ref, onMounted, onUnmounted, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import {
|
||||
captureVideoFrameBlobForLPR,
|
||||
isVideoFrameReadyForLPR,
|
||||
LPR_CAMERA_FRAME_RATE,
|
||||
LPR_CAMERA_VIDEO_HEIGHT,
|
||||
LPR_CAMERA_VIDEO_WIDTH,
|
||||
type LPRFrameEncodeCandidate,
|
||||
type LPRFrameViewportRect,
|
||||
} from "@/components/viewport/page/templates/scanner/lprFrameCapture";
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
captureEnabled?: boolean;
|
||||
captureIntervalMs?: number | null;
|
||||
captureMode?: 'lpr' | 'preview';
|
||||
getFocusViewportRect?: () => LPRFrameViewportRect | null;
|
||||
pausePreview?: boolean;
|
||||
shouldBuildVisualFingerprint?: () => boolean;
|
||||
shouldEncodeFrame?: (_candidate: LPRFrameEncodeCandidate) => boolean | Promise<boolean>;
|
||||
}>(), {
|
||||
captureEnabled: true,
|
||||
captureIntervalMs: null,
|
||||
captureMode: 'lpr',
|
||||
getFocusViewportRect: undefined,
|
||||
pausePreview: false,
|
||||
shouldBuildVisualFingerprint: undefined,
|
||||
shouldEncodeFrame: undefined,
|
||||
});
|
||||
type GetUserMediaConstraints = Parameters<typeof navigator.mediaDevices.getUserMedia>[0];
|
||||
type CameraConstraintCaps = {
|
||||
capFrameRate: boolean;
|
||||
capResolution: boolean;
|
||||
};
|
||||
const LPR_VIDEO_NOT_READY_RETRY_MS = 100;
|
||||
const LPR_PREVIEW_RESUME_CAPTURE_DELAY_MS = LPR_VIDEO_NOT_READY_RETRY_MS;
|
||||
const emits = defineEmits(['camera-toggled', 'scanner-toggled', 'update:frame']);
|
||||
const { t } = useI18n();
|
||||
const videoRef = ref<HTMLVideoElement | null>(null);
|
||||
const canvasRef = ref<HTMLCanvasElement | null>(null);
|
||||
const visualFingerprintCanvasRef = ref<HTMLCanvasElement | null>(null);
|
||||
const cameraStream = ref<MediaStream | null>(null);
|
||||
const isCameraActive = ref(false);
|
||||
const cameraErrorKey = ref('pos.camera_permission_denied');
|
||||
let captureIntervalId: ReturnType<typeof window.setInterval> | null = null;
|
||||
let firstCaptureTimeoutId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
let isFrameCaptureInProgress = false;
|
||||
let isLivePreviewPausedForFrameEncode = false;
|
||||
let hasRequestedVideoPreviewPlay = false;
|
||||
let lastAppliedTrackEnabled: boolean | null = null;
|
||||
let cachedRelativeFocusViewportRect: LPRFrameViewportRect | null = null;
|
||||
let hasCachedRelativeFocusViewportRect = false;
|
||||
let cachedVideoViewportRect: DOMRect | null = null;
|
||||
let videoResizeObserver: ResizeObserver | null = null;
|
||||
import { isCameraMounted, camera } from '@/components/displays/department/pos/steps/mobile/objects/PosDepartmentStepMobileFlow.vue';
|
||||
|
||||
const getCameraErrorKey = (err: unknown) => {
|
||||
const errorName = err instanceof DOMException ? err.name : '';
|
||||
const isDocumentVisible = (): boolean =>
|
||||
typeof document === 'undefined' || document.visibilityState !== 'hidden';
|
||||
|
||||
const shouldRunLivePreview = (): boolean =>
|
||||
!props.pausePreview && !isLivePreviewPausedForFrameEncode && isDocumentVisible();
|
||||
|
||||
const canCaptureFrames = (): boolean =>
|
||||
isCameraActive.value && props.captureEnabled && !props.pausePreview && isDocumentVisible();
|
||||
|
||||
const getCameraErrorName = (err: unknown): string => {
|
||||
if (err instanceof DOMException) {
|
||||
return err.name;
|
||||
}
|
||||
|
||||
return typeof err === 'object' && err !== null
|
||||
? String((err as { name?: unknown }).name ?? '')
|
||||
: '';
|
||||
};
|
||||
|
||||
const getCameraErrorKey = (err: unknown) => {
|
||||
const errorName = getCameraErrorName(err);
|
||||
if (errorName === 'NotFoundError' || errorName === 'DevicesNotFoundError') {
|
||||
return 'pos.no_camera_found';
|
||||
}
|
||||
@@ -22,47 +83,198 @@ const getCameraErrorKey = (err: unknown) => {
|
||||
return 'pos.camera_permission_denied';
|
||||
};
|
||||
|
||||
const shouldRetryWithRelaxedCameraConstraints = (err: unknown): boolean => {
|
||||
const errorName = getCameraErrorName(err);
|
||||
|
||||
return errorName === 'OverconstrainedError' || errorName === 'ConstraintNotSatisfiedError';
|
||||
};
|
||||
|
||||
const getRejectedCameraConstraintName = (err: unknown): string => {
|
||||
if (typeof err !== 'object' || err === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return String((err as { constraint?: unknown }).constraint ?? '').toLowerCase();
|
||||
};
|
||||
|
||||
const getCameraConstraintFallbacks = (err: unknown): CameraConstraintCaps[] => {
|
||||
const rejectedConstraint = getRejectedCameraConstraintName(err);
|
||||
if (rejectedConstraint === 'framerate') {
|
||||
return [
|
||||
{ capFrameRate: false, capResolution: true },
|
||||
{ capFrameRate: false, capResolution: false },
|
||||
];
|
||||
}
|
||||
|
||||
if (['width', 'height', 'aspectratio', 'resizemode'].includes(rejectedConstraint)) {
|
||||
return [
|
||||
{ capFrameRate: true, capResolution: false },
|
||||
{ capFrameRate: false, capResolution: false },
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
{ capFrameRate: false, capResolution: true },
|
||||
{ capFrameRate: true, capResolution: false },
|
||||
{ capFrameRate: false, capResolution: false },
|
||||
];
|
||||
};
|
||||
|
||||
const getCameraConstraints = ({
|
||||
capFrameRate,
|
||||
capResolution,
|
||||
}: CameraConstraintCaps = { capFrameRate: true, capResolution: true }) => ({
|
||||
video: {
|
||||
facingMode: 'environment',
|
||||
zoom: camera.getZoom(),
|
||||
width: capResolution
|
||||
? { ideal: LPR_CAMERA_VIDEO_WIDTH, max: LPR_CAMERA_VIDEO_WIDTH }
|
||||
: { ideal: LPR_CAMERA_VIDEO_WIDTH },
|
||||
height: capResolution
|
||||
? { ideal: LPR_CAMERA_VIDEO_HEIGHT, max: LPR_CAMERA_VIDEO_HEIGHT }
|
||||
: { ideal: LPR_CAMERA_VIDEO_HEIGHT },
|
||||
frameRate: capFrameRate
|
||||
? { ideal: LPR_CAMERA_FRAME_RATE, max: LPR_CAMERA_FRAME_RATE }
|
||||
: { ideal: LPR_CAMERA_FRAME_RATE },
|
||||
|
||||
// New spec
|
||||
advanced: [
|
||||
{ focusMode: 'continuous' },
|
||||
{ torch: false } // Set to true to enable flashlight if supported
|
||||
],
|
||||
// Old spec
|
||||
//focusMode: 'continuous',
|
||||
// Zoom in on the environment camera if available
|
||||
//facingMode: 'environment',
|
||||
//width: { ideal: 1920 },
|
||||
//height: { ideal: 1080 },
|
||||
//aspectRatio: { ideal: 16/9 },
|
||||
//frameRate: { ideal: 30 }
|
||||
}
|
||||
} as unknown as GetUserMediaConstraints);
|
||||
|
||||
const requestCameraStream = async (): Promise<MediaStream> => {
|
||||
try {
|
||||
return await navigator.mediaDevices.getUserMedia(getCameraConstraints());
|
||||
} catch (err) {
|
||||
if (!shouldRetryWithRelaxedCameraConstraints(err)) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
let lastError = err;
|
||||
for (const fallback of getCameraConstraintFallbacks(err)) {
|
||||
try {
|
||||
return await navigator.mediaDevices.getUserMedia(getCameraConstraints(fallback));
|
||||
} catch (fallbackError) {
|
||||
if (!shouldRetryWithRelaxedCameraConstraints(fallbackError)) {
|
||||
throw fallbackError;
|
||||
}
|
||||
lastError = fallbackError;
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
};
|
||||
|
||||
const playVideoPreview = (video: HTMLVideoElement) => {
|
||||
if (hasRequestedVideoPreviewPlay) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasRequestedVideoPreviewPlay = true;
|
||||
const playResult = video.play();
|
||||
if (playResult && typeof playResult.catch === 'function') {
|
||||
void playResult.catch(() => {
|
||||
hasRequestedVideoPreviewPlay = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const pauseVideoPreview = (video: HTMLVideoElement) => {
|
||||
if (!hasRequestedVideoPreviewPlay) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasRequestedVideoPreviewPlay = false;
|
||||
video.pause();
|
||||
};
|
||||
|
||||
const getCameraVideoTracks = (): MediaStreamTrack[] => {
|
||||
if (!cameraStream.value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (typeof cameraStream.value.getVideoTracks === 'function') {
|
||||
return cameraStream.value.getVideoTracks();
|
||||
}
|
||||
|
||||
return cameraStream.value.getTracks().filter(track => track.kind === 'video');
|
||||
};
|
||||
|
||||
const syncCameraVideoTracksEnabled = () => {
|
||||
if (!isCameraActive.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const shouldEnableTracks = shouldRunLivePreview();
|
||||
if (lastAppliedTrackEnabled === shouldEnableTracks) {
|
||||
return;
|
||||
}
|
||||
|
||||
getCameraVideoTracks().forEach((track) => {
|
||||
if (track.enabled !== shouldEnableTracks) {
|
||||
track.enabled = shouldEnableTracks;
|
||||
}
|
||||
});
|
||||
lastAppliedTrackEnabled = shouldEnableTracks;
|
||||
};
|
||||
|
||||
const syncVideoPreviewPlayback = () => {
|
||||
syncCameraVideoTracksEnabled();
|
||||
|
||||
if (!videoRef.value || !isCameraActive.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!shouldRunLivePreview()) {
|
||||
pauseVideoPreview(videoRef.value);
|
||||
return;
|
||||
}
|
||||
|
||||
playVideoPreview(videoRef.value);
|
||||
};
|
||||
|
||||
const pauseLivePreviewForFrameEncode = () => {
|
||||
if (isLivePreviewPausedForFrameEncode || !isCameraActive.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
isLivePreviewPausedForFrameEncode = true;
|
||||
syncVideoPreviewPlayback();
|
||||
};
|
||||
|
||||
const applyCameraStream = (stream: MediaStream) => {
|
||||
isCameraActive.value = true;
|
||||
isCameraMounted.value = true;
|
||||
cameraStream.value = stream;
|
||||
hasRequestedVideoPreviewPlay = false;
|
||||
lastAppliedTrackEnabled = null;
|
||||
if (videoRef.value) {
|
||||
videoRef.value.srcObject = stream;
|
||||
videoRef.value.setAttribute('playsinline', '');
|
||||
syncVideoPreviewPlayback();
|
||||
}
|
||||
startCaptureTimers();
|
||||
};
|
||||
|
||||
function startCamera() {
|
||||
if (cameraStream.value || isCameraActive.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const constraints = {
|
||||
video: {
|
||||
facingMode: 'environment',
|
||||
zoom: camera.getZoom(),
|
||||
width: { ideal: 1920 },
|
||||
height: { ideal: 1080 },
|
||||
frameRate: { ideal: 30 },
|
||||
|
||||
// New spec
|
||||
advanced: [
|
||||
{ focusMode: 'continuous' },
|
||||
{ torch: false } // Set to true to enable flashlight if supported
|
||||
],
|
||||
// Old spec
|
||||
//focusMode: 'continuous',
|
||||
// Zoom in on the environment camera if available
|
||||
//facingMode: 'environment',
|
||||
//width: { ideal: 1920 },
|
||||
//height: { ideal: 1080 },
|
||||
//aspectRatio: { ideal: 16/9 },
|
||||
//frameRate: { ideal: 30 }
|
||||
}
|
||||
};
|
||||
|
||||
navigator.mediaDevices.getUserMedia(constraints)
|
||||
.then((stream) => {
|
||||
isCameraActive.value = true;
|
||||
isCameraMounted.value = true;
|
||||
cameraStream.value = stream;
|
||||
if (videoRef.value) {
|
||||
videoRef.value.srcObject = stream;
|
||||
videoRef.value.setAttribute('playsinline', '');
|
||||
videoRef.value.play();
|
||||
}
|
||||
startCaptureInterval();
|
||||
})
|
||||
requestCameraStream()
|
||||
.then(applyCameraStream)
|
||||
.catch((err) => {
|
||||
isCameraActive.value = false;
|
||||
cameraErrorKey.value = getCameraErrorKey(err);
|
||||
@@ -77,17 +289,126 @@ function clearCaptureInterval() {
|
||||
}
|
||||
}
|
||||
|
||||
function startCaptureInterval() {
|
||||
clearCaptureInterval();
|
||||
captureIntervalId = window.setInterval(() => {
|
||||
if (isCameraActive.value) {
|
||||
getFrame();
|
||||
}
|
||||
}, camera.getImageCaptureDelay(false));
|
||||
function clearFirstCaptureTimeout() {
|
||||
if (firstCaptureTimeoutId !== null) {
|
||||
window.clearTimeout(firstCaptureTimeoutId);
|
||||
firstCaptureTimeoutId = null;
|
||||
}
|
||||
}
|
||||
|
||||
function stopCamera() {
|
||||
const pauseCaptureTimers = () => {
|
||||
clearFirstCaptureTimeout();
|
||||
clearCaptureInterval();
|
||||
};
|
||||
|
||||
function captureFrameIfReady(): Promise<void> {
|
||||
if (canCaptureFrames() && !isFrameCaptureInProgress) {
|
||||
if (!videoRef.value || !isVideoFrameReadyForLPR(videoRef.value)) {
|
||||
scheduleFrameReadinessRetry();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
clearFirstCaptureTimeout();
|
||||
return getFrame()
|
||||
.then(() => undefined)
|
||||
.finally(() => {
|
||||
if (canCaptureFrames()) {
|
||||
startCaptureInterval();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
function startCaptureTimers() {
|
||||
if (!canCaptureFrames()) {
|
||||
pauseCaptureTimers();
|
||||
return;
|
||||
}
|
||||
|
||||
clearCaptureInterval();
|
||||
scheduleFirstCapture();
|
||||
}
|
||||
|
||||
function resumeCaptureTimers(firstCaptureDelayMs = 0) {
|
||||
if (!canCaptureFrames()) {
|
||||
pauseCaptureTimers();
|
||||
return;
|
||||
}
|
||||
|
||||
clearCaptureInterval();
|
||||
scheduleFirstCapture(firstCaptureDelayMs);
|
||||
}
|
||||
|
||||
function scheduleFirstCapture(delayMs = camera.getImageCaptureDelay(true)) {
|
||||
clearFirstCaptureTimeout();
|
||||
firstCaptureTimeoutId = window.setTimeout(() => {
|
||||
firstCaptureTimeoutId = null;
|
||||
captureFrameIfReady();
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
const getRecurringCaptureDelay = (): number => {
|
||||
if (props.captureIntervalMs === null || props.captureIntervalMs === undefined) {
|
||||
return camera.getImageCaptureDelay(false);
|
||||
}
|
||||
|
||||
const customDelay = Number(props.captureIntervalMs);
|
||||
|
||||
if (Number.isFinite(customDelay) && customDelay >= 0) {
|
||||
return Math.floor(customDelay);
|
||||
}
|
||||
|
||||
return camera.getImageCaptureDelay(false);
|
||||
};
|
||||
|
||||
function scheduleFrameReadinessRetry() {
|
||||
if (firstCaptureTimeoutId !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
scheduleFirstCapture(LPR_VIDEO_NOT_READY_RETRY_MS);
|
||||
}
|
||||
|
||||
function startCaptureInterval() {
|
||||
if (!canCaptureFrames()) {
|
||||
clearCaptureInterval();
|
||||
return;
|
||||
}
|
||||
|
||||
clearCaptureInterval();
|
||||
captureIntervalId = window.setTimeout(() => {
|
||||
captureIntervalId = null;
|
||||
void captureFrameIfReady();
|
||||
}, getRecurringCaptureDelay());
|
||||
}
|
||||
|
||||
const clearRelativeFocusViewportRectCache = () => {
|
||||
cachedRelativeFocusViewportRect = null;
|
||||
cachedVideoViewportRect = null;
|
||||
hasCachedRelativeFocusViewportRect = false;
|
||||
};
|
||||
|
||||
const observeVideoGeometry = () => {
|
||||
videoResizeObserver?.disconnect();
|
||||
videoResizeObserver = null;
|
||||
|
||||
if (typeof ResizeObserver === 'undefined' || !videoRef.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
videoResizeObserver = new ResizeObserver(clearRelativeFocusViewportRectCache);
|
||||
videoResizeObserver.observe(videoRef.value);
|
||||
};
|
||||
|
||||
function stopCamera() {
|
||||
clearFirstCaptureTimeout();
|
||||
clearCaptureInterval();
|
||||
clearRelativeFocusViewportRectCache();
|
||||
isLivePreviewPausedForFrameEncode = false;
|
||||
hasRequestedVideoPreviewPlay = false;
|
||||
lastAppliedTrackEnabled = null;
|
||||
if (cameraStream.value) {
|
||||
cameraStream.value.getTracks().forEach(track => track.stop());
|
||||
cameraStream.value = null;
|
||||
@@ -107,67 +428,181 @@ function toggleCamera() {
|
||||
}
|
||||
}
|
||||
|
||||
function handleVideoLoadedData() {
|
||||
clearRelativeFocusViewportRectCache();
|
||||
if (canCaptureFrames()) {
|
||||
scheduleFirstCapture(0);
|
||||
}
|
||||
}
|
||||
|
||||
function handleVisibilityChange() {
|
||||
clearRelativeFocusViewportRectCache();
|
||||
if (isDocumentVisible()) {
|
||||
syncVideoPreviewPlayback();
|
||||
resumeCaptureTimers(0);
|
||||
return;
|
||||
}
|
||||
|
||||
pauseCaptureTimers();
|
||||
syncVideoPreviewPlayback();
|
||||
}
|
||||
|
||||
const shouldUseFocusedLPRCrop = () => props.captureMode === 'lpr';
|
||||
|
||||
const hasUsableRectSize = (rect: { height: number; width: number }): boolean =>
|
||||
Number.isFinite(rect.width)
|
||||
&& Number.isFinite(rect.height)
|
||||
&& rect.width > 0
|
||||
&& rect.height > 0;
|
||||
|
||||
const getVideoViewportRect = (): DOMRect | null => {
|
||||
if (!videoRef.value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (cachedVideoViewportRect !== null) {
|
||||
return cachedVideoViewportRect;
|
||||
}
|
||||
|
||||
const videoRect = videoRef.value.getBoundingClientRect();
|
||||
if (!hasUsableRectSize(videoRect)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
cachedVideoViewportRect = videoRect;
|
||||
return cachedVideoViewportRect;
|
||||
};
|
||||
|
||||
const getRelativeFocusViewportRect = (videoViewportRect: DOMRect | null): LPRFrameViewportRect | null => {
|
||||
if (!shouldUseFocusedLPRCrop() || !props.getFocusViewportRect || !videoRef.value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (hasCachedRelativeFocusViewportRect) {
|
||||
return cachedRelativeFocusViewportRect;
|
||||
}
|
||||
|
||||
const focusRect = props.getFocusViewportRect();
|
||||
if (!focusRect || !hasUsableRectSize(focusRect)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!videoViewportRect || !hasUsableRectSize(videoViewportRect)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
cachedRelativeFocusViewportRect = {
|
||||
height: focusRect.height,
|
||||
width: focusRect.width,
|
||||
x: focusRect.x - videoViewportRect.left,
|
||||
y: focusRect.y - videoViewportRect.top,
|
||||
};
|
||||
hasCachedRelativeFocusViewportRect = true;
|
||||
|
||||
return cachedRelativeFocusViewportRect;
|
||||
};
|
||||
|
||||
const getFrameCaptureOptions = () => {
|
||||
const shouldUseFocusedCrop = shouldUseFocusedLPRCrop();
|
||||
const videoViewportRect = shouldUseFocusedCrop ? getVideoViewportRect() : null;
|
||||
|
||||
return {
|
||||
focusCrop: shouldUseFocusedCrop,
|
||||
focusViewportRect: shouldUseFocusedCrop ? getRelativeFocusViewportRect(videoViewportRect) : null,
|
||||
shouldBuildVisualFingerprint: shouldUseFocusedCrop ? props.shouldBuildVisualFingerprint : undefined,
|
||||
shouldEncode: shouldUseFocusedCrop ? props.shouldEncodeFrame : undefined,
|
||||
...(shouldUseFocusedCrop ? { onFrameDrawn: pauseLivePreviewForFrameEncode } : {}),
|
||||
...(videoViewportRect
|
||||
? {
|
||||
viewportHeight: videoViewportRect.height,
|
||||
viewportWidth: videoViewportRect.width,
|
||||
}
|
||||
: {}),
|
||||
visualFingerprintCanvas: shouldUseFocusedCrop ? visualFingerprintCanvasRef.value : null,
|
||||
};
|
||||
};
|
||||
|
||||
const getFrame = () => {
|
||||
if (!canCaptureFrames()) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
if (videoRef.value && isCameraActive.value) {
|
||||
const canvas = canvasRef.value;
|
||||
if (canvas) {
|
||||
const context = canvas.getContext('2d', {
|
||||
alpha: false,
|
||||
willReadFrequently: true
|
||||
});
|
||||
isFrameCaptureInProgress = true;
|
||||
|
||||
if (!context) {
|
||||
console.error('Failed to get canvas context');
|
||||
return null;
|
||||
}
|
||||
return captureVideoFrameBlobForLPR(videoRef.value, canvas, getFrameCaptureOptions())
|
||||
.then((frameData) => {
|
||||
if (frameData && canCaptureFrames()) {
|
||||
emits('update:frame', frameData);
|
||||
}
|
||||
|
||||
// Set canvas size to match video's native resolution
|
||||
const videoWidth = videoRef.value.videoWidth;
|
||||
const videoHeight = videoRef.value.videoHeight;
|
||||
|
||||
canvas.width = videoWidth;
|
||||
canvas.height = videoHeight;
|
||||
|
||||
// Clear previous frame
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
// Draw current frame
|
||||
context.drawImage(videoRef.value, 0, 0, videoWidth, videoHeight);
|
||||
|
||||
// Get frame data as base64
|
||||
const frameData = canvas.toDataURL('image/jpeg', 0.95);
|
||||
|
||||
// Emit the frame data
|
||||
emits('update:frame', frameData);
|
||||
|
||||
return frameData;
|
||||
return nextTick().then(() => frameData);
|
||||
})
|
||||
.finally(() => {
|
||||
isLivePreviewPausedForFrameEncode = false;
|
||||
isFrameCaptureInProgress = false;
|
||||
syncVideoPreviewPlayback();
|
||||
});
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Promise.resolve(null);
|
||||
};
|
||||
|
||||
// Update canvas dimensions when video size changes
|
||||
watch(() => videoRef.value?.videoWidth, (newWidth) => {
|
||||
if (canvasRef.value && newWidth) {
|
||||
canvasRef.value.width = newWidth;
|
||||
canvasRef.value.height = videoRef.value?.videoHeight || 0;
|
||||
}
|
||||
});
|
||||
|
||||
// Watch for zoom level changes
|
||||
watch(() => camera.getZoom(), (newZoom) => {
|
||||
watch(() => camera.getZoom(), () => {
|
||||
if (isCameraActive.value) {
|
||||
stopCamera();
|
||||
startCamera();
|
||||
}
|
||||
});
|
||||
|
||||
watch(() => props.captureEnabled, (isCaptureEnabled) => {
|
||||
if (isCaptureEnabled) {
|
||||
resumeCaptureTimers(0);
|
||||
} else {
|
||||
pauseCaptureTimers();
|
||||
}
|
||||
});
|
||||
|
||||
watch([() => props.captureMode, () => props.getFocusViewportRect], clearRelativeFocusViewportRectCache);
|
||||
|
||||
watch(() => props.captureIntervalMs, () => {
|
||||
if (canCaptureFrames() && !isFrameCaptureInProgress && firstCaptureTimeoutId === null) {
|
||||
startCaptureInterval();
|
||||
}
|
||||
});
|
||||
|
||||
watch(() => props.pausePreview, (isPreviewPaused) => {
|
||||
syncVideoPreviewPlayback();
|
||||
|
||||
if (isPreviewPaused) {
|
||||
pauseCaptureTimers();
|
||||
return;
|
||||
}
|
||||
|
||||
resumeCaptureTimers(LPR_PREVIEW_RESUME_CAPTURE_DELAY_MS);
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
document.addEventListener('scroll', clearRelativeFocusViewportRectCache, true);
|
||||
window.addEventListener('orientationchange', clearRelativeFocusViewportRectCache);
|
||||
window.addEventListener('resize', clearRelativeFocusViewportRectCache);
|
||||
observeVideoGeometry();
|
||||
isCameraMounted.value = true;
|
||||
startCamera();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
document.removeEventListener('scroll', clearRelativeFocusViewportRectCache, true);
|
||||
window.removeEventListener('orientationchange', clearRelativeFocusViewportRectCache);
|
||||
window.removeEventListener('resize', clearRelativeFocusViewportRectCache);
|
||||
videoResizeObserver?.disconnect();
|
||||
videoResizeObserver = null;
|
||||
clearRelativeFocusViewportRectCache();
|
||||
stopCamera();
|
||||
});
|
||||
|
||||
@@ -191,6 +626,7 @@ watch(() => isCameraActive.value, (newVal) => {
|
||||
autoplay
|
||||
playsinline
|
||||
:class="{ 'is-active': isCameraActive }"
|
||||
@loadeddata="handleVideoLoadedData"
|
||||
>
|
||||
Your browser does not support the video tag.
|
||||
</video>
|
||||
@@ -200,6 +636,12 @@ watch(() => isCameraActive.value, (newVal) => {
|
||||
class="capture-canvas"
|
||||
></canvas>
|
||||
|
||||
<canvas
|
||||
ref="visualFingerprintCanvasRef"
|
||||
class="visual-fingerprint-canvas"
|
||||
aria-hidden="true"
|
||||
></canvas>
|
||||
|
||||
<div v-if="!isCameraActive" class="camera-inactive">
|
||||
<p>{{ t(cameraErrorKey) }}</p>
|
||||
</div>
|
||||
@@ -231,6 +673,10 @@ video {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.visual-fingerprint-canvas {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.camera-inactive {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
|
||||
@@ -0,0 +1,773 @@
|
||||
export const LPR_CAMERA_VIDEO_WIDTH = 1024;
|
||||
export const LPR_CAMERA_VIDEO_HEIGHT = 576;
|
||||
export const LPR_CAMERA_FRAME_RATE = 5;
|
||||
export const LPR_FRAME_MAX_WIDTH = 1024;
|
||||
export const LPR_FRAME_MAX_HEIGHT = 576;
|
||||
export const LPR_FRAME_SCANNER_MAX_SIZE = 384;
|
||||
export const LPR_FRAME_JPEG_QUALITY = 0.72;
|
||||
export const LPR_FRAME_SCANNER_JPEG_QUALITY = 0.6;
|
||||
export const LPR_FRAME_MIME_TYPE = "image/jpeg";
|
||||
export const LPR_FRAME_FILE_NAME = "license-plate.jpg";
|
||||
export const LPR_FRAME_CLIENT_CAPTURE_MS_FIELD = "client_capture_ms";
|
||||
export const LPR_FRAME_CLIENT_DRAW_MS_FIELD = "client_draw_ms";
|
||||
export const LPR_FRAME_CLIENT_ENCODE_MS_FIELD = "client_encode_ms";
|
||||
export const LPR_FRAME_CLIENT_PREFLIGHT_MS_FIELD = "client_preflight_ms";
|
||||
export const LPR_FRAME_CLIENT_VISUAL_FINGERPRINT_MS_FIELD = "client_visual_fingerprint_ms";
|
||||
export const LPR_FRAME_CLIENT_WIDTH_FIELD = "client_frame_width";
|
||||
export const LPR_FRAME_CLIENT_HEIGHT_FIELD = "client_frame_height";
|
||||
export const LPR_FRAME_CLIENT_BYTES_FIELD = "client_frame_bytes";
|
||||
export const LPR_FRAME_FOCUS_ASPECT_RATIO = 16 / 9;
|
||||
export const LPR_FRAME_SCANNER_FOCUS_SCALE = 0.85;
|
||||
export const LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE = 8;
|
||||
const HTML_MEDIA_HAVE_CURRENT_DATA = 2;
|
||||
const LPR_FRAME_FINGERPRINT_SAMPLE_BYTES = 32;
|
||||
const LPR_FRAME_FINGERPRINT_HASH_SEED = 2166136261;
|
||||
const LPR_FRAME_FINGERPRINT_HASH_PRIME = 16777619;
|
||||
const NIBBLE_BIT_COUNT = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];
|
||||
const LPR_ENCODING_CANVAS_CONTEXT_OPTIONS = {
|
||||
alpha: false,
|
||||
desynchronized: true,
|
||||
};
|
||||
|
||||
export type FrameSize = {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
|
||||
export type FrameSizeConstraints = {
|
||||
maxHeight: number;
|
||||
maxWidth: number;
|
||||
};
|
||||
|
||||
export type FrameSourceRect = {
|
||||
height: number;
|
||||
width: number;
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
export type LPRFrameViewportRect = {
|
||||
height: number;
|
||||
width: number;
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
export type LPRFramePayload = {
|
||||
blob: Blob;
|
||||
captureDurationMs: number;
|
||||
captureTimings?: LPRFrameCaptureTimings;
|
||||
filename: string;
|
||||
fingerprint: string;
|
||||
getContentFingerprint?: () => Promise<string>;
|
||||
getVisualFingerprint?: () => string | null;
|
||||
height: number;
|
||||
mimeType: string;
|
||||
visualFingerprint?: string;
|
||||
width: number;
|
||||
};
|
||||
|
||||
export type LPRFrameCaptureTimings = {
|
||||
drawMs: number;
|
||||
encodeMs: number;
|
||||
visualFingerprintMs: number;
|
||||
};
|
||||
|
||||
export type LPRFrameEncodeCandidate = {
|
||||
height: number;
|
||||
visualFingerprint?: string;
|
||||
width: number;
|
||||
};
|
||||
|
||||
export type LPRFrameCaptureOptions = {
|
||||
focusCrop?: boolean;
|
||||
focusScale?: number;
|
||||
focusViewportRect?: LPRFrameViewportRect | null;
|
||||
jpegQuality?: number;
|
||||
onFrameDrawn?: () => void;
|
||||
shouldBuildVisualFingerprint?: () => boolean;
|
||||
shouldEncode?: (candidate: LPRFrameEncodeCandidate) => boolean | Promise<boolean>;
|
||||
viewportHeight?: number;
|
||||
viewportWidth?: number;
|
||||
visualFingerprintCanvas?: HTMLCanvasElement | null;
|
||||
};
|
||||
|
||||
type LPRFrameEncodingCanvas = HTMLCanvasElement | OffscreenCanvas;
|
||||
type LPRFrameEncodingContext = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
|
||||
|
||||
type LPRFrameEncodedCanvas = {
|
||||
blob: Blob;
|
||||
canvas: LPRFrameEncodingCanvas;
|
||||
timings: Pick<LPRFrameCaptureTimings, "drawMs" | "encodeMs">;
|
||||
};
|
||||
|
||||
const offscreenEncodingCanvases = new WeakMap<HTMLCanvasElement, OffscreenCanvas>();
|
||||
const disabledOffscreenEncodingCanvases = new WeakSet<HTMLCanvasElement>();
|
||||
const encodingCanvasContexts = new WeakMap<LPRFrameEncodingCanvas, LPRFrameEncodingContext>();
|
||||
const visualFingerprintCanvasContexts = new WeakMap<HTMLCanvasElement, CanvasRenderingContext2D>();
|
||||
|
||||
const clampNumber = (value: number, min: number, max: number): number =>
|
||||
Math.min(max, Math.max(min, value));
|
||||
|
||||
export const getConstrainedFrameSize = (
|
||||
sourceWidth: number,
|
||||
sourceHeight: number,
|
||||
maxWidth = LPR_FRAME_MAX_WIDTH,
|
||||
maxHeight = LPR_FRAME_MAX_HEIGHT
|
||||
): FrameSize => {
|
||||
if (sourceWidth <= 0 || sourceHeight <= 0) {
|
||||
return { width: 0, height: 0 };
|
||||
}
|
||||
|
||||
const scale = Math.min(1, maxWidth / sourceWidth, maxHeight / sourceHeight);
|
||||
|
||||
return {
|
||||
width: Math.max(1, Math.round(sourceWidth * scale)),
|
||||
height: Math.max(1, Math.round(sourceHeight * scale)),
|
||||
};
|
||||
};
|
||||
|
||||
export const getLPRFrameSizeConstraints = (options: LPRFrameCaptureOptions = {}): FrameSizeConstraints =>
|
||||
options.focusCrop === false
|
||||
? {
|
||||
maxHeight: LPR_FRAME_MAX_HEIGHT,
|
||||
maxWidth: LPR_FRAME_MAX_WIDTH,
|
||||
}
|
||||
: {
|
||||
maxHeight: LPR_FRAME_SCANNER_MAX_SIZE,
|
||||
maxWidth: LPR_FRAME_SCANNER_MAX_SIZE,
|
||||
};
|
||||
|
||||
export const getLPRFrameTargetSize = (
|
||||
sourceRect: FrameSourceRect,
|
||||
options: LPRFrameCaptureOptions = {}
|
||||
): FrameSize => {
|
||||
const constraints = getLPRFrameSizeConstraints(options);
|
||||
|
||||
return getConstrainedFrameSize(
|
||||
sourceRect.width,
|
||||
sourceRect.height,
|
||||
constraints.maxWidth,
|
||||
constraints.maxHeight
|
||||
);
|
||||
};
|
||||
|
||||
export const getVisibleCoverSourceRect = (
|
||||
sourceWidth: number,
|
||||
sourceHeight: number,
|
||||
viewportWidth: number,
|
||||
viewportHeight: number
|
||||
): FrameSourceRect => {
|
||||
if (!Number.isFinite(sourceWidth) || !Number.isFinite(sourceHeight) || sourceWidth <= 0 || sourceHeight <= 0) {
|
||||
return { height: 0, width: 0, x: 0, y: 0 };
|
||||
}
|
||||
|
||||
if (
|
||||
!Number.isFinite(viewportWidth) ||
|
||||
!Number.isFinite(viewportHeight) ||
|
||||
viewportWidth <= 0 ||
|
||||
viewportHeight <= 0
|
||||
) {
|
||||
return { height: sourceHeight, width: sourceWidth, x: 0, y: 0 };
|
||||
}
|
||||
|
||||
const sourceAspectRatio = sourceWidth / sourceHeight;
|
||||
const viewportAspectRatio = viewportWidth / viewportHeight;
|
||||
|
||||
if (viewportAspectRatio > sourceAspectRatio) {
|
||||
const visibleHeight = Math.max(1, Math.min(sourceHeight, Math.round(sourceWidth / viewportAspectRatio)));
|
||||
|
||||
return {
|
||||
height: visibleHeight,
|
||||
width: sourceWidth,
|
||||
x: 0,
|
||||
y: Math.max(0, Math.round((sourceHeight - visibleHeight) / 2)),
|
||||
};
|
||||
}
|
||||
|
||||
const visibleWidth = Math.max(1, Math.min(sourceWidth, Math.round(sourceHeight * viewportAspectRatio)));
|
||||
|
||||
return {
|
||||
height: sourceHeight,
|
||||
width: visibleWidth,
|
||||
x: Math.max(0, Math.round((sourceWidth - visibleWidth) / 2)),
|
||||
y: 0,
|
||||
};
|
||||
};
|
||||
|
||||
export const getCenteredFocusSourceRect = (
|
||||
sourceRect: FrameSourceRect,
|
||||
focusAspectRatio = LPR_FRAME_FOCUS_ASPECT_RATIO,
|
||||
focusScale = 1
|
||||
): FrameSourceRect => {
|
||||
if (
|
||||
!Number.isFinite(sourceRect.width) ||
|
||||
!Number.isFinite(sourceRect.height) ||
|
||||
sourceRect.width <= 0 ||
|
||||
sourceRect.height <= 0
|
||||
) {
|
||||
return { height: 0, width: 0, x: 0, y: 0 };
|
||||
}
|
||||
|
||||
if (!Number.isFinite(focusAspectRatio) || focusAspectRatio <= 0) {
|
||||
return sourceRect;
|
||||
}
|
||||
|
||||
let focusedRect: FrameSourceRect;
|
||||
const sourceAspectRatio = sourceRect.width / sourceRect.height;
|
||||
if (sourceAspectRatio > focusAspectRatio) {
|
||||
const focusedWidth = Math.max(1, Math.min(sourceRect.width, Math.round(sourceRect.height * focusAspectRatio)));
|
||||
|
||||
focusedRect = {
|
||||
height: sourceRect.height,
|
||||
width: focusedWidth,
|
||||
x: sourceRect.x + Math.max(0, Math.round((sourceRect.width - focusedWidth) / 2)),
|
||||
y: sourceRect.y,
|
||||
};
|
||||
} else {
|
||||
const focusedHeight = Math.max(1, Math.min(sourceRect.height, Math.round(sourceRect.width / focusAspectRatio)));
|
||||
|
||||
focusedRect = {
|
||||
height: focusedHeight,
|
||||
width: sourceRect.width,
|
||||
x: sourceRect.x,
|
||||
y: sourceRect.y + Math.max(0, Math.round((sourceRect.height - focusedHeight) / 2)),
|
||||
};
|
||||
}
|
||||
|
||||
if (!Number.isFinite(focusScale) || focusScale <= 0 || focusScale >= 1) {
|
||||
return focusedRect;
|
||||
}
|
||||
|
||||
const scaledWidth = Math.max(1, Math.round(focusedRect.width * focusScale));
|
||||
const scaledHeight = Math.max(1, Math.round(focusedRect.height * focusScale));
|
||||
|
||||
return {
|
||||
height: scaledHeight,
|
||||
width: scaledWidth,
|
||||
x: focusedRect.x + Math.max(0, Math.round((focusedRect.width - scaledWidth) / 2)),
|
||||
y: focusedRect.y + Math.max(0, Math.round((focusedRect.height - scaledHeight) / 2)),
|
||||
};
|
||||
};
|
||||
|
||||
export const getViewportAnchoredFocusSourceRect = (
|
||||
centeredFocusRect: FrameSourceRect,
|
||||
visibleSourceRect: FrameSourceRect,
|
||||
viewportWidth: number,
|
||||
viewportHeight: number,
|
||||
focusViewportRect: LPRFrameViewportRect | null | undefined
|
||||
): FrameSourceRect => {
|
||||
if (
|
||||
!focusViewportRect ||
|
||||
!Number.isFinite(viewportWidth) ||
|
||||
!Number.isFinite(viewportHeight) ||
|
||||
!Number.isFinite(focusViewportRect.x) ||
|
||||
!Number.isFinite(focusViewportRect.y) ||
|
||||
!Number.isFinite(focusViewportRect.width) ||
|
||||
!Number.isFinite(focusViewportRect.height) ||
|
||||
viewportWidth <= 0 ||
|
||||
viewportHeight <= 0 ||
|
||||
focusViewportRect.width <= 0 ||
|
||||
focusViewportRect.height <= 0 ||
|
||||
centeredFocusRect.width <= 0 ||
|
||||
centeredFocusRect.height <= 0 ||
|
||||
visibleSourceRect.width <= 0 ||
|
||||
visibleSourceRect.height <= 0 ||
|
||||
centeredFocusRect.width > visibleSourceRect.width ||
|
||||
centeredFocusRect.height > visibleSourceRect.height
|
||||
) {
|
||||
return centeredFocusRect;
|
||||
}
|
||||
|
||||
const focusCenterX = focusViewportRect.x + (focusViewportRect.width / 2);
|
||||
const focusCenterY = focusViewportRect.y + (focusViewportRect.height / 2);
|
||||
const sourceCenterX = visibleSourceRect.x + ((focusCenterX / viewportWidth) * visibleSourceRect.width);
|
||||
const sourceCenterY = visibleSourceRect.y + ((focusCenterY / viewportHeight) * visibleSourceRect.height);
|
||||
const minX = visibleSourceRect.x;
|
||||
const minY = visibleSourceRect.y;
|
||||
const maxX = visibleSourceRect.x + visibleSourceRect.width - centeredFocusRect.width;
|
||||
const maxY = visibleSourceRect.y + visibleSourceRect.height - centeredFocusRect.height;
|
||||
|
||||
return {
|
||||
height: centeredFocusRect.height,
|
||||
width: centeredFocusRect.width,
|
||||
x: clampNumber(Math.round(sourceCenterX - (centeredFocusRect.width / 2)), minX, maxX),
|
||||
y: clampNumber(Math.round(sourceCenterY - (centeredFocusRect.height / 2)), minY, maxY),
|
||||
};
|
||||
};
|
||||
|
||||
export const getLPRFrameSourceRect = (
|
||||
sourceWidth: number,
|
||||
sourceHeight: number,
|
||||
viewportWidth: number,
|
||||
viewportHeight: number,
|
||||
options: LPRFrameCaptureOptions = {}
|
||||
): FrameSourceRect => {
|
||||
const visibleSourceRect = getVisibleCoverSourceRect(sourceWidth, sourceHeight, viewportWidth, viewportHeight);
|
||||
|
||||
if (options.focusCrop === false) {
|
||||
return visibleSourceRect;
|
||||
}
|
||||
|
||||
const centeredFocusRect = getCenteredFocusSourceRect(
|
||||
visibleSourceRect,
|
||||
LPR_FRAME_FOCUS_ASPECT_RATIO,
|
||||
options.focusScale ?? LPR_FRAME_SCANNER_FOCUS_SCALE
|
||||
);
|
||||
|
||||
return getViewportAnchoredFocusSourceRect(
|
||||
centeredFocusRect,
|
||||
visibleSourceRect,
|
||||
viewportWidth,
|
||||
viewportHeight,
|
||||
options.focusViewportRect
|
||||
);
|
||||
};
|
||||
|
||||
const getVideoFrameSourceRect = (video: HTMLVideoElement, options: LPRFrameCaptureOptions = {}): FrameSourceRect => {
|
||||
const configuredViewportWidth = Number(options.viewportWidth);
|
||||
const configuredViewportHeight = Number(options.viewportHeight);
|
||||
const hasConfiguredViewportSize =
|
||||
Number.isFinite(configuredViewportWidth) &&
|
||||
configuredViewportWidth > 0 &&
|
||||
Number.isFinite(configuredViewportHeight) &&
|
||||
configuredViewportHeight > 0;
|
||||
const viewportWidth = hasConfiguredViewportSize ? configuredViewportWidth : video.clientWidth;
|
||||
const viewportHeight = hasConfiguredViewportSize ? configuredViewportHeight : video.clientHeight;
|
||||
|
||||
return getLPRFrameSourceRect(
|
||||
video.videoWidth,
|
||||
video.videoHeight,
|
||||
viewportWidth,
|
||||
viewportHeight,
|
||||
options
|
||||
);
|
||||
};
|
||||
|
||||
export const isVideoFrameReadyForLPR = (video: HTMLVideoElement): boolean =>
|
||||
video.readyState >= HTML_MEDIA_HAVE_CURRENT_DATA
|
||||
&& video.videoWidth > 0
|
||||
&& video.videoHeight > 0;
|
||||
|
||||
const sampleBlobFingerprintBytes = async (blob: Blob): Promise<string> => {
|
||||
let hash = LPR_FRAME_FINGERPRINT_HASH_SEED;
|
||||
const sampleWidth = Math.min(LPR_FRAME_FINGERPRINT_SAMPLE_BYTES, blob.size);
|
||||
const offsets = [
|
||||
0,
|
||||
Math.max(0, Math.floor(blob.size / 2) - Math.floor(sampleWidth / 2)),
|
||||
Math.max(0, blob.size - sampleWidth),
|
||||
];
|
||||
let previousOffset: number | null = null;
|
||||
|
||||
for (const offset of offsets) {
|
||||
if (offset === previousOffset) {
|
||||
continue;
|
||||
}
|
||||
previousOffset = offset;
|
||||
const bytes = new Uint8Array(await blob.slice(offset, offset + sampleWidth).arrayBuffer());
|
||||
for (let index = 0; index < bytes.length; index += 1) {
|
||||
const byte = bytes[index];
|
||||
hash ^= byte;
|
||||
hash = Math.imul(hash, LPR_FRAME_FINGERPRINT_HASH_PRIME) >>> 0;
|
||||
}
|
||||
}
|
||||
|
||||
return hash.toString(16).padStart(8, "0");
|
||||
};
|
||||
|
||||
export const buildLPRFrameFingerprint = async (
|
||||
blob: Blob,
|
||||
width: number,
|
||||
height: number
|
||||
): Promise<string> => `${width}x${height}:${blob.size}:${await sampleBlobFingerprintBytes(blob)}`;
|
||||
|
||||
export const buildLPRFrameFingerprintKey = (
|
||||
blob: Blob,
|
||||
width: number,
|
||||
height: number
|
||||
): string => `${width}x${height}:${blob.size}`;
|
||||
|
||||
export const getVisualFingerprintDistance = (first: string | null | undefined, second: string | null | undefined): number => {
|
||||
if (!first || !second || first.length !== second.length) {
|
||||
return Number.POSITIVE_INFINITY;
|
||||
}
|
||||
|
||||
let distance = 0;
|
||||
for (let index = 0; index < first.length; index += 1) {
|
||||
const firstNibble = Number.parseInt(first[index], 16);
|
||||
const secondNibble = Number.parseInt(second[index], 16);
|
||||
if (!Number.isInteger(firstNibble) || !Number.isInteger(secondNibble)) {
|
||||
return Number.POSITIVE_INFINITY;
|
||||
}
|
||||
distance += NIBBLE_BIT_COUNT[firstNibble ^ secondNibble];
|
||||
}
|
||||
|
||||
return distance;
|
||||
};
|
||||
|
||||
const buildSourceVisualFingerprint = (
|
||||
source: CanvasImageSource,
|
||||
sourceRect: FrameSourceRect,
|
||||
visualFingerprintCanvas: HTMLCanvasElement | null | undefined
|
||||
): string | null => {
|
||||
if (
|
||||
sourceRect.width <= 0 ||
|
||||
sourceRect.height <= 0 ||
|
||||
!Number.isFinite(sourceRect.x) ||
|
||||
!Number.isFinite(sourceRect.y) ||
|
||||
!Number.isFinite(sourceRect.width) ||
|
||||
!Number.isFinite(sourceRect.height) ||
|
||||
!visualFingerprintCanvas
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (visualFingerprintCanvas.width !== LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE) {
|
||||
visualFingerprintCanvas.width = LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE;
|
||||
}
|
||||
if (visualFingerprintCanvas.height !== LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE) {
|
||||
visualFingerprintCanvas.height = LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE;
|
||||
}
|
||||
|
||||
let context = visualFingerprintCanvasContexts.get(visualFingerprintCanvas) ?? null;
|
||||
if (context === null) {
|
||||
context = visualFingerprintCanvas.getContext("2d", {
|
||||
alpha: false,
|
||||
willReadFrequently: true,
|
||||
});
|
||||
if (context !== null) {
|
||||
visualFingerprintCanvasContexts.set(visualFingerprintCanvas, context);
|
||||
}
|
||||
}
|
||||
if (!context || typeof context.getImageData !== "function") {
|
||||
return null;
|
||||
}
|
||||
|
||||
context.drawImage(
|
||||
source,
|
||||
sourceRect.x,
|
||||
sourceRect.y,
|
||||
sourceRect.width,
|
||||
sourceRect.height,
|
||||
0,
|
||||
0,
|
||||
LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE,
|
||||
LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE
|
||||
);
|
||||
|
||||
const imageData = context.getImageData(
|
||||
0,
|
||||
0,
|
||||
LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE,
|
||||
LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE
|
||||
).data;
|
||||
const sampleCount = LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE * LPR_FRAME_VISUAL_FINGERPRINT_GRID_SIZE;
|
||||
let luminanceTotal = 0;
|
||||
for (let pixelOffset = 0; pixelOffset < imageData.length; pixelOffset += 4) {
|
||||
luminanceTotal +=
|
||||
(imageData[pixelOffset] * 0.299)
|
||||
+ (imageData[pixelOffset + 1] * 0.587)
|
||||
+ (imageData[pixelOffset + 2] * 0.114);
|
||||
}
|
||||
|
||||
const averageLuminance = luminanceTotal / sampleCount;
|
||||
let fingerprint = "";
|
||||
for (let pixelOffset = 0; pixelOffset < imageData.length; pixelOffset += 16) {
|
||||
let nibble = 0;
|
||||
for (let offset = 0; offset < 4; offset += 1) {
|
||||
const offsetPixel = pixelOffset + (offset * 4);
|
||||
const luminance =
|
||||
(imageData[offsetPixel] * 0.299)
|
||||
+ (imageData[offsetPixel + 1] * 0.587)
|
||||
+ (imageData[offsetPixel + 2] * 0.114);
|
||||
if (luminance > averageLuminance) {
|
||||
nibble |= 1 << (3 - offset);
|
||||
}
|
||||
}
|
||||
fingerprint += nibble.toString(16);
|
||||
}
|
||||
|
||||
return fingerprint;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const buildMemoizedLPRFrameContentFingerprint = (
|
||||
blob: Blob,
|
||||
width: number,
|
||||
height: number
|
||||
): (() => Promise<string>) => {
|
||||
let fingerprintPromise: Promise<string> | null = null;
|
||||
|
||||
return () => {
|
||||
fingerprintPromise ??= buildLPRFrameFingerprint(blob, width, height).catch((error) => {
|
||||
fingerprintPromise = null;
|
||||
throw error;
|
||||
});
|
||||
|
||||
return fingerprintPromise;
|
||||
};
|
||||
};
|
||||
|
||||
const nowMs = (): number =>
|
||||
typeof performance !== "undefined" && typeof performance.now === "function"
|
||||
? performance.now()
|
||||
: Date.now();
|
||||
|
||||
const getJpegQualityForFrame = (options: LPRFrameCaptureOptions): number => {
|
||||
if (Number.isFinite(options.jpegQuality) && options.jpegQuality > 0 && options.jpegQuality <= 1) {
|
||||
return options.jpegQuality;
|
||||
}
|
||||
|
||||
return options.focusCrop === false
|
||||
? LPR_FRAME_JPEG_QUALITY
|
||||
: LPR_FRAME_SCANNER_JPEG_QUALITY;
|
||||
};
|
||||
|
||||
const shouldEncodeFrameCandidate = async (
|
||||
candidate: LPRFrameEncodeCandidate,
|
||||
shouldEncode: LPRFrameCaptureOptions["shouldEncode"]
|
||||
): Promise<boolean> => {
|
||||
if (typeof shouldEncode !== "function") {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return await shouldEncode(candidate);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const shouldBuildVisualFingerprintBeforeEncode = (
|
||||
shouldBuildVisualFingerprint: LPRFrameCaptureOptions["shouldBuildVisualFingerprint"]
|
||||
): boolean => {
|
||||
if (typeof shouldBuildVisualFingerprint !== "function") {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return shouldBuildVisualFingerprint();
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const getOffscreenEncodingCanvas = (fallbackCanvas: HTMLCanvasElement): OffscreenCanvas | null => {
|
||||
if (typeof OffscreenCanvas === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (disabledOffscreenEncodingCanvases.has(fallbackCanvas)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let canvas = offscreenEncodingCanvases.get(fallbackCanvas) ?? null;
|
||||
if (canvas === null) {
|
||||
canvas = new OffscreenCanvas(1, 1);
|
||||
offscreenEncodingCanvases.set(fallbackCanvas, canvas);
|
||||
}
|
||||
|
||||
return canvas;
|
||||
};
|
||||
|
||||
const isOffscreenEncodingCanvas = (canvas: LPRFrameEncodingCanvas): canvas is OffscreenCanvas =>
|
||||
typeof OffscreenCanvas !== "undefined" && canvas instanceof OffscreenCanvas;
|
||||
|
||||
const encodeCanvasBlob = (
|
||||
canvas: LPRFrameEncodingCanvas,
|
||||
mimeType: string,
|
||||
quality: number
|
||||
): Promise<Blob | null> => {
|
||||
if (isOffscreenEncodingCanvas(canvas)) {
|
||||
return typeof canvas.convertToBlob === "function"
|
||||
? canvas.convertToBlob({ type: mimeType, quality }).catch(() => null)
|
||||
: Promise.resolve(null);
|
||||
}
|
||||
|
||||
if (typeof canvas.toBlob !== "function") {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
return new Promise<Blob | null>((resolve) => {
|
||||
canvas.toBlob(resolve, mimeType, quality);
|
||||
});
|
||||
};
|
||||
|
||||
const getEncodingCanvasContext = (canvas: LPRFrameEncodingCanvas): LPRFrameEncodingContext | null => {
|
||||
let context = encodingCanvasContexts.get(canvas) ?? null;
|
||||
if (context !== null) {
|
||||
return context;
|
||||
}
|
||||
|
||||
context = canvas.getContext("2d", LPR_ENCODING_CANVAS_CONTEXT_OPTIONS) as LPRFrameEncodingContext | null;
|
||||
if (context !== null) {
|
||||
encodingCanvasContexts.set(canvas, context);
|
||||
}
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
const drawAndEncodeFrame = async (
|
||||
video: HTMLVideoElement,
|
||||
canvas: LPRFrameEncodingCanvas,
|
||||
sourceRect: FrameSourceRect,
|
||||
targetSize: FrameSize,
|
||||
options: LPRFrameCaptureOptions
|
||||
): Promise<LPRFrameEncodedCanvas | null> => {
|
||||
if (canvas.width !== targetSize.width) {
|
||||
canvas.width = targetSize.width;
|
||||
}
|
||||
if (canvas.height !== targetSize.height) {
|
||||
canvas.height = targetSize.height;
|
||||
}
|
||||
|
||||
const context = getEncodingCanvasContext(canvas);
|
||||
|
||||
if (!context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const drawStartedAt = nowMs();
|
||||
context.drawImage(
|
||||
video,
|
||||
sourceRect.x,
|
||||
sourceRect.y,
|
||||
sourceRect.width,
|
||||
sourceRect.height,
|
||||
0,
|
||||
0,
|
||||
targetSize.width,
|
||||
targetSize.height
|
||||
);
|
||||
const drawMs = Math.max(0, nowMs() - drawStartedAt);
|
||||
try {
|
||||
options.onFrameDrawn?.();
|
||||
} catch {
|
||||
// Capture must continue even if preview throttling cannot be applied.
|
||||
}
|
||||
|
||||
const encodeStartedAt = nowMs();
|
||||
const blob = await encodeCanvasBlob(canvas, LPR_FRAME_MIME_TYPE, getJpegQualityForFrame(options));
|
||||
const encodeMs = Math.max(0, nowMs() - encodeStartedAt);
|
||||
if (!blob) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
blob,
|
||||
canvas,
|
||||
timings: {
|
||||
drawMs,
|
||||
encodeMs,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const captureEncodedCanvas = async (
|
||||
video: HTMLVideoElement,
|
||||
fallbackCanvas: HTMLCanvasElement,
|
||||
sourceRect: FrameSourceRect,
|
||||
targetSize: FrameSize,
|
||||
options: LPRFrameCaptureOptions
|
||||
): Promise<LPRFrameEncodedCanvas | null> => {
|
||||
const offscreenCanvas = getOffscreenEncodingCanvas(fallbackCanvas);
|
||||
if (offscreenCanvas !== null) {
|
||||
try {
|
||||
const encoded = await drawAndEncodeFrame(video, offscreenCanvas, sourceRect, targetSize, options);
|
||||
if (encoded !== null) {
|
||||
return encoded;
|
||||
}
|
||||
disabledOffscreenEncodingCanvases.add(fallbackCanvas);
|
||||
} catch {
|
||||
// Some browsers expose OffscreenCanvas but reject drawing live video into it.
|
||||
disabledOffscreenEncodingCanvases.add(fallbackCanvas);
|
||||
}
|
||||
}
|
||||
|
||||
const encoded = await drawAndEncodeFrame(video, fallbackCanvas, sourceRect, targetSize, options);
|
||||
if (encoded === null) {
|
||||
console.error("Failed to capture camera frame");
|
||||
}
|
||||
|
||||
return encoded;
|
||||
};
|
||||
|
||||
export const captureVideoFrameBlobForLPR = async (
|
||||
video: HTMLVideoElement,
|
||||
canvas: HTMLCanvasElement,
|
||||
options: LPRFrameCaptureOptions = {}
|
||||
): Promise<LPRFramePayload | null> => {
|
||||
const captureStartedAt = nowMs();
|
||||
const captureTimings: LPRFrameCaptureTimings = {
|
||||
drawMs: 0,
|
||||
encodeMs: 0,
|
||||
visualFingerprintMs: 0,
|
||||
};
|
||||
const sourceRect = getVideoFrameSourceRect(video, options);
|
||||
const targetSize = getLPRFrameTargetSize(sourceRect, options);
|
||||
|
||||
if (targetSize.width === 0 || targetSize.height === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const visualFingerprintCanvas = options.visualFingerprintCanvas ?? null;
|
||||
let visualFingerprint: string | null = null;
|
||||
if (shouldBuildVisualFingerprintBeforeEncode(options.shouldBuildVisualFingerprint)) {
|
||||
const visualFingerprintStartedAt = nowMs();
|
||||
visualFingerprint = buildSourceVisualFingerprint(video, sourceRect, visualFingerprintCanvas);
|
||||
captureTimings.visualFingerprintMs = Math.max(0, nowMs() - visualFingerprintStartedAt);
|
||||
}
|
||||
let hasTriedLazyVisualFingerprint = false;
|
||||
const candidate: LPRFrameEncodeCandidate = {
|
||||
height: targetSize.height,
|
||||
...(visualFingerprint !== null ? { visualFingerprint } : {}),
|
||||
width: targetSize.width,
|
||||
};
|
||||
|
||||
if (!await shouldEncodeFrameCandidate(candidate, options.shouldEncode)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const encoded = await captureEncodedCanvas(video, canvas, sourceRect, targetSize, options);
|
||||
if (encoded === null) {
|
||||
return null;
|
||||
}
|
||||
const { blob, canvas: encodedCanvas } = encoded;
|
||||
captureTimings.drawMs = encoded.timings.drawMs;
|
||||
captureTimings.encodeMs = encoded.timings.encodeMs;
|
||||
|
||||
const getVisualFingerprint = visualFingerprintCanvas
|
||||
? () => {
|
||||
if (visualFingerprint !== null || hasTriedLazyVisualFingerprint) {
|
||||
return visualFingerprint;
|
||||
}
|
||||
|
||||
hasTriedLazyVisualFingerprint = true;
|
||||
visualFingerprint = buildSourceVisualFingerprint(encodedCanvas, {
|
||||
height: targetSize.height,
|
||||
width: targetSize.width,
|
||||
x: 0,
|
||||
y: 0,
|
||||
}, visualFingerprintCanvas);
|
||||
|
||||
return visualFingerprint;
|
||||
}
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
blob,
|
||||
captureDurationMs: Math.max(0, nowMs() - captureStartedAt),
|
||||
captureTimings,
|
||||
filename: LPR_FRAME_FILE_NAME,
|
||||
fingerprint: buildLPRFrameFingerprintKey(blob, targetSize.width, targetSize.height),
|
||||
getContentFingerprint: buildMemoizedLPRFrameContentFingerprint(blob, targetSize.width, targetSize.height),
|
||||
...(getVisualFingerprint ? { getVisualFingerprint } : {}),
|
||||
height: targetSize.height,
|
||||
mimeType: blob.type || LPR_FRAME_MIME_TYPE,
|
||||
...(visualFingerprint !== null ? { visualFingerprint } : {}),
|
||||
width: targetSize.width,
|
||||
};
|
||||
};
|
||||
@@ -58,6 +58,7 @@ export const installAxiosRequestQueue = () => {
|
||||
data: adapterConfig?.data ?? config?.data ?? null,
|
||||
headers: adapterConfig?.headers ?? config?.headers ?? null,
|
||||
},
|
||||
signal: adapterConfig?.signal ?? config?.signal,
|
||||
});
|
||||
return config;
|
||||
});
|
||||
|
||||
+230
-36
@@ -27,6 +27,7 @@ const requestQueueStateMutable = reactive({
|
||||
batchFailed: 0,
|
||||
activeRequests: [],
|
||||
recentRequests: [],
|
||||
requestInsights: {},
|
||||
errorRequests: [],
|
||||
missingPermissions: [],
|
||||
networkTotals: {
|
||||
@@ -40,6 +41,8 @@ const requestQueueStateMutable = reactive({
|
||||
const requestQueue = [];
|
||||
const activeWorkersByKey = {};
|
||||
let activeWorkers = 0;
|
||||
let trackedActiveWorkers = 0;
|
||||
let trackedPendingJobs = 0;
|
||||
let requestIdCounter = 0;
|
||||
let drainTimer = null;
|
||||
let lastRequestStartedAt = 0;
|
||||
@@ -60,8 +63,8 @@ const startBatchIfNeeded = () => {
|
||||
};
|
||||
|
||||
const syncQueueCounters = () => {
|
||||
requestQueueStateMutable.pending = requestQueue.length;
|
||||
requestQueueStateMutable.active = activeWorkers;
|
||||
requestQueueStateMutable.pending = trackedPendingJobs;
|
||||
requestQueueStateMutable.active = trackedActiveWorkers;
|
||||
};
|
||||
|
||||
const normalizeMethod = (value) => {
|
||||
@@ -79,6 +82,14 @@ const normalizeQueueGroup = (value) => {
|
||||
return value.trim().toUpperCase();
|
||||
};
|
||||
|
||||
const normalizeInsightKey = (value) => {
|
||||
if (typeof value !== "string") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return value.trim().toLowerCase();
|
||||
};
|
||||
|
||||
const normalizeUrl = (value) => {
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
return "(unknown endpoint)";
|
||||
@@ -107,13 +118,13 @@ const toSafeText = (value) => {
|
||||
return "";
|
||||
}
|
||||
|
||||
let text = "";
|
||||
let text;
|
||||
if (typeof value === "string") {
|
||||
text = value;
|
||||
} else {
|
||||
try {
|
||||
text = JSON.stringify(value, null, 2);
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
text = String(value);
|
||||
}
|
||||
}
|
||||
@@ -184,7 +195,7 @@ const parseJsonIfString = (value) => {
|
||||
|
||||
try {
|
||||
return JSON.parse(trimmedValue);
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
@@ -301,6 +312,34 @@ const wait = (durationMs) => new Promise((resolve) => {
|
||||
setTimeout(resolve, Math.max(0, durationMs || 0));
|
||||
});
|
||||
|
||||
const isAbortSignal = (signal) =>
|
||||
signal && typeof signal === "object" && typeof signal.aborted === "boolean";
|
||||
|
||||
const isRequestAbortError = (error) =>
|
||||
error?.name === "AbortError" || error?.name === "CanceledError" || error?.code === "ERR_CANCELED";
|
||||
|
||||
const createAbortError = () => {
|
||||
if (typeof DOMException === "function") {
|
||||
return new DOMException("Request aborted.", "AbortError");
|
||||
}
|
||||
|
||||
const error = new Error("Request aborted.");
|
||||
error.name = "AbortError";
|
||||
error.code = "ERR_CANCELED";
|
||||
return error;
|
||||
};
|
||||
|
||||
const isJobAborted = (job) => isAbortSignal(job?.signal) && job.signal.aborted === true;
|
||||
|
||||
const decrementBatchTotalForCanceledJob = () => {
|
||||
const completedOrFailed = Number(requestQueueStateMutable.batchCompleted || 0)
|
||||
+ Number(requestQueueStateMutable.batchFailed || 0);
|
||||
requestQueueStateMutable.batchTotal = Math.max(
|
||||
completedOrFailed,
|
||||
Number(requestQueueStateMutable.batchTotal || 0) - 1
|
||||
);
|
||||
};
|
||||
|
||||
const getErrorStatusCode = (error) => {
|
||||
const code = Number.parseInt(error?.response?.status, 10);
|
||||
return Number.isFinite(code) ? code : null;
|
||||
@@ -311,6 +350,28 @@ const getResponseStatusCode = (response) => {
|
||||
return Number.isFinite(code) ? code : null;
|
||||
};
|
||||
|
||||
const getHeaderValue = (headers, name) => {
|
||||
if (!headers || typeof name !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof headers.get === "function") {
|
||||
const value = headers.get(name);
|
||||
return typeof value === "string" && value.length > 0 ? value : null;
|
||||
}
|
||||
|
||||
const normalizedName = name.toLowerCase();
|
||||
const matchingKey = Object.keys(headers).find((key) => String(key).toLowerCase() === normalizedName);
|
||||
if (!matchingKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const value = headers[matchingKey];
|
||||
return typeof value === "string" && value.length > 0 ? value : null;
|
||||
};
|
||||
|
||||
const getServerTimingHeader = (response) => getHeaderValue(response?.headers, "server-timing");
|
||||
|
||||
const getRetriesForStatusCode = (statusCode, retryByStatusCode) => {
|
||||
if (statusCode === null) {
|
||||
return 0;
|
||||
@@ -330,6 +391,10 @@ const computeRetryDelayMs = (attemptNumber) => {
|
||||
};
|
||||
|
||||
const shouldRetryAttempt = (error, job, attemptNumber) => {
|
||||
if (isRequestAbortError(error) || isJobAborted(job)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const statusCode = getErrorStatusCode(error);
|
||||
const retryByStatusCode = job.retryByStatusCode ?? queueConfig.retryByStatusCode;
|
||||
const retriesForCode = getRetriesForStatusCode(statusCode, retryByStatusCode);
|
||||
@@ -346,27 +411,38 @@ const shouldRetryAttempt = (error, job, attemptNumber) => {
|
||||
|
||||
const executeJobWithRetries = async (job) => {
|
||||
let attemptCount = 0;
|
||||
const shouldRecordNetworkTotals = job.skipNetworkTotals !== true;
|
||||
|
||||
while (true) {
|
||||
if (isJobAborted(job)) {
|
||||
throw createAbortError();
|
||||
}
|
||||
|
||||
attemptCount += 1;
|
||||
const method = normalizeMethod(job.method);
|
||||
const url = normalizeUrl(job.url);
|
||||
addNetworkTotals({
|
||||
outgoingRequests: 1,
|
||||
outgoingBytes: estimateRequestBytes(job, method, url),
|
||||
});
|
||||
if (shouldRecordNetworkTotals) {
|
||||
addNetworkTotals({
|
||||
outgoingRequests: 1,
|
||||
outgoingBytes: job.skipRequestByteAccounting ? 0 : estimateRequestBytes(job, method, url),
|
||||
});
|
||||
}
|
||||
try {
|
||||
const response = await job.requestFactory();
|
||||
addNetworkTotals({
|
||||
ingoingResponses: 1,
|
||||
ingoingBytes: estimateResponseBytes(response),
|
||||
});
|
||||
return { response, attemptCount };
|
||||
} catch (error) {
|
||||
if (error?.response) {
|
||||
if (shouldRecordNetworkTotals) {
|
||||
addNetworkTotals({
|
||||
ingoingResponses: 1,
|
||||
ingoingBytes: estimateResponseBytes(error.response, error?.message ?? "Request failed"),
|
||||
ingoingBytes: job.skipResponseByteAccounting ? 0 : estimateResponseBytes(response),
|
||||
});
|
||||
}
|
||||
return { response, attemptCount };
|
||||
} catch (error) {
|
||||
if (shouldRecordNetworkTotals && error?.response) {
|
||||
addNetworkTotals({
|
||||
ingoingResponses: 1,
|
||||
ingoingBytes: job.skipResponseByteAccounting
|
||||
? 0
|
||||
: estimateResponseBytes(error.response, error?.message ?? "Request failed"),
|
||||
});
|
||||
}
|
||||
const attemptNumber = attemptCount - 1;
|
||||
@@ -405,6 +481,15 @@ const pushRecentRequest = (entry) => {
|
||||
requestQueueStateMutable.recentRequests = next.slice(0, limit);
|
||||
};
|
||||
|
||||
const upsertRequestInsight = (key, entry) => {
|
||||
const normalizedKey = normalizeInsightKey(key);
|
||||
if (!normalizedKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
requestQueueStateMutable.requestInsights[normalizedKey] = entry;
|
||||
};
|
||||
|
||||
const pushErrorRequest = (entry) => {
|
||||
const limit = Math.max(1, Number(queueConfig.errorHistoryLimit) || 10);
|
||||
const next = [entry, ...requestQueueStateMutable.errorRequests];
|
||||
@@ -499,24 +584,58 @@ export const reportComponentMissingPermission = (permission, options = {}) => {
|
||||
}]);
|
||||
};
|
||||
|
||||
const removePendingAbortListener = (job) => {
|
||||
if (
|
||||
job?.abortPendingListener
|
||||
&& isAbortSignal(job.signal)
|
||||
&& typeof job.signal.removeEventListener === "function"
|
||||
) {
|
||||
job.signal.removeEventListener("abort", job.abortPendingListener);
|
||||
}
|
||||
if (job) {
|
||||
job.abortPendingListener = null;
|
||||
}
|
||||
};
|
||||
|
||||
const rejectCanceledQueuedJob = (job) => {
|
||||
removePendingAbortListener(job);
|
||||
if (job.trackProgressCounters !== false) {
|
||||
trackedPendingJobs = Math.max(0, trackedPendingJobs - 1);
|
||||
decrementBatchTotalForCanceledJob();
|
||||
syncQueueCounters();
|
||||
}
|
||||
job.reject(createAbortError());
|
||||
};
|
||||
|
||||
const runJob = (job) => {
|
||||
const method = normalizeMethod(job.method);
|
||||
const concurrencyKey = getJobConcurrencyKey(job);
|
||||
const queueGroup = normalizeQueueGroup(job.queueGroup) || null;
|
||||
const startedAt = Date.now();
|
||||
|
||||
removePendingAbortListener(job);
|
||||
|
||||
activeWorkers += 1;
|
||||
activeWorkersByKey[concurrencyKey] = Number(activeWorkersByKey[concurrencyKey] || 0) + 1;
|
||||
if (job.trackProgressCounters !== false) {
|
||||
trackedActiveWorkers += 1;
|
||||
}
|
||||
lastRequestStartedAt = startedAt;
|
||||
upsertActiveRequest(job, startedAt);
|
||||
syncQueueCounters();
|
||||
if (job.trackActiveRequest !== false) {
|
||||
upsertActiveRequest(job, startedAt);
|
||||
}
|
||||
if (job.trackProgressCounters !== false) {
|
||||
syncQueueCounters();
|
||||
}
|
||||
|
||||
Promise.resolve()
|
||||
.then(() => executeJobWithRetries(job))
|
||||
.then(({ response, attemptCount }) => {
|
||||
const completedAt = Date.now();
|
||||
requestQueueStateMutable.batchCompleted += 1;
|
||||
pushRecentRequest({
|
||||
if (job.trackProgressCounters !== false) {
|
||||
requestQueueStateMutable.batchCompleted += 1;
|
||||
}
|
||||
const completedEntry = {
|
||||
id: job.id,
|
||||
method,
|
||||
queueGroup,
|
||||
@@ -529,12 +648,26 @@ const runJob = (job) => {
|
||||
completedAt,
|
||||
queueDurationMs: Math.max(0, startedAt - job.enqueuedAt),
|
||||
requestDurationMs: Math.max(0, completedAt - startedAt),
|
||||
});
|
||||
serverTiming: getServerTimingHeader(response),
|
||||
};
|
||||
upsertRequestInsight(job.insightKey, completedEntry);
|
||||
if (job.recordRecentOnSuccess !== false) {
|
||||
pushRecentRequest(completedEntry);
|
||||
}
|
||||
job.resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const completedAt = Date.now();
|
||||
if (isRequestAbortError(error)) {
|
||||
if (job.trackProgressCounters !== false) {
|
||||
decrementBatchTotalForCanceledJob();
|
||||
}
|
||||
job.reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
const statusCode = getErrorStatusCode(error);
|
||||
const attemptCount = Math.max(1, Number(error?.__queueAttemptCount) || 1);
|
||||
const requestSnapshot = {
|
||||
method,
|
||||
url: job.url,
|
||||
@@ -550,29 +683,35 @@ const runJob = (job) => {
|
||||
data: error?.response?.data ?? null,
|
||||
headers: redactHeaders(error?.response?.headers ?? null),
|
||||
};
|
||||
requestQueueStateMutable.batchFailed += 1;
|
||||
pushRecentRequest({
|
||||
if (job.trackProgressCounters !== false) {
|
||||
requestQueueStateMutable.batchFailed += 1;
|
||||
}
|
||||
const failedEntry = {
|
||||
id: job.id,
|
||||
method,
|
||||
queueGroup,
|
||||
url: job.url,
|
||||
success: false,
|
||||
statusCode,
|
||||
attemptCount: Math.max(1, Number(error?.__queueAttemptCount) || 1),
|
||||
attemptCount,
|
||||
queuedAt: job.enqueuedAt,
|
||||
startedAt,
|
||||
completedAt,
|
||||
queueDurationMs: Math.max(0, startedAt - job.enqueuedAt),
|
||||
requestDurationMs: Math.max(0, completedAt - startedAt),
|
||||
});
|
||||
serverTiming: getServerTimingHeader(error?.response),
|
||||
};
|
||||
upsertRequestInsight(job.insightKey, failedEntry);
|
||||
pushRecentRequest(failedEntry);
|
||||
pushErrorRequest({
|
||||
id: job.id,
|
||||
method,
|
||||
queueGroup,
|
||||
url: job.url,
|
||||
statusCode,
|
||||
attemptCount: Math.max(1, Number(error?.__queueAttemptCount) || 1),
|
||||
attemptCount,
|
||||
requestDurationMs: Math.max(0, completedAt - startedAt),
|
||||
serverTiming: getServerTimingHeader(error?.response),
|
||||
requestText: toSafeText(requestSnapshot),
|
||||
responseText: toSafeText(responseSnapshot),
|
||||
});
|
||||
@@ -581,7 +720,7 @@ const runJob = (job) => {
|
||||
method,
|
||||
url: job.url,
|
||||
statusCode,
|
||||
attemptCount: Math.max(1, Number(error?.__queueAttemptCount) || 1),
|
||||
attemptCount,
|
||||
requestDurationMs: Math.max(0, completedAt - startedAt),
|
||||
request: requestSnapshot,
|
||||
response: responseSnapshot,
|
||||
@@ -605,12 +744,19 @@ const runJob = (job) => {
|
||||
})
|
||||
.finally(() => {
|
||||
activeWorkers -= 1;
|
||||
if (job.trackProgressCounters !== false) {
|
||||
trackedActiveWorkers = Math.max(0, trackedActiveWorkers - 1);
|
||||
}
|
||||
activeWorkersByKey[concurrencyKey] = Math.max(0, Number(activeWorkersByKey[concurrencyKey] || 1) - 1);
|
||||
if (activeWorkersByKey[concurrencyKey] === 0) {
|
||||
delete activeWorkersByKey[concurrencyKey];
|
||||
}
|
||||
removeActiveRequest(job.id);
|
||||
syncQueueCounters();
|
||||
if (job.trackActiveRequest !== false) {
|
||||
removeActiveRequest(job.id);
|
||||
}
|
||||
if (job.trackProgressCounters !== false) {
|
||||
syncQueueCounters();
|
||||
}
|
||||
scheduleDrain();
|
||||
});
|
||||
};
|
||||
@@ -631,7 +777,15 @@ const drainQueue = async () => {
|
||||
}
|
||||
|
||||
const [nextJob] = requestQueue.splice(nextRunnableJobIndex, 1);
|
||||
syncQueueCounters();
|
||||
if (isJobAborted(nextJob)) {
|
||||
rejectCanceledQueuedJob(nextJob);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nextJob.trackProgressCounters !== false) {
|
||||
trackedPendingJobs = Math.max(0, trackedPendingJobs - 1);
|
||||
syncQueueCounters();
|
||||
}
|
||||
runJob(nextJob);
|
||||
}
|
||||
};
|
||||
@@ -643,10 +797,18 @@ export const enqueueRequest = (requestFactory, options = {}) => {
|
||||
|
||||
const method = normalizeMethod(options.method);
|
||||
const url = normalizeUrl(options.url);
|
||||
startBatchIfNeeded();
|
||||
const signal = isAbortSignal(options.signal) ? options.signal : null;
|
||||
if (signal?.aborted) {
|
||||
return Promise.reject(createAbortError());
|
||||
}
|
||||
|
||||
const trackProgressCounters = options.trackProgressCounters !== false;
|
||||
if (trackProgressCounters) {
|
||||
startBatchIfNeeded();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
requestQueue.push({
|
||||
const job = {
|
||||
id: ++requestIdCounter,
|
||||
requestFactory,
|
||||
resolve,
|
||||
@@ -659,9 +821,37 @@ export const enqueueRequest = (requestFactory, options = {}) => {
|
||||
shouldRetry: typeof options.shouldRetry === "function" ? options.shouldRetry : null,
|
||||
queueGroup: normalizeQueueGroup(options.queueGroup),
|
||||
concurrencyLimit: options.concurrencyLimit || null,
|
||||
});
|
||||
requestQueueStateMutable.batchTotal += 1;
|
||||
syncQueueCounters();
|
||||
skipRequestByteAccounting: options.skipRequestByteAccounting === true,
|
||||
skipResponseByteAccounting: options.skipResponseByteAccounting === true,
|
||||
skipNetworkTotals: options.skipNetworkTotals === true,
|
||||
insightKey: normalizeInsightKey(options.insightKey),
|
||||
recordRecentOnSuccess: options.recordRecentOnSuccess !== false,
|
||||
trackActiveRequest: options.trackActiveRequest !== false,
|
||||
trackProgressCounters,
|
||||
signal,
|
||||
abortPendingListener: null,
|
||||
};
|
||||
|
||||
if (signal && typeof signal.addEventListener === "function") {
|
||||
job.abortPendingListener = () => {
|
||||
const pendingIndex = requestQueue.findIndex((pendingJob) => pendingJob.id === job.id);
|
||||
if (pendingIndex < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
requestQueue.splice(pendingIndex, 1);
|
||||
rejectCanceledQueuedJob(job);
|
||||
scheduleDrain();
|
||||
};
|
||||
signal.addEventListener("abort", job.abortPendingListener, { once: true });
|
||||
}
|
||||
|
||||
requestQueue.push(job);
|
||||
if (job.trackProgressCounters !== false) {
|
||||
trackedPendingJobs += 1;
|
||||
requestQueueStateMutable.batchTotal += 1;
|
||||
syncQueueCounters();
|
||||
}
|
||||
scheduleDrain();
|
||||
});
|
||||
};
|
||||
@@ -680,8 +870,11 @@ export const clearMissingPermissions = () => {
|
||||
};
|
||||
|
||||
export const __resetRequestQueueForTests = () => {
|
||||
requestQueue.forEach(removePendingAbortListener);
|
||||
requestQueue.length = 0;
|
||||
activeWorkers = 0;
|
||||
trackedActiveWorkers = 0;
|
||||
trackedPendingJobs = 0;
|
||||
requestIdCounter = 0;
|
||||
Object.keys(activeWorkersByKey).forEach((key) => delete activeWorkersByKey[key]);
|
||||
lastRequestStartedAt = 0;
|
||||
@@ -695,6 +888,7 @@ export const __resetRequestQueueForTests = () => {
|
||||
requestQueueStateMutable.batchFailed = 0;
|
||||
requestQueueStateMutable.activeRequests = [];
|
||||
requestQueueStateMutable.recentRequests = [];
|
||||
requestQueueStateMutable.requestInsights = {};
|
||||
requestQueueStateMutable.errorRequests = [];
|
||||
requestQueueStateMutable.missingPermissions = [];
|
||||
requestQueueStateMutable.networkTotals = {
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { axiosMock } = vi.hoisted(() => ({
|
||||
const { axiosMock, fetchMock } = vi.hoisted(() => ({
|
||||
axiosMock: vi.fn(),
|
||||
fetchMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("axios", () => ({
|
||||
@@ -40,9 +41,27 @@ const createDeferred = () => {
|
||||
return { promise, resolve, reject };
|
||||
};
|
||||
|
||||
const createFetchResponse = ({ body = "", headers = {}, status = 200, statusText = "OK" } = {}) => {
|
||||
const normalizedHeaders = Object.fromEntries(
|
||||
Object.entries(headers).map(([key, value]) => [String(key).toLowerCase(), value])
|
||||
);
|
||||
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
statusText,
|
||||
headers: {
|
||||
get: vi.fn((name) => normalizedHeaders[String(name).toLowerCase()] ?? null),
|
||||
},
|
||||
text: vi.fn(() => Promise.resolve(body)),
|
||||
};
|
||||
};
|
||||
|
||||
describe("authenticatedRequest", () => {
|
||||
beforeEach(() => {
|
||||
axiosMock.mockReset();
|
||||
fetchMock.mockReset();
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
__resetRequestQueueForTests();
|
||||
__resetReleaseTimelineForTests();
|
||||
__configureRequestQueueForTests({
|
||||
@@ -76,11 +95,193 @@ describe("authenticatedRequest", () => {
|
||||
|
||||
expect(result).toBe(response);
|
||||
expect(catchCallable).not.toHaveBeenCalled();
|
||||
expect(requestQueueState.batchCompleted).toBe(1);
|
||||
expect(requestQueueState.batchCompleted).toBe(0);
|
||||
expect(requestQueueState.batchFailed).toBe(0);
|
||||
expect(requestQueueState.recentRequests).toHaveLength(0);
|
||||
expect(requestQueueState.requestInsights.scanner).toEqual(
|
||||
expect.objectContaining({
|
||||
url: expect.stringContaining("/modules/scanner/lpr"),
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
})
|
||||
);
|
||||
expect(requestQueueState.errorRequests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("passes scanner lpr FormData through without generic network accounting", async () => {
|
||||
const response = {
|
||||
status: 200,
|
||||
data: {
|
||||
success: true,
|
||||
data: {
|
||||
license_plate_number: "AB12345",
|
||||
},
|
||||
},
|
||||
};
|
||||
const payload = new FormData();
|
||||
payload.append("image", new Blob(["frame"], { type: "image/jpeg" }), "license-plate.jpg");
|
||||
|
||||
axiosMock.mockResolvedValueOnce(response);
|
||||
|
||||
await authenticatedRequest("/modules/scanner/lpr", "POST", payload);
|
||||
|
||||
const axiosConfig = axiosMock.mock.calls[0]?.[0];
|
||||
expect(axiosConfig).toEqual(
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
data: payload,
|
||||
__skipRequestQueue: true,
|
||||
})
|
||||
);
|
||||
expect(axiosConfig.headers["Content-Type"]).toBeUndefined();
|
||||
expect(requestQueueState.networkTotals.outgoingRequests).toBe(0);
|
||||
expect(requestQueueState.networkTotals.outgoingBytes).toBe(0);
|
||||
expect(requestQueueState.networkTotals.ingoingResponses).toBe(0);
|
||||
expect(requestQueueState.networkTotals.ingoingBytes).toBe(0);
|
||||
expect(requestQueueState.recentRequests).toHaveLength(0);
|
||||
expect(requestQueueState.requestInsights.scanner?.url).toContain("/modules/scanner/lpr");
|
||||
});
|
||||
|
||||
it("passes scanner lpr raw image content type through without generic network accounting", async () => {
|
||||
const response = {
|
||||
status: 200,
|
||||
data: {
|
||||
success: true,
|
||||
data: {
|
||||
license_plate_number: "AB12345",
|
||||
},
|
||||
},
|
||||
};
|
||||
const payload = new Blob(["frame"], { type: "image/jpeg" });
|
||||
|
||||
axiosMock.mockResolvedValueOnce(response);
|
||||
|
||||
await authenticatedRequest("/modules/scanner/lpr", "POST", payload, null, null, {
|
||||
headers: {
|
||||
"Content-Type": "image/jpeg",
|
||||
},
|
||||
});
|
||||
|
||||
const axiosConfig = axiosMock.mock.calls[0]?.[0];
|
||||
expect(axiosConfig).toEqual(
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
data: payload,
|
||||
__skipRequestQueue: true,
|
||||
})
|
||||
);
|
||||
expect(axiosConfig.headers).toEqual(
|
||||
expect.objectContaining({
|
||||
"Content-Type": "image/jpeg",
|
||||
})
|
||||
);
|
||||
expect(requestQueueState.networkTotals.outgoingRequests).toBe(0);
|
||||
expect(requestQueueState.networkTotals.ingoingResponses).toBe(0);
|
||||
expect(requestQueueState.recentRequests).toHaveLength(0);
|
||||
expect(requestQueueState.requestInsights.scanner?.url).toContain("/modules/scanner/lpr");
|
||||
});
|
||||
|
||||
it("uses fetch transport for scanner raw image requests when requested", async () => {
|
||||
const payload = new Blob(["frame"], { type: "image/jpeg" });
|
||||
const signal = new AbortController().signal;
|
||||
fetchMock.mockResolvedValueOnce(
|
||||
createFetchResponse({
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
data: {
|
||||
license_plate_number: "AB12345",
|
||||
},
|
||||
}),
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"server-timing": "lpr_total;dur=17.000",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const result = await authenticatedRequest("/modules/scanner/lpr", "POST", payload, null, null, {
|
||||
headers: {
|
||||
"Content-Type": "image/jpeg",
|
||||
},
|
||||
signal,
|
||||
transport: "fetch",
|
||||
});
|
||||
|
||||
expect(result.data).toEqual({
|
||||
success: true,
|
||||
data: {
|
||||
license_plate_number: "AB12345",
|
||||
},
|
||||
});
|
||||
expect(axiosMock).not.toHaveBeenCalled();
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
expect.stringContaining("/modules/scanner/lpr"),
|
||||
expect.objectContaining({
|
||||
body: payload,
|
||||
method: "POST",
|
||||
signal,
|
||||
headers: expect.objectContaining({
|
||||
Authorization: "Bearer token",
|
||||
"Content-Type": "image/jpeg",
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(requestQueueState.networkTotals.outgoingRequests).toBe(0);
|
||||
expect(requestQueueState.recentRequests).toHaveLength(0);
|
||||
expect(requestQueueState.requestInsights.scanner?.serverTiming).toBe("lpr_total;dur=17.000");
|
||||
});
|
||||
|
||||
it("rejects failed fetch transport responses with an axios-like response", async () => {
|
||||
fetchMock.mockResolvedValueOnce(
|
||||
createFetchResponse({
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
data: {
|
||||
message: "Plate Recognizer unavailable.",
|
||||
},
|
||||
}),
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"server-timing": "lpr_total;dur=44.000",
|
||||
},
|
||||
status: 503,
|
||||
statusText: "Service Unavailable",
|
||||
})
|
||||
);
|
||||
const catchCallable = vi.fn();
|
||||
|
||||
await expect(
|
||||
authenticatedRequest("/modules/scanner/lpr", "POST", new Blob(["frame"]), catchCallable, null, {
|
||||
headers: {
|
||||
"Content-Type": "image/jpeg",
|
||||
},
|
||||
transport: "fetch",
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
name: "AxiosError",
|
||||
response: {
|
||||
status: 503,
|
||||
statusText: "Service Unavailable",
|
||||
data: {
|
||||
success: false,
|
||||
data: {
|
||||
message: "Plate Recognizer unavailable.",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(catchCallable).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
name: "AxiosError",
|
||||
response: expect.objectContaining({
|
||||
status: 503,
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(requestQueueState.errorRequests[0].serverTiming).toBe("lpr_total;dur=44.000");
|
||||
});
|
||||
|
||||
it("continues to reject scanner errors", async () => {
|
||||
const error = {
|
||||
response: {
|
||||
@@ -291,7 +492,7 @@ describe("authenticatedRequest", () => {
|
||||
await expect(Promise.all([request1, request2, request3])).resolves.toHaveLength(3);
|
||||
});
|
||||
|
||||
it("keeps POS scanner and Stripe invoice requests from blocking ordinary POS order mutations", async () => {
|
||||
it("serializes POS scanner requests without blocking Stripe invoices or ordinary POS order mutations", async () => {
|
||||
const scannerOne = createDeferred();
|
||||
const scannerTwo = createDeferred();
|
||||
const stripeInvoice = createDeferred();
|
||||
@@ -317,23 +518,23 @@ describe("authenticatedRequest", () => {
|
||||
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(4);
|
||||
expect(axiosMock).toHaveBeenCalledTimes(3);
|
||||
expect(axiosMock.mock.calls.map(([config]) => config.url)).toEqual([
|
||||
expect.stringContaining("/modules/scanner/lpr"),
|
||||
expect.stringContaining("/modules/scanner/lpr"),
|
||||
expect.stringContaining("/modules/stripe/invoice"),
|
||||
expect.stringMatching(/\/orders$/),
|
||||
]);
|
||||
expect(requestQueueState.active).toBe(4);
|
||||
expect(requestQueueState.active).toBe(2);
|
||||
expect(requestQueueState.pending).toBe(0);
|
||||
expect(requestQueueState.activeRequests.map((request) => request.queueGroup)).toEqual([
|
||||
"POS_SCANNER",
|
||||
"POS_SCANNER",
|
||||
"POS_STRIPE",
|
||||
null,
|
||||
]);
|
||||
expect(requestQueueState.activeRequests.map((request) => request.queueGroup)).toEqual(["POS_STRIPE", null]);
|
||||
expect(requestQueueState.activeRequests.some((request) => request.queueGroup === "POS_SCANNER")).toBe(false);
|
||||
|
||||
scannerOne.resolve({ status: 200, data: { success: true } });
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(4);
|
||||
expect(axiosMock.mock.calls[3][0].url).toContain("/modules/scanner/lpr");
|
||||
|
||||
scannerTwo.resolve({ status: 200, data: { success: true } });
|
||||
stripeInvoice.resolve({ status: 200, data: { id: "in_1" } });
|
||||
orderCreate.resolve({ status: 200, data: { id: 42 } });
|
||||
@@ -341,6 +542,99 @@ describe("authenticatedRequest", () => {
|
||||
await expect(Promise.all([request1, request2, request3, request4])).resolves.toHaveLength(4);
|
||||
});
|
||||
|
||||
it("drops queued scanner LPR requests when their signal aborts before Axios starts", async () => {
|
||||
const scannerOne = createDeferred();
|
||||
const scannerTwo = createDeferred();
|
||||
axiosMock.mockImplementation(({ data }) => {
|
||||
if (data?.base64_image === "image-one") {
|
||||
return scannerOne.promise;
|
||||
}
|
||||
if (data?.base64_image === "image-two") {
|
||||
return scannerTwo.promise;
|
||||
}
|
||||
return Promise.reject(new Error("stale scanner frame should not start"));
|
||||
});
|
||||
|
||||
const request1 = authenticatedRequest("/modules/scanner/lpr", "post", { base64_image: "image-one" });
|
||||
const request2 = authenticatedRequest("/modules/scanner/lpr", "post", { base64_image: "image-two" });
|
||||
const controller = new AbortController();
|
||||
const staleRequest = authenticatedRequest(
|
||||
"/modules/scanner/lpr",
|
||||
"post",
|
||||
{ base64_image: "stale-image" },
|
||||
null,
|
||||
null,
|
||||
{ signal: controller.signal }
|
||||
);
|
||||
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(1);
|
||||
expect(requestQueueState.active).toBe(0);
|
||||
expect(requestQueueState.pending).toBe(0);
|
||||
expect(requestQueueState.batchTotal).toBe(0);
|
||||
|
||||
controller.abort();
|
||||
await expect(staleRequest).rejects.toMatchObject({ name: "AbortError" });
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(1);
|
||||
expect(requestQueueState.pending).toBe(0);
|
||||
expect(requestQueueState.batchTotal).toBe(0);
|
||||
expect(requestQueueState.batchFailed).toBe(0);
|
||||
expect(requestQueueState.errorRequests).toHaveLength(0);
|
||||
|
||||
scannerOne.resolve({ status: 200, data: { success: true } });
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(2);
|
||||
expect(requestQueueState.active).toBe(0);
|
||||
expect(requestQueueState.pending).toBe(0);
|
||||
|
||||
scannerTwo.resolve({ status: 200, data: { success: true } });
|
||||
|
||||
await expect(Promise.all([request1, request2])).resolves.toHaveLength(2);
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(requestQueueState.batchCompleted).toBe(0);
|
||||
expect(requestQueueState.batchFailed).toBe(0);
|
||||
});
|
||||
|
||||
it("does not record active scanner LPR aborts as failed API requests", async () => {
|
||||
axiosMock.mockImplementation(
|
||||
({ signal }) =>
|
||||
new Promise((resolve, reject) => {
|
||||
signal.addEventListener("abort", () => {
|
||||
const error = new Error("canceled");
|
||||
error.code = "ERR_CANCELED";
|
||||
reject(error);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const controller = new AbortController();
|
||||
const request = authenticatedRequest("/modules/scanner/lpr", "post", { base64_image: "image-one" }, null, null, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(axiosMock).toHaveBeenCalledTimes(1);
|
||||
expect(requestQueueState.active).toBe(0);
|
||||
expect(requestQueueState.batchTotal).toBe(0);
|
||||
|
||||
controller.abort();
|
||||
|
||||
await expect(request).rejects.toMatchObject({ code: "ERR_CANCELED" });
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(requestQueueState.active).toBe(0);
|
||||
expect(requestQueueState.pending).toBe(0);
|
||||
expect(requestQueueState.batchTotal).toBe(0);
|
||||
expect(requestQueueState.batchFailed).toBe(0);
|
||||
expect(requestQueueState.errorRequests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("does not retry POS Stripe invoice mutations", async () => {
|
||||
__configureRequestQueueForTests({
|
||||
retryByStatusCode: { 500: 1 },
|
||||
|
||||
@@ -0,0 +1,891 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
buildLPRFrameFingerprint,
|
||||
buildLPRFrameFingerprintKey,
|
||||
captureVideoFrameBlobForLPR,
|
||||
getCenteredFocusSourceRect,
|
||||
getConstrainedFrameSize,
|
||||
getLPRFrameSourceRect,
|
||||
getLPRFrameTargetSize,
|
||||
getVisualFingerprintDistance,
|
||||
getVisibleCoverSourceRect,
|
||||
isVideoFrameReadyForLPR,
|
||||
LPR_CAMERA_VIDEO_HEIGHT,
|
||||
LPR_CAMERA_VIDEO_WIDTH,
|
||||
LPR_FRAME_FILE_NAME,
|
||||
LPR_FRAME_FOCUS_ASPECT_RATIO,
|
||||
LPR_FRAME_JPEG_QUALITY,
|
||||
LPR_FRAME_MIME_TYPE,
|
||||
LPR_FRAME_SCANNER_FOCUS_SCALE,
|
||||
LPR_FRAME_SCANNER_MAX_SIZE,
|
||||
LPR_FRAME_SCANNER_JPEG_QUALITY,
|
||||
} from "@/components/viewport/page/templates/scanner/lprFrameCapture";
|
||||
|
||||
describe("LPR frame capture", () => {
|
||||
it("constrains 16:9 camera frames before encoding", () => {
|
||||
expect(getConstrainedFrameSize(1920, 1080)).toEqual({
|
||||
width: 1024,
|
||||
height: 576,
|
||||
});
|
||||
});
|
||||
|
||||
it("uses the full camera frame when rendered video dimensions are unavailable", () => {
|
||||
expect(getVisibleCoverSourceRect(1280, 720, 0, 0)).toEqual({
|
||||
height: 720,
|
||||
width: 1280,
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("checks video readiness before camera LPR capture starts", () => {
|
||||
expect(
|
||||
isVideoFrameReadyForLPR({
|
||||
readyState: 1,
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
})
|
||||
).toBe(false);
|
||||
expect(
|
||||
isVideoFrameReadyForLPR({
|
||||
readyState: 2,
|
||||
videoHeight: 0,
|
||||
videoWidth: 1280,
|
||||
})
|
||||
).toBe(false);
|
||||
expect(
|
||||
isVideoFrameReadyForLPR({
|
||||
readyState: 2,
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
})
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("crops a landscape camera frame to the visible portrait preview before encoding", () => {
|
||||
expect(getVisibleCoverSourceRect(1280, 720, 390, 844)).toEqual({
|
||||
height: 720,
|
||||
width: 333,
|
||||
x: 474,
|
||||
y: 0,
|
||||
});
|
||||
expect(getConstrainedFrameSize(333, 720)).toEqual({
|
||||
width: 266,
|
||||
height: 576,
|
||||
});
|
||||
});
|
||||
|
||||
it("crops the visible preview to the centered scanner focus area before encoding", () => {
|
||||
expect(
|
||||
getCenteredFocusSourceRect({
|
||||
height: 1080,
|
||||
width: 1920,
|
||||
x: 0,
|
||||
y: 0,
|
||||
})
|
||||
).toEqual({
|
||||
height: 1080,
|
||||
width: 1920,
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
expect(getLPRFrameSourceRect(1280, 720, 390, 844)).toEqual({
|
||||
height: 159,
|
||||
width: 283,
|
||||
x: 499,
|
||||
y: 281,
|
||||
});
|
||||
expect(getLPRFrameSourceRect(1280, 720, 390, 844, { focusCrop: false })).toEqual({
|
||||
height: 720,
|
||||
width: 333,
|
||||
x: 474,
|
||||
y: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the live stream cap aligned with attachment preview max while reducing scanner crop pixels", () => {
|
||||
expect(LPR_CAMERA_VIDEO_WIDTH).toBe(1024);
|
||||
expect(LPR_CAMERA_VIDEO_HEIGHT).toBe(576);
|
||||
expect(getLPRFrameSourceRect(LPR_CAMERA_VIDEO_WIDTH, LPR_CAMERA_VIDEO_HEIGHT, 390, 844)).toEqual({
|
||||
height: 128,
|
||||
width: 226,
|
||||
x: 399,
|
||||
y: 224,
|
||||
});
|
||||
expect(
|
||||
getLPRFrameTargetSize(
|
||||
getLPRFrameSourceRect(LPR_CAMERA_VIDEO_WIDTH, LPR_CAMERA_VIDEO_HEIGHT, 390, 844, { focusCrop: false }),
|
||||
{ focusCrop: false }
|
||||
)
|
||||
).toEqual({
|
||||
height: 576,
|
||||
width: 266,
|
||||
});
|
||||
});
|
||||
|
||||
it("can tighten the centered scanner crop while keeping a margin around the outline", () => {
|
||||
expect(LPR_FRAME_FOCUS_ASPECT_RATIO).toBe(16 / 9);
|
||||
expect(LPR_FRAME_SCANNER_FOCUS_SCALE).toBe(0.85);
|
||||
expect(
|
||||
getCenteredFocusSourceRect(
|
||||
{
|
||||
height: 720,
|
||||
width: 333,
|
||||
x: 474,
|
||||
y: 0,
|
||||
},
|
||||
LPR_FRAME_FOCUS_ASPECT_RATIO,
|
||||
LPR_FRAME_SCANNER_FOCUS_SCALE
|
||||
)
|
||||
).toEqual({
|
||||
height: 159,
|
||||
width: 283,
|
||||
x: 499,
|
||||
y: 281,
|
||||
});
|
||||
expect(getLPRFrameSourceRect(1280, 720, 390, 844, { focusScale: 1 })).toEqual({
|
||||
height: 187,
|
||||
width: 333,
|
||||
x: 474,
|
||||
y: 267,
|
||||
});
|
||||
});
|
||||
|
||||
it("anchors the scanner crop to a viewport focus rect when available", () => {
|
||||
expect(
|
||||
getLPRFrameSourceRect(1280, 720, 390, 844, {
|
||||
focusViewportRect: {
|
||||
height: 234,
|
||||
width: 234,
|
||||
x: 78,
|
||||
y: 205,
|
||||
},
|
||||
})
|
||||
).toEqual({
|
||||
height: 159,
|
||||
width: 283,
|
||||
x: 499,
|
||||
y: 195,
|
||||
});
|
||||
expect(
|
||||
getLPRFrameSourceRect(1280, 720, 390, 844, {
|
||||
focusViewportRect: {
|
||||
height: 234,
|
||||
width: 234,
|
||||
x: 78,
|
||||
y: -100,
|
||||
},
|
||||
})
|
||||
).toEqual({
|
||||
height: 159,
|
||||
width: 283,
|
||||
x: 499,
|
||||
y: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("uses a smaller max size for scanner LPR frames than preview frames", () => {
|
||||
expect(
|
||||
getLPRFrameTargetSize({
|
||||
height: 1080,
|
||||
width: 1440,
|
||||
x: 240,
|
||||
y: 0,
|
||||
})
|
||||
).toEqual({
|
||||
height: 288,
|
||||
width: LPR_FRAME_SCANNER_MAX_SIZE,
|
||||
});
|
||||
expect(
|
||||
getLPRFrameTargetSize(
|
||||
{
|
||||
height: 720,
|
||||
width: 333,
|
||||
x: 474,
|
||||
y: 0,
|
||||
},
|
||||
{ focusCrop: false }
|
||||
)
|
||||
).toEqual({
|
||||
height: 576,
|
||||
width: 266,
|
||||
});
|
||||
});
|
||||
|
||||
it("captures a focused downscaled JPEG blob frame for multipart LPR uploads", async () => {
|
||||
const blob = new Blob(["small-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const toBlob = vi.fn((callback) => callback(blob));
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
captureTimings: {
|
||||
drawMs: expect.any(Number),
|
||||
encodeMs: expect.any(Number),
|
||||
visualFingerprintMs: expect.any(Number),
|
||||
},
|
||||
filename: LPR_FRAME_FILE_NAME,
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
getContentFingerprint: expect.any(Function),
|
||||
height: 216,
|
||||
mimeType: LPR_FRAME_MIME_TYPE,
|
||||
width: 384,
|
||||
});
|
||||
expect(canvas.width).toBe(384);
|
||||
expect(canvas.height).toBe(216);
|
||||
expect(frame.captureDurationMs).toBeGreaterThanOrEqual(0);
|
||||
expect(frame.captureTimings.drawMs).toBeGreaterThanOrEqual(0);
|
||||
expect(frame.captureTimings.encodeMs).toBeGreaterThanOrEqual(0);
|
||||
expect(frame.captureTimings.visualFingerprintMs).toBeGreaterThanOrEqual(0);
|
||||
expect(LPR_FRAME_SCANNER_JPEG_QUALITY).toBe(0.6);
|
||||
expect(canvas.getContext).toHaveBeenCalledWith("2d", {
|
||||
alpha: false,
|
||||
desynchronized: true,
|
||||
});
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 384, 216);
|
||||
expect(toBlob).toHaveBeenCalledWith(expect.any(Function), LPR_FRAME_MIME_TYPE, LPR_FRAME_SCANNER_JPEG_QUALITY);
|
||||
});
|
||||
|
||||
it("notifies after drawing the current video frame before JPEG encoding starts", async () => {
|
||||
const blob = new Blob(["small-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const events = [];
|
||||
const drawImage = vi.fn(() => {
|
||||
events.push("draw");
|
||||
});
|
||||
const toBlob = vi.fn((callback) => {
|
||||
events.push("encode");
|
||||
callback(blob);
|
||||
});
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
const onFrameDrawn = vi.fn(() => {
|
||||
events.push("drawn-callback");
|
||||
});
|
||||
|
||||
await captureVideoFrameBlobForLPR(video, canvas, { onFrameDrawn });
|
||||
|
||||
expect(onFrameDrawn).toHaveBeenCalledTimes(1);
|
||||
expect(events).toEqual(["draw", "drawn-callback", "encode"]);
|
||||
});
|
||||
|
||||
it("reuses the 2d encoding context for repeated captures on the same canvas", async () => {
|
||||
const blob = new Blob(["cached-context-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
await captureVideoFrameBlobForLPR(video, canvas);
|
||||
await captureVideoFrameBlobForLPR(video, canvas);
|
||||
|
||||
expect(canvas.getContext).toHaveBeenCalledTimes(1);
|
||||
expect(drawImage).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("uses a reusable OffscreenCanvas encoder when the browser supports it", async () => {
|
||||
const blob = new Blob(["offscreen-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const offscreenCanvases = [];
|
||||
const offscreenConstructor = vi.fn(function FakeOffscreenCanvas(width, height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.drawImage = vi.fn();
|
||||
this.getContext = vi.fn(() => ({
|
||||
drawImage: this.drawImage,
|
||||
}));
|
||||
this.convertToBlob = vi.fn(() => Promise.resolve(blob));
|
||||
offscreenCanvases.push(this);
|
||||
});
|
||||
const fallbackDrawImage = vi.fn();
|
||||
const fallbackToBlob = vi.fn();
|
||||
const fallbackCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fallbackDrawImage,
|
||||
})),
|
||||
toBlob: fallbackToBlob,
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
vi.stubGlobal("OffscreenCanvas", offscreenConstructor);
|
||||
try {
|
||||
const frame = await captureVideoFrameBlobForLPR(video, fallbackCanvas);
|
||||
const secondFrame = await captureVideoFrameBlobForLPR(video, fallbackCanvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
height: 216,
|
||||
mimeType: LPR_FRAME_MIME_TYPE,
|
||||
width: 384,
|
||||
});
|
||||
expect(secondFrame).toMatchObject({
|
||||
blob,
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
});
|
||||
expect(offscreenConstructor).toHaveBeenCalledTimes(1);
|
||||
expect(offscreenConstructor).toHaveBeenCalledWith(1, 1);
|
||||
expect(offscreenCanvases[0].width).toBe(384);
|
||||
expect(offscreenCanvases[0].height).toBe(216);
|
||||
expect(offscreenCanvases[0].drawImage).toHaveBeenCalledTimes(2);
|
||||
expect(offscreenCanvases[0].drawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 384, 216);
|
||||
expect(offscreenCanvases[0].convertToBlob).toHaveBeenCalledWith({
|
||||
type: LPR_FRAME_MIME_TYPE,
|
||||
quality: LPR_FRAME_SCANNER_JPEG_QUALITY,
|
||||
});
|
||||
expect(fallbackCanvas.width).toBe(0);
|
||||
expect(fallbackCanvas.height).toBe(0);
|
||||
expect(fallbackCanvas.getContext).not.toHaveBeenCalled();
|
||||
expect(fallbackDrawImage).not.toHaveBeenCalled();
|
||||
expect(fallbackToBlob).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
|
||||
it("falls back to the DOM canvas when OffscreenCanvas cannot encode a blob", async () => {
|
||||
const blob = new Blob(["fallback-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const offscreenDrawImage = vi.fn();
|
||||
const offscreenConstructor = vi.fn(function FakeOffscreenCanvas(width, height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.getContext = vi.fn(() => ({
|
||||
drawImage: offscreenDrawImage,
|
||||
}));
|
||||
});
|
||||
const fallbackDrawImage = vi.fn();
|
||||
const fallbackToBlob = vi.fn((callback) => callback(blob));
|
||||
const fallbackCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fallbackDrawImage,
|
||||
})),
|
||||
toBlob: fallbackToBlob,
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
vi.stubGlobal("OffscreenCanvas", offscreenConstructor);
|
||||
try {
|
||||
const frame = await captureVideoFrameBlobForLPR(video, fallbackCanvas);
|
||||
const secondFrame = await captureVideoFrameBlobForLPR(video, fallbackCanvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
height: 216,
|
||||
width: 384,
|
||||
});
|
||||
expect(secondFrame).toMatchObject({
|
||||
blob,
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
});
|
||||
expect(offscreenConstructor).toHaveBeenCalledTimes(1);
|
||||
expect(offscreenDrawImage).toHaveBeenCalledTimes(1);
|
||||
expect(offscreenDrawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 384, 216);
|
||||
expect(fallbackCanvas.width).toBe(384);
|
||||
expect(fallbackCanvas.height).toBe(216);
|
||||
expect(fallbackDrawImage).toHaveBeenCalledTimes(2);
|
||||
expect(fallbackDrawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 384, 216);
|
||||
expect(fallbackToBlob).toHaveBeenCalledTimes(2);
|
||||
expect(fallbackToBlob).toHaveBeenCalledWith(
|
||||
expect.any(Function),
|
||||
LPR_FRAME_MIME_TYPE,
|
||||
LPR_FRAME_SCANNER_JPEG_QUALITY
|
||||
);
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
|
||||
it("adds a stable visual fingerprint when canvas pixels can be sampled", async () => {
|
||||
const blob = new Blob(["small-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const fingerprintDrawImage = vi.fn();
|
||||
const pixels = new Uint8ClampedArray(8 * 8 * 4);
|
||||
for (let index = 0; index < 8 * 8; index += 1) {
|
||||
const offset = index * 4;
|
||||
const value = index % 8 < 4 ? 30 : 220;
|
||||
pixels[offset] = value;
|
||||
pixels[offset + 1] = value;
|
||||
pixels[offset + 2] = value;
|
||||
pixels[offset + 3] = 255;
|
||||
}
|
||||
const getImageData = vi.fn(() => ({
|
||||
data: pixels,
|
||||
}));
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const fingerprintCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fingerprintDrawImage,
|
||||
getImageData,
|
||||
})),
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, { visualFingerprintCanvas: fingerprintCanvas });
|
||||
|
||||
expect(frame.visualFingerprint).toMatch(/^[0-9a-f]{16}$/);
|
||||
expect(frame.visualFingerprint).toBe("0f0f0f0f0f0f0f0f");
|
||||
expect(fingerprintCanvas.width).toBe(8);
|
||||
expect(fingerprintCanvas.height).toBe(8);
|
||||
expect(fingerprintDrawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 8, 8);
|
||||
expect(getImageData).toHaveBeenCalledTimes(1);
|
||||
expect(getImageData).toHaveBeenCalledWith(0, 0, 8, 8);
|
||||
expect(getVisualFingerprintDistance(frame.visualFingerprint, frame.visualFingerprint)).toBe(0);
|
||||
expect(getVisualFingerprintDistance("0000000000000000", "000000000000000f")).toBe(4);
|
||||
});
|
||||
|
||||
it("reuses the visual fingerprint context for repeated scene checks", async () => {
|
||||
const blob = new Blob(["visual-context-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const fingerprintDrawImage = vi.fn();
|
||||
const pixels = new Uint8ClampedArray(8 * 8 * 4).fill(200);
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const fingerprintCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fingerprintDrawImage,
|
||||
getImageData: vi.fn(() => ({
|
||||
data: pixels,
|
||||
})),
|
||||
})),
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
await captureVideoFrameBlobForLPR(video, canvas, { visualFingerprintCanvas: fingerprintCanvas });
|
||||
await captureVideoFrameBlobForLPR(video, canvas, { visualFingerprintCanvas: fingerprintCanvas });
|
||||
|
||||
expect(fingerprintCanvas.getContext).toHaveBeenCalledTimes(1);
|
||||
expect(fingerprintDrawImage).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("can skip full canvas capture before drawing a visually unchanged candidate", async () => {
|
||||
const drawImage = vi.fn();
|
||||
const fingerprintDrawImage = vi.fn();
|
||||
const pixels = new Uint8ClampedArray(8 * 8 * 4).fill(200);
|
||||
const shouldEncode = vi.fn(() => false);
|
||||
const toBlob = vi.fn();
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
const fingerprintCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fingerprintDrawImage,
|
||||
getImageData: vi.fn(() => ({
|
||||
data: pixels,
|
||||
})),
|
||||
})),
|
||||
};
|
||||
const video = {
|
||||
videoHeight: 6,
|
||||
videoWidth: 8,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, {
|
||||
focusScale: 1,
|
||||
shouldEncode,
|
||||
visualFingerprintCanvas: fingerprintCanvas,
|
||||
});
|
||||
|
||||
expect(frame).toBeNull();
|
||||
expect(canvas.width).toBe(0);
|
||||
expect(canvas.height).toBe(0);
|
||||
expect(drawImage).not.toHaveBeenCalled();
|
||||
expect(fingerprintDrawImage).toHaveBeenCalledWith(video, 0, 1, 8, 5, 0, 0, 8, 8);
|
||||
expect(shouldEncode).toHaveBeenCalledWith({
|
||||
height: 5,
|
||||
visualFingerprint: expect.stringMatching(/^[0-9a-f]{16}$/),
|
||||
width: 8,
|
||||
});
|
||||
expect(toBlob).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("can defer the visual fingerprint until a posted frame is known to need it", async () => {
|
||||
const blob = new Blob(["deferred-visual"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const fingerprintDrawImage = vi.fn();
|
||||
const pixels = new Uint8ClampedArray(8 * 8 * 4);
|
||||
for (let index = 0; index < 8 * 8; index += 1) {
|
||||
const offset = index * 4;
|
||||
const value = index % 8 < 4 ? 30 : 220;
|
||||
pixels[offset] = value;
|
||||
pixels[offset + 1] = value;
|
||||
pixels[offset + 2] = value;
|
||||
pixels[offset + 3] = 255;
|
||||
}
|
||||
const getImageData = vi.fn(() => ({
|
||||
data: pixels,
|
||||
}));
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const fingerprintCanvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage: fingerprintDrawImage,
|
||||
getImageData,
|
||||
})),
|
||||
};
|
||||
const shouldBuildVisualFingerprint = vi.fn(() => false);
|
||||
const shouldEncode = vi.fn(() => true);
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, {
|
||||
shouldBuildVisualFingerprint,
|
||||
shouldEncode,
|
||||
visualFingerprintCanvas: fingerprintCanvas,
|
||||
});
|
||||
|
||||
expect(frame.visualFingerprint).toBeUndefined();
|
||||
expect(frame.getVisualFingerprint).toEqual(expect.any(Function));
|
||||
expect(shouldBuildVisualFingerprint).toHaveBeenCalledTimes(1);
|
||||
expect(shouldEncode).toHaveBeenCalledWith({
|
||||
height: 216,
|
||||
width: 384,
|
||||
});
|
||||
expect(fingerprintDrawImage).not.toHaveBeenCalled();
|
||||
|
||||
expect(frame.getVisualFingerprint()).toBe("0f0f0f0f0f0f0f0f");
|
||||
expect(fingerprintDrawImage).toHaveBeenCalledWith(canvas, 0, 0, 384, 216, 0, 0, 8, 8);
|
||||
expect(getImageData).toHaveBeenCalledTimes(1);
|
||||
expect(frame.getVisualFingerprint()).toBe("0f0f0f0f0f0f0f0f");
|
||||
expect(getImageData).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("captures only the centered focus area from the visible portrait preview for multipart LPR uploads", async () => {
|
||||
const blob = new Blob(["portrait-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const toBlob = vi.fn((callback) => callback(blob));
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
const video = {
|
||||
clientHeight: 844,
|
||||
clientWidth: 390,
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
fingerprint: `283x159:${blob.size}`,
|
||||
getContentFingerprint: expect.any(Function),
|
||||
height: 159,
|
||||
width: 283,
|
||||
});
|
||||
expect(canvas.width).toBe(283);
|
||||
expect(canvas.height).toBe(159);
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 499, 281, 283, 159, 0, 0, 283, 159);
|
||||
});
|
||||
|
||||
it("uses supplied viewport dimensions without reading rendered video layout", async () => {
|
||||
const blob = new Blob(["cached-viewport-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const getClientHeight = vi.fn(() => 0);
|
||||
const getClientWidth = vi.fn(() => 0);
|
||||
const video = {
|
||||
get clientHeight() {
|
||||
return getClientHeight();
|
||||
},
|
||||
get clientWidth() {
|
||||
return getClientWidth();
|
||||
},
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, {
|
||||
viewportHeight: 844,
|
||||
viewportWidth: 390,
|
||||
});
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
fingerprint: `283x159:${blob.size}`,
|
||||
height: 159,
|
||||
width: 283,
|
||||
});
|
||||
expect(getClientHeight).not.toHaveBeenCalled();
|
||||
expect(getClientWidth).not.toHaveBeenCalled();
|
||||
expect(drawImage.mock.calls[0][0]).toBe(video);
|
||||
expect(drawImage.mock.calls[0].slice(1)).toEqual([499, 281, 283, 159, 0, 0, 283, 159]);
|
||||
});
|
||||
|
||||
it("captures from the scanner outline center when a focus rect is supplied", async () => {
|
||||
const blob = new Blob(["focused-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const video = {
|
||||
clientHeight: 844,
|
||||
clientWidth: 390,
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, {
|
||||
focusViewportRect: {
|
||||
height: 234,
|
||||
width: 234,
|
||||
x: 78,
|
||||
y: 205,
|
||||
},
|
||||
});
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
fingerprint: `283x159:${blob.size}`,
|
||||
height: 159,
|
||||
width: 283,
|
||||
});
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 499, 195, 283, 159, 0, 0, 283, 159);
|
||||
});
|
||||
|
||||
it("can capture the full visible preview crop for non-LPR attachment frames", async () => {
|
||||
const blob = new Blob(["attachment-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const toBlob = vi.fn((callback) => callback(blob));
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
const video = {
|
||||
clientHeight: 844,
|
||||
clientWidth: 390,
|
||||
videoHeight: 720,
|
||||
videoWidth: 1280,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas, { focusCrop: false });
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
fingerprint: `266x576:${blob.size}`,
|
||||
getContentFingerprint: expect.any(Function),
|
||||
height: 576,
|
||||
width: 266,
|
||||
});
|
||||
expect(canvas.width).toBe(266);
|
||||
expect(canvas.height).toBe(576);
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 474, 0, 333, 720, 0, 0, 266, 576);
|
||||
expect(toBlob).toHaveBeenCalledWith(expect.any(Function), LPR_FRAME_MIME_TYPE, LPR_FRAME_JPEG_QUALITY);
|
||||
});
|
||||
|
||||
it("does not reset canvas dimensions when the target frame size is unchanged", async () => {
|
||||
const blob = new Blob(["same-size-frame"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const drawImage = vi.fn();
|
||||
const toBlob = vi.fn((callback) => callback(blob));
|
||||
const widthSetter = vi.fn();
|
||||
const heightSetter = vi.fn();
|
||||
const canvas = {
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob,
|
||||
};
|
||||
Object.defineProperty(canvas, "width", {
|
||||
get: () => 384,
|
||||
set: widthSetter,
|
||||
});
|
||||
Object.defineProperty(canvas, "height", {
|
||||
get: () => 216,
|
||||
set: heightSetter,
|
||||
});
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
fingerprint: `384x216:${blob.size}`,
|
||||
getContentFingerprint: expect.any(Function),
|
||||
height: 216,
|
||||
width: 384,
|
||||
});
|
||||
expect(widthSetter).not.toHaveBeenCalled();
|
||||
expect(heightSetter).not.toHaveBeenCalled();
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 144, 81, 1632, 918, 0, 0, 384, 216);
|
||||
});
|
||||
|
||||
it("does not wait for content fingerprint bytes before returning a captured frame", async () => {
|
||||
const pendingArrayBuffer = new Promise(() => {});
|
||||
const blob = {
|
||||
size: 123,
|
||||
type: LPR_FRAME_MIME_TYPE,
|
||||
slice: vi.fn(() => ({
|
||||
arrayBuffer: vi.fn(() => pendingArrayBuffer),
|
||||
})),
|
||||
};
|
||||
const drawImage = vi.fn();
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({
|
||||
drawImage,
|
||||
})),
|
||||
toBlob: vi.fn((callback) => callback(blob)),
|
||||
};
|
||||
const video = {
|
||||
videoWidth: 1920,
|
||||
videoHeight: 1080,
|
||||
};
|
||||
|
||||
const frame = await captureVideoFrameBlobForLPR(video, canvas);
|
||||
|
||||
expect(frame).toMatchObject({
|
||||
blob,
|
||||
captureDurationMs: expect.any(Number),
|
||||
fingerprint: "384x216:123",
|
||||
getContentFingerprint: expect.any(Function),
|
||||
height: 216,
|
||||
width: 384,
|
||||
});
|
||||
expect(blob.slice).not.toHaveBeenCalled();
|
||||
const contentFingerprintPromise = frame.getContentFingerprint();
|
||||
expect(blob.slice).toHaveBeenCalled();
|
||||
expect(frame.getContentFingerprint()).toBe(contentFingerprintPromise);
|
||||
});
|
||||
|
||||
it("samples duplicate content fingerprint offsets only once for small blobs", async () => {
|
||||
const slice = vi.fn(() => ({
|
||||
arrayBuffer: vi.fn(() => Promise.resolve(new Uint8Array([1, 2, 3, 4]).buffer)),
|
||||
}));
|
||||
const blob = {
|
||||
size: 4,
|
||||
slice,
|
||||
};
|
||||
|
||||
await expect(buildLPRFrameFingerprint(blob, 300, 225)).resolves.toMatch(/^300x225:4:[0-9a-f]{8}$/);
|
||||
|
||||
expect(slice).toHaveBeenCalledTimes(1);
|
||||
expect(slice).toHaveBeenCalledWith(0, 4);
|
||||
});
|
||||
|
||||
it("builds stable blob fingerprints without using capture time", async () => {
|
||||
const first = new Blob(["same-size-a"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const second = new Blob(["same-size-a"], { type: LPR_FRAME_MIME_TYPE });
|
||||
const changed = new Blob(["same-size-b"], { type: LPR_FRAME_MIME_TYPE });
|
||||
|
||||
await expect(buildLPRFrameFingerprint(second, 1024, 576)).resolves.toBe(
|
||||
await buildLPRFrameFingerprint(first, 1024, 576)
|
||||
);
|
||||
await expect(buildLPRFrameFingerprint(changed, 1024, 576)).resolves.not.toBe(
|
||||
await buildLPRFrameFingerprint(first, 1024, 576)
|
||||
);
|
||||
expect(buildLPRFrameFingerprintKey(first, 1024, 576)).toBe(`1024x576:${first.size}`);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -410,28 +410,227 @@ describe("RequestQueueProgress", () => {
|
||||
const wrapper = mount(RequestQueueProgress);
|
||||
const pingRequest = createDeferred();
|
||||
const bookingsRequest = createDeferred();
|
||||
const scannerRequest = createDeferred();
|
||||
|
||||
const requestOne = enqueueRequest(() => pingRequest.promise, { method: "GET", url: "/ping" });
|
||||
const requestTwo = enqueueRequest(() => bookingsRequest.promise, { method: "GET", url: "/order-bookings" });
|
||||
const requestThree = enqueueRequest(() => scannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
|
||||
await triggerShiftTriplePress();
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const scannerServerTiming =
|
||||
"lpr_client_capture;dur=12.345, lpr_client_preflight;dur=1.500, lpr_client_visual_fingerprint;dur=0.750, lpr_client_draw;dur=2.250, lpr_client_encode;dur=8.500, lpr_client_frame_width;dur=300.000, lpr_client_frame_height;dur=225.000, lpr_client_frame_bytes;dur=12345.000, lpr_cache_miss;dur=1.000, lpr_cache;dur=0.750, lpr_local;dur=2.500, lpr_upstream_processing;dur=30.250, lpr_total;dur=42.500, lpr_upstream_total;dur=40.000";
|
||||
pingRequest.resolve({ status: 200 });
|
||||
bookingsRequest.resolve({ status: 200 });
|
||||
await Promise.all([requestOne, requestTwo]);
|
||||
scannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": scannerServerTiming,
|
||||
},
|
||||
});
|
||||
await Promise.all([requestOne, requestTwo, requestThree]);
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const insights = wrapper.get("[data-testid='request-queue-bottom-request-insights']");
|
||||
expect(insights.text()).toContain("Ping");
|
||||
expect(insights.text()).toContain("Bookings");
|
||||
expect(insights.text()).toContain("ms");
|
||||
expect(insights.text()).toContain("Scanner");
|
||||
expect(insights.text()).toContain("browser");
|
||||
expect(insights.text()).toContain("cap 12.345 ms");
|
||||
expect(insights.text()).toContain("prep 1.5 ms");
|
||||
expect(insights.text()).toContain("vf 0.75 ms");
|
||||
expect(insights.text()).toContain("draw 2.25 ms");
|
||||
expect(insights.text()).toContain("enc 8.5 ms");
|
||||
expect(insights.text()).toContain("img 300x225");
|
||||
expect(insights.text()).toContain("bytes 12.1 KB");
|
||||
expect(insights.text()).toContain("cache miss");
|
||||
expect(insights.text()).toContain("cache 0.75 ms");
|
||||
expect(insights.text()).toContain("local 2.5 ms");
|
||||
expect(insights.text()).toContain("proc 30.25 ms");
|
||||
expect(insights.text()).toContain("up 40 ms");
|
||||
expect(insights.text()).toContain("srv 42.5 ms");
|
||||
|
||||
const pingInsight = wrapper.get("[data-testid='request-queue-insight-ping']");
|
||||
const bookingsInsight = wrapper.get("[data-testid='request-queue-insight-bookings']");
|
||||
const scannerInsight = wrapper.get("[data-testid='request-queue-insight-scanner']");
|
||||
expect(pingInsight.text()).toMatch(/now|s ago|m ago/);
|
||||
expect(bookingsInsight.text()).toMatch(/now|s ago|m ago/);
|
||||
expect(scannerInsight.text()).toMatch(/now|s ago|m ago/);
|
||||
expect(scannerInsight.get(".request-queue-progress__bottom-request-latency").attributes("title")).toBe(
|
||||
scannerServerTiming
|
||||
);
|
||||
});
|
||||
|
||||
it("shows scanner cache hits without upstream timing", async () => {
|
||||
const wrapper = mount(RequestQueueProgress);
|
||||
const scannerRequest = createDeferred();
|
||||
|
||||
const request = enqueueRequest(() => scannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
|
||||
await triggerShiftTriplePress();
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const scannerServerTiming =
|
||||
"lpr_client_capture;dur=9.500, lpr_cache_hit;dur=1.000, lpr_cache;dur=0.430, lpr_local;dur=1.200, lpr_total;dur=1.200";
|
||||
scannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": scannerServerTiming,
|
||||
},
|
||||
});
|
||||
await request;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const scannerInsight = wrapper.get("[data-testid='request-queue-insight-scanner']");
|
||||
expect(scannerInsight.text()).toContain("cache hit");
|
||||
expect(scannerInsight.text()).toContain("cache 0.43 ms");
|
||||
expect(scannerInsight.text()).toContain("local 1.2 ms");
|
||||
expect(scannerInsight.text()).toContain("srv 1.2 ms");
|
||||
expect(scannerInsight.text()).not.toContain("up ");
|
||||
expect(scannerInsight.get(".request-queue-progress__bottom-request-latency").attributes("title")).toBe(
|
||||
scannerServerTiming
|
||||
);
|
||||
});
|
||||
|
||||
it("shows scanner insights when successful frames skip the generic recent request list", async () => {
|
||||
const wrapper = mount(RequestQueueProgress);
|
||||
const staleRecentRequest = createDeferred();
|
||||
const scannerRequest = createDeferred();
|
||||
|
||||
const staleRequest = enqueueRequest(() => staleRecentRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
});
|
||||
const request = enqueueRequest(() => scannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
insightKey: "scanner",
|
||||
recordRecentOnSuccess: false,
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
|
||||
await triggerShiftTriplePress();
|
||||
await flushManyMicrotasks();
|
||||
|
||||
staleRecentRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": "lpr_client_capture;dur=99.000, lpr_total;dur=120.000",
|
||||
},
|
||||
});
|
||||
await staleRequest;
|
||||
await flushManyMicrotasks();
|
||||
vi.advanceTimersByTime(5);
|
||||
|
||||
const scannerServerTiming = "lpr_client_capture;dur=8.000, lpr_total;dur=24.000";
|
||||
scannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": scannerServerTiming,
|
||||
},
|
||||
});
|
||||
await request;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(requestQueueState.recentRequests).toHaveLength(1);
|
||||
expect(requestQueueState.recentRequests[0].serverTiming).toContain("dur=99.000");
|
||||
const scannerInsight = wrapper.get("[data-testid='request-queue-insight-scanner']");
|
||||
expect(scannerInsight.text()).toContain("browser");
|
||||
expect(scannerInsight.text()).toContain("cap 8 ms");
|
||||
expect(scannerInsight.text()).toContain("srv 24 ms");
|
||||
expect(scannerInsight.text()).not.toContain("99 ms");
|
||||
expect(scannerInsight.get(".request-queue-progress__bottom-request-latency").attributes("title")).toBe(
|
||||
scannerServerTiming
|
||||
);
|
||||
});
|
||||
|
||||
it("updates scanner request insights without replacing the insights container", async () => {
|
||||
const firstScannerRequest = createDeferred();
|
||||
const secondScannerRequest = createDeferred();
|
||||
const firstRequest = enqueueRequest(() => firstScannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
insightKey: "scanner",
|
||||
recordRecentOnSuccess: false,
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
|
||||
firstScannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": "lpr_total;dur=24.000",
|
||||
},
|
||||
});
|
||||
await firstRequest;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const requestInsightsRef = requestQueueState.requestInsights;
|
||||
expect(requestInsightsRef.scanner?.serverTiming).toBe("lpr_total;dur=24.000");
|
||||
|
||||
const secondRequest = enqueueRequest(() => secondScannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
insightKey: "scanner",
|
||||
recordRecentOnSuccess: false,
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
secondScannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": "lpr_total;dur=18.000",
|
||||
},
|
||||
});
|
||||
await secondRequest;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(requestQueueState.requestInsights).toBe(requestInsightsRef);
|
||||
expect(requestQueueState.requestInsights.scanner?.serverTiming).toBe("lpr_total;dur=18.000");
|
||||
});
|
||||
|
||||
it("shows scanner queue wait separately from browser request time", async () => {
|
||||
const wrapper = mount(RequestQueueProgress);
|
||||
const blockingRequest = createDeferred();
|
||||
const scannerRequest = createDeferred();
|
||||
|
||||
const requestOne = enqueueRequest(() => blockingRequest.promise, { method: "POST", url: "/blocking-post" });
|
||||
const requestTwo = enqueueRequest(() => scannerRequest.promise, {
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
});
|
||||
await flushManyMicrotasks();
|
||||
|
||||
await triggerShiftTriplePress();
|
||||
await flushManyMicrotasks();
|
||||
|
||||
vi.advanceTimersByTime(37);
|
||||
blockingRequest.resolve({ status: 200 });
|
||||
await requestOne;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
vi.advanceTimersByTime(63);
|
||||
scannerRequest.resolve({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": "lpr_total;dur=21.000",
|
||||
},
|
||||
});
|
||||
await requestTwo;
|
||||
await flushManyMicrotasks();
|
||||
|
||||
const scannerInsight = wrapper.get("[data-testid='request-queue-insight-scanner']");
|
||||
expect(scannerInsight.text()).toContain("browser 63 ms");
|
||||
expect(scannerInsight.text()).toContain("queue 37 ms");
|
||||
expect(scannerInsight.text()).toContain("net 42 ms");
|
||||
expect(scannerInsight.text()).toContain("srv 21 ms");
|
||||
});
|
||||
|
||||
it("does not emit recursive update errors while processing many requests", async () => {
|
||||
@@ -458,6 +657,57 @@ describe("RequestQueueProgress", () => {
|
||||
expect(hasRecursiveError).toBe(false);
|
||||
});
|
||||
|
||||
it("stores server timing headers with completed and failed request entries", async () => {
|
||||
__configureRequestQueueForTests({
|
||||
errorHistoryLimit: 2,
|
||||
retryByStatusCode: {},
|
||||
});
|
||||
|
||||
const completed = enqueueRequest(
|
||||
async () => ({
|
||||
status: 200,
|
||||
headers: {
|
||||
"server-timing": "lpr_total;dur=42.500",
|
||||
},
|
||||
data: { ok: true },
|
||||
}),
|
||||
{
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
}
|
||||
);
|
||||
const failed = enqueueRequest(
|
||||
async () => {
|
||||
throw {
|
||||
message: "Scanner failed",
|
||||
response: {
|
||||
status: 500,
|
||||
headers: {
|
||||
"Server-Timing": "lpr_upstream;dur=450.000",
|
||||
},
|
||||
data: { ok: false },
|
||||
},
|
||||
};
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
url: "/modules/scanner/lpr",
|
||||
}
|
||||
).catch((error) => error);
|
||||
|
||||
await Promise.all([completed, failed]);
|
||||
await flushManyMicrotasks();
|
||||
|
||||
expect(requestQueueState.recentRequests).toHaveLength(2);
|
||||
expect(requestQueueState.recentRequests.find((request) => request.success === true)?.serverTiming).toBe(
|
||||
"lpr_total;dur=42.500"
|
||||
);
|
||||
expect(requestQueueState.recentRequests.find((request) => request.success === false)?.serverTiming).toBe(
|
||||
"lpr_upstream;dur=450.000"
|
||||
);
|
||||
expect(requestQueueState.errorRequests[0].serverTiming).toBe("lpr_upstream;dur=450.000");
|
||||
});
|
||||
|
||||
it("stores errors with request and response payloads and respects configured error limit", async () => {
|
||||
__configureRequestQueueForTests({
|
||||
errorHistoryLimit: 1,
|
||||
|
||||
@@ -0,0 +1,883 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { flushPromises } from "@vue/test-utils";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { mountWithApp } from "./helpers/mountWithApp.js";
|
||||
import ScannerCamera from "@/components/viewport/page/templates/scanner/graphics/ScannerCamera.vue";
|
||||
import { captureVideoFrameBlobForLPR } from "@/components/viewport/page/templates/scanner/lprFrameCapture";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
captureVideoFrameBlobForLPR: vi.fn(),
|
||||
getLPRFrameSourceRect: vi.fn(),
|
||||
getLPRFrameTargetSize: vi.fn(),
|
||||
getUserMedia: vi.fn(),
|
||||
isVideoFrameReadyForLPR: vi.fn(),
|
||||
trackStop: vi.fn(),
|
||||
videoTrack: null,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/viewport/page/templates/scanner/lprFrameCapture", () => ({
|
||||
captureVideoFrameBlobForLPR: mocks.captureVideoFrameBlobForLPR,
|
||||
getLPRFrameSourceRect: mocks.getLPRFrameSourceRect,
|
||||
getLPRFrameTargetSize: mocks.getLPRFrameTargetSize,
|
||||
isVideoFrameReadyForLPR: mocks.isVideoFrameReadyForLPR,
|
||||
LPR_CAMERA_FRAME_RATE: 5,
|
||||
LPR_CAMERA_VIDEO_HEIGHT: 576,
|
||||
LPR_CAMERA_VIDEO_WIDTH: 1024,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/displays/department/pos/steps/mobile/objects/PosDepartmentStepMobileFlow.vue", () => ({
|
||||
camera: {
|
||||
getImageCaptureDelay: vi.fn((isFirstCapture) => (isFirstCapture ? 25 : 1000)),
|
||||
getZoom: vi.fn(() => 1),
|
||||
},
|
||||
isCameraMounted: { value: false },
|
||||
}));
|
||||
|
||||
const setDocumentVisibility = (visibilityState) => {
|
||||
Object.defineProperty(document, "visibilityState", {
|
||||
configurable: true,
|
||||
value: visibilityState,
|
||||
});
|
||||
};
|
||||
|
||||
const overconstrainedError = (constraint) => ({
|
||||
constraint,
|
||||
name: "OverconstrainedError",
|
||||
});
|
||||
|
||||
describe("ScannerCamera capture gating", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
setDocumentVisibility("visible");
|
||||
mocks.captureVideoFrameBlobForLPR.mockReset();
|
||||
mocks.getLPRFrameSourceRect.mockReset();
|
||||
mocks.getLPRFrameSourceRect.mockReturnValue({ height: 1, width: 1, x: 0, y: 0 });
|
||||
mocks.getLPRFrameTargetSize.mockReset();
|
||||
mocks.getLPRFrameTargetSize.mockReturnValue({ width: 1, height: 1 });
|
||||
mocks.captureVideoFrameBlobForLPR.mockResolvedValue({
|
||||
blob: new Blob(["frame"], { type: "image/jpeg" }),
|
||||
filename: "frame.jpg",
|
||||
fingerprint: "frame",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
});
|
||||
mocks.isVideoFrameReadyForLPR.mockReset();
|
||||
mocks.isVideoFrameReadyForLPR.mockReturnValue(true);
|
||||
mocks.getUserMedia.mockReset();
|
||||
mocks.trackStop.mockReset();
|
||||
mocks.videoTrack = {
|
||||
enabled: true,
|
||||
kind: "video",
|
||||
stop: mocks.trackStop,
|
||||
};
|
||||
mocks.getUserMedia.mockResolvedValue({
|
||||
getTracks: () => [mocks.videoTrack],
|
||||
getVideoTracks: () => [mocks.videoTrack],
|
||||
});
|
||||
Object.defineProperty(navigator, "mediaDevices", {
|
||||
configurable: true,
|
||||
value: {
|
||||
getUserMedia: mocks.getUserMedia,
|
||||
},
|
||||
});
|
||||
Object.defineProperty(HTMLMediaElement.prototype, "play", {
|
||||
configurable: true,
|
||||
value: vi.fn(() => Promise.resolve()),
|
||||
});
|
||||
Object.defineProperty(HTMLMediaElement.prototype, "pause", {
|
||||
configurable: true,
|
||||
value: vi.fn(),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setDocumentVisibility("visible");
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("does not encode frames while capture is disabled", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: false,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(100);
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.getUserMedia).toHaveBeenCalledTimes(1);
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("pauses frame capture while the page is hidden and resumes when visible", async () => {
|
||||
setDocumentVisibility("hidden");
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(2000);
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.getUserMedia).toHaveBeenCalledTimes(1);
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
|
||||
setDocumentVisibility("visible");
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(0);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
await flushPromises();
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not emit an in-flight captured frame after the page becomes hidden", async () => {
|
||||
let resolveFrame;
|
||||
mocks.captureVideoFrameBlobForLPR.mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
resolveFrame = resolve;
|
||||
})
|
||||
);
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
setDocumentVisibility("hidden");
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
resolveFrame({
|
||||
blob: new Blob(["hidden-frame"], { type: "image/jpeg" }),
|
||||
filename: "hidden-frame.jpg",
|
||||
fingerprint: "hidden-frame",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
expect(wrapper.emitted("update:frame")).toBeUndefined();
|
||||
|
||||
setDocumentVisibility("visible");
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(0);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
expect(wrapper.emitted("update:frame")).toHaveLength(1);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("encodes frames when capture is enabled", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledWith(
|
||||
expect.any(HTMLVideoElement),
|
||||
expect.any(HTMLCanvasElement),
|
||||
{
|
||||
focusCrop: true,
|
||||
focusViewportRect: null,
|
||||
onFrameDrawn: expect.any(Function),
|
||||
shouldBuildVisualFingerprint: undefined,
|
||||
shouldEncode: undefined,
|
||||
visualFingerprintCanvas: expect.any(HTMLCanvasElement),
|
||||
}
|
||||
);
|
||||
expect(wrapper.emitted("update:frame")?.[0]?.[0]).toMatchObject({
|
||||
filename: "frame.jpg",
|
||||
fingerprint: "frame",
|
||||
});
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("pauses the live preview only after the current frame has been drawn for encoding", async () => {
|
||||
let resolveFrame;
|
||||
mocks.captureVideoFrameBlobForLPR.mockImplementationOnce((_video, _canvas, options) => {
|
||||
expect(mocks.videoTrack.enabled).toBe(true);
|
||||
options.onFrameDrawn();
|
||||
expect(mocks.videoTrack.enabled).toBe(false);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
resolveFrame = resolve;
|
||||
});
|
||||
});
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.videoTrack.enabled).toBe(false);
|
||||
|
||||
resolveFrame({
|
||||
blob: new Blob(["encoded-frame"], { type: "image/jpeg" }),
|
||||
filename: "encoded-frame.jpg",
|
||||
fingerprint: "encoded-frame",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.videoTrack.enabled).toBe(true);
|
||||
expect(wrapper.emitted("update:frame")?.[0]?.[0]).toMatchObject({
|
||||
filename: "encoded-frame.jpg",
|
||||
fingerprint: "encoded-frame",
|
||||
});
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("pauses the live preview while requested and resumes it afterward", async () => {
|
||||
const play = HTMLMediaElement.prototype.play;
|
||||
const pause = HTMLMediaElement.prototype.pause;
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(play).toHaveBeenCalledTimes(1);
|
||||
expect(pause).not.toHaveBeenCalled();
|
||||
|
||||
await wrapper.setProps({ pausePreview: true });
|
||||
await flushPromises();
|
||||
|
||||
expect(pause).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.videoTrack.enabled).toBe(false);
|
||||
|
||||
await wrapper.setProps({ pausePreview: false });
|
||||
await flushPromises();
|
||||
|
||||
expect(play).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.videoTrack.enabled).toBe(true);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not replay an already running preview after each preview-mode capture", async () => {
|
||||
const play = HTMLMediaElement.prototype.play;
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
captureMode: "preview",
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(play).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(1000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
expect(play).toHaveBeenCalledTimes(1);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not capture frozen preview frames while the live preview is paused", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
await wrapper.setProps({ pausePreview: true });
|
||||
await flushPromises();
|
||||
|
||||
vi.advanceTimersByTime(2000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
await wrapper.setProps({ pausePreview: false });
|
||||
await flushPromises();
|
||||
|
||||
vi.advanceTimersByTime(99);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not precompute canvas dimensions outside the capture helper", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getLPRFrameSourceRect).not.toHaveBeenCalled();
|
||||
expect(mocks.getLPRFrameTargetSize).not.toHaveBeenCalled();
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("caps the requested live camera stream resolution and frame rate", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.getUserMedia).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
video: expect.objectContaining({
|
||||
frameRate: { ideal: 5, max: 5 },
|
||||
height: { ideal: 576, max: 576 },
|
||||
width: { ideal: 1024, max: 1024 },
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("preserves the resolution cap when the camera rejects the frame-rate cap", async () => {
|
||||
mocks.getUserMedia.mockRejectedValueOnce(overconstrainedError("frameRate")).mockResolvedValueOnce({
|
||||
getTracks: () => [mocks.videoTrack],
|
||||
getVideoTracks: () => [mocks.videoTrack],
|
||||
});
|
||||
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.getUserMedia).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.getUserMedia.mock.calls[0][0]).toEqual(
|
||||
expect.objectContaining({
|
||||
video: expect.objectContaining({
|
||||
frameRate: { ideal: 5, max: 5 },
|
||||
height: { ideal: 576, max: 576 },
|
||||
width: { ideal: 1024, max: 1024 },
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0]).toEqual(
|
||||
expect.objectContaining({
|
||||
video: expect.objectContaining({
|
||||
frameRate: { ideal: 5 },
|
||||
height: { ideal: 576, max: 576 },
|
||||
width: { ideal: 1024, max: 1024 },
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.frameRate).not.toHaveProperty("max");
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.height).toHaveProperty("max", 576);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.width).toHaveProperty("max", 1024);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("preserves the frame-rate cap when the camera rejects the resolution cap", async () => {
|
||||
mocks.getUserMedia.mockRejectedValueOnce(overconstrainedError("width")).mockResolvedValueOnce({
|
||||
getTracks: () => [mocks.videoTrack],
|
||||
getVideoTracks: () => [mocks.videoTrack],
|
||||
});
|
||||
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(mocks.getUserMedia).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.getUserMedia.mock.calls[0][0]).toEqual(
|
||||
expect.objectContaining({
|
||||
video: expect.objectContaining({
|
||||
frameRate: { ideal: 5, max: 5 },
|
||||
height: { ideal: 576, max: 576 },
|
||||
width: { ideal: 1024, max: 1024 },
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0]).toEqual(
|
||||
expect.objectContaining({
|
||||
video: expect.objectContaining({
|
||||
frameRate: { ideal: 5, max: 5 },
|
||||
height: { ideal: 576 },
|
||||
width: { ideal: 1024 },
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.frameRate).toHaveProperty("max", 5);
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.height).not.toHaveProperty("max");
|
||||
expect(mocks.getUserMedia.mock.calls[1][0].video.width).not.toHaveProperty("max");
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("uses the visible preview crop when capture mode is preview", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
captureMode: "preview",
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledWith(
|
||||
expect.any(HTMLVideoElement),
|
||||
expect.any(HTMLCanvasElement),
|
||||
{
|
||||
focusCrop: false,
|
||||
focusViewportRect: null,
|
||||
shouldBuildVisualFingerprint: undefined,
|
||||
shouldEncode: undefined,
|
||||
visualFingerprintCanvas: null,
|
||||
}
|
||||
);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("passes a video-relative focus rect when a scanner target is provided", async () => {
|
||||
const getFocusViewportRect = vi.fn(() => ({
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 10,
|
||||
y: 20,
|
||||
}));
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
getFocusViewportRect,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.spyOn(wrapper.get("video").element, "getBoundingClientRect").mockReturnValue({
|
||||
bottom: 854,
|
||||
height: 844,
|
||||
left: 5,
|
||||
right: 395,
|
||||
toJSON: () => ({}),
|
||||
top: 10,
|
||||
width: 390,
|
||||
x: 5,
|
||||
y: 10,
|
||||
});
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(getFocusViewportRect).toHaveBeenCalled();
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledWith(
|
||||
expect.any(HTMLVideoElement),
|
||||
expect.any(HTMLCanvasElement),
|
||||
{
|
||||
focusCrop: true,
|
||||
focusViewportRect: {
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 5,
|
||||
y: 10,
|
||||
},
|
||||
onFrameDrawn: expect.any(Function),
|
||||
shouldBuildVisualFingerprint: undefined,
|
||||
shouldEncode: undefined,
|
||||
viewportHeight: 844,
|
||||
viewportWidth: 390,
|
||||
visualFingerprintCanvas: expect.any(HTMLCanvasElement),
|
||||
}
|
||||
);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("reuses cached video-relative focus geometry until viewport geometry changes", async () => {
|
||||
const getFocusViewportRect = vi.fn(() => ({
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 10,
|
||||
y: 20,
|
||||
}));
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
getFocusViewportRect,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
const videoRectSpy = vi.spyOn(wrapper.get("video").element, "getBoundingClientRect").mockReturnValue({
|
||||
bottom: 854,
|
||||
height: 844,
|
||||
left: 5,
|
||||
right: 395,
|
||||
toJSON: () => ({}),
|
||||
top: 10,
|
||||
width: 390,
|
||||
x: 5,
|
||||
y: 10,
|
||||
});
|
||||
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(1000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
expect(getFocusViewportRect).toHaveBeenCalledTimes(1);
|
||||
expect(videoRectSpy).toHaveBeenCalledTimes(1);
|
||||
expect(captureVideoFrameBlobForLPR.mock.calls[1][2].focusViewportRect).toEqual({
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 5,
|
||||
y: 10,
|
||||
});
|
||||
expect(captureVideoFrameBlobForLPR.mock.calls[1][2]).toEqual(
|
||||
expect.objectContaining({
|
||||
viewportHeight: 844,
|
||||
viewportWidth: 390,
|
||||
})
|
||||
);
|
||||
|
||||
getFocusViewportRect.mockReturnValue({
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 30,
|
||||
y: 45,
|
||||
});
|
||||
videoRectSpy.mockReturnValue({
|
||||
bottom: 859,
|
||||
height: 844,
|
||||
left: 8,
|
||||
right: 398,
|
||||
toJSON: () => ({}),
|
||||
top: 15,
|
||||
width: 390,
|
||||
x: 8,
|
||||
y: 15,
|
||||
});
|
||||
window.dispatchEvent(new Event("resize"));
|
||||
vi.advanceTimersByTime(1000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(3);
|
||||
expect(getFocusViewportRect).toHaveBeenCalledTimes(2);
|
||||
expect(videoRectSpy).toHaveBeenCalledTimes(2);
|
||||
expect(captureVideoFrameBlobForLPR.mock.calls[2][2].focusViewportRect).toEqual({
|
||||
height: 90,
|
||||
width: 100,
|
||||
x: 22,
|
||||
y: 30,
|
||||
});
|
||||
expect(captureVideoFrameBlobForLPR.mock.calls[2][2]).toEqual(
|
||||
expect.objectContaining({
|
||||
viewportHeight: 844,
|
||||
viewportWidth: 390,
|
||||
})
|
||||
);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("uses the shorter first-capture delay before the recurring capture interval", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(24);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("uses a custom recurring capture interval when provided", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
captureIntervalMs: 350,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(349);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
await wrapper.setProps({ captureIntervalMs: 200 });
|
||||
await flushPromises();
|
||||
|
||||
vi.advanceTimersByTime(199);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(3);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not schedule another recurring capture while frame encoding is still settling", async () => {
|
||||
let resolveSlowFrame;
|
||||
mocks.captureVideoFrameBlobForLPR
|
||||
.mockResolvedValueOnce({
|
||||
blob: new Blob(["first"], { type: "image/jpeg" }),
|
||||
filename: "first.jpg",
|
||||
fingerprint: "first",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
})
|
||||
.mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
resolveSlowFrame = resolve;
|
||||
})
|
||||
)
|
||||
.mockResolvedValue({
|
||||
blob: new Blob(["next"], { type: "image/jpeg" }),
|
||||
filename: "next.jpg",
|
||||
fingerprint: "next",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
});
|
||||
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
vi.advanceTimersByTime(5000);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
resolveSlowFrame({
|
||||
blob: new Blob(["slow"], { type: "image/jpeg" }),
|
||||
filename: "slow.jpg",
|
||||
fingerprint: "slow",
|
||||
height: 1,
|
||||
mimeType: "image/jpeg",
|
||||
width: 1,
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(3);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("captures immediately and restarts the interval when capture is re-enabled", async () => {
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: false,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
await wrapper.setProps({ captureEnabled: true });
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(0);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("retries shortly when the first capture fires before the video frame is ready", async () => {
|
||||
mocks.isVideoFrameReadyForLPR.mockReturnValue(false);
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
mocks.isVideoFrameReadyForLPR.mockReturnValue(true);
|
||||
vi.advanceTimersByTime(99);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).not.toHaveBeenCalled();
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(2);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("captures from the video loadeddata event before the first-capture timer fires", async () => {
|
||||
mocks.isVideoFrameReadyForLPR.mockReturnValue(false);
|
||||
const wrapper = mountWithApp(ScannerCamera, {
|
||||
props: {
|
||||
captureEnabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await flushPromises();
|
||||
mocks.isVideoFrameReadyForLPR.mockReturnValue(true);
|
||||
await wrapper.get("video").trigger("loadeddata");
|
||||
vi.advanceTimersByTime(0);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(25);
|
||||
await flushPromises();
|
||||
|
||||
expect(captureVideoFrameBlobForLPR).toHaveBeenCalledTimes(1);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user