fix serial printer closing
This commit is contained in:
parent
dec4b848b6
commit
b4d1c5db4e
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@ node_modules/
|
||||
|
||||
# Build output
|
||||
dist/
|
||||
*.js
|
||||
|
||||
# Database
|
||||
*.sqlite3
|
||||
|
@ -4,7 +4,7 @@
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"dev": "vite --host 0.0.0.0",
|
||||
"build": "tsc && vite build",
|
||||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||
"preview": "vite preview"
|
||||
|
@ -5,7 +5,7 @@
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"start": "node src/index.js",
|
||||
"dev": "ts-node-dev --respawn --transpile-only src/index.ts",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
|
@ -5,7 +5,7 @@ import logger from '../logger';
|
||||
import { PrintHistoryRepository, StepRepository } from '../db/repositories';
|
||||
|
||||
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');
|
||||
return new SerialPrinter(printHistoryRepo, stepRepo);
|
||||
} else {
|
||||
@ -15,4 +15,4 @@ export function createPrinter(printHistoryRepo: PrintHistoryRepository, stepRepo
|
||||
}
|
||||
|
||||
export { SerialPrinter } from './serial-printer';
|
||||
export { TestPrinter } from './test-printer';
|
||||
export { TestPrinter } from './test-printer';
|
||||
|
@ -15,8 +15,8 @@ export const BARCODE_CONFIG = {
|
||||
} as const;
|
||||
|
||||
export const PAPER_CONFIG = {
|
||||
BANNER_LENGTH: 32,
|
||||
CUT_LINES: 2,
|
||||
BANNER_LENGTH: 24,
|
||||
CUT_LINES: 4,
|
||||
} as const;
|
||||
|
||||
export const ALIGNMENT = {
|
||||
|
@ -24,12 +24,26 @@ export class SerialPrinter implements PrinterInterface {
|
||||
constructor(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository) {
|
||||
this.printHistoryRepo = printHistoryRepo;
|
||||
this.stepRepository = stepRepo;
|
||||
this.initializePrinter();
|
||||
this.initializeDevice();
|
||||
}
|
||||
|
||||
private async initializePrinter() {
|
||||
private initializeDevice() {
|
||||
try {
|
||||
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) => {
|
||||
this.device?.open((err) => {
|
||||
if (err) {
|
||||
@ -40,12 +54,24 @@ export class SerialPrinter implements PrinterInterface {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const options = { encoding: PRINTER_CONFIG.ENCODING };
|
||||
this.printer = new Printer(this.device, options);
|
||||
logger.info('Printer initialized successfully');
|
||||
logger.info('Printer opened successfully');
|
||||
} 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);
|
||||
|
||||
try {
|
||||
await this.openPrinter();
|
||||
|
||||
// Print header with task ID as barcode
|
||||
await this.printer
|
||||
.font(FONT.DEFAULT)
|
||||
@ -69,14 +97,14 @@ export class SerialPrinter implements PrinterInterface {
|
||||
.size(FONT_SIZES.LARGE.width, FONT_SIZES.LARGE.height)
|
||||
.text(formatUtils.formatCheckbox(`Task: ${task.name}`))
|
||||
.text(formatUtils.createBanner('=', PAPER_CONFIG.BANNER_LENGTH))
|
||||
.text('')
|
||||
// .text('')
|
||||
.align(ALIGNMENT.LEFT);
|
||||
|
||||
// Print task ID as barcode
|
||||
await this.printer
|
||||
.barcode(task.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
|
||||
.text('')
|
||||
.text('');
|
||||
.barcode(task.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS);
|
||||
// .text('')
|
||||
// .text('');
|
||||
|
||||
// Print steps
|
||||
for (let i = 0; i < taskSteps.length; i++) {
|
||||
@ -92,18 +120,19 @@ export class SerialPrinter implements PrinterInterface {
|
||||
.text(stepSection[0])
|
||||
.text(stepSection[1])
|
||||
.size(FONT_SIZES.SMALL.width, FONT_SIZES.SMALL.height)
|
||||
.text(stepSection[3])
|
||||
.text('');
|
||||
.text(stepSection[3]);
|
||||
// .text('');
|
||||
|
||||
// Print step ID as barcode
|
||||
await this.printer
|
||||
.barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
|
||||
.text('');
|
||||
.barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS);
|
||||
// .text('');
|
||||
}
|
||||
|
||||
await this.printer
|
||||
.cut(true, PAPER_CONFIG.CUT_LINES)
|
||||
.close();
|
||||
.cut(true, PAPER_CONFIG.CUT_LINES);
|
||||
|
||||
await this.closePrinter();
|
||||
|
||||
logger.info(`Printed task ${task.id}`);
|
||||
|
||||
@ -124,6 +153,8 @@ export class SerialPrinter implements PrinterInterface {
|
||||
}
|
||||
|
||||
try {
|
||||
await this.openPrinter();
|
||||
|
||||
// Get the task name for context
|
||||
const task = await this.stepRepository.findTaskById(step.id);
|
||||
const stepNumber = await this.stepRepository.findStepNumber(step.id);
|
||||
@ -150,8 +181,9 @@ export class SerialPrinter implements PrinterInterface {
|
||||
// Print step ID as barcode
|
||||
await this.printer
|
||||
.barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
|
||||
.cut(true, PAPER_CONFIG.CUT_LINES)
|
||||
.close();
|
||||
.cut(true, PAPER_CONFIG.CUT_LINES);
|
||||
|
||||
await this.closePrinter();
|
||||
|
||||
logger.info(`Printed step ${step.id}`);
|
||||
|
||||
@ -165,4 +197,4 @@ export class SerialPrinter implements PrinterInterface {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user