From 9db024a14d4ba222572af47a4105ceb81155a19a Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 27 Aug 2021 08:41:26 +0200 Subject: [PATCH 1/4] Add python printer bridge --- src/printer-driver/python-bridge-printer.ts | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 src/printer-driver/python-bridge-printer.ts diff --git a/src/printer-driver/python-bridge-printer.ts b/src/printer-driver/python-bridge-printer.ts new file mode 100755 index 0000000..3e033f5 --- /dev/null +++ b/src/printer-driver/python-bridge-printer.ts @@ -0,0 +1,41 @@ +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'; + + +export default class PythonBridgePrinterDriver implements PrinterDriverInterface { + async print(buffer: Buffer): Promise { + return new Promise(resolve => { + 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}`); + + const python = spawn('python3', ['../python_print/main.py', tempFile]); + + python.stdout.on('data', function (data) { + console.log('Pipe data from python script ...'); + console.log(data.toString()); + }); + python.on('error', function (...args) { + console.log('Python error', args); + }); + python.on('exit', function (code, signal) { + console.log('Python exit', code, signal); + }); + python.on('close', function (code, signal) { + console.log('Python close', code, signal); + resolve(); + }); + + }); + } +} From ca4d4b588d36fed5018bc6bb6a890558e5591648 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 27 Aug 2021 23:18:04 +0200 Subject: [PATCH 2/4] Refactor bridge command --- src/printer-driver/command-printer.ts | 55 +++++++++++++++++++++ src/printer-driver/python-bridge-printer.ts | 41 --------------- 2 files changed, 55 insertions(+), 41 deletions(-) create mode 100755 src/printer-driver/command-printer.ts delete mode 100755 src/printer-driver/python-bridge-printer.ts diff --git a/src/printer-driver/command-printer.ts b/src/printer-driver/command-printer.ts new file mode 100755 index 0000000..305956d --- /dev/null +++ b/src/printer-driver/command-printer.ts @@ -0,0 +1,55 @@ +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'; + + +export default class CommandPrinterDriver implements PrinterDriverInterface { + + parameters: [string]; + command: string; + + constructor(command:string, parameters:[string] ){ + this.parameters = parameters; + this.command = command; + } + + 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 { + return new Promise(resolve => { + const imageFile = this.createImageFileFromBuffer(buffer); + this.parameters.push(imageFile); + const process = spawn(this.command, this.parameters); + + 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(); + }); + + }); + } +} diff --git a/src/printer-driver/python-bridge-printer.ts b/src/printer-driver/python-bridge-printer.ts deleted file mode 100755 index 3e033f5..0000000 --- a/src/printer-driver/python-bridge-printer.ts +++ /dev/null @@ -1,41 +0,0 @@ -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'; - - -export default class PythonBridgePrinterDriver implements PrinterDriverInterface { - async print(buffer: Buffer): Promise { - return new Promise(resolve => { - 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}`); - - const python = spawn('python3', ['../python_print/main.py', tempFile]); - - python.stdout.on('data', function (data) { - console.log('Pipe data from python script ...'); - console.log(data.toString()); - }); - python.on('error', function (...args) { - console.log('Python error', args); - }); - python.on('exit', function (code, signal) { - console.log('Python exit', code, signal); - }); - python.on('close', function (code, signal) { - console.log('Python close', code, signal); - resolve(); - }); - - }); - } -} From 7bc03656e35303e029568eea762c5f8c49ae0f4c Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 3 Sep 2021 11:38:38 +0200 Subject: [PATCH 3/4] Add documentation --- src/printer-driver/command-printer.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/printer-driver/command-printer.ts b/src/printer-driver/command-printer.ts index 305956d..219dc37 100755 --- a/src/printer-driver/command-printer.ts +++ b/src/printer-driver/command-printer.ts @@ -5,10 +5,21 @@ 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] ){ @@ -16,6 +27,10 @@ export default class CommandPrinterDriver implements PrinterDriverInterface { 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 }); @@ -32,8 +47,9 @@ export default class CommandPrinterDriver implements PrinterDriverInterface { async print(buffer: Buffer): Promise { return new Promise(resolve => { const imageFile = this.createImageFileFromBuffer(buffer); - this.parameters.push(imageFile); - const process = spawn(this.command, this.parameters); + 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 ...'); From 11507a2267ab35c880dee65f54ca193d414ba56d Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Fri, 3 Sep 2021 11:41:19 +0200 Subject: [PATCH 4/4] Add command printer example configuration --- src/client.ts | 5 +++++ 1 file changed, 5 insertions(+) mode change 100644 => 100755 src/client.ts diff --git a/src/client.ts b/src/client.ts old mode 100644 new mode 100755 index b13c03a..a3bf213 --- a/src/client.ts +++ b/src/client.ts @@ -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); @@ -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;