From 93933124a45c12338c8ef57e9b221d24d1373671 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 14:51:42 +0800 Subject: [PATCH 1/4] update --- PrinterDev.js | 64 +++++++++++++++++++++++++++++++++++++++++ app.js | 18 +++++++++++- config.json | 3 +- gdata-characteristic.js | 48 ++++++++----------------------- localEvent.js | 10 +++++++ main-service.js | 13 ++++++--- 6 files changed, 113 insertions(+), 43 deletions(-) create mode 100644 PrinterDev.js create mode 100644 localEvent.js 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 From 8c5f7d144ab13c7cc942799cbdeccd52782f176e Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 16:10:57 +0800 Subject: [PATCH 2/4] remove comment --- gdata-characteristic.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/gdata-characteristic.js b/gdata-characteristic.js index cbf0ca7..38473ab 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -29,13 +29,8 @@ class DataCharacteristic extends bleno.Characteristic { tmp = [] console.log(data[0]) } else if (data.length == 1 && data[0] == 0xff) { - //console.log(tmp) console.log(Buffer.from(tmp).toString()) 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)] } From efc277d8a55772b23d54988313681531e81fb31e Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 21:20:22 +0800 Subject: [PATCH 3/4] Auto stash before rebase of "origin/master" --- PrinterDev.js | 16 ++++++++++++++-- gdata-characteristic.js | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index 8d1b959..cd6e025 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -51,9 +51,21 @@ class PrinterDevice { .align('ct') .size(1, 1) for (let i in strs) { - printer.text(strs[i], 'big5') + this._printer.text(strs[i], 'big5') + } + this._printer.cut(true, 8) + } + + async close() { + if (!this._device) return + if (this._type == 'serial') { + let self = this + await new Promise((resolve, reject) => { + self._device.close(() => { + resolve(1) + }) + }) } - printer.cut(true, 8) } get isOpen() { diff --git a/gdata-characteristic.js b/gdata-characteristic.js index 38473ab..2fcd438 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -3,6 +3,7 @@ const config = require('./config.json') const localEvent = require('./localEvent') var tmp = [] +var idx = 0 const notifyDesciptor = new bleno.Descriptor({ uuid: '8888', @@ -27,7 +28,7 @@ class DataCharacteristic extends bleno.Characteristic { if (data.length == 1 && data[0] == 0x00) { tmp = [] - console.log(data[0]) + idx = 0 } else if (data.length == 1 && data[0] == 0xff) { console.log(Buffer.from(tmp).toString()) localEvent.emit('print', Buffer.from(tmp).toString()) From 57fc3dff2d7e49c72b386a94913e11a65976acab Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 21:23:55 +0800 Subject: [PATCH 4/4] add ctrl+c closeSerial --- app.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app.js b/app.js index 6c85a13..abbd305 100644 --- a/app.js +++ b/app.js @@ -31,6 +31,7 @@ bleno.on('advertisingStart', function (error) { }) localEvent.on('print', async (str) => { + console.log(`get printer string >>> \n${str}`) let status = false if (printer.isOpen) { try { @@ -41,4 +42,8 @@ localEvent.on('print', async (str) => { } localEvent.emit('printResult', status) +}) + +process.on('SIGINT', () => { + printer.close() }) \ No newline at end of file