|
| 1 | +import { exec } from 'child_process'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { Logger, windowsPrinterStatus } from '.'; |
| 5 | + |
| 6 | +const logger = new Logger('receipt'); |
| 7 | + |
| 8 | +export async function checkReceiptPrinter(printers: object[]) { |
| 9 | + if (process.platform === 'linux') { |
| 10 | + const usbDevices = fs.readdirSync('/dev/usb'); |
| 11 | + for (const f of usbDevices) { |
| 12 | + if (f.startsWith('lp')) { |
| 13 | + const lpid = fs.readFileSync(`/sys/class/usbmisc/${f}/device/ieee1284_id`, 'utf8').trim(); |
| 14 | + logger.info(`USB Printer ${f} found: ${lpid}`); |
| 15 | + logger.info(`If you want to use this printer for balloon print, please set balloon: /dev/usb/${f} in config.yaml.`); |
| 16 | + } |
| 17 | + } |
| 18 | + if (!usbDevices.length) logger.info('If you want to use balloon client, please connect your receipt printer first.'); |
| 19 | + } else if (process.platform === 'win32') { |
| 20 | + const shared = printers.filter((p: any) => p.DeviceID).filter((p: any) => p.ShareName).map((p: any) => ({ |
| 21 | + printer: `\\\\${p.SystemName}\\${p.ShareName}`, |
| 22 | + device: p.DeviceID, |
| 23 | + description: p.Caption, |
| 24 | + status: windowsPrinterStatus[p.PrinterStatus] ? windowsPrinterStatus[p.PrinterStatus] : 'unknown', |
| 25 | + })); |
| 26 | + for (const printer of shared) { |
| 27 | + logger.info(`Receipt Shared Printer ${printer.printer}(${printer.device})) found: ${printer.description}`); |
| 28 | + logger.info(`If you want to use this printer for balloon print, please set balloon: ${printer.printer} in config.yaml.`); |
| 29 | + } |
| 30 | + if (!shared.length) logger.info('If you want to use balloon client, please share your receipt printer on settings first.'); |
| 31 | + } else if (process.platform === 'darwin') { |
| 32 | + logger.info('If you want to use balloon client, please set balloon: "{printer name}" in config.yaml.'); |
| 33 | + } else logger.info('If you want to use balloon client, please run this on Linux/Windows/MacOS'); |
| 34 | +} |
| 35 | + |
| 36 | +export async function checkReceiptStatus(printer) { |
| 37 | + if (process.platform !== 'linux') { |
| 38 | + printer = { printer: printer.printer }; |
| 39 | + return; |
| 40 | + } |
| 41 | + const lp = printer.printer.split('/').pop(); |
| 42 | + const oldPrinter = printer; |
| 43 | + printer = { |
| 44 | + printer: printer.printer, |
| 45 | + info: fs.readFileSync(`/sys/class/usbmisc/${lp}/device/ieee1284_id`, 'utf8').trim(), |
| 46 | + }; |
| 47 | + if (!oldPrinter || oldPrinter.info === printer.info) return; |
| 48 | + logger.info('Printer changed:', printer.printer, printer.info); |
| 49 | + const usbDevices = fs.readdirSync('/dev/usb'); |
| 50 | + for (const f of usbDevices) { |
| 51 | + if (f.startsWith('lp')) { |
| 52 | + const lpid = fs.readFileSync(`/sys/class/usbmisc/${f}/device/ieee1284_id`, 'utf8').trim(); |
| 53 | + if (lpid === oldPrinter.info) { |
| 54 | + logger.info('Printer found:', f, ':', lpid); |
| 55 | + oldPrinter.printer = `/dev/usb/${f}`; |
| 56 | + printer = oldPrinter; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if (oldPrinter.info !== printer.info) throw Error('Printer not found, please check the printer connection.'); |
| 62 | +} |
| 63 | + |
| 64 | +export async function receiptPrint(text, printer) { |
| 65 | + fs.writeFileSync(path.resolve(process.cwd(), 'data', 'balloon.txt'), text); |
| 66 | + if (process.platform === 'win32') { |
| 67 | + exec(`COPY /B "${path.resolve(process.cwd(), 'data', 'balloon.txt')}" "${printer.printer}"`, (err, stdout, stderr) => { |
| 68 | + if (err) logger.error(err); |
| 69 | + if (stdout) logger.info(stdout); |
| 70 | + if (stderr) logger.error(stderr); |
| 71 | + }); |
| 72 | + } else if (process.platform === 'darwin') exec(`lpr -P ${printer.printer} -o raw ${path.resolve(process.cwd(), 'data', 'balloon.txt')}`); |
| 73 | + else fs.writeFileSync(path.resolve(printer.printer), text); |
| 74 | +} |
0 commit comments