// import { Config } from 'kits' import Scan365PrintApi from './365Device/print'; import ScanYlyPrintApi from './365Device/print_yly'; import mpos from 'qm-mposkit'; import Hardwarekit from 'qm-hardwarekit'; import { Platform, AsyncStorage } from 'react-native'; import { connectUsbs, destroy as UsbDestroy } from './usb'; let Bluetooth = Hardwarekit.Bluetooth; let Sprt = Hardwarekit.Sprt; let DeviceStorage = Hardwarekit.devices; const LklPayment = mpos.LklPayment; // const Powa = Hardwarekit.Powa; function connect_usb_print(brand: string, model: string) { if ('android' === Platform.OS) { //初始化usb打印机设备 // if (Dsd.isSUNMI(brand)) { connectUsbs(); // } } } function disconnect_usb_print() { UsbDestroy(); } function create(brand: string, model: string) { // if ('ios' === Platform.OS) { // if (Powa) Powa.create(); // } // if ('android' === Platform.OS) { // //初始化usb打印机设备 // if (Dsd.isSUNMI(brand) || model == 'wintec_anypos') { // connectUsbs(); // } // } if (model == 'wintec_anypos' || model == 'rk3288') { connectUsbs(); } if (brand.includes('V8') && model.includes('V8')) { LklPayment.create(); } get_devices(devices => { console.log('devices ====> ', devices); let sprts = devices.filter(device => { return (device.from === 'x30' || device.from === 'sprt.58') && 1 == device.bondState; }); // if (sprts.length > 0) { if ('ios' === Platform.OS || sprts.length > 0) { Bluetooth.create(); BLE_connect_listener(null); BLE_disconnet_listener(null); BLE_off(null); //重新连接 sprts.map(item => { console.log('-------- Sprt.connect --------', item); if (item.address && 1 == item.bondState) Sprt.connect(item.address); }); } //检查365打印机状态 devices.map(item => { if (item.from === 'print.365') { Scan365PrintApi.getPrinterStatus(item.name, item.address).then(value => { if (__DEV__) console.log('result :', value); item.bondState = value ? 1 : 0; }); } if (item.from === 'print.yly') { ScanYlyPrintApi.getPrinterStatus(item.name, item.address).then(value => { if (__DEV__) console.log('result :', value); item.bondState = value ? 1 : 0; }); } }); DeviceStorage.put(devices); }); } let remove = callback => data => { console.log('-------- device.remove --------', data); DeviceStorage.remove(data, callback); }; let add = callback => data => { DeviceStorage.get().then(devices => { let ts = []; console.log('-------- device.add --------', data); let targets = devices.filter(value => value.name === data.name); if (0 === targets.length) { if (data.from == 'sprt.58') { ts = devices.filter(value => value.from != data.from); ts = [...ts, data]; } else { devices.push(data); ts = devices; } } else { devices.map(item => { if (item.name === data.name) { item.bondState = data.bondState; item = Object.assign(item, data); } ts.push(item); }); } DeviceStorage.put(ts, _ => { if (callback) callback(ts); }); }); }; export let enable_print = (address: String, printMode: 'auto' | 'manual') => { return new Promise(resolve => { DeviceStorage.get().then(devices => { if (0 === devices.length) { resolve(null); } for (let data of devices) { if (address === data.address) { data.printMode = printMode; stateByAddress(() => { resolve(null); })(data); } else { resolve(null); } } }); }); }; export let enable_self_print = (printMode: 'auto' | 'manual') => { return AsyncStorage.setItem('self_print', printMode); }; export let get_self_print_enable = () => { return AsyncStorage.getItem('self_print'); }; let add_array = callback => datas => { DeviceStorage.get().then(devices => { let ts = []; for (let data of datas) { console.log('-------- device.add --------', data); let targets = devices.filter(value => value.name === data.name); if (0 === targets.length) { if (data.from == 'sprt.58') { ts = devices.filter(value => value.from != data.from); ts = [...ts, data]; } else { devices.push(data); ts = devices; } } else { devices.map(item => { if (item.name === data.name) { item.bondState = data.bondState; item = Object.assign(item, data); } ts.push(item); }); } DeviceStorage.put(ts, _ => { if (callback) callback(ts); }); } }); }; let get_devices = callback => { DeviceStorage.get().then(items => { if (callback) callback(items); }); }; let BLE_off = (callback?: Function) => { //蓝牙设备断开 Bluetooth.toOff(() => { console.log('-------- BLE_off --------'); DeviceStorage.get().then(items => { items.forEach(element => { element.bondState = 0; }); DeviceStorage.put(items, _ => { if (callback) callback(items); }); }); }); }; let connect_by_device_bondstate = callback => rowData => { DeviceStorage.get().then(items => { console.log('-------- state connect --------', rowData); items.map(item => { if (item.address == rowData.address && item.name == rowData.name) { switch (rowData.from) { case 'sprt.58': items.map(item => { if (item.address === rowData.address) { // item.bondState = 0; item = Object.assign(item, rowData); } }); DeviceStorage.put(items, _ => { if (1 === rowData.bondState) { console.log('disconnect =>', rowData); Sprt.disconnect(rowData.address); } else { console.log('connect =>', items, rowData); Sprt.connect(rowData.address); } }); break; case 'x30': items.map(item => { if (item.address === rowData.address) { // item.bondState = 0; item = Object.assign(item, rowData); } }); DeviceStorage.put(items, _ => { if (1 === rowData.bondState) { console.log('disconnect =>', rowData); Sprt.disconnect(rowData.address); } else { console.log('connect =>', items, rowData); Sprt.connect(rowData.address); } }); break; case 'print.365': if (1 == rowData.bondState) { item.bondState = 0; DeviceStorage.put(items, _ => { if (callback) callback(items); }); } else { Scan365PrintApi.getPrinterStatus(rowData.name, rowData.address).then(value => { if (!value) return; item.bondState = 1; DeviceStorage.put(items, _ => { if (callback) callback(items); }); }); } break; case 'print.yly': if (1 == rowData.bondState) { item.bondState = 0; DeviceStorage.put(items, _ => { if (callback) callback(items); }); } else { ScanYlyPrintApi.getPrinterStatus(rowData.name, rowData.address).then(value => { if (!value) return; item.bondState = 1; DeviceStorage.put(items, _ => { if (callback) callback(items); }); }); } } } }); }); }; let BLE_disconnet_listener = (callback?: Function) => { Bluetooth.toDiscount(data => { console.log('-------- BLE_discounted --------', data); DeviceStorage.get().then(items => { items.map(item => { if (item.address === data.address) { item.bondState = 0; } }); DeviceStorage.put(items, _ => { if (callback) { callback(items); } }); }); }); }; let BLE_connect_listener = (callback?: Function) => { Bluetooth.toBonded(r => { DeviceStorage.get().then(items => { items.forEach(item => { if (item.address === r.address) { var data = Object.assign({}, item, { bondState: 1 }); console.log('-------- BLE_connected --------', data); stateByAddress(callback)(data); } }); }); }); }; let BLE_connect_listener_from = (fromStr: string) => (callback?: Function) => { Bluetooth.toBonded(r => { var data = Object.assign({}, r, { from: fromStr, bondState: 1 }); console.log('-------- BLE_connected --------', fromStr, data); stateByAddress(callback)(data); }); }; let BLE_scan_finish_listener = callback => { Bluetooth.toOver(data => { console.log('-------- BLE_over --------', data); if (callback) { callback(); } }); }; let BLE_scan_listener = callback => { Bluetooth.toFounded(data => { console.log('-------- BLE_founded --------', data); if (callback) callback(data); }); }; let BLE_scan = callback => { Bluetooth.discovery().then( data => { console.log('-------- BLE_discovery --------', data); if (callback) callback(); }, error => { // QMToast.show(error.message); if (callback) callback(error); if (__DEV__) console.log('-------- BLE_discovery --------', error.message); }, ); }; let BLE_connect = rowData => { console.log('-------- BLE_connect --------', rowData); DeviceStorage.get() .then(items => { console.log('====== paired ======', items); if (items) { items.map(item => { if (rowData.address === item.address) { item.from = 'sprt.58'; item.bondState = 1; // msg.emit('device:add', item); add(item); return new Promise(resolve => { resolve(false); }); } }); return new Promise(resolve => { resolve(true); }); } else { return new Promise(resolve => { resolve(true); }); } }) .then(r => { if (r) Sprt.connect(rowData.address); }); }; let stateByAddress = callback => data => { DeviceStorage.get().then(items => { let targets = items.filter(device => { return device.address === data.address; }); var ts = []; if (targets.length === 0) { ts = [...items, data]; } else { let iss = items.filter(device => { return device.address != data.address; }); ts = [...iss, Object.assign({}, targets[0], data)]; } DeviceStorage.put(ts, _ => { if (callback) callback(ts); }); }); }; let destroy = () => { Bluetooth.destroy(); }; // let create = () => { // DeviceStorage.get().then(devices => { // let count = devices.filter(item => item.from === 'sprt.58').length; // if(count > 0){ // Bluetooth.create(); // } // }); // }; let setHost = host => { Scan365PrintApi.setHost(host); ScanYlyPrintApi.setHost(host); }; export default { BLE_connect, BLE_connect_listener, BLE_connect_listener_from, BLE_disconnet_listener, BLE_scan, BLE_scan_listener, BLE_scan_finish_listener, BLE_off, get_devices, add, enable_print, enable_self_print, get_self_print_enable, add_array, create, remove, destroy, connect_usb_print, disconnect_usb_print, connect_by_device_bondstate, setHost, };