From 1fef6d69251f93b953ea6df4123cfab6a551388a Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Mon, 11 Dec 2017 14:38:46 -0500 Subject: [PATCH] Pass an intval.state object into ble lib --- index.js | 9 ++++++++- lib/ble/index.js | 12 +++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ee66fd8..70c95d8 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const restify = require('restify') const log = require('./lib/log')('main') const fs = require('fs') -const ble = require('./lib/ble') +const BLE = require('./lib/ble') const intval = require('./lib/intval') const sequence = require('./lib/sequence') @@ -18,6 +18,8 @@ let app = restify.createServer({ version: '0.0.1' }) +let ble + function createServer () { app.use(restify.plugins.queryParser()) app.use(restify.plugins.bodyParser({ mapParams: false })) @@ -287,6 +289,10 @@ function index (req, res, next) { }) } +function bleGetState () { + return intval.state +} + function init () { createServer() @@ -294,6 +300,7 @@ function init () { console.log(str) }) intval.init() + ble = new BLE(bleGetState) } init() diff --git a/lib/ble/index.js b/lib/ble/index.js index 2349bbd..3312aed 100644 --- a/lib/ble/index.js +++ b/lib/ble/index.js @@ -19,6 +19,7 @@ const NETWORK = os.networkInterfaces() const MAC = getMac() || spoofMac() let currentWifi = 'disconnected' +let getState const chars = [] @@ -123,9 +124,11 @@ class BLE { * * @constructor */ - constructor () { + constructor (bleGetState) { log.info('Starting bluetooth service') + getState = bleGetState + bleno.on('stateChange', state => { const BLE_ID = `${DEVICE_ID}_${MAC}` log.info('stateChange', { state : state }) @@ -159,7 +162,7 @@ class BLE { }) } _onWrite (data, offset, withoutResponse, callback) { - let result + let result = {} let utf8 let obj if (offset) { @@ -168,6 +171,7 @@ class BLE { return callback(result) } utf8 = data.toString('utf8') + obj = JSON.parse(utf8) console.dir(obj) result = bleno.Characteristic.RESULT_SUCCESS @@ -175,6 +179,7 @@ class BLE { } _onRead (offset, callback) { const result = bleno.Characteristic.RESULT_SUCCESS + const state = getState() const data = new Buffer(JSON.stringify( { success : true } )) callback(result, data.slice(offset, data.length)) } @@ -187,6 +192,7 @@ class BLE { on (eventName, callback) { this[`_on${capitalize(eventName)}`] = callback } + } -module.exports = new BLE() \ No newline at end of file +module.exports = BLE \ No newline at end of file