import { startPrint, printlnText, printQrCode, printBarCode, printColumnsText, line, lineWrap, endPrint, doInterface, } from './print-interface'; import Hardwarekit from 'qm-hardwarekit'; import { Platform } from 'react-native'; const Dsd = Hardwarekit.Dsd; import { get_self_print_enable } from './builder'; export interface IPrinterListItem { func: | string | 'printstart' | 'printText' | 'printQrCode' | 'printColumnsText' | 'printlnText' | 'line' | 'lineWrap' | 'printend'; params: any[]; } let DeviceStorage = Hardwarekit.devices; var self = null; export function printDriver(brand: string, model: string, defaultPrinterModal: string = '') { self = doInterface(brand, model, defaultPrinterModal); console.log('QianmiPrint:doInterface', brand, model, defaultPrinterModal, JSON.stringify(self)); } export function devices(): Promise { return new Promise(resolve => { DeviceStorage.get().then(async devices => { console.log('print interface devices => ', devices); let ts = devices.filter(device => { return device.bondState && device.bondState === 1; }); if (self) { self.printMode = await get_self_print_enable(); } resolve(self ? [...ts, self] : ts); }); }); } export function getSelf(brand: string, model: string) { if ('ios' === Platform.OS) { // return { name: 'IOS.powa', from: 'print.powa', bondState: 1 }; } else if ('android' === Platform.OS) { if (Dsd.isSUNMI(brand)) { switch (model) { case 't1host': return { name: 'SUNMI.t1host', from: 'sunmi.80', bondState: 1 }; case 'T1': return { name: 'SUNMI.t1host', from: 'sunmi.80', bondState: 1 }; case 'P1N': case 'P1': return { name: 'SUNMI.P1', from: 'sunmi.58', bondState: 1 }; case 'V1': return { name: 'SUNMI.V1', from: 'sunmi.58', bondState: 1 }; case 'T2': return { name: 'SUNMI.t2host', from: 'sunmi.80', bondState: 1 }; case 'S2': return { name: 'SUNMI.S2', from: 'sunmi.80', bondState: 1 }; case 'D2': return null; case 'D2_d': return null; case 'D1': return null; default: return { name: 'SUNMI.default', from: 'sunmi.58', bondState: 1 }; } } else if (brand.includes('V8') && model.includes('V8')) { return { name: 'lkl', from: 'lkl', bondState: 1 }; } else if (brand.includes('Android') && model.includes('rk3288')) { return { name: 'rk3288', from: 'purong.rk3288', bondState: 1 }; } else if (brand.includes('Allwinner') && model.includes('DS83X')) { return { name: 'ds83x', from: 'purong.usb.ds83x', bondState: 1 }; } else { return null; } } } export function print(f: (d: any) => void) { devices().then(async (items: Array) => { for (let task of items) { __DEV__ && console.log('device => ', task); if (f && task) await f(task); } // items.map((item) => { // __DEV__ && console.log('device => ', item); // if (f && item) f(item); // }); }); } let PrinterList: IPrinterListItem[]; export let Printer_start = device => { __DEV__ && console.log('Printer_start'); PrinterList = []; return startPrint(device); }; export let Printer_additem = (item: IPrinterListItem) => { return new Promise(async resolve => { PrinterList.push(item); resolve(null); }); }; export let Printer_end = async () => { __DEV__ && console.log('PrinterList=>', PrinterList); __DEV__ && console.log('Printer_end start'); let fQrCodeParams = null; for (let item of PrinterList) { // PrinterList.forEach(async (item) => { //不使用eval 就写死函数名 switch (item.func) { case 'printlnText': if (printlnText) await printlnText.apply(this, item.params); break; case 'printColumnsText': if (printColumnsText) await printColumnsText.apply(this, item.params); break; // case "printQrCode": if (printQrCode) printQrCode.apply(this, item.params); break; case 'printQrCode': if (printQrCode) fQrCodeParams = item.params; break; case 'printBarCode': if (printBarCode) await printQrCode.apply(this, item.params); break; case 'line': if (line) await line.apply(this, item.params); break; case 'lineWrap': if (lineWrap) await lineWrap.apply(this, item.params); break; } // }); } if (fQrCodeParams && printQrCode) await printQrCode.apply(this, fQrCodeParams); await endPrint(); return new Promise(async resolve => { __DEV__ && console.log('Printer_end ok'); resolve(null); }); }; export default { getSelf, Printer_start, Printer_additem, Printer_end, print, printDriver, };