devicestack
Advanced tools
Comparing version 1.1.3 to 1.1.4
10
index.js
var index; | ||
if (typeof module.exports !== 'undefined') { | ||
index = module.exports; | ||
index = module.exports; | ||
} else { | ||
index = root.index = {}; | ||
index = root.index = {}; | ||
} | ||
@@ -20,5 +20,5 @@ | ||
try { | ||
index.SerialDevice = require('./lib/serialdevice'); | ||
index.SerialDeviceLoader = require('./lib/serialdeviceloader'); | ||
index.SerialDeviceGuider = require('./lib/serialdeviceguider'); | ||
index.SerialDevice = require('./lib/serialdevice'); | ||
index.SerialDeviceLoader = require('./lib/serialdeviceloader'); | ||
index.SerialDeviceGuider = require('./lib/serialdeviceguider'); | ||
} catch(e) { } |
@@ -1,5 +0,5 @@ | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2 | ||
, util = require('util') | ||
, _ = require('lodash') | ||
, uuid = require('node-uuid').v4; | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require('util'), | ||
_ = require('lodash'), | ||
uuid = require('node-uuid').v4; | ||
@@ -16,55 +16,55 @@ /** | ||
function Connection(device) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
}); | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
this.id = uuid(); | ||
this.device = device; | ||
this.id = uuid(); | ||
this.device = device; | ||
this.attributes = { id: this.id, device: this.device }; | ||
this.attributes = { id: this.id, device: this.device }; | ||
this.device.on('open', this.openHandle = function(callback) { | ||
if (self.log) self.log('connecting connection with id ' + self.id); | ||
self.emit('connecting', self); | ||
if (self.onConnecting) { | ||
self.onConnecting(function() { | ||
if (self.log) self.log('connect connection with id ' + self.id); | ||
self.emit('connect', self); | ||
if (callback) callback(null, self); | ||
}); | ||
} else { | ||
if (self.log) self.log('connect connection with id ' + self.id); | ||
self.emit('connect', self); | ||
if (callback) callback(null, self); | ||
} | ||
}); | ||
this.device.on('open', this.openHandle = function(callback) { | ||
if (self.log) self.log('connecting connection with id ' + self.id); | ||
self.emit('connecting', self); | ||
if (self.onConnecting) { | ||
self.onConnecting(function() { | ||
if (self.log) self.log('connect connection with id ' + self.id); | ||
self.emit('connect', self); | ||
if (callback) callback(null, self); | ||
}); | ||
} else { | ||
if (self.log) self.log('connect connection with id ' + self.id); | ||
self.emit('connect', self); | ||
if (callback) callback(null, self); | ||
} | ||
}); | ||
this.device.on('closing', this.closingHandle = function(callback) { | ||
self.close(callback); | ||
}); | ||
this.device.on('closing', this.closingHandle = function(callback) { | ||
self.close(callback); | ||
}); | ||
this.device.on('close', this.closeHandle = function(callback) { | ||
if (self.log) self.log('disconnect connection with id ' + self.id); | ||
self.emit('disconnect', self); | ||
self.removeAllListeners(); | ||
self.removeAllListeners('connect'); | ||
self.removeAllListeners('connecting'); | ||
self.removeAllListeners('disconnect'); | ||
self.removeAllListeners('disconnecting'); | ||
self.device.removeListener('open', self.openHandle); | ||
self.device.removeListener('closing', self.closingHandle); | ||
// if (callback) callback(null, self); | ||
}); | ||
this.device.on('close', this.closeHandle = function(callback) { | ||
if (self.log) self.log('disconnect connection with id ' + self.id); | ||
self.emit('disconnect', self); | ||
self.removeAllListeners(); | ||
self.removeAllListeners('connect'); | ||
self.removeAllListeners('connecting'); | ||
self.removeAllListeners('disconnect'); | ||
self.removeAllListeners('disconnecting'); | ||
self.device.removeListener('open', self.openHandle); | ||
self.device.removeListener('closing', self.closingHandle); | ||
// if (callback) callback(null, self); | ||
}); | ||
} | ||
@@ -86,9 +86,9 @@ | ||
Connection.prototype.set = function(data) { | ||
if (arguments.length === 2) { | ||
this.attributes[arguments[0]] = arguments[1]; | ||
} else { | ||
for(var m in data) { | ||
this.attributes[m] = data[m]; | ||
} | ||
if (arguments.length === 2) { | ||
this.attributes[arguments[0]] = arguments[1]; | ||
} else { | ||
for(var m in data) { | ||
this.attributes[m] = data[m]; | ||
} | ||
} | ||
}; | ||
@@ -105,3 +105,3 @@ | ||
Connection.prototype.get = function(attr) { | ||
return this.attributes[attr]; | ||
return this.attributes[attr]; | ||
}; | ||
@@ -117,12 +117,12 @@ | ||
Connection.prototype.close = function(callback) { | ||
var self = this; | ||
if (this.log) this.log('disconnecting connection with id ' + self.id); | ||
this.emit('disconnecting', this); | ||
if (this.onDisconnecting) { | ||
this.onDisconnecting(function() { | ||
self.device.close(callback, true); | ||
}); | ||
} else { | ||
this.device.close(callback, true); | ||
} | ||
var self = this; | ||
if (this.log) this.log('disconnecting connection with id ' + self.id); | ||
this.emit('disconnecting', this); | ||
if (this.onDisconnecting) { | ||
this.onDisconnecting(function() { | ||
self.device.close(callback, true); | ||
}); | ||
} else { | ||
this.device.close(callback, true); | ||
} | ||
}; | ||
@@ -132,8 +132,8 @@ | ||
Connection.prototype.toJSON = function() { | ||
var parse = JSON.deserialize || JSON.parse; | ||
var json = parse(JSON.stringify(this.attributes)); | ||
json.device = this.device ? this.device.toJSON() : undefined; | ||
return json; | ||
var parse = JSON.deserialize || JSON.parse; | ||
var json = parse(JSON.stringify(this.attributes)); | ||
json.device = this.device ? this.device.toJSON() : undefined; | ||
return json; | ||
}; | ||
module.exports = Connection; |
@@ -1,5 +0,5 @@ | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2 | ||
, util = require('util') | ||
, _ = require('lodash') | ||
, uuid = require('node-uuid').v4; | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require('util'), | ||
_ = require('lodash'), | ||
uuid = require('node-uuid').v4; | ||
@@ -11,21 +11,21 @@ /** | ||
function Device(Connection) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
}); | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
this.id = uuid(); | ||
this.Connection = Connection; | ||
this.id = uuid(); | ||
this.Connection = Connection; | ||
this.attributes = { id: this.id }; | ||
this.attributes = { id: this.id }; | ||
} | ||
@@ -47,9 +47,9 @@ | ||
Device.prototype.set = function(data) { | ||
if (arguments.length === 2) { | ||
this.attributes[arguments[0]] = arguments[1]; | ||
} else { | ||
for(var m in data) { | ||
this.attributes[m] = data[m]; | ||
} | ||
if (arguments.length === 2) { | ||
this.attributes[arguments[0]] = arguments[1]; | ||
} else { | ||
for(var m in data) { | ||
this.attributes[m] = data[m]; | ||
} | ||
} | ||
}; | ||
@@ -66,3 +66,3 @@ | ||
Device.prototype.get = function(attr) { | ||
return this.attributes[attr]; | ||
return this.attributes[attr]; | ||
}; | ||
@@ -78,8 +78,8 @@ | ||
Device.prototype.connect = function(callback) { | ||
if (this.Connection) { | ||
if (this.log) this.log('opening device with id ' + this.id); | ||
this.emit('opening'); | ||
this.connection = new this.Connection(this); | ||
} | ||
this.open(callback); | ||
if (this.Connection) { | ||
if (this.log) this.log('opening device with id ' + this.id); | ||
this.emit('opening'); | ||
this.connection = new this.Connection(this); | ||
} | ||
this.open(callback); | ||
}; | ||
@@ -94,10 +94,10 @@ | ||
Device.prototype.disconnect = function(callback) { | ||
var self = this; | ||
var self = this; | ||
if (this.connection && this.connection.close) { | ||
if (this.log) this.log('closing device with id ' + this.id); | ||
this.emit('closing', callback); | ||
} else { | ||
this.close(callback); | ||
} | ||
if (this.connection && this.connection.close) { | ||
if (this.log) this.log('closing device with id ' + this.id); | ||
this.emit('closing', callback); | ||
} else { | ||
this.close(callback); | ||
} | ||
}; | ||
@@ -110,3 +110,3 @@ | ||
Device.prototype.send = function(data) { | ||
this.emit('send', data); | ||
this.emit('send', data); | ||
}; | ||
@@ -116,8 +116,8 @@ | ||
Device.prototype.toJSON = function() { | ||
var parse = JSON.deserialize || JSON.parse; | ||
var json = parse(JSON.stringify(this.attributes)); | ||
json.connection = this.connection ? { id: this.connection.id } : undefined; | ||
return json; | ||
var parse = JSON.deserialize || JSON.parse; | ||
var json = parse(JSON.stringify(this.attributes)); | ||
json.connection = this.connection ? { id: this.connection.id } : undefined; | ||
return json; | ||
}; | ||
module.exports = Device; |
@@ -1,5 +0,5 @@ | ||
var util = require('util') | ||
, EventEmitter2 = require('eventemitter2').EventEmitter2 | ||
, _ = require('lodash') | ||
, async = require('async'); | ||
var util = require('util'), | ||
EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
_ = require('lodash'), | ||
async = require('async'); | ||
@@ -13,88 +13,88 @@ /** | ||
function DeviceGuider(deviceLoader) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
}); | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
this.deviceLoader = deviceLoader; | ||
this.lookupStarted = false; | ||
this.currentState = { | ||
connectionMode: 'manualconnect', | ||
plugged: [], | ||
connected: [], | ||
getDevice: function(id) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.id === id; | ||
}); | ||
}, | ||
getConnection: function(id) { | ||
var found = _.find(self.currentState.connected, function(dc) { | ||
return dc.connection.id === id; | ||
}); | ||
this.deviceLoader = deviceLoader; | ||
this.lookupStarted = false; | ||
this.currentState = { | ||
connectionMode: 'manualconnect', | ||
plugged: [], | ||
connected: [], | ||
getDevice: function(id) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.id === id; | ||
}); | ||
}, | ||
getConnection: function(id) { | ||
var found = _.find(self.currentState.connected, function(dc) { | ||
return dc.connection.id === id; | ||
}); | ||
return found ? found.connection : null; | ||
}, | ||
getDeviceByPort: function(port) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.get('portName') === port; | ||
}); | ||
}, | ||
getConnectedDevice: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.id === id; | ||
}); | ||
}, | ||
getConnectedDeviceByPort: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.get('portName') === port; | ||
}); | ||
}, | ||
getDeviceByConnection: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.connection.id === id; | ||
}); | ||
}, | ||
getConnectionByDevice: function(id) { | ||
var found = _.find(self.currentState.connected, function(dc) { | ||
return dc.id === id; | ||
}); | ||
return found ? found.connection : null; | ||
}, | ||
getDeviceByPort: function(port) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.get('portName') === port; | ||
}); | ||
}, | ||
getConnectedDevice: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.id === id; | ||
}); | ||
}, | ||
getConnectedDeviceByPort: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.get('portName') === port; | ||
}); | ||
}, | ||
getDeviceByConnection: function(id) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.connection.id === id; | ||
}); | ||
}, | ||
getConnectionByDevice: function(id) { | ||
var found = _.find(self.currentState.connected, function(dc) { | ||
return dc.id === id; | ||
}); | ||
return found ? found.connection : null; | ||
} | ||
}; | ||
return found ? found.connection : null; | ||
} | ||
}; | ||
this.deviceLoader.on('plug', function(device) { | ||
self.currentState.plugged.push(device); | ||
this.deviceLoader.on('plug', function(device) { | ||
self.currentState.plugged.push(device); | ||
if (self.log) self.log('plug device with id ' + device.id); | ||
self.emit('plug', device); | ||
if (self.log) self.log('plug device with id ' + device.id); | ||
self.emit('plug', device); | ||
if (self.currentState.connectionMode !== 'manualconnect') { | ||
if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) { | ||
self.connectDevice(device); | ||
} | ||
} | ||
}); | ||
if (self.currentState.connectionMode !== 'manualconnect') { | ||
if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) { | ||
self.connectDevice(device); | ||
} | ||
} | ||
this.deviceLoader.on('unplug', function(device) { | ||
self.currentState.plugged = _.reject(self.currentState.plugged, function(d) { | ||
return d.id === device.id; | ||
}); | ||
self.currentState.connected = _.reject(self.currentState.connected, function(dc) { | ||
return dc.id === device.id; | ||
}); | ||
this.deviceLoader.on('unplug', function(device) { | ||
self.currentState.plugged = _.reject(self.currentState.plugged, function(d) { | ||
return d.id === device.id; | ||
}); | ||
self.currentState.connected = _.reject(self.currentState.connected, function(dc) { | ||
return dc.id === device.id; | ||
}); | ||
if (self.log) self.log('unplug device with id ' + device.id); | ||
self.emit('unplug', device); | ||
}); | ||
if (self.log) self.log('unplug device with id ' + device.id); | ||
self.emit('unplug', device); | ||
}); | ||
} | ||
@@ -107,11 +107,11 @@ | ||
DeviceGuider.prototype.on = function(eventname, callback) { | ||
if (this.deviceLoader && (eventname === 'plug' || eventname === 'unplug')) { | ||
this.deviceLoader.on.apply(this.deviceLoader, _.toArray(arguments)); | ||
if (!this.deviceLoader.isRunning) { | ||
this.lookupStarted = true; | ||
this.deviceLoader.startLookup(); | ||
} | ||
} else { | ||
EventEmitter2.prototype.on.apply(this, _.toArray(arguments)); | ||
if (this.deviceLoader && (eventname === 'plug' || eventname === 'unplug')) { | ||
this.deviceLoader.on.apply(this.deviceLoader, _.toArray(arguments)); | ||
if (!this.deviceLoader.isRunning) { | ||
this.lookupStarted = true; | ||
this.deviceLoader.startLookup(); | ||
} | ||
} else { | ||
EventEmitter2.prototype.on.apply(this, _.toArray(arguments)); | ||
} | ||
}; | ||
@@ -121,11 +121,11 @@ | ||
DeviceGuider.prototype.removeListener = function(eventname, callback) { | ||
if (this.deviceLoader && (eventname === 'plug' || eventname === 'unplug')) { | ||
this.deviceLoader.removeListener.apply(this.deviceLoader, _.toArray(arguments)); | ||
if (this.deviceLoader.listeners('plug').length === 0 && this.deviceLoader.listeners('unplug').length === 0 ) { | ||
this.lookupStarted = false; | ||
this.deviceLoader.stopLookup(); | ||
} | ||
} else { | ||
EventEmitter2.prototype.removeListener.apply(this, _.toArray(arguments)); | ||
if (this.deviceLoader && (eventname === 'plug' || eventname === 'unplug')) { | ||
this.deviceLoader.removeListener.apply(this.deviceLoader, _.toArray(arguments)); | ||
if (this.deviceLoader.listeners('plug').length === 0 && this.deviceLoader.listeners('unplug').length === 0 ) { | ||
this.lookupStarted = false; | ||
this.deviceLoader.stopLookup(); | ||
} | ||
} else { | ||
EventEmitter2.prototype.removeListener.apply(this, _.toArray(arguments)); | ||
} | ||
}; | ||
@@ -139,3 +139,3 @@ | ||
DeviceGuider.prototype.checkConnectionMode = function(mode) { | ||
return mode !== this.currentState.connectionMode; | ||
return mode !== this.currentState.connectionMode; | ||
}; | ||
@@ -149,12 +149,12 @@ | ||
DeviceGuider.prototype.changeConnectionMode = function(mode) { | ||
var self = this; | ||
var self = this; | ||
var somethingChanged = this.checkConnectionMode(mode); | ||
this.currentState.connectionMode = mode; | ||
var somethingChanged = this.checkConnectionMode(mode); | ||
this.currentState.connectionMode = mode; | ||
if (somethingChanged) { | ||
this.emit('connectionModeChanged', this.currentState.connectionMode); | ||
} | ||
if (somethingChanged) { | ||
this.emit('connectionModeChanged', this.currentState.connectionMode); | ||
} | ||
return somethingChanged; | ||
return somethingChanged; | ||
}; | ||
@@ -169,17 +169,17 @@ | ||
DeviceGuider.prototype.getCurrentState = function(callback) { | ||
var self = this; | ||
if (this.currentState.plugged.length > 0) { | ||
callback(null, this.currentState); | ||
var self = this; | ||
if (this.currentState.plugged.length > 0) { | ||
callback(null, this.currentState); | ||
} else { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(function(err, devices) { | ||
callback(null, self.currentState); | ||
}); | ||
} else { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(function(err, devices) { | ||
callback(null, self.currentState); | ||
}); | ||
} else { | ||
this.deviceLoader.trigger(function(err, devices) { | ||
callback(null, self.currentState); | ||
}); | ||
} | ||
this.deviceLoader.trigger(function(err, devices) { | ||
callback(null, self.currentState); | ||
}); | ||
} | ||
} | ||
}; | ||
@@ -193,19 +193,19 @@ | ||
DeviceGuider.prototype.autoconnect = function() { | ||
var self = this; | ||
var self = this; | ||
if (this.changeConnectionMode('autoconnect')) { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(); | ||
} | ||
if (this.changeConnectionMode('autoconnect')) { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(); | ||
} | ||
var toConnect = _.reject(this.currentState.plugged, function(d) { | ||
return _.find(self.currentState.connected, function(c) { | ||
return c.id == d.id; | ||
}); | ||
}); | ||
var toConnect = _.reject(this.currentState.plugged, function(d) { | ||
return _.find(self.currentState.connected, function(c) { | ||
return c.id == d.id; | ||
}); | ||
}); | ||
_.each(toConnect, function(dev) { | ||
self.connectDevice(dev); | ||
}); | ||
} | ||
_.each(toConnect, function(dev) { | ||
self.connectDevice(dev); | ||
}); | ||
} | ||
}; | ||
@@ -220,28 +220,28 @@ | ||
DeviceGuider.prototype.manualconnect = function(holdConnections, callback) { | ||
var self = this; | ||
var self = this; | ||
if (arguments.length === 1) { | ||
if (_.isFunction(holdConnections)) { | ||
callback = holdConnections; | ||
holdConnections = false; | ||
} | ||
if (arguments.length === 1) { | ||
if (_.isFunction(holdConnections)) { | ||
callback = holdConnections; | ||
holdConnections = false; | ||
} | ||
} | ||
if (this.changeConnectionMode('manualconnect')) { | ||
if (this.currentState.connected.length === 0) { | ||
if (callback) { return callback(null); } | ||
} | ||
if (this.changeConnectionMode('manualconnect')) { | ||
if (this.currentState.connected.length === 0) { | ||
if (callback) { return callback(null); } | ||
} | ||
if (!holdConnections) { | ||
async.forEachSeries(this.currentState.connected, function(dc, clb) { | ||
self.closeConnection(dc.connection, clb); | ||
}, function(err) { | ||
if (callback) { return callback(err); } | ||
}); | ||
} else { | ||
if (callback) { return callback(null); } | ||
} | ||
if (!holdConnections) { | ||
async.forEachSeries(this.currentState.connected, function(dc, clb) { | ||
self.closeConnection(dc.connection, clb); | ||
}, function(err) { | ||
if (callback) { return callback(err); } | ||
}); | ||
} else { | ||
if (callback) { return callback(null); } | ||
if (callback) { return callback(null); } | ||
} | ||
} else { | ||
if (callback) { return callback(null); } | ||
} | ||
}; | ||
@@ -257,21 +257,21 @@ | ||
DeviceGuider.prototype.autoconnectOne = function(callback) { | ||
var self = this; | ||
var self = this; | ||
function doIt() { | ||
if (self.currentState.plugged.length > 0 && self.currentState.connected.length === 0) { | ||
self.connectDevice(self.currentState.plugged[0], callback); | ||
} else { | ||
if (callback) callback('No device available!'); | ||
} | ||
function doIt() { | ||
if (self.currentState.plugged.length > 0 && self.currentState.connected.length === 0) { | ||
self.connectDevice(self.currentState.plugged[0], callback); | ||
} else { | ||
if (callback) callback('No device available!'); | ||
} | ||
} | ||
if (this.changeConnectionMode('autoconnectOne')) { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(doIt); | ||
} else { | ||
doIt(); | ||
} | ||
if (this.changeConnectionMode('autoconnectOne')) { | ||
if (!this.deviceLoader.isRunning && !this.lookupStarted) { | ||
this.deviceLoader.startLookup(doIt); | ||
} else { | ||
if (callback) { return callback(null); } | ||
doIt(); | ||
} | ||
} else { | ||
if (callback) { return callback(null); } | ||
} | ||
}; | ||
@@ -286,34 +286,34 @@ | ||
DeviceGuider.prototype.connectDevice = function(deviceOrId, callback) { | ||
var self = this | ||
, device = deviceOrId; | ||
var self = this, | ||
device = deviceOrId; | ||
if (_.isString(deviceOrId)) { | ||
device = this.currentState.getDevice(deviceOrId); | ||
if (_.isString(deviceOrId)) { | ||
device = this.currentState.getDevice(deviceOrId); | ||
} | ||
device.connect(function(err, connection) { | ||
if (err) { | ||
if (self.log) self.log('error calling connect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, connection); | ||
} | ||
return; | ||
} | ||
self.currentState.connected.push(device); | ||
device.connect(function(err, connection) { | ||
if (err) { | ||
if (self.log) self.log('error calling connect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, connection); | ||
} | ||
return; | ||
} | ||
self.currentState.connected.push(device); | ||
connection.on('disconnect', function(conn) { | ||
self.currentState.connected = _.reject(self.currentState.connected, function(dc) { | ||
return dc.connection.id === conn.id; | ||
}); | ||
connection.on('disconnect', function(conn) { | ||
self.currentState.connected = _.reject(self.currentState.connected, function(dc) { | ||
return dc.connection.id === conn.id; | ||
}); | ||
if (self.log) self.log('disconnect device with id ' + conn.device.id + ' and connection id ' + conn.id); | ||
self.emit('disconnect', conn); | ||
}); | ||
if (self.log) self.log('disconnect device with id ' + conn.device.id + ' and connection id ' + conn.id); | ||
self.emit('disconnect', conn); | ||
}); | ||
if (self.log) self.log('connect device with id ' + device.id + ' and connection id ' + connection.id); | ||
self.emit('connect', connection); | ||
if (self.log) self.log('connect device with id ' + device.id + ' and connection id ' + connection.id); | ||
self.emit('connect', connection); | ||
if (callback) callback(err, connection); | ||
}); | ||
if (callback) callback(err, connection); | ||
}); | ||
}; | ||
@@ -328,27 +328,27 @@ | ||
DeviceGuider.prototype.disconnectDevice = function(deviceOrId, callback) { | ||
var self = this | ||
, device = deviceOrId; | ||
var self = this, | ||
device = deviceOrId; | ||
if (_.isString(deviceOrId)) { | ||
device = this.currentState.getConnectedDevice(deviceOrId); | ||
} | ||
if (_.isString(deviceOrId)) { | ||
device = this.currentState.getConnectedDevice(deviceOrId); | ||
} | ||
if (!device) { | ||
if (this.log) this.log('Device not connected!'); | ||
self.emit('error', 'Device not connected!'); | ||
if (callback) { return callback('Device not connected!'); } | ||
if (!device) { | ||
if (this.log) this.log('Device not connected!'); | ||
self.emit('error', 'Device not connected!'); | ||
if (callback) { return callback('Device not connected!'); } | ||
} | ||
device.disconnect(function(err) { | ||
if (err) { | ||
if (self.log) self.log('error calling disconnect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, device.connection); | ||
} | ||
return; | ||
} | ||
device.disconnect(function(err) { | ||
if (err) { | ||
if (self.log) self.log('error calling disconnect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, device.connection); | ||
} | ||
return; | ||
} | ||
if (callback) callback(err, device.connection); | ||
}); | ||
if (callback) callback(err, device.connection); | ||
}); | ||
}; | ||
@@ -363,23 +363,23 @@ | ||
DeviceGuider.prototype.closeConnection = function(connectionOrId, callback) { | ||
var self = this | ||
, connection = connectionOrId; | ||
var self = this, | ||
connection = connectionOrId; | ||
if (_.isString(connectionOrId)) { | ||
connection = this.currentState.getConnection(connectionOrId); | ||
if (_.isString(connectionOrId)) { | ||
connection = this.currentState.getConnection(connectionOrId); | ||
} | ||
connection.close(function(err) { | ||
if (err) { | ||
if (self.log) self.log('error calling disconnect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, connection); | ||
} | ||
return; | ||
} | ||
connection.close(function(err) { | ||
if (err) { | ||
if (self.log) self.log('error calling disconnect' + JSON.stringify(err)); | ||
self.emit('error', err); | ||
if (callback) { | ||
return callback(err, connection); | ||
} | ||
return; | ||
} | ||
if (callback) callback(err, connection); | ||
}); | ||
if (callback) callback(err, connection); | ||
}); | ||
}; | ||
module.exports = DeviceGuider; |
@@ -1,4 +0,4 @@ | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2 | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require('util'), | ||
_ = require('lodash'); | ||
@@ -9,21 +9,21 @@ /** | ||
function DeviceLoader() { | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
}); | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
this.lookupIntervalId = null; | ||
this.oldDevices = []; | ||
this.lookupIntervalId = null; | ||
this.oldDevices = []; | ||
this.isRunning = false; | ||
this.isRunning = false; | ||
} | ||
@@ -41,30 +41,30 @@ | ||
DeviceLoader.prototype.trigger = function(callback) { | ||
var self = this; | ||
this.lookup(function(err, devices) { | ||
if (err) { | ||
if (self.log) self.log(err); | ||
if (callback) callback(err, devices); | ||
return; | ||
} | ||
var self = this; | ||
this.lookup(function(err, devices) { | ||
if (err) { | ||
if (self.log) self.log(err); | ||
if (callback) callback(err, devices); | ||
return; | ||
} | ||
if (self.oldDevices.length !== devices.length) { | ||
var devs = []; | ||
if (self.oldDevices.length > devices.length) { | ||
devs = _.difference(self.oldDevices, devices); | ||
_.each(devs, function(d) { | ||
if (self.log) self.log('unplug device with id ' + device.id); | ||
self.emit('unplug', d); | ||
}); | ||
} else { | ||
devs = _.difference(devices, self.oldDevices); | ||
_.each(devs, function(d) { | ||
if (self.log) self.log('plug device with id ' + device.id); | ||
self.emit('plug', d); | ||
}); | ||
} | ||
if (self.oldDevices.length !== devices.length) { | ||
var devs = []; | ||
if (self.oldDevices.length > devices.length) { | ||
devs = _.difference(self.oldDevices, devices); | ||
_.each(devs, function(d) { | ||
if (self.log) self.log('unplug device with id ' + device.id); | ||
self.emit('unplug', d); | ||
}); | ||
} else { | ||
devs = _.difference(devices, self.oldDevices); | ||
_.each(devs, function(d) { | ||
if (self.log) self.log('plug device with id ' + device.id); | ||
self.emit('plug', d); | ||
}); | ||
} | ||
self.oldDevices = devices; | ||
} | ||
if (callback) callback(err, devices); | ||
}); | ||
self.oldDevices = devices; | ||
} | ||
if (callback) callback(err, devices); | ||
}); | ||
}; | ||
@@ -79,30 +79,30 @@ | ||
DeviceLoader.prototype.startLookup = function(interval, callback) { | ||
if (this.lookupIntervalId) { | ||
this.stopLookup(); | ||
} | ||
if (this.lookupIntervalId) { | ||
this.stopLookup(); | ||
} | ||
this.isRunning = true; | ||
this.isRunning = true; | ||
if (!callback && _.isFunction(interval)) { | ||
callback = interval; | ||
interval = null; | ||
} | ||
if (!callback && _.isFunction(interval)) { | ||
callback = interval; | ||
interval = null; | ||
} | ||
var self = this; | ||
interval = interval || 500; | ||
var self = this; | ||
interval = interval || 500; | ||
this.oldDevices = []; | ||
this.trigger(function(err, devices) { | ||
this.oldDevices = []; | ||
this.trigger(function(err, devices) { | ||
var triggering = false; | ||
self.lookupIntervalId = setInterval(function() { | ||
if (triggering) return; | ||
triggering = true; | ||
self.trigger(function() { | ||
triggering = false; | ||
}); | ||
}, interval); | ||
var triggering = false; | ||
self.lookupIntervalId = setInterval(function() { | ||
if (triggering) return; | ||
triggering = true; | ||
self.trigger(function() { | ||
triggering = false; | ||
}); | ||
}, interval); | ||
if (callback) { callback(err, devices); } | ||
}); | ||
if (callback) { callback(err, devices); } | ||
}); | ||
}; | ||
@@ -114,9 +114,9 @@ | ||
DeviceLoader.prototype.stopLookup = function() { | ||
if (this.lookupIntervalId) { | ||
clearInterval(this.lookupIntervalId); | ||
this.lookupIntervalId = null; | ||
this.isRunning = false; | ||
} | ||
if (this.lookupIntervalId) { | ||
clearInterval(this.lookupIntervalId); | ||
this.lookupIntervalId = null; | ||
this.isRunning = false; | ||
} | ||
}; | ||
module.exports = DeviceLoader; |
@@ -1,4 +0,4 @@ | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2 | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require('util'), | ||
_ = require('lodash'); | ||
@@ -16,57 +16,57 @@ /** | ||
function FrameHandler(deviceOrFrameHandler) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
// call super class | ||
EventEmitter2.call(this, { | ||
wildcard: true, | ||
delimiter: ':', | ||
maxListeners: 1000 // default would be 10! | ||
}); | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (this.log) { | ||
this.log = _.wrap(this.log, function(func, msg) { | ||
func(self.constructor.name + ': ' + msg); | ||
}); | ||
} | ||
if (deviceOrFrameHandler) { | ||
this.incomming = []; | ||
deviceOrFrameHandler.on('receive', function (frame) { | ||
var unwrappedFrame; | ||
if (self.analyzeNextFrame) { | ||
self.incomming = self.incomming.concat(Array.prototype.slice.call(frame, 0)); | ||
} else { | ||
if (self.unwrapFrame) { | ||
unwrappedFrame = self.unwrapFrame(_.clone(frame)); | ||
if (self.log) self.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
self.emit('receive', unwrappedFrame); | ||
} else { | ||
if (self.log) self.log('receive frame: ' + frame.toHexDebug()); | ||
self.emit('receive', frame); | ||
} | ||
} | ||
}); | ||
if (deviceOrFrameHandler) { | ||
this.incomming = []; | ||
deviceOrFrameHandler.on('receive', function (frame) { | ||
var unwrappedFrame; | ||
if (self.analyzeNextFrame) { | ||
self.incomming = self.incomming.concat(Array.prototype.slice.call(frame, 0)); | ||
} else { | ||
if (self.unwrapFrame) { | ||
unwrappedFrame = self.unwrapFrame(_.clone(frame)); | ||
if (self.log) self.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
self.emit('receive', unwrappedFrame); | ||
} else { | ||
if (self.log) self.log('receive frame: ' + frame.toHexDebug()); | ||
self.emit('receive', frame); | ||
} | ||
} | ||
}); | ||
deviceOrFrameHandler.on('close', function() { | ||
if (self.log) self.log('close'); | ||
self.stop(); | ||
self.emit('close'); | ||
self.removeAllListeners(); | ||
deviceOrFrameHandler.removeAllListeners(); | ||
deviceOrFrameHandler.removeAllListeners('receive'); | ||
}); | ||
} | ||
deviceOrFrameHandler.on('close', function() { | ||
if (self.log) self.log('close'); | ||
self.stop(); | ||
self.emit('close'); | ||
self.removeAllListeners(); | ||
deviceOrFrameHandler.removeAllListeners(); | ||
deviceOrFrameHandler.removeAllListeners('receive'); | ||
}); | ||
this.on('send', function(frame) { | ||
if (self.wrapFrame) { | ||
var wrappedFrame = self.wrapFrame(_.clone(frame)); | ||
if (self.log) self.log('send wrapped frame: ' + wrappedFrame.toHexDebug()); | ||
deviceOrFrameHandler.send(wrappedFrame); | ||
} else { | ||
if (self.log) self.log('send frame: ' + frame.toHexDebug()); | ||
deviceOrFrameHandler.send(frame); | ||
} | ||
}); | ||
this.on('send', function(frame) { | ||
if (self.wrapFrame) { | ||
var wrappedFrame = self.wrapFrame(_.clone(frame)); | ||
if (self.log) self.log('send wrapped frame: ' + wrappedFrame.toHexDebug()); | ||
deviceOrFrameHandler.send(wrappedFrame); | ||
} else { | ||
if (self.log) self.log('send frame: ' + frame.toHexDebug()); | ||
deviceOrFrameHandler.send(frame); | ||
} | ||
}); | ||
this.start(); | ||
this.start(); | ||
} | ||
@@ -84,41 +84,41 @@ | ||
FrameHandler.prototype.start = function(interval, callback) { | ||
if (!callback && _.isFunction(interval)) { | ||
callback = interval; | ||
interval = null; | ||
} | ||
if (!callback && _.isFunction(interval)) { | ||
callback = interval; | ||
interval = null; | ||
} | ||
if (!this.analyzeNextFrame) { | ||
if (callback) { callback(null); } | ||
return; | ||
} | ||
if (!this.analyzeNextFrame) { | ||
if (callback) { callback(null); } | ||
return; | ||
} | ||
if (this.lookupIntervalId) { | ||
this.stopLookup(); | ||
} | ||
if (this.lookupIntervalId) { | ||
this.stopLookup(); | ||
} | ||
var self = this; | ||
interval = interval || 50; | ||
var self = this; | ||
interval = interval || 50; | ||
var analyzing = false; | ||
this.lookupIntervalId = setInterval(function() { | ||
if (analyzing) { | ||
return; | ||
} | ||
var analyzing = false; | ||
this.lookupIntervalId = setInterval(function() { | ||
if (analyzing) { | ||
return; | ||
} | ||
analyzing = true; | ||
var nextFrame; | ||
while ((nextFrame = self.analyzeNextFrame(self.incomming))) { | ||
if (self.unwrapFrame) { | ||
var unwrappedFrame = self.unwrapFrame(_.clone(nextFrame)); | ||
if (self.log) self.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
self.emit('receive', unwrappedFrame); | ||
} else { | ||
if (self.log) self.log('receive frame: ' + nextFrame.toHexDebug()); | ||
self.emit('receive', nextFrame); | ||
} | ||
} | ||
analyzing = false; | ||
}, interval); | ||
analyzing = true; | ||
var nextFrame; | ||
while ((nextFrame = self.analyzeNextFrame(self.incomming))) { | ||
if (self.unwrapFrame) { | ||
var unwrappedFrame = self.unwrapFrame(_.clone(nextFrame)); | ||
if (self.log) self.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
self.emit('receive', unwrappedFrame); | ||
} else { | ||
if (self.log) self.log('receive frame: ' + nextFrame.toHexDebug()); | ||
self.emit('receive', nextFrame); | ||
} | ||
} | ||
analyzing = false; | ||
}, interval); | ||
if (callback) { callback(null); } | ||
if (callback) { callback(null); } | ||
}; | ||
@@ -130,6 +130,6 @@ | ||
FrameHandler.prototype.stop = function() { | ||
if (this.lookupIntervalId) { | ||
clearInterval(this.lookupIntervalId); | ||
this.lookupIntervalId = null; | ||
} | ||
if (this.lookupIntervalId) { | ||
clearInterval(this.lookupIntervalId); | ||
this.lookupIntervalId = null; | ||
} | ||
}; | ||
@@ -142,5 +142,5 @@ | ||
FrameHandler.prototype.send = function(data) { | ||
this.emit('send', data); | ||
this.emit('send', data); | ||
}; | ||
module.exports = FrameHandler; |
@@ -1,5 +0,5 @@ | ||
var sp = require("serialport") | ||
, Device = require('./device') | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var sp = require("serialport"), | ||
Device = require('./device'), | ||
util = require('util'), | ||
_ = require('lodash'); | ||
@@ -14,9 +14,8 @@ /** | ||
function SerialDevice(port, settings, Connection) { | ||
var self = this; | ||
// call super class | ||
Device.call(this, Connection); | ||
// call super class | ||
Device.call(this, Connection); | ||
this.set('portName', port); | ||
this.set('settings', settings); | ||
this.set('portName', port); | ||
this.set('settings', settings); | ||
} | ||
@@ -33,29 +32,29 @@ | ||
SerialDevice.prototype.open = function(callback) { | ||
var self = this; | ||
var self = this; | ||
this.serialPort = new sp.SerialPort(this.get('portName'), this.get('settings')); | ||
this.serialPort = new sp.SerialPort(this.get('portName'), this.get('settings')); | ||
this.serialPort.on('open', function() { | ||
if (self.log) self.log('open device with id ' + self.id); | ||
self.emit('open', callback); | ||
if (!self.connection && callback) callback(null); | ||
}); | ||
this.serialPort.on('open', function() { | ||
if (self.log) self.log('open device with id ' + self.id); | ||
self.emit('open', callback); | ||
if (!self.connection && callback) callback(null); | ||
}); | ||
this.serialPort.on('close', function() { | ||
if (self.log) self.log('close device with id ' + self.id); | ||
self.emit('close'); | ||
self.removeAllListeners(); | ||
self.serialPort.removeAllListeners(); | ||
self.serialPort.removeAllListeners('open'); | ||
}); | ||
this.serialPort.on('close', function() { | ||
if (self.log) self.log('close device with id ' + self.id); | ||
self.emit('close'); | ||
self.removeAllListeners(); | ||
self.serialPort.removeAllListeners(); | ||
self.serialPort.removeAllListeners('open'); | ||
}); | ||
this.serialPort.on('data', function(data) { | ||
if (self.log) self.log('<< ' + data.toHexDebug()); | ||
self.emit('receive', data.toArray()); | ||
}); | ||
this.serialPort.on('data', function(data) { | ||
if (self.log) self.log('<< ' + data.toHexDebug()); | ||
self.emit('receive', data.toArray()); | ||
}); | ||
this.on('send', function(data) { | ||
if (self.log) self.log('>> ' + data.toHexDebug()); | ||
self.serialPort.write(data.toBuffer()); | ||
}); | ||
this.on('send', function(data) { | ||
if (self.log) self.log('>> ' + data.toHexDebug()); | ||
self.serialPort.write(data.toBuffer()); | ||
}); | ||
}; | ||
@@ -70,8 +69,8 @@ | ||
SerialDevice.prototype.close = function(callback, fire) { | ||
var self = this; | ||
this.serialPort.close(function(err) { | ||
if (callback && (!self.connection || fire)) callback(err); | ||
}); | ||
var self = this; | ||
this.serialPort.close(function(err) { | ||
if (callback && (!self.connection || fire)) callback(err); | ||
}); | ||
}; | ||
module.exports = SerialDevice; |
@@ -1,4 +0,4 @@ | ||
var DeviceGuider = require('./deviceguider') | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var DeviceGuider = require('./deviceguider'), | ||
util = require('util'), | ||
_ = require('lodash'); | ||
@@ -12,18 +12,18 @@ /** | ||
function SerialDeviceGuider(deviceLoader) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
DeviceGuider.call(this, deviceLoader); | ||
// call super class | ||
DeviceGuider.call(this, deviceLoader); | ||
this.currentState.getDeviceByPort = function(port) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.get('portName') === port; | ||
}); | ||
}; | ||
this.currentState.getConnectedDeviceByPort = function(port) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.get('portName') === port; | ||
}); | ||
}; | ||
this.currentState.getDeviceByPort = function(port) { | ||
return _.find(self.currentState.plugged, function(d) { | ||
return d.get('portName') === port; | ||
}); | ||
}; | ||
this.currentState.getConnectedDeviceByPort = function(port) { | ||
return _.find(self.currentState.connected, function(dc) { | ||
return dc.get('portName') === port; | ||
}); | ||
}; | ||
} | ||
@@ -40,3 +40,3 @@ | ||
SerialDeviceGuider.prototype.connect = function(port, callback) { | ||
this.connectDevice(new this.deviceLoader.Device(port), callback); | ||
this.connectDevice(new this.deviceLoader.Device(port), callback); | ||
}; | ||
@@ -51,6 +51,6 @@ | ||
SerialDeviceGuider.prototype.disconnect = function(port, callback) { | ||
var device = this.currentState.getConnectedDeviceByPort(port); | ||
this.disconnectDevice(device, callback); | ||
var device = this.currentState.getConnectedDeviceByPort(port); | ||
this.disconnectDevice(device, callback); | ||
}; | ||
module.exports = SerialDeviceGuider; |
@@ -1,5 +0,5 @@ | ||
var sp = require("serialport") | ||
, DeviceLoader = require('./deviceloader') | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var sp = require("serialport"), | ||
DeviceLoader = require('./deviceloader'), | ||
util = require('util'), | ||
_ = require('lodash'); | ||
@@ -11,8 +11,7 @@ /** | ||
function SerialDeviceLoader(Device) { | ||
var self = this; | ||
// call super class | ||
DeviceLoader.call(this); | ||
// call super class | ||
DeviceLoader.call(this); | ||
this.Device = Device; | ||
this.Device = Device; | ||
} | ||
@@ -28,16 +27,16 @@ | ||
SerialDeviceLoader.prototype.lookup = function(callback) { | ||
var self = this; | ||
sp.list(function(err, ports) { | ||
if (err) { return callback(err); } | ||
var self = this; | ||
sp.list(function(err, ports) { | ||
if (err) { return callback(err); } | ||
var resPorts = self.filter(ports); | ||
var resPorts = self.filter(ports); | ||
var devices = _.map(resPorts, function(p) { | ||
return new self.Device(p.comName); | ||
}) || []; | ||
var devices = _.map(resPorts, function(p) { | ||
return new self.Device(p.comName); | ||
}) || []; | ||
callback(null, devices); | ||
}); | ||
callback(null, devices); | ||
}); | ||
}; | ||
module.exports = SerialDeviceLoader; |
{ | ||
"name": "devicestack", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "This module helps you to represent a device and its protocol.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -318,2 +318,6 @@ <pre> | ||
### array | toHexString | ||
Converts a byte array to a hex string. | ||
[0x01, 0x00].toHexString() // returns '0100' | ||
### buffer | toArray | ||
@@ -331,2 +335,6 @@ Converts a buffer object to a byte array. | ||
### string | toArray | ||
Converts a hex string to a byte array. | ||
'0100'.toArray() // returns [0x01, 0x00] | ||
### enum | ||
@@ -333,0 +341,0 @@ The Enum type is automatically registered in node.js. |
@@ -12,3 +12,2 @@ var Device = require('../test/fixture/device'); | ||
console.log('closed ' + counterClose + ' times!'); | ||
}); | ||
@@ -15,0 +14,0 @@ device.on('open', function() { |
@@ -1,120 +0,120 @@ | ||
var expect = require('expect.js') | ||
, Connection = require('./fixture/connection') | ||
, Device = require('./fixture/device'); | ||
var expect = require('expect.js'), | ||
Connection = require('./fixture/connection'), | ||
Device = require('./fixture/device'); | ||
describe('Connection', function() { | ||
var connection; | ||
var device = new Device(); | ||
var connection; | ||
var device = new Device(); | ||
describe('creating a connection object', function() { | ||
describe('creating a connection object', function() { | ||
beforeEach(function() { | ||
connection = new Connection(device); | ||
}); | ||
beforeEach(function() { | ||
connection = new Connection(device); | ||
}); | ||
it('it should have all expected values', function() { | ||
it('it should have all expected values', function() { | ||
expect(connection.id).to.be.a('string'); | ||
expect(connection.close).to.be.a('function'); | ||
expect(connection.set).to.be.a('function'); | ||
expect(connection.get).to.be.a('function'); | ||
expect(connection.toJSON).to.be.a('function'); | ||
expect(connection.executeCommand).to.be.a('function'); | ||
expect(connection.id).to.be.a('string'); | ||
expect(connection.close).to.be.a('function'); | ||
expect(connection.set).to.be.a('function'); | ||
expect(connection.get).to.be.a('function'); | ||
expect(connection.toJSON).to.be.a('function'); | ||
expect(connection.executeCommand).to.be.a('function'); | ||
}); | ||
}); | ||
describe('calling connect on the device', function() { | ||
describe('calling connect on the device', function() { | ||
it('it should emit connecting', function(done) { | ||
it('it should emit connecting', function(done) { | ||
connection.once('connecting', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
connection.once('connecting', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
}); | ||
}); | ||
it('it should emit connect', function(done) { | ||
it('it should emit connect', function(done) { | ||
connection.once('connect', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
}); | ||
connection.once('connect', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
describe('calling executeCommand', function() { | ||
}); | ||
it('it should emit send on device', function(done) { | ||
}); | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
connection.executeCommand([]); | ||
describe('calling executeCommand', function() { | ||
}); | ||
it('it should emit send on device', function(done) { | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
connection.executeCommand([]); | ||
describe('calling disconnect on the device', function() { | ||
}); | ||
it('it should emit disconnecting', function(done) { | ||
}); | ||
connection.once('disconnecting', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
describe('calling disconnect on the device', function() { | ||
}); | ||
it('it should emit disconnecting', function(done) { | ||
it('it should emit disconnect', function(done) { | ||
connection.once('disconnecting', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
connection.once('disconnect', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
}); | ||
}); | ||
it('it should emit disconnect', function(done) { | ||
connection.once('disconnect', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
describe('calling close', function() { | ||
}); | ||
it('it should emit disconnecting', function(done) { | ||
}); | ||
connection.once('disconnecting', function() { | ||
done(); | ||
}); | ||
connection.close(); | ||
describe('calling close', function() { | ||
}); | ||
it('it should emit disconnecting', function(done) { | ||
it('it should emit disconnect', function(done) { | ||
connection.once('disconnecting', function() { | ||
done(); | ||
}); | ||
connection.close(); | ||
connection.once('disconnect', function() { | ||
done(); | ||
}); | ||
connection.close(); | ||
}); | ||
}); | ||
it('it should emit disconnect', function(done) { | ||
describe('with a callback', function() { | ||
connection.once('disconnect', function() { | ||
done(); | ||
}); | ||
connection.close(); | ||
it('it should call the callback', function(done) { | ||
}); | ||
connection.close(function() { | ||
done(); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
}); | ||
connection.close(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,208 +0,208 @@ | ||
var expect = require('expect.js') | ||
, deviceguider = require('./fixture/deviceguider'); | ||
var expect = require('expect.js'), | ||
deviceguider = require('./fixture/deviceguider'); | ||
describe('DeviceGuider', function() { | ||
describe('having a deviceguider object', function() { | ||
describe('having a deviceguider object', function() { | ||
it('it should have all expected values', function() { | ||
it('it should have all expected values', function() { | ||
expect(deviceguider.getCurrentState).to.be.a('function'); | ||
expect(deviceguider.autoconnect).to.be.a('function'); | ||
expect(deviceguider.manualconnect).to.be.a('function'); | ||
expect(deviceguider.autoconnectOne).to.be.a('function'); | ||
expect(deviceguider.disconnectDevice).to.be.a('function'); | ||
expect(deviceguider.connectDevice).to.be.a('function'); | ||
expect(deviceguider.closeConnection).to.be.a('function'); | ||
expect(deviceguider.changeConnectionMode).to.be.a('function'); | ||
expect(deviceguider.getCurrentState).to.be.a('function'); | ||
expect(deviceguider.autoconnect).to.be.a('function'); | ||
expect(deviceguider.manualconnect).to.be.a('function'); | ||
expect(deviceguider.autoconnectOne).to.be.a('function'); | ||
expect(deviceguider.disconnectDevice).to.be.a('function'); | ||
expect(deviceguider.connectDevice).to.be.a('function'); | ||
expect(deviceguider.closeConnection).to.be.a('function'); | ||
expect(deviceguider.changeConnectionMode).to.be.a('function'); | ||
}); | ||
}); | ||
describe('calling getCurrentState', function() { | ||
describe('calling getCurrentState', function() { | ||
it('it should callback the correct object', function(done) { | ||
it('it should callback the correct object', function(done) { | ||
deviceguider.getCurrentState(function(err, currentState) { | ||
expect(currentState.plugged).to.be.an('array'); | ||
expect(currentState.connected).to.be.an('array'); | ||
expect(currentState.connectionMode).to.be.a('string'); | ||
expect(currentState.getDevice).to.be.a('function'); | ||
expect(currentState.getConnection).to.be.a('function'); | ||
expect(currentState.getDeviceByConnection).to.be.a('function'); | ||
expect(currentState.getConnectionByDevice).to.be.a('function'); | ||
done(); | ||
}); | ||
}); | ||
deviceguider.getCurrentState(function(err, currentState) { | ||
expect(currentState.plugged).to.be.an('array'); | ||
expect(currentState.connected).to.be.an('array'); | ||
expect(currentState.connectionMode).to.be.a('string'); | ||
expect(currentState.getDevice).to.be.a('function'); | ||
expect(currentState.getConnection).to.be.a('function'); | ||
expect(currentState.getDeviceByConnection).to.be.a('function'); | ||
expect(currentState.getConnectionByDevice).to.be.a('function'); | ||
done(); | ||
}); | ||
describe('calling changeConnectionMode', function() { | ||
}); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
}); | ||
describe('changing the mode', function() { | ||
describe('calling changeConnectionMode', function() { | ||
it('it should emit connectionModeChanged', function(done) { | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
deviceguider.once('connectionModeChanged', function(connectionMode) { | ||
expect(connectionMode).to.eql('autoconnect'); | ||
done(); | ||
}); | ||
describe('changing the mode', function() { | ||
deviceguider.changeConnectionMode('autoconnect'); | ||
it('it should emit connectionModeChanged', function(done) { | ||
}); | ||
deviceguider.once('connectionModeChanged', function(connectionMode) { | ||
expect(connectionMode).to.eql('autoconnect'); | ||
done(); | ||
}); | ||
it('it should return true', function() { | ||
deviceguider.changeConnectionMode('autoconnect'); | ||
var result = deviceguider.changeConnectionMode('autoconnect'); | ||
expect(result).to.eql(true); | ||
}); | ||
}); | ||
it('it should return true', function() { | ||
}); | ||
var result = deviceguider.changeConnectionMode('autoconnect'); | ||
expect(result).to.eql(true); | ||
describe('not changing the mode', function() { | ||
}); | ||
it('it should return false', function() { | ||
var result = deviceguider.changeConnectionMode('manualconnect'); | ||
expect(result).to.eql(false); | ||
}); | ||
}); | ||
describe('not changing the mode', function() { | ||
}); | ||
it('it should return false', function() { | ||
var result = deviceguider.changeConnectionMode('manualconnect'); | ||
expect(result).to.eql(false); | ||
}); | ||
describe('calling autoconnect', function() { | ||
}); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
}); | ||
it('it should automatically connect plugged devices', function(done) { | ||
describe('calling autoconnect', function() { | ||
deviceguider.once('connect', function(connection) { | ||
expect(connection).to.be.an('object'); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
it('it should automatically connect plugged devices', function(done) { | ||
}); | ||
deviceguider.once('connect', function(connection) { | ||
expect(connection).to.be.an('object'); | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
describe('calling autoconnectOne', function() { | ||
}); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
}); | ||
it('it should automatically connect the first plugged device by emitting connect', function(done) { | ||
describe('calling autoconnectOne', function() { | ||
deviceguider.once('connect', function(connection) { | ||
expect(connection).to.be.an('object'); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(done); | ||
}); | ||
done(); | ||
}); | ||
deviceguider.autoconnectOne(); | ||
it('it should automatically connect the first plugged device by emitting connect', function(done) { | ||
}); | ||
deviceguider.once('connect', function(connection) { | ||
expect(connection).to.be.an('object'); | ||
it('it should automatically connect the first plugged device', function(done) { | ||
done(); | ||
}); | ||
deviceguider.autoconnectOne(); | ||
deviceguider.autoconnectOne(function(err, connection) { | ||
expect(connection).to.be.ok(err); | ||
expect(connection).to.be.an('object'); | ||
}); | ||
done(); | ||
}); | ||
it('it should automatically connect the first plugged device', function(done) { | ||
}); | ||
deviceguider.autoconnectOne(function(err, connection) { | ||
expect(connection).to.be.ok(err); | ||
expect(connection).to.be.an('object'); | ||
done(); | ||
}); | ||
describe('calling disconnectDevice', function() { | ||
}); | ||
var toDisconnect; | ||
}); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(function() { | ||
deviceguider.once('connect', function(connection) { | ||
toDisconnect = connection.device; | ||
describe('calling disconnectDevice', function() { | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
}); | ||
}); | ||
var toDisconnect; | ||
it('it should emit disconnect', function(done) { | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(function() { | ||
deviceguider.once('connect', function(connection) { | ||
toDisconnect = connection.device; | ||
deviceguider.once('disconnect', function(connection) { | ||
done(); | ||
}); | ||
deviceguider.disconnectDevice(toDisconnect); | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
}); | ||
}); | ||
}); | ||
it('it should emit disconnect', function(done) { | ||
describe('with a callback', function() { | ||
deviceguider.once('disconnect', function(connection) { | ||
done(); | ||
}); | ||
deviceguider.disconnectDevice(toDisconnect); | ||
it('it should call the callback', function(done) { | ||
}); | ||
deviceguider.disconnectDevice(toDisconnect, function(connection) { | ||
done(); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
}); | ||
deviceguider.disconnectDevice(toDisconnect, function(connection) { | ||
done(); | ||
}); | ||
}); | ||
describe('calling closeConnection with the id', function() { | ||
}); | ||
var toClose; | ||
}); | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(function() { | ||
deviceguider.once('connect', function(connection) { | ||
toClose = connection; | ||
describe('calling closeConnection with the id', function() { | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
}); | ||
}); | ||
var toClose; | ||
it('it should emit disconnect', function(done) { | ||
beforeEach(function(done) { | ||
deviceguider.manualconnect(function() { | ||
deviceguider.once('connect', function(connection) { | ||
toClose = connection; | ||
deviceguider.once('disconnect', function(connection) { | ||
done(); | ||
}); | ||
deviceguider.closeConnection(toClose.id); | ||
done(); | ||
}); | ||
deviceguider.autoconnect(); | ||
}); | ||
}); | ||
}); | ||
it('it should emit disconnect', function(done) { | ||
describe('with a callback', function() { | ||
deviceguider.once('disconnect', function(connection) { | ||
done(); | ||
}); | ||
deviceguider.closeConnection(toClose.id); | ||
it('it should call the callback', function(done) { | ||
}); | ||
deviceguider.closeConnection(toClose.id, function(connection) { | ||
done(); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
}); | ||
deviceguider.closeConnection(toClose.id, function(connection) { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,107 +0,107 @@ | ||
var expect = require('expect.js') | ||
, deviceloader = require('./fixture/deviceloader').create(); | ||
var expect = require('expect.js'), | ||
deviceloader = require('./fixture/deviceloader').create(); | ||
describe('DeviceLoader', function() { | ||
describe('having a deviceloader object', function() { | ||
describe('having a deviceloader object', function() { | ||
it('it should have all expected values', function() { | ||
it('it should have all expected values', function() { | ||
expect(deviceloader.lookup).to.be.a('function'); | ||
expect(deviceloader.trigger).to.be.a('function'); | ||
expect(deviceloader.startLookup).to.be.a('function'); | ||
expect(deviceloader.stopLookup).to.be.a('function'); | ||
expect(deviceloader.lookup).to.be.a('function'); | ||
expect(deviceloader.trigger).to.be.a('function'); | ||
expect(deviceloader.startLookup).to.be.a('function'); | ||
expect(deviceloader.stopLookup).to.be.a('function'); | ||
}); | ||
}); | ||
describe('calling lookup', function() { | ||
describe('calling lookup', function() { | ||
it('it should return a device array', function(done) { | ||
it('it should return a device array', function(done) { | ||
deviceloader.lookup(function(err, devices) { | ||
expect(devices).to.be.an('array'); | ||
done(); | ||
}); | ||
}); | ||
deviceloader.lookup(function(err, devices) { | ||
expect(devices).to.be.an('array'); | ||
done(); | ||
}); | ||
describe('calling trigger', function() { | ||
}); | ||
it('it should call lookup', function(done) { | ||
}); | ||
deviceloader.once('lookup', function(err, devices) { | ||
done(); | ||
}); | ||
deviceloader.trigger(); | ||
describe('calling trigger', function() { | ||
}); | ||
it('it should call lookup', function(done) { | ||
it('it should return the devices', function(done) { | ||
deviceloader.once('lookup', function(err, devices) { | ||
done(); | ||
}); | ||
deviceloader.trigger(); | ||
deviceloader.trigger(function(err, devices) { | ||
expect(devices).to.be.an('array'); | ||
expect(devices).to.have.length(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('it should return the devices', function(done) { | ||
deviceloader.trigger(function(err, devices) { | ||
expect(devices).to.be.an('array'); | ||
expect(devices).to.have.length(2); | ||
done(); | ||
}); | ||
describe('calling startLookup', function() { | ||
}); | ||
beforeEach(function() { | ||
deviceloader.stopLookup(); | ||
}); | ||
}); | ||
it('it should emit plug when a device is added', function(done) { | ||
describe('calling startLookup', function() { | ||
deviceloader.once('plug', function(device) { | ||
done(); | ||
}); | ||
deviceloader.startLookup(10); | ||
beforeEach(function() { | ||
deviceloader.stopLookup(); | ||
}); | ||
}); | ||
it('it should emit plug when a device is added', function(done) { | ||
it('it should emit unplug when a device is removed', function(done) { | ||
deviceloader.once('plug', function(device) { | ||
done(); | ||
}); | ||
deviceloader.startLookup(10); | ||
deviceloader.once('unplug', function(device) { | ||
done(); | ||
}); | ||
deviceloader.startLookup(10); | ||
setTimeout(function() { | ||
deviceloader.startDevices = [deviceloader.startDevices.slice(0, 1)]; | ||
}, 20); | ||
}); | ||
}); | ||
it('it should emit unplug when a device is removed', function(done) { | ||
deviceloader.once('unplug', function(device) { | ||
done(); | ||
}); | ||
deviceloader.startLookup(10); | ||
setTimeout(function() { | ||
deviceloader.startDevices = [deviceloader.startDevices.slice(0, 1)]; | ||
}, 20); | ||
describe('calling stopLookup', function() { | ||
}); | ||
before(function() { | ||
deviceloader.stopLookup(); | ||
}); | ||
}); | ||
it('it should stop calling lookup', function(done) { | ||
describe('calling stopLookup', function() { | ||
deviceloader.once('lookup', function() { | ||
deviceloader.stopLookup(); | ||
deviceloader.once('lookup', function() { | ||
expect(undefined).to.be.ok(); | ||
}); | ||
setTimeout(function() { | ||
done(); | ||
}, 20); | ||
}); | ||
deviceloader.startLookup(10); | ||
before(function() { | ||
deviceloader.stopLookup(); | ||
}); | ||
}); | ||
it('it should stop calling lookup', function(done) { | ||
deviceloader.once('lookup', function() { | ||
deviceloader.stopLookup(); | ||
deviceloader.once('lookup', function() { | ||
expect(undefined).to.be.ok(); | ||
}); | ||
setTimeout(function() { | ||
done(); | ||
}, 20); | ||
}); | ||
deviceloader.startLookup(10); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,285 +0,285 @@ | ||
var expect = require('expect.js') | ||
, Device = require('./fixture/device'); | ||
var expect = require('expect.js'), | ||
Device = require('./fixture/device'); | ||
describe('Device', function() { | ||
describe('without Connection object', function() { | ||
describe('without Connection object', function() { | ||
var device; | ||
var device; | ||
describe('creating a device object', function() { | ||
describe('creating a device object', function() { | ||
it('it should have all expected values', function() { | ||
it('it should have all expected values', function() { | ||
device = new Device(true); | ||
expect(device.id).to.be.a('string'); | ||
expect(device.open).to.be.a('function'); | ||
expect(device.close).to.be.a('function'); | ||
expect(device.send).to.be.a('function'); | ||
expect(device.set).to.be.a('function'); | ||
expect(device.get).to.be.a('function'); | ||
expect(device.toJSON).to.be.a('function'); | ||
device = new Device(true); | ||
expect(device.id).to.be.a('string'); | ||
expect(device.open).to.be.a('function'); | ||
expect(device.close).to.be.a('function'); | ||
expect(device.send).to.be.a('function'); | ||
expect(device.set).to.be.a('function'); | ||
expect(device.get).to.be.a('function'); | ||
expect(device.toJSON).to.be.a('function'); | ||
}); | ||
}); | ||
describe('calling open', function() { | ||
describe('calling open', function() { | ||
it('it should emit open', function(done) { | ||
it('it should emit open', function(done) { | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.open(); | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.open(); | ||
}); | ||
}); | ||
describe('with a callback', function() { | ||
describe('with a callback', function() { | ||
it('it should call the callback', function(done) { | ||
it('it should call the callback', function(done) { | ||
device.open(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
device.open(function() { | ||
done(); | ||
}); | ||
describe('emitting send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
}); | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.emit('send', []); | ||
}); | ||
}); | ||
describe('emitting send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
describe('calling send', function() { | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.emit('send', []); | ||
it('it should emit receive', function(done) { | ||
}); | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.send([]); | ||
}); | ||
}); | ||
describe('calling send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
describe('calling close', function() { | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.send([]); | ||
it('it should emit close', function(done) { | ||
}); | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.close(); | ||
}); | ||
}); | ||
describe('calling close', function() { | ||
describe('with a callback', function() { | ||
it('it should emit close', function(done) { | ||
it('it should call the callback', function(done) { | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.close(); | ||
device.close(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
device.close(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('with Connection object', function() { | ||
}); | ||
var device; | ||
describe('with Connection object', function() { | ||
describe('creating a device object', function() { | ||
var device; | ||
it('it should have all expected values', function() { | ||
describe('creating a device object', function() { | ||
device = new Device(); | ||
expect(device.id).to.be.a('string'); | ||
expect(device.open).to.be.a('function'); | ||
expect(device.close).to.be.a('function'); | ||
expect(device.send).to.be.a('function'); | ||
it('it should have all expected values', function() { | ||
}); | ||
device = new Device(); | ||
expect(device.id).to.be.a('string'); | ||
expect(device.open).to.be.a('function'); | ||
expect(device.close).to.be.a('function'); | ||
expect(device.send).to.be.a('function'); | ||
describe('calling open', function() { | ||
}); | ||
it('it should emit open', function(done) { | ||
describe('calling open', function() { | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.open(); | ||
it('it should emit open', function(done) { | ||
}); | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.open(); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
describe('with a callback', function() { | ||
device.open(function() { | ||
done(); | ||
}); | ||
it('it should call the callback', function(done) { | ||
}); | ||
}); | ||
device.open(function() { | ||
done(); | ||
}); | ||
describe('emitting send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
}); | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.emit('send', []); | ||
}); | ||
}); | ||
describe('emitting send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
describe('calling send', function() { | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.emit('send', []); | ||
it('it should emit receive', function(done) { | ||
}); | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.send(); | ||
}); | ||
}); | ||
describe('calling send', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
describe('calling close', function() { | ||
device.once('receive', function() { | ||
done(); | ||
}); | ||
device.send(); | ||
it('it should emit close', function(done) { | ||
}); | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.close(); | ||
}); | ||
}); | ||
describe('calling close', function() { | ||
describe('with a callback', function() { | ||
it('it should emit close', function(done) { | ||
it('it should call the callback', function(done) { | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.close(); | ||
device.close(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
device.close(function() { | ||
done(); | ||
}); | ||
describe('calling connect', function() { | ||
}); | ||
it('it should emit opening', function(done) { | ||
}); | ||
device.once('opening', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
}); | ||
}); | ||
describe('calling connect', function() { | ||
it('it should emit open', function(done) { | ||
it('it should emit opening', function(done) { | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
device.once('opening', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
}); | ||
}); | ||
it('it should a new connection property with a generated id', function(done) { | ||
it('it should emit open', function(done) { | ||
device.once('open', function() { | ||
expect(device.connection).to.be.an('object'); | ||
expect(device.connection.id).to.be.a('string'); | ||
device.once('open', function() { | ||
done(); | ||
}); | ||
device.connect(); | ||
done(); | ||
}); | ||
device.connect(); | ||
}); | ||
}); | ||
it('it should a new connection property with a generated id', function(done) { | ||
describe('with a callback', function() { | ||
device.once('open', function() { | ||
expect(device.connection).to.be.an('object'); | ||
expect(device.connection.id).to.be.a('string'); | ||
var device2 = new Device(); | ||
done(); | ||
}); | ||
device.connect(); | ||
it('it should call the callback', function(done) { | ||
}); | ||
device2.connect(function() { | ||
done(); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
var device2 = new Device(); | ||
}); | ||
it('it should call the callback', function(done) { | ||
device2.connect(function() { | ||
done(); | ||
}); | ||
describe('calling disconnect', function() { | ||
}); | ||
it('it should emit closing', function(done) { | ||
}); | ||
device.once('closing', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
}); | ||
}); | ||
describe('calling disconnect', function() { | ||
it('it should emit close', function(done) { | ||
it('it should emit closing', function(done) { | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
device.once('closing', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
}); | ||
}); | ||
describe('with a callback', function() { | ||
it('it should emit close', function(done) { | ||
it('it should call the callback', function(done) { | ||
device.once('close', function() { | ||
done(); | ||
}); | ||
device.disconnect(); | ||
var device2 = new Device(); | ||
}); | ||
device2.disconnect(function() { | ||
done(); | ||
}); | ||
describe('with a callback', function() { | ||
}); | ||
it('it should call the callback', function(done) { | ||
}); | ||
var device2 = new Device(); | ||
device2.disconnect(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,16 +0,16 @@ | ||
var Connection = require('../../index').Connection | ||
, util = require('util') | ||
, _ = require('lodash') | ||
, FrameHandler = require('./framehandler'); | ||
var Connection = require('../../index').Connection, | ||
util = require('util'), | ||
_ = require('lodash'), | ||
FrameHandler = require('./framehandler'); | ||
function MyConnection(device) { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
Connection.call(this, device); | ||
// call super class | ||
Connection.call(this, device); | ||
this.frameHandler = new FrameHandler(this.device); | ||
this.frameHandler.on('receive', function (frame) { | ||
// forward to appropriate command... | ||
}); | ||
this.frameHandler = new FrameHandler(this.device); | ||
this.frameHandler.on('receive', function (frame) { | ||
// forward to appropriate command... | ||
}); | ||
} | ||
@@ -21,15 +21,15 @@ | ||
// MyConnection.prototype.onConnecting = function(callback) { | ||
// // Need to send some commands before definitely connected? | ||
// if (callback) callback(); | ||
// // Need to send some commands before definitely connected? | ||
// if (callback) callback(); | ||
// }; | ||
// MyConnection.prototype.onDisconnecting = function(callback) { | ||
// // Need to send some commands before definitely closed? | ||
// if (callback) callback(); | ||
// // Need to send some commands before definitely closed? | ||
// if (callback) callback(); | ||
// }; | ||
MyConnection.prototype.executeCommand = function(commandData, callback) { | ||
this.frameHandler.send('send', commandData); | ||
this.frameHandler.send('send', commandData); | ||
}; | ||
module.exports = MyConnection; |
@@ -1,10 +0,10 @@ | ||
var Device = require('../../index').Device | ||
, Connection = require('./connection') | ||
, util = require('util') | ||
, _ = require('lodash'); | ||
var Device = require('../../index').Device, | ||
Connection = require('./connection'), | ||
util = require('util'), | ||
_ = require('lodash'); | ||
function MyDevice(withoutConnection) { | ||
var Conn = withoutConnection ? null : Connection; | ||
// call super class | ||
Device.call(this, Conn); | ||
var Conn = withoutConnection ? null : Connection; | ||
// call super class | ||
Device.call(this, Conn); | ||
} | ||
@@ -15,26 +15,26 @@ | ||
MyDevice.prototype.open = function(callback) { | ||
var self = this; | ||
var self = this; | ||
setTimeout(function() { | ||
self.emit('open', callback); | ||
if (!self.connection && callback) callback(); | ||
}, 10); | ||
this.on('send', function(data) { | ||
setTimeout(function() { | ||
self.emit('open', callback); | ||
if (!self.connection && callback) callback(); | ||
}, 10); | ||
this.on('send', function(data) { | ||
setTimeout(function() { | ||
self.emit('receive', data); | ||
}, 5); | ||
}); | ||
self.emit('receive', data); | ||
}, 5); | ||
}); | ||
}; | ||
MyDevice.prototype.close = function(callback, fire) { | ||
var self = this; | ||
var self = this; | ||
setTimeout(function() { | ||
self.emit('close', callback); | ||
self.removeAllListeners(); | ||
if (callback && (!self.connection || fire)) callback(null); | ||
}, 10); | ||
setTimeout(function() { | ||
self.emit('close', callback); | ||
self.removeAllListeners(); | ||
if (callback && (!self.connection || fire)) callback(null); | ||
}, 10); | ||
}; | ||
module.exports = MyDevice; |
@@ -1,8 +0,8 @@ | ||
var DeviceGuider = require('../../index').DeviceGuider | ||
, util = require('util'); | ||
var DeviceGuider = require('../../index').DeviceGuider, | ||
util = require('util'); | ||
function MyDeviceGuider() { | ||
// call super class | ||
DeviceGuider.call(this, require('./deviceloader').create()); | ||
// call super class | ||
DeviceGuider.call(this, require('./deviceloader').create()); | ||
} | ||
@@ -9,0 +9,0 @@ |
@@ -1,18 +0,18 @@ | ||
var DeviceLoader = require('../../index').DeviceLoader | ||
, util = require('util') | ||
, _ = require('lodash') | ||
, Device = require('./device'); | ||
var DeviceLoader = require('../../index').DeviceLoader, | ||
util = require('util'), | ||
_ = require('lodash'), | ||
Device = require('./device'); | ||
function MyDeviceLoader() { | ||
var self = this; | ||
var self = this; | ||
// call super class | ||
DeviceLoader.call(this); | ||
// call super class | ||
DeviceLoader.call(this); | ||
this.Device = Device; | ||
this.Device = Device; | ||
this.startDevices = [ | ||
new Device(), | ||
new Device() | ||
]; | ||
this.startDevices = [ | ||
new Device(), | ||
new Device() | ||
]; | ||
} | ||
@@ -23,8 +23,8 @@ | ||
MyDeviceLoader.prototype.lookup = function(callback) { | ||
var devices = this.startDevices; | ||
try { | ||
this.emit('lookup'); | ||
} catch(e) { | ||
} | ||
callback(null, devices); | ||
var devices = this.startDevices; | ||
try { | ||
this.emit('lookup'); | ||
} catch(e) { | ||
} | ||
callback(null, devices); | ||
}; | ||
@@ -34,3 +34,3 @@ | ||
module.exports.create = function() { | ||
return new MyDeviceLoader(); | ||
return new MyDeviceLoader(); | ||
}; |
@@ -1,7 +0,7 @@ | ||
var FrameHandler = require('../../index').FrameHandler | ||
, util = require('util'); | ||
var FrameHandler = require('../../index').FrameHandler, | ||
util = require('util'); | ||
function MyFrameHandler(device) { | ||
// call super class | ||
FrameHandler.call(this, device); | ||
// call super class | ||
FrameHandler.call(this, device); | ||
} | ||
@@ -12,13 +12,13 @@ | ||
// MyFrameHandler.prototype.analyzeNextFrame = function(incomming) { | ||
// return incomming.splice(0); | ||
// return incomming.splice(0); | ||
// }; | ||
// MyFrameHandler.prototype.unwrapFrame = function(frame) { | ||
// return frame; | ||
// return frame; | ||
// }; | ||
// MyFrameHandler.prototype.wrapFrame = function(frame) { | ||
// return frame; | ||
// return frame; | ||
// }; | ||
module.exports = MyFrameHandler; |
@@ -1,53 +0,53 @@ | ||
var expect = require('expect.js') | ||
, FrameHandler = require('./fixture/framehandler') | ||
, Device = require('./fixture/device'); | ||
var expect = require('expect.js'), | ||
FrameHandler = require('./fixture/framehandler'), | ||
Device = require('./fixture/device'); | ||
describe('FrameHandler', function() { | ||
var device = new Device(); | ||
var framehandler = new FrameHandler(device); | ||
var device = new Device(); | ||
var framehandler = new FrameHandler(device); | ||
before(function(done) { | ||
device.open(done); | ||
}); | ||
before(function(done) { | ||
device.open(done); | ||
}); | ||
describe('emitting send', function() { | ||
describe('emitting send', function() { | ||
it('it should emit send on device', function(done) { | ||
it('it should emit send on device', function(done) { | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
framehandler.emit('send', []); | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
framehandler.emit('send', []); | ||
}); | ||
}); | ||
describe('calling send', function() { | ||
}); | ||
it('it should emit send on device', function(done) { | ||
describe('calling send', function() { | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
framehandler.send([]); | ||
it('it should emit send on device', function(done) { | ||
}); | ||
device.once('send', function() { | ||
done(); | ||
}); | ||
framehandler.send([]); | ||
}); | ||
describe('receiving data from device', function() { | ||
}); | ||
it('it should emit receive', function(done) { | ||
describe('receiving data from device', function() { | ||
framehandler.once('receive', function() { | ||
done(); | ||
}); | ||
device.send([]); | ||
it('it should emit receive', function(done) { | ||
}); | ||
framehandler.once('receive', function() { | ||
done(); | ||
}); | ||
device.send([]); | ||
}); | ||
}); | ||
}); |
if (!Array.prototype.toHexDebug) { | ||
/** | ||
* Converts a "byte" array to a readable hex string. | ||
* @return {String} The result array. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toHexDebug(); // returns '01-00' | ||
*/ | ||
Array.prototype.toHexDebug = function() { | ||
var res = ''; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (i > 0) { | ||
res += '-'; | ||
} | ||
var hex = this[i].toString(16); | ||
hex = hex.length < 2 ? '0' + hex : hex; | ||
res += hex; | ||
} | ||
return res.toUpperCase(); | ||
}; | ||
/** | ||
* Converts a "byte" array to a readable hex string. | ||
* @return {String} The result string. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toHexDebug(); // returns '01-00' | ||
*/ | ||
Array.prototype.toHexDebug = function() { | ||
var res = ''; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (i > 0) { | ||
res += '-'; | ||
} | ||
var hex = this[i].toString(16); | ||
hex = hex.length < 2 ? '0' + hex : hex; | ||
res += hex; | ||
} | ||
return res.toUpperCase(); | ||
}; | ||
} | ||
if (!Array.prototype.toHexString) { | ||
/** | ||
* Converts a "byte" array to a hex string. | ||
* @return {String} The result array. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toHexString(); // returns '0100' | ||
*/ | ||
Array.prototype.toHexString = function() { | ||
var res = ''; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
var hex = this[i].toString(16); | ||
hex = hex.length < 2 ? '0' + hex : hex; | ||
res += hex; | ||
} | ||
return res.toUpperCase(); | ||
}; | ||
} | ||
if (!Array.prototype.toBuffer) { | ||
/** | ||
* Converts a "byte" array to a buffer object. | ||
* @return {Buffer} The result buffer object. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toBuffer(); | ||
*/ | ||
Array.prototype.toBuffer = function() { | ||
return new Buffer(this); | ||
}; | ||
/** | ||
* Converts a "byte" array to a buffer object. | ||
* @return {Buffer} The result buffer object. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toBuffer(); | ||
*/ | ||
Array.prototype.toBuffer = function() { | ||
return new Buffer(this); | ||
}; | ||
} | ||
@@ -40,17 +60,17 @@ | ||
/** | ||
* Converts a "byte" array to a number. | ||
* @return {Number} The result number. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toNumber(); // returns 64 | ||
*/ | ||
Array.prototype.toNumber = function() { | ||
var value = 0; | ||
for ( var i = 0; i < this.length; i++) { | ||
value = (value * 256) + this[i]; | ||
} | ||
/** | ||
* Converts a "byte" array to a number. | ||
* @return {Number} The result number. | ||
* | ||
* @example: | ||
* [0x01, 0x00].toNumber(); // returns 64 | ||
*/ | ||
Array.prototype.toNumber = function() { | ||
var value = 0; | ||
for ( var i = 0; i < this.length; i++) { | ||
value = (value * 256) + this[i]; | ||
} | ||
return value; | ||
}; | ||
return value; | ||
}; | ||
} |
if (!Buffer.prototype.toHexDebug) { | ||
/** | ||
* Converts a buffer object to a readable hex string. | ||
* @return {String} The result hex string. | ||
* | ||
* @example: | ||
* (new Buffer([0x01, 0x00])).toHexDebug(); // returns '01-00' | ||
*/ | ||
Buffer.prototype.toHexDebug = function() { | ||
var str = this.toString('hex'); | ||
var res = ''; | ||
for (var i = 0, len = str.length; i < len; i += 2) { | ||
if (i > 0) { | ||
res += '-'; | ||
} | ||
res += str[i] + str[i+ 1]; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* Converts a buffer object to a readable hex string. | ||
* @return {String} The result hex string. | ||
* | ||
* @example: | ||
* (new Buffer([0x01, 0x00])).toHexDebug(); // returns '01-00' | ||
*/ | ||
Buffer.prototype.toHexDebug = function() { | ||
var str = this.toString('hex'); | ||
var res = ''; | ||
for (var i = 0, len = str.length; i < len; i += 2) { | ||
if (i > 0) { | ||
res += '-'; | ||
} | ||
res += str[i] + str[i+ 1]; | ||
} | ||
return res; | ||
}; | ||
} | ||
@@ -25,12 +25,12 @@ | ||
/** | ||
* Converts a buffer object to a "byte" array. | ||
* @return {Array} The result array. | ||
* | ||
* @example: | ||
* (new Buffer([0x01, 0x00])).toArray(); // returns [0x01, 0x00] | ||
*/ | ||
Buffer.prototype.toArray = function() { | ||
return Array.prototype.slice.call(this, 0); | ||
}; | ||
/** | ||
* Converts a buffer object to a "byte" array. | ||
* @return {Array} The result array. | ||
* | ||
* @example: | ||
* (new Buffer([0x01, 0x00])).toArray(); // returns [0x01, 0x00] | ||
*/ | ||
Buffer.prototype.toArray = function() { | ||
return Array.prototype.slice.call(this, 0); | ||
}; | ||
} |
require('./array'); | ||
require('./buffer'); | ||
require('./number'); | ||
require('./string'); | ||
require('enum').register(); |
if (!Number.prototype.toArray) { | ||
/** | ||
* Converts a number to a "byte" array. | ||
* @param {Number} size The size of the array . [optional] | ||
* @return {Array} The result array. | ||
* | ||
* @example: | ||
* 64.toArray(); // returns [0x01, 0x00] | ||
*/ | ||
Number.prototype.toArray = function(size) { | ||
if (!size) { | ||
if (this > 0xffffffffffff) { | ||
size = 8; | ||
} else if (this > 0xffffffff) { | ||
size = 6; | ||
} else if (this > 0xffff) { | ||
size = 4; | ||
} else if (this > 0xff || this < 0xff) { | ||
size = 2; | ||
} | ||
} | ||
/** | ||
* Converts a number to a "byte" array. | ||
* @param {Number} size The size of the array . [optional] | ||
* @return {Array} The result array. | ||
* | ||
* @example: | ||
* 64.toArray(); // returns [0x01, 0x00] | ||
*/ | ||
Number.prototype.toArray = function(size) { | ||
if (!size) { | ||
if (this > 0xffffffffffff) { | ||
size = 8; | ||
} else if (this > 0xffffffff) { | ||
size = 6; | ||
} else if (this > 0xffff) { | ||
size = 4; | ||
} else if (this > 0xff || this < 0xff) { | ||
size = 2; | ||
} | ||
} | ||
var bytes = []; | ||
var bytes = []; | ||
var n = this; | ||
var n = this; | ||
for ( var i = size - 1; i >= 0; i--) { | ||
var b = n & 0xff; | ||
bytes[i] = b; | ||
n = (n - b) / 256 ; | ||
} | ||
for (var i = size - 1; i >= 0; i--) { | ||
var b = n & 0xff; | ||
bytes[i] = b; | ||
n = (n - b) / 256 ; | ||
} | ||
return bytes; | ||
}; | ||
return bytes; | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34
1724
695
82479