Tidying up

This commit is contained in:
Martin Donnelly 2016-07-12 12:10:09 +01:00
parent e1f87d9135
commit 04e79f76c6
2 changed files with 171 additions and 12 deletions

View File

@ -8,8 +8,6 @@
var cp = require('child_process');
var spawn = cp.spawn;
var fork = cp.fork;
var spawnP = require('child-process-promise').spawn;
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
@ -30,15 +28,13 @@ bluetest = module.exports = function(options) {
console.log('+ processLuxData');
var m, e, lux;
var p = data.indexOf(10);
var l = data.length - (p + 1);
var _data = data.toString('ascii',0, p - 1);
// Var _data = data.toString('ascii');
// logger.debug('buffer:', data);
// logger.warn('ascii:', _data);
var _data = data.toString('ascii',0, p-1);
//var _data = data.toString('ascii');
//logger.debug('buffer:', data);
//logger.warn('ascii:', _data);
logger.debug('>--',_data);
logger.debug('>--',_data);
var wH = _data.slice(-5).split(' ');
var wB = [];
wB[0] = parseInt(wH[0], 16);
@ -77,7 +73,7 @@ logger.debug('>--',_data);
core.connectGatt = function() {
logger.info('trying to connect using gatttool');
core.gatt = spawn('gatttool', ['-I']);
core.gatt = spawn('gatttool', ['-b', core.mac, '-I']);
var cStr;
core.gatt.on('error', function(err) {
logger.error(err);
@ -93,7 +89,7 @@ logger.debug('>--',_data);
core.gatt.stdout.on('data', (data) => {
//logger.info(`stdout: ${data}`);
// Logger.info(`stdout: ${data}`);
if (data.indexOf('Connection successful') > -1) {
@ -115,7 +111,7 @@ logger.debug('>--',_data);
});
logger.info('Trying to connect to ', core.mac);
cStr = 'connect ' + core.mac + '\n';
cStr = 'connect\n';
core.gatt.stdin.write(cStr);
};

163
lib/ti_index.js Normal file
View File

@ -0,0 +1,163 @@
/**
*
* User: Martin Donnelly
* Date: 2016-07-06
* Time: 13:37
*
*/
var cp = require('child_process');
var spawn = cp.spawn;
var fork = cp.fork;
var spawnP = require('child-process-promise').spawn;
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
var util = require('util');
var logger = require('log4js').getLogger();
var bluetest;
bluetest = module.exports = function(options) {
'use strict';
var core = {
mac: '', gatt: null
};
core.processLuxData = function(data) {
console.log('+ processLuxData');
var m, e, lux;
var p = data.indexOf(10);
var l = data.length - (p + 1);
var _data = data.toString('ascii',0, p-1);
//var _data = data.toString('ascii');
//logger.debug('buffer:', data);
//logger.warn('ascii:', _data);
logger.debug('>--',_data);
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[0] << 16) + wB[1];
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);
console.log('- processLuxData');
};
core.enableLuxNotification = function() {
logger.info('Enable Luxomity Notifications...');
core.gatt.stdin.write('char-write-req 0x0042 0100\n');
core.gatt.stdin.write('char-write-req 0x0044 01\n');
};
core.gattConnected = function() {
logger.debug('Gatt connected!!!!!');
core.enableLuxNotification();
// Core.gatt.stdin.write('primary\n');
};
core.connectGatt = function() {
logger.info('trying to connect using gatttool');
core.gatt = spawn('gatttool', ['-I']);
var cStr;
core.gatt.on('error', function(err) {
logger.error(err);
});
core.gatt.on('exit', function(code) {
logger.debug('gatt exit code', code);
});
core.gatt.on('message', function(m) {
logger.info('message:', m);
});
core.gatt.stdout.on('data', (data) => {
//logger.info(`stdout: ${data}`);
if (data.indexOf('Connection successful') > -1) {
busEmitter.emit('connected');
}
if (data.indexOf('Notification handle = 0x0041') > -1) {
busEmitter.emit('processLux', data);
}
});
core.gatt.stderr.on('data', (data) => {
logger.info(`stderr: ${data}`);
});
core.gatt.on('close', (code) => {
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 {
console.log('You need to pass a mac address.');
process.exit(1);
}
logger.info('Working with:', options);
var hciconfig = spawn(tool_path + 'hciconfig', [hcidev, 'up']);
hciconfig.on('exit', function(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.');
return;
} else {
core.connectGatt();
}
});
};
busEmitter.on('connected', core.gattConnected);
busEmitter.on('processLux', core.processLuxData);
core.init(options);
};
util.inherits(bluetest, EventEmitter);