From 32af5548e622dd4770c400ffb7ce04a37d5fa4bd Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 14 Aug 2017 23:17:03 +0800 Subject: [PATCH] add printer func --- config.json | 3 +++ gdata-characteristic.js | 24 +++++++++++++++++++++++- package.json | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index a540f7d..74309ac 100644 --- a/config.json +++ b/config.json @@ -8,5 +8,8 @@ "data":"00000002-8f05-4e30-beb0-6fa7ea3dfc3e" } } + }, + "printer": { + "serial": "/dev/ttyUSB0" } } diff --git a/gdata-characteristic.js b/gdata-characteristic.js index a453fd5..1a80861 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -1,5 +1,13 @@ const bleno = require('bleno') const config = require('./config.json') +const escpos = require('escpos') +const device = escpos.Serial(config.printer.serial) +const printer = escpos.Printer(device) +let isOpen = false + +device.open(()=>{ + isOpen = true +}) var tmp = [] @@ -26,7 +34,8 @@ class DataCharacteristic extends bleno.Characteristic { console.log(data[0]) } else if (data.length == 1 && data[0] == 0xff) { //console.log(tmp) - console.log(new Buffer(tmp).toString()) + console.log(Buffer.from(tmp).toString()) + printString(Buffer.from(tmp).toString()) } else { tmp = [...tmp, ...data.slice(2, data.length)] } @@ -37,4 +46,17 @@ class DataCharacteristic extends bleno.Characteristic { } } + +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]) + } + printer.cut(true, 8) +} + module.exports = DataCharacteristic diff --git a/package.json b/package.json index 62b2064..ac0c481 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "license": "MIT", "dependencies": { "bleno": "^0.4.2", + "escpos": "^2.4.3", "uuid": "^3.1.0" } }