Skip to content

Commit

Permalink
adds pi finder npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece committed Feb 3, 2015
1 parent cc05451 commit abc587b
Show file tree
Hide file tree
Showing 15 changed files with 6,911 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
.DS_Store
node_modules
pi_finder/build
pi_finder/src/*.zip
pi_finder/src/*.gz
pi_finder/main/*.zip
pi_finder/main/*.gz

# debs we build on the fly
packages/build/occi_*.deb
Expand Down
21 changes: 21 additions & 0 deletions pi_finder/package/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Adafruit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions pi_finder/package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Adafruit Raspberry Pi Finder

This package is an atom-shell app, and is not useful outside of that context. Please
visit the [Adafruit Pi Finder](https://github.com/adafruit/Adafruit-Pi-Finder) repo for
more information.
109 changes: 109 additions & 0 deletions pi_finder/package/arp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// a heavily simplified fork of https://www.npmjs.com/package/node-arp
// removes ping and cleans up a few other things

var util = require('util'),
spawn = require('child_process').spawn,
os = require('os').platform();

function arp(ip, cb) {

if(os.indexOf('linux') === 0) {
linux(ip, cb);
} else if(os.indexOf('win') === 0) {
windows(ip, cb);
} else if(os.indexOf('darwin') === 0) {
mac(ip, cb);
}

};

function linux(ip, cb) {

var arp = spawn('arp', [ '-n', ip ]),
buffer = '';

arp.stdout.on('data', function (data) {
buffer += data;
});

arp.on('close', function(code) {

var lines = buffer.split('\n');

if (code !== 0) {
return cb('arp failed');
}

if (lines.length >= 2) {
var chunks = lines[1].split(' ').filter(String);
return cb(null, chunks[2]);
}

cb(null, false);

});

};

function windows(ip, cb) {

var arp = spawn('arp', ['-a', ip]),
buffer = '';

arp.stdout.on('data', function(data) {
buffer += data;
});

arp.on('close', function(code) {

var lines = buffer.split('\r\n');

if (code !== 0) {
return cb('arp failed');
}

for(var i = 3; i < lines.length; i++) {

var chunks = lines[i].split(' ').filter(String);

if (chunks[0] === ip) {
return cb(null, chunks[1].replace(/-/g, ':'));
}

}

cb(null, false);

});

};

function mac(ip, cb) {

var arp = spawn('arp', ['-n', ip]),
buffer = '';

arp.stdout.on('data', function(data) {
buffer += data;
});

arp.on('close', function(code) {

var chunks = buffer.split(' ').filter(String);

if (code !== 0) {
return cb('arp failed');
}

if (chunks[3] !== 'no') {
var mac = chunks[3].replace(/^0:/g, '00:').replace(/:0:/g, ':00:').replace(/:0$/g, ':00');
return cb(null, mac);
}

cb(null, false);

});

};

exports = module.exports = arp;
102 changes: 102 additions & 0 deletions pi_finder/package/finder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var ip = require('ip'),
events = require('events'),
util = require('util'),
ping = require('./ping.js'),
async = require('async'),
arp = require('./arp.js');

/**** Finder is an event emitter ****/
util.inherits(Finder, events.EventEmitter);

/**** Finder prototype ****/
var proto = Finder.prototype;

/**** Expose Finder ****/
exports = module.exports = Finder;

/**** Finder constructor ****/
function Finder(options) {

if (!(this instanceof Finder)) {
return new Finder(options);
}

events.EventEmitter.call(this);

util._extend(this, options || {});

// grab the user's IP
this.ip = ip.address();
// grab the first three octects of the IP
this.subnet = this.ip.substr(0, this.ip.lastIndexOf('.')) || false;

}

proto.ip = false;
proto.subnet = false;

proto.start = function(cb) {

async.times(
255,
this.ping.bind(this),
this.finish.bind(this, cb)
);

};

proto.ping = function(position, next) {

var host = this.subnet + '.' + position;

this.emit('ip', host);

ping(host, function(){

this.arp(host, next);

}.bind(this));

};

proto.arp = function(host, next) {

arp(host, function(err, mac) {

if (err || !mac) {
return next();
}

if(/b8:27:eb/.test(mac)) {
return next(null, host);
}

next();

}.bind(this));

};

proto.finish = function(cb, err, ips) {

var results = [];

if(err) {
return cb(err);
}

if(Array.isArray(ips)) {

results = ips.filter(function(i) {
return i;
});

}

if(! results.length) {
return cb();
}

cb(null, results);

};
Loading

0 comments on commit abc587b

Please sign in to comment.