Pass an intval.state object into ble lib

This commit is contained in:
mmcwilliams 2017-12-11 14:38:46 -05:00
parent f1a5ae5aff
commit 1fef6d6925
2 changed files with 17 additions and 4 deletions

View File

@ -4,7 +4,7 @@ const restify = require('restify')
const log = require('./lib/log')('main') const log = require('./lib/log')('main')
const fs = require('fs') const fs = require('fs')
const ble = require('./lib/ble') const BLE = require('./lib/ble')
const intval = require('./lib/intval') const intval = require('./lib/intval')
const sequence = require('./lib/sequence') const sequence = require('./lib/sequence')
@ -18,6 +18,8 @@ let app = restify.createServer({
version: '0.0.1' version: '0.0.1'
}) })
let ble
function createServer () { function createServer () {
app.use(restify.plugins.queryParser()) app.use(restify.plugins.queryParser())
app.use(restify.plugins.bodyParser({ mapParams: false })) app.use(restify.plugins.bodyParser({ mapParams: false }))
@ -287,6 +289,10 @@ function index (req, res, next) {
}) })
} }
function bleGetState () {
return intval.state
}
function init () { function init () {
createServer() createServer()
@ -294,6 +300,7 @@ function init () {
console.log(str) console.log(str)
}) })
intval.init() intval.init()
ble = new BLE(bleGetState)
} }
init() init()

View File

@ -19,6 +19,7 @@ const NETWORK = os.networkInterfaces()
const MAC = getMac() || spoofMac() const MAC = getMac() || spoofMac()
let currentWifi = 'disconnected' let currentWifi = 'disconnected'
let getState
const chars = [] const chars = []
@ -123,9 +124,11 @@ class BLE {
* *
* @constructor * @constructor
*/ */
constructor () { constructor (bleGetState) {
log.info('Starting bluetooth service') log.info('Starting bluetooth service')
getState = bleGetState
bleno.on('stateChange', state => { bleno.on('stateChange', state => {
const BLE_ID = `${DEVICE_ID}_${MAC}` const BLE_ID = `${DEVICE_ID}_${MAC}`
log.info('stateChange', { state : state }) log.info('stateChange', { state : state })
@ -159,7 +162,7 @@ class BLE {
}) })
} }
_onWrite (data, offset, withoutResponse, callback) { _onWrite (data, offset, withoutResponse, callback) {
let result let result = {}
let utf8 let utf8
let obj let obj
if (offset) { if (offset) {
@ -168,6 +171,7 @@ class BLE {
return callback(result) return callback(result)
} }
utf8 = data.toString('utf8') utf8 = data.toString('utf8')
obj = JSON.parse(utf8) obj = JSON.parse(utf8)
console.dir(obj) console.dir(obj)
result = bleno.Characteristic.RESULT_SUCCESS result = bleno.Characteristic.RESULT_SUCCESS
@ -175,6 +179,7 @@ class BLE {
} }
_onRead (offset, callback) { _onRead (offset, callback) {
const result = bleno.Characteristic.RESULT_SUCCESS const result = bleno.Characteristic.RESULT_SUCCESS
const state = getState()
const data = new Buffer(JSON.stringify( { success : true } )) const data = new Buffer(JSON.stringify( { success : true } ))
callback(result, data.slice(offset, data.length)) callback(result, data.slice(offset, data.length))
} }
@ -187,6 +192,7 @@ class BLE {
on (eventName, callback) { on (eventName, callback) {
this[`_on${capitalize(eventName)}`] = callback this[`_on${capitalize(eventName)}`] = callback
} }
} }
module.exports = new BLE() module.exports = BLE