Skip to content

Commit

Permalink
added debug printer
Browse files Browse the repository at this point in the history
  • Loading branch information
enoy19 committed Aug 27, 2023
1 parent 043eee4 commit 75959ad
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 22 additions & 0 deletions src/lib/server/print/printers/debugPrinter.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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));
}
}
8 changes: 6 additions & 2 deletions src/lib/server/print/printers/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -56,10 +57,13 @@ function printerFromConfig<T extends PrinterType>(
) {
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}`);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/server/print/printers/serialPrinter.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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);
}

/**
* @override
*/
public async print(zpl: string): Promise<void> {
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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/server/print/printers/tcpPrinter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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);
}

public async print(zpl: string): Promise<void> {
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);
Expand Down
13 changes: 13 additions & 0 deletions src/routes/printers/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 75959ad

Please sign in to comment.