fix serial printer closing

This commit is contained in:
Sean Sube 2025-06-17 18:14:26 -05:00
parent dec4b848b6
commit b4d1c5db4e
No known key found for this signature in database
GPG Key ID: 3EED7B957D362AF1
6 changed files with 59 additions and 26 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ node_modules/
# Build output # Build output
dist/ dist/
*.js
# Database # Database
*.sqlite3 *.sqlite3

View File

@ -4,7 +4,7 @@
"version": "0.0.0", "version": "0.0.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite --host 0.0.0.0",
"build": "tsc && vite build", "build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview" "preview": "vite preview"

View File

@ -5,7 +5,7 @@
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "node dist/index.js", "start": "node src/index.js",
"dev": "ts-node-dev --respawn --transpile-only src/index.ts", "dev": "ts-node-dev --respawn --transpile-only src/index.ts",
"test": "jest", "test": "jest",
"test:watch": "jest --watch", "test:watch": "jest --watch",

View File

@ -5,7 +5,7 @@ import logger from '../logger';
import { PrintHistoryRepository, StepRepository } from '../db/repositories'; import { PrintHistoryRepository, StepRepository } from '../db/repositories';
export function createPrinter(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository): Printer { export function createPrinter(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository): Printer {
if (process.env.NODE_ENV === 'production') { if (process.env.PRINTER_DRIVER === 'serial') {
logger.info('Using serial printer driver'); logger.info('Using serial printer driver');
return new SerialPrinter(printHistoryRepo, stepRepo); return new SerialPrinter(printHistoryRepo, stepRepo);
} else { } else {
@ -15,4 +15,4 @@ export function createPrinter(printHistoryRepo: PrintHistoryRepository, stepRepo
} }
export { SerialPrinter } from './serial-printer'; export { SerialPrinter } from './serial-printer';
export { TestPrinter } from './test-printer'; export { TestPrinter } from './test-printer';

View File

@ -15,8 +15,8 @@ export const BARCODE_CONFIG = {
} as const; } as const;
export const PAPER_CONFIG = { export const PAPER_CONFIG = {
BANNER_LENGTH: 32, BANNER_LENGTH: 24,
CUT_LINES: 2, CUT_LINES: 4,
} as const; } as const;
export const ALIGNMENT = { export const ALIGNMENT = {

View File

@ -24,12 +24,26 @@ export class SerialPrinter implements PrinterInterface {
constructor(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository) { constructor(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository) {
this.printHistoryRepo = printHistoryRepo; this.printHistoryRepo = printHistoryRepo;
this.stepRepository = stepRepo; this.stepRepository = stepRepo;
this.initializePrinter(); this.initializeDevice();
} }
private async initializePrinter() { private initializeDevice() {
try { try {
this.device = new USB(); this.device = new USB();
const options = { encoding: PRINTER_CONFIG.ENCODING };
this.printer = new Printer(this.device, options);
logger.info('Printer device initialized successfully');
} catch (error) {
logger.error('Failed to initialize printer device:', error);
}
}
private async openPrinter(): Promise<void> {
if (!this.device) {
throw new Error('Printer device not initialized');
}
try {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
this.device?.open((err) => { this.device?.open((err) => {
if (err) { if (err) {
@ -40,12 +54,24 @@ export class SerialPrinter implements PrinterInterface {
resolve(); resolve();
}); });
}); });
logger.info('Printer opened successfully');
const options = { encoding: PRINTER_CONFIG.ENCODING };
this.printer = new Printer(this.device, options);
logger.info('Printer initialized successfully');
} catch (error) { } catch (error) {
logger.error('Failed to initialize printer:', error); logger.error('Failed to open printer:', error);
throw error;
}
}
private async closePrinter(): Promise<void> {
if (!this.printer) {
throw new Error('Printer not initialized');
}
try {
await this.printer.close();
logger.info('Printer closed successfully');
} catch (error) {
logger.error('Failed to close printer:', error);
throw error;
} }
} }
@ -61,6 +87,8 @@ export class SerialPrinter implements PrinterInterface {
const taskSteps = await this.getTaskSteps(db, task); const taskSteps = await this.getTaskSteps(db, task);
try { try {
await this.openPrinter();
// Print header with task ID as barcode // Print header with task ID as barcode
await this.printer await this.printer
.font(FONT.DEFAULT) .font(FONT.DEFAULT)
@ -69,14 +97,14 @@ export class SerialPrinter implements PrinterInterface {
.size(FONT_SIZES.LARGE.width, FONT_SIZES.LARGE.height) .size(FONT_SIZES.LARGE.width, FONT_SIZES.LARGE.height)
.text(formatUtils.formatCheckbox(`Task: ${task.name}`)) .text(formatUtils.formatCheckbox(`Task: ${task.name}`))
.text(formatUtils.createBanner('=', PAPER_CONFIG.BANNER_LENGTH)) .text(formatUtils.createBanner('=', PAPER_CONFIG.BANNER_LENGTH))
.text('') // .text('')
.align(ALIGNMENT.LEFT); .align(ALIGNMENT.LEFT);
// Print task ID as barcode // Print task ID as barcode
await this.printer await this.printer
.barcode(task.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS) .barcode(task.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS);
.text('') // .text('')
.text(''); // .text('');
// Print steps // Print steps
for (let i = 0; i < taskSteps.length; i++) { for (let i = 0; i < taskSteps.length; i++) {
@ -92,18 +120,19 @@ export class SerialPrinter implements PrinterInterface {
.text(stepSection[0]) .text(stepSection[0])
.text(stepSection[1]) .text(stepSection[1])
.size(FONT_SIZES.SMALL.width, FONT_SIZES.SMALL.height) .size(FONT_SIZES.SMALL.width, FONT_SIZES.SMALL.height)
.text(stepSection[3]) .text(stepSection[3]);
.text(''); // .text('');
// Print step ID as barcode // Print step ID as barcode
await this.printer await this.printer
.barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS) .barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS);
.text(''); // .text('');
} }
await this.printer await this.printer
.cut(true, PAPER_CONFIG.CUT_LINES) .cut(true, PAPER_CONFIG.CUT_LINES);
.close();
await this.closePrinter();
logger.info(`Printed task ${task.id}`); logger.info(`Printed task ${task.id}`);
@ -124,6 +153,8 @@ export class SerialPrinter implements PrinterInterface {
} }
try { try {
await this.openPrinter();
// Get the task name for context // Get the task name for context
const task = await this.stepRepository.findTaskById(step.id); const task = await this.stepRepository.findTaskById(step.id);
const stepNumber = await this.stepRepository.findStepNumber(step.id); const stepNumber = await this.stepRepository.findStepNumber(step.id);
@ -150,8 +181,9 @@ export class SerialPrinter implements PrinterInterface {
// Print step ID as barcode // Print step ID as barcode
await this.printer await this.printer
.barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS) .barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
.cut(true, PAPER_CONFIG.CUT_LINES) .cut(true, PAPER_CONFIG.CUT_LINES);
.close();
await this.closePrinter();
logger.info(`Printed step ${step.id}`); logger.info(`Printed step ${step.id}`);
@ -165,4 +197,4 @@ export class SerialPrinter implements PrinterInterface {
throw error; throw error;
} }
} }
} }