Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/client.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Printer from './device/printer';
import ConsolePrinterDriver from './printer-driver/console';
import FilesystemPrinterDriver from './printer-driver/filesystem-printer';
// import EscposPrinter from './printer-driver/escpos';
import CommandPrinterDriver from './printer-driver/command-printer';
// import StarPrinterDriver from './printer-driver/star';

const readFile = promisify(fs.readFile);
Expand Down Expand Up @@ -101,6 +102,10 @@ export default async (
// case 'escpos':
// printerDriver = new EscposPrinter();
// break;
case 'command':
// Example configuration for Command printer which uses a python script
printerDriver = new CommandPrinterDriver("python3", ['../python_print/main.py']);
break;
// case 'star':
// printerDriver = new StarPrinterDriver();
// break;
Expand Down
71 changes: 71 additions & 0 deletions src/printer-driver/command-printer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { PrinterDriverInterface, PrintingResult } from '.';
import fs from 'fs';
import os from 'os';
import path from 'path';
import bitmapify from '../decoder/parser/bitmapify';
import { spawn } from 'child_process';

/**
* This class implements a spawning mechanism to print via any command / executable
* by providing the path to the created bitmap file. This can be used for example
* to print via a python script.
*/
export default class CommandPrinterDriver implements PrinterDriverInterface {

/**
* The Parameters that will be passed on to the command
*/
parameters: [string];

/**
* The command to be executed
*/
command: string;

constructor(command:string, parameters:[string] ){
this.parameters = parameters;
this.command = command;
}

/**
* @param buffer A Buffer object containing raw data encapsultated
* @returns The path to the created bitmap file.
*/
createImageFileFromBuffer(buffer: Buffer){
const tempDir = path.join(os.tmpdir(), 'sirius-client');
fs.mkdirSync(tempDir, { recursive: true });

const randomName = Math.random().toString(36);
const tempFile = path.join(tempDir, randomName + '.bmp');

const bitmap = bitmapify(buffer);
fs.writeFileSync(tempFile, bitmap);
console.log(`Written: ${tempFile}`);
return tempFile;
}

async print(buffer: Buffer): Promise<PrintingResult> {
return new Promise<void>(resolve => {
const imageFile = this.createImageFileFromBuffer(buffer);
const commandParams = [...this.parameters];
commandParams.push(imageFile);
const process = spawn(this.command, commandParams);

process.stdout.on('data', function (data) {
console.log('Pipe data from command ...');
console.log(data.toString());
});
process.on('error', function (...args) {
console.log('Command error', args);
});
process.on('exit', function (code, signal) {
console.log('Command exit', code, signal);
});
process.on('close', function (code, signal) {
console.log('Command close', code, signal);
resolve();
});

});
}
}