From ae105fca88943d535bc97516587cd4bc4d4df151 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 23:31:25 +0800 Subject: [PATCH 1/7] add ble printer message parser method --- PrinterDev.js | 37 ++++++++++++++++++++++++++++++------- app.js | 12 +++++++++++- config.json | 3 ++- main-service.js | 3 ++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index cd6e025..73a180d 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -44,16 +44,39 @@ class PrinterDevice { }) } - printerString(str) { - if (!this._isOpen || !this._printer) return - let strs = str.split(/\n/) + async printerString(str) { + if (!this._isOpen || !this._printer) return false + this._printer.font('a') .align('ct') .size(1, 1) - for (let i in strs) { - this._printer.text(strs[i], 'big5') + + let cmds = str.split(/__/).filter(t => t) + + for (let i in cmds) { + let tmp = cmds[i] + if (tmp[0] === '&') { + switch (tmp[1]) { + case 'a': + let align = tmp.substring(2) + if (!align) break + this._printer.align(align) + break + case 's': + let size = tmp.substring(2) + let sarr = size.trim().split(',') + if (sarr != 2) break + this._printer.size(sarr[0], sarr[1]) + break + } + } else { + this._printer.text(tmp, 'big5') + } } - this._printer.cut(true, 8) + + this._printer.cut(true, config.printer.cutFeed || 4) + + return true } async close() { @@ -61,7 +84,7 @@ class PrinterDevice { if (this._type == 'serial') { let self = this await new Promise((resolve, reject) => { - self._device.close(() => { + self._printer.close(() => { resolve(1) }) }) diff --git a/app.js b/app.js index abbd305..b2f6dc4 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,16 @@ const MainService = require('./main-service') const printer = require('./PrinterDev') +async function initSystem(){ + try{ + await printer.connect() + }catch(err){ + throw err + } +} + +initSystem() + bleno.on('stateChange', state => { console.log(`bt device state ${state}`) if (state == 'poweredOn') { @@ -31,7 +41,6 @@ bleno.on('advertisingStart', function (error) { }) localEvent.on('print', async (str) => { - console.log(`get printer string >>> \n${str}`) let status = false if (printer.isOpen) { try { @@ -46,4 +55,5 @@ localEvent.on('print', async (str) => { process.on('SIGINT', () => { printer.close() + process.exit(0) }) \ No newline at end of file diff --git a/config.json b/config.json index 9b2bba9..58bd2f0 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,7 @@ } }, "printer": { - "serial": "/dev/ttyUSB0" + "serial": "/dev/ttyUSB0", + "cutFeed": 8 } } diff --git a/main-service.js b/main-service.js index 4dc21c3..5d8d164 100644 --- a/main-service.js +++ b/main-service.js @@ -17,7 +17,8 @@ class MainService extends bleno.PrimaryService { } } -localEvent.on('printResult', async str => { +localEvent.on('printResult', async state => { + let str = state ? 'ok' : 'fail' dataCharacteristic.sendNotidy(str) }) From 2462c92c9b67a09d37b45bd89e9deebaf7111669 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 24 Aug 2017 10:51:22 +0800 Subject: [PATCH 2/7] add iconv module and change print method --- PrinterDev.js | 11 +++++++++-- package.json | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index 73a180d..cf40a1e 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -2,7 +2,7 @@ const EventEmitter = require('events') const escpos = require('escpos') const config = require('./config.json') const fs = require('fs') - +const iconv = require('iconv-lite') // const device = new escpos.Serial(config.printer.serial) // const printer = new escpos.Printer(device) @@ -16,6 +16,11 @@ class PrinterDevice { this._type = null // type = serial or console } + encodeStr(str){ + if(!str || typeof str != 'string') return false + return iconv.encode(str, 'big5') + } + async connect() { let chkSerial = await new Promise((resolve, reject) => { fs.access(config.printer.serial, err => { @@ -66,11 +71,13 @@ class PrinterDevice { let size = tmp.substring(2) let sarr = size.trim().split(',') if (sarr != 2) break + if(!isFinite(sarr[0]) || !isFinite(sarr[1])) break + sarr = sarr.map(t => Math.floor(t)) this._printer.size(sarr[0], sarr[1]) break } } else { - this._printer.text(tmp, 'big5') + this._printer.print(this.encodeStr(tmp)) } } diff --git a/package.json b/package.json index ac0c481..3ff193f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dependencies": { "bleno": "^0.4.2", "escpos": "^2.4.3", + "iconv-lite": "^0.4.18", "uuid": "^3.1.0" } } From d7f491961a5c634204703321118d684eb231e658 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 24 Aug 2017 13:54:57 +0800 Subject: [PATCH 3/7] add ble package check --- gdata-characteristic.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gdata-characteristic.js b/gdata-characteristic.js index 2fcd438..c75a73e 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -4,6 +4,7 @@ const localEvent = require('./localEvent') var tmp = [] var idx = 0 +var failFlag = false const notifyDesciptor = new bleno.Descriptor({ uuid: '8888', @@ -29,12 +30,23 @@ class DataCharacteristic extends bleno.Characteristic { if (data.length == 1 && data[0] == 0x00) { tmp = [] idx = 0 + failFlag = false } else if (data.length == 1 && data[0] == 0xff) { console.log(Buffer.from(tmp).toString()) - localEvent.emit('print', Buffer.from(tmp).toString()) - } else { + if (!failFlag) { + localEvent.emit('print', Buffer.from(tmp).toString()) + } + } else if (!failFlag) { + let pidx = ((data[0] & 0xff) << 8) + (data[1] & 0xff) + if (idx !== pidx) { + // insert fail method + failFlag = true + this.sendNotidy('fail') + } + idx++ tmp = [...tmp, ...data.slice(2, data.length)] } + if (!withoutResponse) { console.log('send response') callback(this.RESULT_SUCCESS) From 8c2913e8d6f37235bd72acd0bd1e9a4144b3d939 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 25 Aug 2017 10:45:10 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=A4=9A=E9=A4=98?= =?UTF-8?q?=E8=A8=BB=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrinterDev.js | 4 ---- main-service.js | 1 - 2 files changed, 5 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index cf40a1e..137df75 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -3,10 +3,6 @@ const escpos = require('escpos') const config = require('./config.json') const fs = require('fs') const iconv = require('iconv-lite') -// const device = new escpos.Serial(config.printer.serial) - -// const printer = new escpos.Printer(device) -// let isOpen = false class PrinterDevice { constructor() { diff --git a/main-service.js b/main-service.js index 5d8d164..eec9898 100644 --- a/main-service.js +++ b/main-service.js @@ -2,7 +2,6 @@ const bleno = require('bleno') const config = require('./config.json') const localEvent = require('./localEvent') -// const TimeCharacteristic = require('./time-characteristic') const GdataCharacteristic = require('./gdata-characteristic') const dataCharacteristic = new GdataCharacteristic() From fc7425e97d417280b06288b654ecd58e24000cfc Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 1 Sep 2017 20:17:08 +0800 Subject: [PATCH 5/7] fix bug and add service file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.修正列印大小無法寫入BUG 2.加入SYSTEMD系統服務腳本 --- PrinterDev.js | 2 +- initscript/systemd/ble-server.service | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 initscript/systemd/ble-server.service diff --git a/PrinterDev.js b/PrinterDev.js index 137df75..32ac7c7 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -66,7 +66,7 @@ class PrinterDevice { case 's': let size = tmp.substring(2) let sarr = size.trim().split(',') - if (sarr != 2) break + if (sarr.length != 2) break if(!isFinite(sarr[0]) || !isFinite(sarr[1])) break sarr = sarr.map(t => Math.floor(t)) this._printer.size(sarr[0], sarr[1]) diff --git a/initscript/systemd/ble-server.service b/initscript/systemd/ble-server.service new file mode 100644 index 0000000..f3d07da --- /dev/null +++ b/initscript/systemd/ble-server.service @@ -0,0 +1,11 @@ +[Unit] +Description=BLE Printer Server + +[Service] +ExecStart=/usr/bin/node /usr/local/node/ble-server/app.js +Restart=always +User=root +Environment="NODE_ENV=production" + +[Install] +WantedBy=multi-user.target \ No newline at end of file From eedc57e2851de26d37c27cf7b2b89ecf8884059a Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 1 Sep 2017 20:27:27 +0800 Subject: [PATCH 6/7] fix node exec path --- initscript/systemd/ble-server.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initscript/systemd/ble-server.service b/initscript/systemd/ble-server.service index f3d07da..b5abb26 100644 --- a/initscript/systemd/ble-server.service +++ b/initscript/systemd/ble-server.service @@ -2,7 +2,7 @@ Description=BLE Printer Server [Service] -ExecStart=/usr/bin/node /usr/local/node/ble-server/app.js +ExecStart=/usr/local/bin/node /usr/local/node/ble-server/app.js Restart=always User=root Environment="NODE_ENV=production" From ca119388d71c5054448a940e2cb14b23417e5320 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 2 Sep 2017 21:59:31 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'initscript/systemd/bl?= =?UTF-8?q?e-server.service'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- initscript/systemd/ble-server.service | 1 + 1 file changed, 1 insertion(+) diff --git a/initscript/systemd/ble-server.service b/initscript/systemd/ble-server.service index b5abb26..4bd5415 100644 --- a/initscript/systemd/ble-server.service +++ b/initscript/systemd/ble-server.service @@ -1,5 +1,6 @@ [Unit] Description=BLE Printer Server +After=bluetooth.target [Service] ExecStart=/usr/local/bin/node /usr/local/node/ble-server/app.js