Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

devicestack

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

devicestack - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

127

lib/deviceguider.js

@@ -24,4 +24,3 @@ var util = require('util')

this.currentState = {
doAutoconnect: false,
connectOne: true,
connectionMode: 'autoconnectOne',
plugged: [],

@@ -41,2 +40,17 @@ connected: [],

},
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) {

@@ -62,4 +76,4 @@ return _.find(self.currentState.connected, function(dc) {

if (self.currentState.doAutoconnect) {
if (!self.currentState.connectOne || self.currentState.connectOne && self.currentState.connected.length === 0) {
if (self.currentState.connectionMode !== 'manualconnect') {
if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) {
self.connectDevice(device);

@@ -87,2 +101,17 @@ }

DeviceGuider.prototype.changeConnectionMode = function(mode) {
var somethingChanged = false;
if (mode !== this.currentState.connectionMode) {
somethingChanged = true;
this.currentState.connectionMode = mode;
}
if (somethingChanged) {
this.emit('connectionModeChanged', this.currentState.connectionMode);
}
return somethingChanged;
};
DeviceGuider.prototype.getCurrentState = function(callback) {

@@ -103,41 +132,53 @@ var self = this;

this.currentState.doAutoconnect = true;
this.currentState.connectOne = false;
if (this.changeConnectionMode('autoconnect')) {
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);
});
}
};
DeviceGuider.prototype.manualconnect = function(callback) {
this.currentState.doAutoconnect = false;
var self = this;
if (this.currentState.connected.length === 0) {
return callback(null);
if (this.changeConnectionMode('manualconnect')) {
if (this.currentState.connected.length === 0) {
if (callback) { return callback(null); }
}
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); }
}
async.forEach(this.currentState.connected, function(dc, clb) {
dc.connection.close(clb);
}, callback);
};
DeviceGuider.prototype.autoconnectOne = function(callback) {
this.currentState.doAutoconnect = true;
this.currentState.connectOne = true;
if (this.currentState.plugged.length > 0 && this.currentState.connected.length === 0) {
this.connectDevice(this.currentState.plugged[0], callback);
if (this.changeConnectionMode('autoconnectOne')) {
if (this.currentState.plugged.length > 0 && this.currentState.connected.length === 0) {
this.connectDevice(this.currentState.plugged[0], callback);
} else {
if (callback) callback('No device available!');
}
} else {
if (callback) callback('No device available!');
if (callback) { return callback(null); }
}
};
DeviceGuider.prototype.connectDevice = function(device, callback) {
var self = this;
DeviceGuider.prototype.connectDevice = function(deviceOrId, callback) {
var self = this
, device = deviceOrId;
if (_.isString(deviceOrId)) {
device = this.currentState.getDevice(deviceOrId);
}
device.connect(function(err, connection) {

@@ -174,5 +215,21 @@ if (err) {

DeviceGuider.prototype.disconnectDevice = function(device, callback) {
var self = this;
DeviceGuider.prototype.disconnect = function(port, callback) {
var device = this.currentState.getConnectedDeviceByPort(port);
this.disconnectDevice(device, callback);
};
DeviceGuider.prototype.disconnectDevice = function(deviceOrId, callback) {
var self = this
, device = 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!'); }
}
device.disconnect(function(err) {

@@ -192,6 +249,10 @@ if (err) {

DeviceGuider.prototype.closeConnection = function(id, callback) {
var connection = this.currentState.getConnection(id);
DeviceGuider.prototype.closeConnection = function(connectionOrId, callback) {
var self = this
, connection = connectionOrId;
if (_.isString(connectionOrId)) {
connection = this.currentState.getConnection(connectionOrId);
}
connection.close(function(err) {

@@ -198,0 +259,0 @@ if (err) {

{
"name": "devicestack",
"version": "1.0.0",
"version": "1.0.1",
"description": "This module helps you to represent a device and its protocol.",

@@ -5,0 +5,0 @@ "private": false,

@@ -15,4 +15,7 @@ var expect = require('expect.js')

expect(deviceguider.connect).to.be.a('function');
expect(deviceguider.disconnect).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');

@@ -42,4 +45,3 @@ });

expect(currentState.connected).to.be.an('array');
expect(currentState.doAutoconnect).to.be.a('boolean');
expect(currentState.connectOne).to.be.a('boolean');
expect(currentState.connectionMode).to.be.a('string');
expect(currentState.getDevice).to.be.a('function');

@@ -56,2 +58,43 @@ expect(currentState.getConnection).to.be.a('function');

describe('calling changeConnectionMode', function() {
beforeEach(function(done) {
deviceguider.manualconnect(done);
});
describe('changing the mode', function() {
it('it should emit connectionModeChanged', function(done) {
deviceguider.once('connectionModeChanged', function(connectionMode) {
expect(connectionMode).to.eql('autoconnect');
done();
});
deviceguider.changeConnectionMode('autoconnect');
});
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('calling autoconnect', function() {

@@ -58,0 +101,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc