bluetest/lib/index.js
2016-07-07 10:36:23 +01:00

173 lines
3.5 KiB
JavaScript

/**
*
* 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 = module.exports = function(options) {
'use strict';
var core = {
mac: '',
gatt: null
};
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.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() {
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(m);
});
core.gatt.stdout.on('data', (data) => {
logger.info(`stdout: ${data}`);
if (data.indexOf('Connection successful') > -1) {
busEmitter.emit('connected');
}
});
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);
core.init(options);
};
util.inherits(bluetest, EventEmitter);