process luxometer

This commit is contained in:
Martin Donnelly 2016-07-07 10:58:40 +01:00
parent 06bf56c610
commit 355caa6254

View File

@ -18,12 +18,37 @@ var util = require('util');
var logger = require('log4js').getLogger(); var logger = require('log4js').getLogger();
var bluetest = module.exports = function(options) { var bluetest;
bluetest = module.exports = function(options) {
'use strict'; 'use strict';
var core = { var core = {
mac: '', mac: '', gatt: null
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() { core.enableLuxNotification = function() {
@ -36,52 +61,7 @@ var bluetest = module.exports = function(options) {
logger.debug('Gatt connected!!!!!'); logger.debug('Gatt connected!!!!!');
core.enableLuxNotification(); core.enableLuxNotification();
// core.gatt.stdin.write('primary\n'); // 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.connectGatt = function() { core.connectGatt = function() {
@ -108,6 +88,11 @@ var bluetest = module.exports = function(options) {
busEmitter.emit('connected'); busEmitter.emit('connected');
} }
if (data.indexOf('Notification handle = 0x0041') > -1) {
busEmitter.emit('processLux', data);
}
}); });
core.gatt.stderr.on('data', (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.warn(`child process exited with code ${code}`);
}); });
logger.info('Trying to connect to ', core.mac); logger.info('Trying to connect to ', core.mac);
cStr = 'connect ' + core.mac + '\n'; cStr = 'connect ' + core.mac + '\n';
core.gatt.stdin.write(cStr); core.gatt.stdin.write(cStr);
}; };
core.init = function(options) { core.init = function(options) {
var tool_path = ''; var tool_path = '';
var hcidev = 'hvi0'; var hcidev = 'hvi0';
if (typeof options.mac !== 'undefined') { if (typeof options.mac !== 'undefined') {
core.mac = options.mac; core.mac = options.mac;
} else { }
else {
console.log('You need to pass a mac address.'); console.log('You need to pass a mac address.');
process.exit(1); process.exit(1);
} }
logger.info('Working with:', options); logger.info('Working with:', options);
var hciconfig = spawn(tool_path + 'hciconfig', [hcidev, 'up']); var hciconfig = spawn(tool_path + 'hciconfig', [hcidev, 'up']);
hciconfig.on('exit', function(code) { hciconfig.on('exit', function(code) {
logger.debug('code', code); logger.debug('code', code);
if (code !== 0) { if (code !== 0) {
// Could not get the device UP, maybe due to permissions, should run with sudo. // 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; return;
} else { }
else {
core.connectGatt(); core.connectGatt();
} }
}); });
}; };
busEmitter.on('connected', core.gattConnected); busEmitter.on('connected', core.gattConnected);
busEmitter.on('processLux', core.processLuxData);
core.init(options); core.init(options);
}; };
util.inherits(bluetest, EventEmitter); util.inherits(bluetest, EventEmitter);