diff --git a/src/lib/constants.ts b/src/lib/constants.ts index b81cc29..89feda6 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -5,4 +5,4 @@ export const baudRates = [ 76800, 115200, 230400, 460800, 576000, 921600 ]; -export const printerTypes = ['serial', 'tcp'] as const; +export const printerTypes = ['serial', 'tcp', 'debug'] as const; diff --git a/src/lib/server/print/printers/debugPrinter.ts b/src/lib/server/print/printers/debugPrinter.ts new file mode 100644 index 0000000..2e9c05c --- /dev/null +++ b/src/lib/server/print/printers/debugPrinter.ts @@ -0,0 +1,22 @@ +import { getDataFilePathFor, mkdirIfNotExists } from '$lib/server/fileUtil'; +import type { PrinterConfig } from '$lib/types'; +import { writeFile } from 'fs/promises'; +import { Printer } from './printer'; + +export class DebugPrinter extends Printer { + constructor(identifier: string, private readonly config: PrinterConfig<'debug'>) { + super(identifier); + } + + public async print(zpl: string): Promise { + console.debug(`ZPL:\n\n ${zpl}`); + + await mkdirIfNotExists('debug'); + + const filename = Date.now(); + const zplPath = getDataFilePathFor(`debug/${filename}.zpl`); + await writeFile(zplPath, zpl); + + await new Promise((resolve) => setTimeout(resolve, 1000)); + } +} diff --git a/src/lib/server/print/printers/index.ts b/src/lib/server/print/printers/index.ts index cb16b2d..034f2ca 100644 --- a/src/lib/server/print/printers/index.ts +++ b/src/lib/server/print/printers/index.ts @@ -1,5 +1,6 @@ import type { PrinterConfig, PrinterConfigs, PrinterOptions, PrinterType } from '$lib/types'; import { createEmptyJsonFileIfNotExists, readJson, storeObject } from '../../fileUtil'; +import { DebugPrinter } from './debugPrinter'; import type { Printer } from './printer'; import { SerialPrinter } from './serialPrinter'; import { TcpPrinter } from './tcpPrinter'; @@ -56,10 +57,13 @@ function printerFromConfig( ) { switch (printerConfig.type) { case 'serial': { - return new SerialPrinter(identifier, printerConfig.options as PrinterOptions<'serial'>); + return new SerialPrinter(identifier, printerConfig as PrinterConfig<'serial'>); } case 'tcp': { - return new TcpPrinter(identifier, printerConfig.options as PrinterOptions<'tcp'>); + return new TcpPrinter(identifier, printerConfig as PrinterConfig<'tcp'>); + } + case 'debug': { + return new DebugPrinter(identifier, printerConfig as PrinterConfig<'debug'>); } default: throw new Error(`unknown printer type ${printerConfig.type}`); diff --git a/src/lib/server/print/printers/serialPrinter.ts b/src/lib/server/print/printers/serialPrinter.ts index 9573334..a03b87d 100644 --- a/src/lib/server/print/printers/serialPrinter.ts +++ b/src/lib/server/print/printers/serialPrinter.ts @@ -1,10 +1,10 @@ import { SerialPort } from 'serialport'; import { Printer } from './printer'; import { fileExists } from '../../fileUtil'; -import type { PrinterOptions } from '$lib/types'; +import type { PrinterConfig } from '$lib/types'; export class SerialPrinter extends Printer { - constructor(identifier: string, private readonly options: PrinterOptions<'serial'>) { + constructor(identifier: string, private readonly config: PrinterConfig<'serial'>) { super(identifier); } @@ -12,12 +12,12 @@ export class SerialPrinter extends Printer { * @override */ public async print(zpl: string): Promise { - const path = this.options.path; + const path = this.config.options.path; if (!path || !(await fileExists(path, false))) { throw new Error(`${path} not found`); } - const port = new SerialPort(this.options); + const port = new SerialPort(this.config.options); try { await new Promise((resolve, reject) => { diff --git a/src/lib/server/print/printers/tcpPrinter.ts b/src/lib/server/print/printers/tcpPrinter.ts index a08d8aa..5be84ff 100644 --- a/src/lib/server/print/printers/tcpPrinter.ts +++ b/src/lib/server/print/printers/tcpPrinter.ts @@ -1,9 +1,9 @@ -import type { PrinterOptions } from '$lib/types'; +import type { PrinterConfig } from '$lib/types'; import { Printer } from './printer'; import * as net from 'net'; export class TcpPrinter extends Printer { - constructor(identifier: string, private readonly options: PrinterOptions<'tcp'>) { + constructor(identifier: string, private readonly config: PrinterConfig<'tcp'>) { super(identifier); } @@ -11,7 +11,7 @@ export class TcpPrinter extends Printer { const client = new net.Socket(); await new Promise((resolve, reject) => { - client.connect(this.options.port, this.options.host, () => { + client.connect(this.config.options.port, this.config.options.host, () => { client.write(zpl, () => { client.end(); resolve(undefined); diff --git a/src/routes/printers/+page.server.ts b/src/routes/printers/+page.server.ts index 829aad4..4c3df07 100644 --- a/src/routes/printers/+page.server.ts +++ b/src/routes/printers/+page.server.ts @@ -93,6 +93,19 @@ export const actions: Actions = { break; } + case 'debug': { + await savePrinter( + identifier as string, + newIdentifier as string, + dpmm, + widthMm, + heightMm, + type, + {} as never + ); + + break; + } default: console.error(`unknown printer type: ${type}`); return fail(500);