2017-08-09 10:15:07 +00:00
|
|
|
const bleno = require('bleno')
|
2017-09-07 15:40:51 +00:00
|
|
|
const config = require('../config.json')
|
|
|
|
const localEvent = require('../localEvent')
|
2017-08-09 10:15:07 +00:00
|
|
|
|
2017-08-10 12:18:43 +00:00
|
|
|
var tmp = []
|
2017-08-23 13:20:22 +00:00
|
|
|
var idx = 0
|
2017-08-24 05:54:57 +00:00
|
|
|
var failFlag = false
|
2017-08-10 12:18:43 +00:00
|
|
|
|
2017-08-16 14:03:35 +00:00
|
|
|
const notifyDesciptor = new bleno.Descriptor({
|
|
|
|
uuid: '8888',
|
|
|
|
value: 0
|
|
|
|
})
|
|
|
|
|
2017-08-09 10:15:07 +00:00
|
|
|
class DataCharacteristic extends bleno.Characteristic {
|
2017-09-05 10:36:35 +00:00
|
|
|
constructor () {
|
2017-08-09 10:15:07 +00:00
|
|
|
super({
|
2017-09-07 15:40:51 +00:00
|
|
|
uuid: config.ble.uuid.characteristic,
|
2017-08-16 14:03:35 +00:00
|
|
|
properties: ['write', 'writeWithoutResponse', 'notify'],
|
2017-08-09 10:15:07 +00:00
|
|
|
descriptors: [
|
2017-08-16 14:03:35 +00:00
|
|
|
notifyDesciptor
|
2017-08-09 10:15:07 +00:00
|
|
|
]
|
|
|
|
})
|
2017-08-16 14:03:35 +00:00
|
|
|
this._notifyFunc = null
|
2017-08-09 10:15:07 +00:00
|
|
|
}
|
2017-08-10 14:31:02 +00:00
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
onWriteRequest (data, offset, withoutResponse, callback) {
|
2017-08-09 10:15:07 +00:00
|
|
|
console.log(`offset :: ${offset} , withoutRes :: ${withoutResponse}`)
|
|
|
|
console.log(`get data`, data)
|
2017-08-10 12:18:43 +00:00
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
if (data.length === 1 && data[0] === 0x00) {
|
2017-08-10 14:31:02 +00:00
|
|
|
tmp = []
|
2017-08-23 13:20:22 +00:00
|
|
|
idx = 0
|
2017-08-24 05:54:57 +00:00
|
|
|
failFlag = false
|
2017-09-05 10:36:35 +00:00
|
|
|
} else if (data.length === 1 && data[0] === 0xff) {
|
2017-08-14 15:17:03 +00:00
|
|
|
console.log(Buffer.from(tmp).toString())
|
2017-08-24 05:54:57 +00:00
|
|
|
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++
|
2017-08-10 14:31:02 +00:00
|
|
|
tmp = [...tmp, ...data.slice(2, data.length)]
|
|
|
|
}
|
2017-08-24 05:54:57 +00:00
|
|
|
|
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-16 14:03:35 +00:00
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
sendNotidy (str) {
|
2017-08-23 06:51:42 +00:00
|
|
|
if (!this._notifyFunc) return
|
|
|
|
this._notifyFunc(Buffer.from(str))
|
|
|
|
}
|
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
onSubscribe (maxValueSize, cb) {
|
2017-08-16 14:03:35 +00:00
|
|
|
console.log(maxValueSize)
|
|
|
|
this._notifyFunc = cb
|
|
|
|
}
|
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
onUnsubscribe () {
|
2017-08-16 14:03:35 +00:00
|
|
|
this._notifyFunc = null
|
|
|
|
}
|
2017-08-09 10:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 01:24:14 +00:00
|
|
|
module.exports = DataCharacteristic
|