-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mKeRix/dev
v0.3.0
- Loading branch information
Showing
10 changed files
with
295 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var config = require('config'); | ||
var bleacon = require('bleacon'); | ||
var console = process.console; | ||
|
||
var KalmanFilter = require('kalmanjs').default; | ||
|
||
var channel = config.get('ibeacon.channel'); | ||
|
||
function iBeaconScanner(callback) { | ||
// constructor | ||
this.callback = callback; | ||
this.kalmanManager = {}; | ||
|
||
this._init(); | ||
console.info('iBeacon scanner was initialized'); | ||
} | ||
|
||
iBeaconScanner.prototype._init = function () { | ||
bleacon.startScanning(); | ||
bleacon.on('discover', this._handlePacket.bind(this)); | ||
}; | ||
|
||
iBeaconScanner.prototype._handlePacket = function (ibeacon) { | ||
// check if we have a whitelist | ||
// and if we do, if this id is listed there | ||
var whitelist = config.get('ibeacon.whitelist') || []; | ||
|
||
var id = ibeacon.uuid + '-' + ibeacon.major + '-' + ibeacon.minor; | ||
|
||
if (whitelist.length == 0 || whitelist.indexOf(id) > -1) { | ||
|
||
// default hardcoded value for beacon tx power | ||
var txPower = ibeacon.measuredPower || -59; | ||
var distance = this._calculateDistance(ibeacon.rssi, txPower); | ||
|
||
// max distance parameter checking | ||
var maxDistance = config.get('ibeacon.max_distance') || 0; | ||
if (maxDistance == 0 || ibeacon.accuracy <= maxDistance) { | ||
var filteredDistance = this._filter(id, distance); | ||
|
||
var payload = { | ||
id: id, | ||
uuid: ibeacon.uuid, | ||
major: ibeacon.major, | ||
minor: ibeacon.minor, | ||
rssi: ibeacon.rssi, | ||
distance: filteredDistance, | ||
accuracy: ibeacon.accuracy, | ||
measuredpower: ibeacon.measuredPower, | ||
proximity: ibeacon.proximity | ||
}; | ||
|
||
this.callback(channel, payload); | ||
} | ||
} | ||
}; | ||
|
||
iBeaconScanner.prototype._calculateDistance = function (rssi, txPower) { | ||
if (rssi == 0) { | ||
return -1.0; | ||
} | ||
|
||
var ratio = rssi * 1.0 / txPower; | ||
if (ratio < 1.0) { | ||
return Math.pow(ratio, 10); | ||
} | ||
else { | ||
return (0.89976) * Math.pow(ratio, 7.7095) + 0.111; | ||
} | ||
}; | ||
|
||
iBeaconScanner.prototype._filter = function (id, distance) { | ||
if (!this.kalmanManager.hasOwnProperty(id)) { | ||
this.kalmanManager[id] = new KalmanFilter({ | ||
R: config.get('ibeacon.system_noise') || 0.01, | ||
Q: config.get('ibeacon.measurement_noise') || 3 | ||
}); | ||
} | ||
|
||
return this.kalmanManager[id].filter(distance); | ||
}; | ||
|
||
|
||
module.exports = iBeaconScanner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var config = require('config'); | ||
var exec = require('child_process').exec; | ||
var console = process.console; | ||
|
||
function Shell(callback) { | ||
// constructor | ||
this.callback = callback; | ||
|
||
this.options = { | ||
qos: config.get('shell.qos'), | ||
retain: config.get('shell.retain') | ||
}; | ||
|
||
this._init(); | ||
} | ||
|
||
Shell.prototype._init = function () { | ||
var that = this; | ||
var commands = config.get('shell.commands'); | ||
|
||
commands.forEach(function (command) { | ||
var regex = new RegExp(command.regexp); | ||
|
||
setInterval(function() { | ||
that.executeCommand(command.command, regex, command.channel, command.float) | ||
}, command.interval); | ||
|
||
console.info('Shell command \"' + command.command + '\" initialized') | ||
}); | ||
}; | ||
|
||
Shell.prototype.executeCommand = function (command, regex, channel, convertToFloat) { | ||
var that = this; | ||
|
||
exec(command, function(err, stdout, stderr) { | ||
if (err) { | ||
console.error(err); | ||
} | ||
if (stderr) { | ||
console.warning(stderr); | ||
} | ||
|
||
if (stdout) { | ||
var matches = stdout.match(regex); | ||
|
||
if (matches) { | ||
var match = matches[1]; | ||
if (convertToFloat) { | ||
match = parseFloat(match); | ||
} | ||
|
||
var payload = { | ||
value: match | ||
}; | ||
|
||
that.callback(channel, payload, that.options); | ||
} | ||
} | ||
}) | ||
}; | ||
|
||
module.exports = Shell; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.