diff --git a/.gitignore b/.gitignore index 7ae90d7..ac7c049 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ node_modules/ # Build output dist/ +*.js # Database *.sqlite3 diff --git a/client/package.json b/client/package.json index 4b7a2d1..edbd119 100644 --- a/client/package.json +++ b/client/package.json @@ -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" diff --git a/server/package.json b/server/package.json index 98f01ae..2507b94 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/printer/index.ts b/server/src/printer/index.ts index 09d0cb5..e9d5b6d 100644 --- a/server/src/printer/index.ts +++ b/server/src/printer/index.ts @@ -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'; \ No newline at end of file +export { TestPrinter } from './test-printer'; diff --git a/server/src/printer/printer-constants.ts b/server/src/printer/printer-constants.ts index e3a5cce..9dcfd82 100644 --- a/server/src/printer/printer-constants.ts +++ b/server/src/printer/printer-constants.ts @@ -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 = { diff --git a/server/src/printer/serial-printer.ts b/server/src/printer/serial-printer.ts index 8c8269e..9d00e39 100644 --- a/server/src/printer/serial-printer.ts +++ b/server/src/printer/serial-printer.ts @@ -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 { + if (!this.device) { + throw new Error('Printer device not initialized'); + } + + try { await new Promise((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 { + 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; } } -} \ No newline at end of file +}