Merge branch 'release/v0.0.1'

This commit is contained in:
Jay 2017-09-04 10:11:06 +08:00
commit 207c95d9ee
7 changed files with 80 additions and 18 deletions

View File

@ -2,11 +2,7 @@ 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
const iconv = require('iconv-lite')
class PrinterDevice {
constructor() {
@ -16,6 +12,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 => {
@ -44,16 +45,41 @@ 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.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])
break
}
} else {
this._printer.print(this.encodeStr(tmp))
}
}
this._printer.cut(true, 8)
this._printer.cut(true, config.printer.cutFeed || 4)
return true
}
async close() {
@ -61,7 +87,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

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

View File

@ -0,0 +1,12 @@
[Unit]
Description=BLE Printer Server
After=bluetooth.target
[Service]
ExecStart=/usr/local/bin/node /usr/local/node/ble-server/app.js
Restart=always
User=root
Environment="NODE_ENV=production"
[Install]
WantedBy=multi-user.target

View File

@ -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()
@ -17,7 +16,8 @@ class MainService extends bleno.PrimaryService {
}
}
localEvent.on('printResult', async str => {
localEvent.on('printResult', async state => {
let str = state ? 'ok' : 'fail'
dataCharacteristic.sendNotidy(str)
})

View File

@ -6,6 +6,7 @@
"dependencies": {
"bleno": "^0.4.2",
"escpos": "^2.4.3",
"iconv-lite": "^0.4.18",
"uuid": "^3.1.0"
}
}