ble-server/gdata-characteristic.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-08-09 10:15:07 +00:00
const bleno = require('bleno')
2017-08-10 01:24:14 +00:00
const config = require('./config.json')
2017-08-14 15:17:03 +00:00
const escpos = require('escpos')
const device = escpos.Serial(config.printer.serial)
const printer = escpos.Printer(device)
let isOpen = false
device.open(()=>{
isOpen = true
})
2017-08-09 10:15:07 +00:00
2017-08-10 12:18:43 +00:00
var tmp = []
2017-08-09 10:15:07 +00:00
class DataCharacteristic extends bleno.Characteristic {
constructor() {
super({
2017-08-10 01:24:14 +00:00
uuid: config.uuid.service.func.data,
2017-08-09 10:15:07 +00:00
properties: ['write', 'writeWithoutResponse'],
descriptors: [
new bleno.Descriptor({
uuid: '8888',
value: 'Now'
})
]
})
}
2017-08-10 14:31:02 +00:00
2017-08-09 10:15:07 +00:00
onWriteRequest(data, offset, withoutResponse, callback) {
console.log(`offset :: ${offset} , withoutRes :: ${withoutResponse}`)
console.log(`get data`, data)
2017-08-10 12:18:43 +00:00
2017-08-10 14:31:02 +00:00
if (data.length == 1 && data[0] == 0x00) {
tmp = []
console.log(data[0])
} else if (data.length == 1 && data[0] == 0xff) {
//console.log(tmp)
2017-08-14 15:17:03 +00:00
console.log(Buffer.from(tmp).toString())
printString(Buffer.from(tmp).toString())
2017-08-10 14:31:02 +00:00
} else {
tmp = [...tmp, ...data.slice(2, data.length)]
}
2017-08-10 12:18:43 +00:00
if (!withoutResponse) {
2017-08-10 14:31:02 +00:00
console.log('send response')
2017-08-09 10:15:07 +00:00
callback(this.RESULT_SUCCESS)
}
}
}
2017-08-14 15:17:03 +00:00
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)
}
2017-08-10 01:24:14 +00:00
module.exports = DataCharacteristic