Merge branch 'feature/parser_print_string' into develop

This commit is contained in:
Jay 2017-08-23 23:31:35 +08:00
commit 748c021ff2
4 changed files with 45 additions and 10 deletions

View File

@ -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
}
this._printer.cut(true, 8)
} else {
this._printer.text(tmp, 'big5')
}
}
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)
})
})

12
app.js
View File

@ -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)
})

View File

@ -9,6 +9,7 @@
}
},
"printer": {
"serial": "/dev/ttyUSB0"
"serial": "/dev/ttyUSB0",
"cutFeed": 8
}
}

View File

@ -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)
})