diff --git a/lib/index.js b/lib/index.js index 809e1ef..9c6ef52 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,12 +18,37 @@ var util = require('util'); var logger = require('log4js').getLogger(); -var bluetest = module.exports = function(options) { +var bluetest; +bluetest = module.exports = function(options) { 'use strict'; var core = { - mac: '', - gatt: null + mac: '', gatt: null + }; + + core.processLuxData = function(data) { + var m, e, lux; + var wH = data.slice(-5).split(' '); + var wB = []; + wB[0] = parseInt(wH[0], 16); + wB[1] = parseInt(wH[1], 16); + logger.debug(wH); + logger.debug(wB); + + var word = (wB[1] << 16) + wB[0]; + + var raw = new Uint16Array(1); + raw[0] = word; + + logger.debug(word); + + m = raw & 0x0FFF; + e = (raw & 0xF000) >> 12; + + lux = m * (0.01 * Math.pow(2.0, e)); + + logger.info('Lux: ', lux); + }; core.enableLuxNotification = function() { @@ -36,57 +61,12 @@ var bluetest = module.exports = function(options) { logger.debug('Gatt connected!!!!!'); core.enableLuxNotification(); - // core.gatt.stdin.write('primary\n'); - }; - - core.connectGattP = function() { - core.gatt = spawn('gatttool',['-I']); - var child = core.gatt.childProcess; - - child.on('error', function(err) { - logger.error('child',err); - }); - child.on('exit', function(code) { - - logger.debug('child','gatt exit code', code); - }); - - child.on('message', function(m) { - logger.info('child',m); - }); - - child.stdout.on('data', (data) => { - - logger.info('child',`stdout: ${data}`); - - if (data.indexOf('Connection successful') > -1) { - - busEmitter.emit('child','connected'); - } - }); - - child.stderr.on('data', (data) => { - logger.info('child',`stderr: ${data}`); - }); - - child.on('close', (code) => { - logger.warn('child',`child process exited with code ${code}`); - }); - - - core.gatt.then(function () { - console.log('[spawn] done!'); - }) - .catch(function (err) { - console.error('[spawn] ERROR: ', err); - }); - - + // Core.gatt.stdin.write('primary\n'); }; core.connectGatt = function() { logger.info('trying to connect using gatttool'); - core.gatt = spawn('gatttool',['-I']); + core.gatt = spawn('gatttool', ['-I']); var cStr; core.gatt.on('error', function(err) { logger.error(err); @@ -108,6 +88,11 @@ var bluetest = module.exports = function(options) { busEmitter.emit('connected'); } + + if (data.indexOf('Notification handle = 0x0041') > -1) { + busEmitter.emit('processLux', data); + } + }); core.gatt.stderr.on('data', (data) => { @@ -118,55 +103,52 @@ var bluetest = module.exports = function(options) { logger.warn(`child process exited with code ${code}`); }); - - logger.info('Trying to connect to ', core.mac); cStr = 'connect ' + core.mac + '\n'; core.gatt.stdin.write(cStr); - }; core.init = function(options) { - var tool_path = ''; var hcidev = 'hvi0'; if (typeof options.mac !== 'undefined') { core.mac = options.mac; - } else { + } + else { console.log('You need to pass a mac address.'); process.exit(1); } - logger.info('Working with:' , options); - + logger.info('Working with:', options); var hciconfig = spawn(tool_path + 'hciconfig', [hcidev, 'up']); hciconfig.on('exit', function(code) { - logger.debug('code',code); - + logger.debug('code', code); if (code !== 0) { // Could not get the device UP, maybe due to permissions, should run with sudo. - busEmitter.emit('error','hciconfig: failed to bring up device ' + hcidev + '. Try running with sudo.'); + busEmitter.emit('error', + 'hciconfig: failed to bring up device ' + hcidev + '. Try running with sudo.'); return; - } else { + } + else { core.connectGatt(); } - }); }; busEmitter.on('connected', core.gattConnected); + busEmitter.on('processLux', core.processLuxData); core.init(options); }; util.inherits(bluetest, EventEmitter);