Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable es6 class inheritance #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions lib/noble-device.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var events = require('events');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var util = require('./util');
var noble = require('noble');

var GENERIC_ACCESS_UUID = '1800';
var DEVICE_NAME_UUID = '2a00';
Expand All @@ -16,7 +17,118 @@ function NobleDevice(peripheral) {
this.connectedAndSetUp = false;
}

util.inherits(NobleDevice, events.EventEmitter);
util.inherits(NobleDevice, EventEmitter);

NobleDevice.SCAN_UUIDS = [];
NobleDevice.SCAN_DUPLICATES = false;

NobleDevice.is = function(peripheral) {
return true;
};

NobleDevice.emitter = new EventEmitter();

NobleDevice.onDiscover = function(peripheral) {
if (this.is(peripheral)) {
var device = new this(peripheral);

this.emitter.emit('discover', device);
}
};

NobleDevice.onStateChange = function(state) {
if (state === 'poweredOn' && this.emitter.listeners('discover').length > 0) {
this.startScanning();
}
};

NobleDevice.startScanning = function() {
noble.startScanning(this.SCAN_UUIDS, this.SCAN_DUPLICATES);
};

NobleDevice.stopScanning = function() {
noble.stopScanning();
};

NobleDevice.discoverAll = function(callback) {
this.emitter.addListener('discover', callback);

if (this.emitter.listeners('discover').length === 1) {
this.onDiscover = this.onDiscover.bind(this);
this.onStateChange = this.onStateChange.bind(this);

noble.on('discover', this.onDiscover);
noble.on('stateChange', this.onStateChange);

if (noble.state === 'poweredOn') {
this.startScanning();
}
}
};

NobleDevice.stopDiscoverAll = function(discoverCallback) {
this.emitter.removeListener('discover', discoverCallback);

if (this.emitter.listeners('discover').length === 0) {
noble.removeListener('discover', this.onDiscover);
noble.removeListener('stateChange', this.onStateChange);

this.stopScanning();
}
};

NobleDevice.discover = function(callback) {
var _this = this;
var onDiscover = function(device) {
_this.stopDiscoverAll(onDiscover);

callback(device);
};

callback._nobleDeviceOnDiscover = onDiscover;

this.discoverAll(onDiscover);
};

NobleDevice.stopDiscover = function(callback) {
var onDiscover = callback._nobleDeviceOnDiscover;

if (onDiscover) {
this.stopDiscoverAll(onDiscover);
}
};

NobleDevice.discoverWithFilter = function(filter, callback) {
var _this = this;
var onDiscoverWithFilter = function(device) {
if (filter(device)) {
_this.stopDiscoverAll(onDiscoverWithFilter);

callback(device);
}
};

this.discoverAll(onDiscoverWithFilter);
};

NobleDevice.discoverById = function(id, callback) {
this.discoverWithFilter(function(device) {
return (device.id === id);
}, callback);
};

// deprecated
NobleDevice.discoverByUuid = function(uuid, callback) {
this.discoverWithFilter(function(device) {
return (device.uuid === uuid);
}, callback);
};

NobleDevice.discoverByAddress = function(address, callback) {
this.discoverWithFilter(function(device) {
return (device.address === address);
}, callback);
};

NobleDevice.prototype.onDisconnect = function() {
this.connectedAndSetUp = false;
Expand Down
125 changes: 7 additions & 118 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,14 @@
var events = require('events');
var util = require('util');

var noble = require('noble');

var EventEmitter = events.EventEmitter;

var NobleDevice = require('./noble-device');

function Util() {
}

Util.inherits = function(constructor, superConstructor) {
util.inherits(constructor, superConstructor);

if (superConstructor === NobleDevice) {
constructor.SCAN_UUIDS = constructor.SCAN_UUIDS || [];
constructor.SCAN_DUPLICATES = constructor.SCAN_DUPLICATES || false;

constructor.is = constructor.is || function(peripheral) {
return true;
};

constructor.emitter = new EventEmitter();

constructor.onDiscover = function(peripheral) {
if (constructor.is(peripheral)) {
var device = new constructor(peripheral);

constructor.emitter.emit('discover', device);
}
};

constructor.onStateChange = function(state) {
if (state === 'poweredOn' && constructor.emitter.listeners('discover').length > 0) {
constructor.startScanning();
}
};

constructor.startScanning = function() {
noble.startScanning(constructor.SCAN_UUIDS, constructor.SCAN_DUPLICATES);
};

constructor.stopScanning = function() {
noble.stopScanning();
};

constructor.discoverAll = function(callback) {
constructor.emitter.addListener('discover', callback);

if (constructor.emitter.listeners('discover').length === 1) {
noble.on('discover', constructor.onDiscover);
noble.on('stateChange', constructor.onStateChange);

if (noble.state === 'poweredOn') {
constructor.startScanning();
}
}
};

constructor.stopDiscoverAll = function(discoverCallback) {
constructor.emitter.removeListener('discover', discoverCallback);

if (constructor.emitter.listeners('discover').length === 0) {
noble.removeListener('discover', constructor.onDiscover);
noble.removeListener('stateChange', constructor.onStateChange);

constructor.stopScanning();
}
};

constructor.discover = function(callback) {
var onDiscover = function(device) {
constructor.stopDiscoverAll(onDiscover);

callback(device);
};

callback._nobleDeviceOnDiscover = onDiscover;

constructor.discoverAll(onDiscover);
};

constructor.stopDiscover = function(callback) {
var onDiscover = callback._nobleDeviceOnDiscover;

if (onDiscover) {
constructor.stopDiscoverAll(onDiscover);
}
};

constructor.discoverWithFilter = function(filter, callback) {
var onDiscoverWithFilter = function(device) {
if (filter(device)) {
constructor.stopDiscoverAll(onDiscoverWithFilter);

callback(device);
}
};

constructor.discoverAll(onDiscoverWithFilter);
};

constructor.discoverById = function(id, callback) {
constructor.discoverWithFilter(function(device) {
return (device.id === id);
}, callback);
};

// deprecated
constructor.discoverByUuid = function(uuid, callback) {
constructor.discoverWithFilter(function(device) {
return (device.uuid === uuid);
}, callback);
};

constructor.discoverByAddress = function(address, callback) {
constructor.discoverWithFilter(function(device) {
return (device.address === address);
}, callback);
};
Util.inherits = function inherits(subClass, superClass) {
if (typeof superClass !== 'function' && superClass !== null) {
throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: { value: subClass, enumerable: false, writable: true, configurable: true }
});
if (superClass) Object.setPrototypeOf(subClass, superClass);
};

Util.mixin = function(constructor, mixin, includedMethods, excludedMethods) {
Expand Down