Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/test/tmp/
/test_output.txt
trash_bin/
/.antigravitycli/
46 changes: 20 additions & 26 deletions src/PaperSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ export function parsePaperSize(input: string): PaperSizeMm | null {
}
}

function normalizeInput(input?: string | null): string | undefined {
return typeof input === "string" ? input.trim() || undefined : undefined;
}

function applyOrientation(
preset: PaperSizeMm,
orientation?: "portrait" | "landscape" | null,
): PaperSizeMm {
if (orientation === "landscape" && preset.widthMm < preset.heightMm) {
return { widthMm: preset.heightMm, heightMm: preset.widthMm };
}
if (orientation === "portrait" && preset.widthMm > preset.heightMm) {
return { widthMm: preset.heightMm, heightMm: preset.widthMm };
}
return { ...preset };
}

/**
* Resolves a paper size configuration (preset name or custom dimension string)
* to millimeters, without any device clamping.
Expand All @@ -137,14 +154,8 @@ export function validateAndResolvePaperSize(
paperDimInput?: string | null,
orientation?: "portrait" | "landscape" | null,
): ResolvedPaperSize | null {
const normalizedSize =
typeof paperSizeInput === "string"
? paperSizeInput.trim() || undefined
: undefined;
const normalizedDim =
typeof paperDimInput === "string"
? paperDimInput.trim() || undefined
: undefined;
const normalizedSize = normalizeInput(paperSizeInput);
const normalizedDim = normalizeInput(paperDimInput);

if (normalizedSize !== undefined && normalizedDim !== undefined) {
throw new Error(
Expand Down Expand Up @@ -174,24 +185,7 @@ export function validateAndResolvePaperSize(
throw new Error(`Unknown paper size preset: "${normalizedSize}".`);
}

let resolvedMm = { ...preset };
if (
orientation === "landscape" &&
resolvedMm.widthMm < resolvedMm.heightMm
) {
resolvedMm = {
widthMm: resolvedMm.heightMm,
heightMm: resolvedMm.widthMm,
};
} else if (
orientation === "portrait" &&
resolvedMm.widthMm > resolvedMm.heightMm
) {
resolvedMm = {
widthMm: resolvedMm.heightMm,
heightMm: resolvedMm.widthMm,
};
}
const resolvedMm = applyOrientation(preset, orientation);

return { resolvedMm, source: normalizedSize.toUpperCase() };
}
Expand Down
4 changes: 1 addition & 3 deletions src/PathHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export default class PathHelper {
return i;
}
}
return Promise.reject(
new Error(`Unable to find the valid scan number in folder ${folder}`),
);
throw new Error(`Unable to find the valid scan number in folder ${folder}`);
}

static async makeUnique(filePath: string, date: Date): Promise<string> {
Expand Down
110 changes: 73 additions & 37 deletions src/commands/adfAutoscanCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,58 @@ function checkCapabilities(
}
}

async function executeAutoscanIteration(
adfAutoScanConfig: AdfAutoScanConfig,
deviceCapabilities: DeviceCapabilities,
folder: string,
tempFolder: string,
scanCount: number,
): Promise<{ success: boolean; isDeviceAlive: boolean }> {
try {
await waitAdfLoaded(
adfAutoScanConfig.pollingInterval,
adfAutoScanConfig.startScanDelay,
deviceCapabilities.getScanStatus,
);

console.log(`Scan event captured, saving scan #${scanCount}`);

await scanFromAdf(
scanCount,
folder,
tempFolder,
adfAutoScanConfig,
deviceCapabilities,
new Date(),
);
return { success: true, isDeviceAlive: true };
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
} else {
console.log(e);
}
const isAlive = await HPApi.isAlive();
return { success: false, isDeviceAlive: isAlive };
}
}

async function handleLoopDelayOrTermination(
errorCount: number,
deviceUp: boolean,
deviceUpPollingInterval: number,
): Promise<{ keepActive: boolean; deviceUp: boolean }> {
const keepActive = errorCount < 50;

if (!deviceUp) {
await HPApi.waitDeviceUp(deviceUpPollingInterval);
return { keepActive, deviceUp: true };
}

await HPApi.delay(1000);
return { keepActive, deviceUp };
}

export async function adfAutoscanCmd(
adfAutoScanConfig: AdfAutoScanConfig,
deviceUpPollingInterval: number,
Expand Down Expand Up @@ -52,47 +104,31 @@ export async function adfAutoscanCmd(
while (keepActive) {
iteration++;
console.log(`Iteration ${iteration} (Errors so far: ${errorCount})`);
try {
await waitAdfLoaded(
adfAutoScanConfig.pollingInterval,
adfAutoScanConfig.startScanDelay,
deviceCapabilities.getScanStatus,
);

scanCount++;
const result = await executeAutoscanIteration(
adfAutoScanConfig,
deviceCapabilities,
folder,
tempFolder,
scanCount + 1,
);

console.log(`Scan event captured, saving scan #${scanCount}`);

await scanFromAdf(
scanCount,
folder,
tempFolder,
adfAutoScanConfig,
deviceCapabilities,
new Date(),
);
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
} else {
console.log(e);
}
if (await HPApi.isAlive()) {
errorCount++;
} else {
deviceUp = false;
}
if (result.success) {
scanCount++;
}

if (errorCount === 50) {
keepActive = false;
if (!result.success && result.isDeviceAlive) {
errorCount++;
}

if (!deviceUp) {
await HPApi.waitDeviceUp(deviceUpPollingInterval);
deviceUp = true;
} else {
await HPApi.delay(1000);
if (!result.success && !result.isDeviceAlive) {
deviceUp = false;
}

const nextState = await handleLoopDelayOrTermination(
errorCount,
deviceUp,
deviceUpPollingInterval,
);
keepActive = nextState.keepActive;
deviceUp = nextState.deviceUp;
}
}
12 changes: 6 additions & 6 deletions src/commands/listenCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export async function listenCmd(
}
}

async function processScanWithDestination(
export async function processScanWithDestination(
destination: WalkupDestination,
selectedScanTarget: SelectedScanTarget,
lastDuplexMode: DuplexMode,
Expand Down Expand Up @@ -214,7 +214,7 @@ async function processScanWithDestination(
return { scanCount, frontOfDoubleSidedScanContext, duplexMode };
}

async function handleScanResult(
export async function handleScanResult(
duplexMode: DuplexMode,
frontOfDoubleSidedScanContext: FrontOfDoubleSidedScanContext | null,
scanConfig: ScanConfig,
Expand Down Expand Up @@ -267,7 +267,7 @@ async function handleScanResult(
return frontOfDoubleSidedScanContext;
}

function determineDuplexModes(
export function determineDuplexModes(
destination: WalkupDestination,
selectedScanTarget: SelectedScanTarget,
previousDuplexMode: DuplexMode,
Expand Down Expand Up @@ -355,7 +355,7 @@ interface ScanParameters {
scanCount: number;
}

async function setupScanParameters(
export async function setupScanParameters(
duplexMode: DuplexMode,
targetDuplexMode: TargetDuplexMode,
destination: WalkupDestination,
Expand Down Expand Up @@ -417,7 +417,7 @@ async function setupScanParameters(
return { pageCountingStrategy, scanToPdf, scanDate, scanCount };
}

async function processFinishedPartialDuplexScan(
export async function processFinishedPartialDuplexScan(
lastScanTarget: SelectedScanTarget,
selectedScanTarget: SelectedScanTarget,
scanCount: number,
Expand All @@ -438,7 +438,7 @@ async function processFinishedPartialDuplexScan(
);
}

interface FrontOfDoubleSidedScanContext {
export interface FrontOfDoubleSidedScanContext {
scanConfig: ScanConfig;
folder: string;
tempFolder: string;
Expand Down
Loading
Loading