mirror of
https://gitlab.silvrtree.co.uk/martind2000/bluetest.git
synced 2025-01-27 12:56:17 +00:00
165 lines
3.6 KiB
JavaScript
165 lines
3.6 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;
|
|
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);
|
|
//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[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);
|
|
*/
|
|
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);
|