diff --git a/PrinterDev.js b/PrinterDev.js new file mode 100644 index 0000000..8d1b959 --- /dev/null +++ b/PrinterDev.js @@ -0,0 +1,64 @@ +const EventEmitter = require('events') +const escpos = require('escpos') +const config = require('./config.json') +const fs = require('fs') + +// const device = new escpos.Serial(config.printer.serial) + +// const printer = new escpos.Printer(device) +// let isOpen = false + +class PrinterDevice { + constructor() { + this._isOpen = false + this._device = null + this._printer = null + this._type = null // type = serial or console + } + + async connect() { + let chkSerial = await new Promise((resolve, reject) => { + fs.access(config.printer.serial, err => { + if (err) return resolve(false) + return resolve(true) + }) + }) + if (chkSerial) { + this._device = new escpos.Serial(config.printer.serial) + this._type = 'serial' + } else { + this._device = new escpos.Console() + this._type = 'console' + } + this._printer = new escpos.Printer(this._device) + this.openSerial() + } + + async openSerial() { + if (!this._device || this._type != 'serial') return + this._isOpen = await new Promise((resolve, reject) => { + this._device.open(err => { + if (err) return resolve(false) + return resolve(true) + }) + }) + } + + printerString(str) { + if (!this._isOpen || !this._printer) return + let strs = str.split(/\n/) + this._printer.font('a') + .align('ct') + .size(1, 1) + for (let i in strs) { + printer.text(strs[i], 'big5') + } + printer.cut(true, 8) + } + + get isOpen() { + return this._isOpen ? true : false + } +} + +module.exports = new PrinterDevice() \ No newline at end of file diff --git a/app.js b/app.js index d195fa7..6c85a13 100644 --- a/app.js +++ b/app.js @@ -2,9 +2,12 @@ const bleno = require('bleno') const config = require('./config.json') const adapterName = 'BLE_Printer' const serverUUID = config.uuid.main - +const localEvent = require('./localEvent') +const fs = require('fs') const MainService = require('./main-service') +const printer = require('./PrinterDev') + bleno.on('stateChange', state => { console.log(`bt device state ${state}`) if (state == 'poweredOn') { @@ -26,3 +29,16 @@ bleno.on('advertisingStart', function (error) { }) } }) + +localEvent.on('print', async (str) => { + let status = false + if (printer.isOpen) { + try { + status = await printer.printerString(str) + } catch (err) { + status = false + } + } + + localEvent.emit('printResult', status) +}) \ No newline at end of file diff --git a/config.json b/config.json index 74309ac..9b2bba9 100644 --- a/config.json +++ b/config.json @@ -4,8 +4,7 @@ "service":{ "id":"dd535b71-8f05-4e30-beb0-6fa7ea3dfc3e", "func":{ - "time":"00000001-8f05-4e30-beb0-6fa7ea3dfc3e", - "data":"00000002-8f05-4e30-beb0-6fa7ea3dfc3e" + "data":"00000001-8f05-4e30-beb0-6fa7ea3dfc3e" } } }, diff --git a/gdata-characteristic.js b/gdata-characteristic.js index 25e75c5..cbf0ca7 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -1,13 +1,6 @@ const bleno = require('bleno') const config = require('./config.json') -const escpos = require('escpos') -const device = new escpos.Serial(config.printer.serial) -const printer = new escpos.Printer(device) -let isOpen = false - -device.open(()=>{ - isOpen = true -}) +const localEvent = require('./localEvent') var tmp = [] @@ -38,10 +31,11 @@ class DataCharacteristic extends bleno.Characteristic { } else if (data.length == 1 && data[0] == 0xff) { //console.log(tmp) console.log(Buffer.from(tmp).toString()) - printString(Buffer.from(tmp).toString()) - if(this._notifyFunc) { - this._notifyFunc(Buffer.from([0x00,0x01])) - } + localEvent.emit('print', Buffer.from(tmp).toString()) + // printString(Buffer.from(tmp).toString()) + // if(this._notifyFunc) { + // this._notifyFunc(Buffer.from([0x00,0x01])) + // } } else { tmp = [...tmp, ...data.slice(2, data.length)] } @@ -51,37 +45,19 @@ class DataCharacteristic extends bleno.Characteristic { } } + sendNotidy(str) { + if (!this._notifyFunc) return + this._notifyFunc(Buffer.from(str)) + } + onSubscribe(maxValueSize, cb) { console.log(maxValueSize) this._notifyFunc = cb } - onUnsubscribe () { + onUnsubscribe() { this._notifyFunc = null } } - -function printString(str){ - if(!isOpen) return - let strs = str.split(/\n/) - printer.font('a') - .align('ct') - .size(1,1) - for(let i in strs){ - printer.text(strs[i], 'big5') - } - printer.cut(true, 8) -} - -process.on('SIGINT', async ()=>{ - await new Promise((resolve, reject) => { - device.close(()=>{ - console.log('serial device close') - resolve(null) - }) - }) - process.exit(0) -}) - module.exports = DataCharacteristic diff --git a/localEvent.js b/localEvent.js new file mode 100644 index 0000000..24798fb --- /dev/null +++ b/localEvent.js @@ -0,0 +1,10 @@ +const EventEmitter = require('events') +const util = require('util') + +class LocalEvent extends EventEmitter { + constructor(){ + super() + } +} + +module.exports = new LocalEvent() \ No newline at end of file diff --git a/main-service.js b/main-service.js index e69b608..4dc21c3 100644 --- a/main-service.js +++ b/main-service.js @@ -1,19 +1,24 @@ const bleno = require('bleno') const config = require('./config.json') +const localEvent = require('./localEvent') -const TimeCharacteristic = require('./time-characteristic') +// const TimeCharacteristic = require('./time-characteristic') const GdataCharacteristic = require('./gdata-characteristic') +const dataCharacteristic = new GdataCharacteristic() class MainService extends bleno.PrimaryService { - constructor(){ + constructor() { super({ uuid: config.uuid.service.id, characteristics: [ - new TimeCharacteristic(), - new GdataCharacteristic() + dataCharacteristic ] }) } } +localEvent.on('printResult', async str => { + dataCharacteristic.sendNotidy(str) +}) + module.exports = MainService