Skip to content

Instantly share code, notes, and snippets.

@kmundnic
Last active April 19, 2017 16:52
Show Gist options
  • Save kmundnic/cdf9104bfff0829efe4e780cbc22e6c4 to your computer and use it in GitHub Desktop.
Save kmundnic/cdf9104bfff0829efe4e780cbc22e6c4 to your computer and use it in GitHub Desktop.
Logging bluetooth devices using a reelyActive reelceiver
// Karel Mundnich
// April 12, 2017
var reelib = require('reelib');
var barnacles = require('barnacles');
var barnowl = require('barnowl');
var chickadee = require('chickadee');
var sniffypedia = require('sniffypedia');
var fs = require('fs');
var keypress = require('keypress');
var associations = new chickadee();
var notifications = new barnacles();
var middleware = new barnowl();
middleware.bind( { protocol: 'serial',
path: 'auto' } );
notifications.bind( { barnowl: middleware } );
notifications.bind( { chickadee: associations } ); // Bind with chickadee to populate the info we need in the events
function deviceType(event) {
var index = event.deviceUrl.indexOf('/', 30) + 1; // We use 30 to get the second-to-last '/'
var deviceType = event.deviceUrl.substring(index);
return deviceType;
}
function currentTime(event) {
return reelib.time.toLocalTimeOfDay(reelib.time.toTimestamp(event.time));
}
function appearance(event) {
return currentTime(event) + ',' + event.rssi + ',' + event.deviceId + ',appeared,' + deviceType(event);
}
function keepAlive(event) {
return currentTime(event) + ',' + event.rssi + ',' + event.deviceId + ',remains,' + deviceType(event);
}
function disappearance(event) {
return currentTime(event) + ',' + event.rssi + ',' + event.deviceId + ',disappeared,' + deviceType(event);
}
function flag() {
return reelib.time.toLocalTimeOfDay(reelib.time.getCurrentISO()) + ',' + '0,flag,flag,flag';
}
function iphone_flag() {
return reelib.time.toLocalTimeOfDay(reelib.time.getCurrentISO()) + ',' + '0,flag,flag,Apple_Inc/';
}
function other_flag() {
return reelib.time.toLocalTimeOfDay(reelib.time.getCurrentISO()) + ',' + '0,flag,flag,Any_Curious-Device/';
}
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
file = 'log-' + reelib.time.getCurrent() + '.csv';
// Logging
notifications.on('appearance', function(event) {
eventString = appearance(event);
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
});
notifications.on('keep-alive', function(event) {
eventString = keepAlive(event);
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
});
notifications.on('disappearance', function(event) {
eventString = disappearance(event);
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
});
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
try {
if (key && key.ctrl && key.name == 'c') {
process.exit();
}
if (key.name == 'i') {
eventString = iphone_flag();
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
}
if (key.name == 'o') {
eventString = other_flag();
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
}
if (key.name == 'space') {
eventString = flag();
console.log(eventString);
fs.appendFile(file, eventString + '\r\n');
}
}
catch (TypeError) {
// do nothing
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment