This commit is contained in:
Jay 2017-08-23 14:51:42 +08:00
parent ff9e696fe4
commit 93933124a4
6 changed files with 113 additions and 43 deletions

64
PrinterDev.js Normal file
View File

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

18
app.js
View File

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

View File

@ -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"
}
}
},

View File

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

10
localEvent.js Normal file
View File

@ -0,0 +1,10 @@
const EventEmitter = require('events')
const util = require('util')
class LocalEvent extends EventEmitter {
constructor(){
super()
}
}
module.exports = new LocalEvent()

View File

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