From 4aadc5a9df55ea3a0fd5873c2c4aac70b993b25f Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Tue, 12 Dec 2017 13:09:13 -0500 Subject: [PATCH] Use callback model for bluetooth write events. --- index.js | 24 +++++++++++++++--------- lib/ble/index.js | 10 +++++++--- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 338c1b0..0221199 100644 --- a/index.js +++ b/index.js @@ -41,10 +41,14 @@ function createServer () { }) } -function bleBindings () { - +function createBLE () { + ble = new BLE(() => { + return intval.status() + }) + ble.on('frame', bFrame) } +//Restify functions function rDir (req, res, next) { let dir = true let set = false @@ -279,6 +283,13 @@ function rSequence (req, res, next) { } } +//Ble functions + +function bFrame (cb) { + console.log(str) + cb({ dir: true, len : 630 }) +} + function index (req, res, next) { fs.readFile(INDEXPATH, 'utf8', (err, data) => { if (err) { @@ -290,14 +301,9 @@ function index (req, res, next) { } function init () { - createServer() intval.init() - ble = new BLE(() => { - return intval.status() - }) - ble.on('data', (str) => { - console.log(str) - }) + createServer() + createBLE() } init() diff --git a/lib/ble/index.js b/lib/ble/index.js index 4878379..cf52e67 100644 --- a/lib/ble/index.js +++ b/lib/ble/index.js @@ -171,11 +171,15 @@ class BLE { return callback(result) } utf8 = data.toString('utf8') - console.log(utf8) obj = JSON.parse(utf8) - console.dir(obj) result = bleno.Characteristic.RESULT_SUCCESS - return callback(result) + if (obj.type && this[`_on${capitalize(obj.type)}`]) { + this[`_on${capitalize(obj.type)}`]( data => { + return callback(result, data.slice(offset, data.length)) + }) + } else { + return callback(result) + } } _onRead (offset, callback) { const result = bleno.Characteristic.RESULT_SUCCESS