Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@


*.log
.idea/
yarn.lock
package-lock.json

Expand Down
33 changes: 13 additions & 20 deletions packages/usb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
const os = require('os');
const util = require('util');
const EventEmitter = require('events');
let usb = null;

let { usb, findByIds, getDeviceList } = require("usb");
/**
* [USB Class Codes ]
* @type {Object}
Expand All @@ -20,19 +19,15 @@ const IFACE_CLASS = {
* [function USB]
* @param {[type]} vid [description]
* @param {[type]} pid [description]
* @return {[type]} [description]
* @return {USB} [description]
*/
function USB(vid, pid){

if (!usb) {
usb = require('usb');
}

EventEmitter.call(this);
var self = this;
let self = this;
this.device = null;
if(vid && pid){
this.device = usb.findByIds(vid, pid);
this.device = findByIds(vid, pid);
}else if(vid){
// Set spesific USB device from devices array as coming from USB.findPrinter() function.
// for example
Expand All @@ -42,15 +37,15 @@ function USB(vid, pid){
// const device = new escpos.USB(Device1); OR device = new escpos.USB(Device2);
this.device = vid;
}else{
var devices = USB.findPrinter();
let devices = USB.findPrinter();
if(devices && devices.length)
this.device = devices[0];
}
if (!this.device)
throw new Error('Can not find printer');

usb.on('detach', function(device){
if(device == self.device) {
if(device === self.device) {
self.emit('detach' , device);
self.emit('disconnect', device);
self.device = null;
Expand All @@ -59,17 +54,14 @@ function USB(vid, pid){

return this;

};
}

/**
* [findPrinter description]
* @return {[type]} [description]
*/
USB.findPrinter = function(){
if (!usb) {
usb = require('usb');
}
return usb.getDeviceList().filter(function(device){
return getDeviceList().filter(function(device){
try{
return device.configDescriptor.interfaces.filter(function(iface){
return iface.filter(function(conf){
Expand Down Expand Up @@ -106,7 +98,7 @@ util.inherits(USB, EventEmitter);
* @return {[type]} [description]
*/
USB.prototype.open = function (callback){
let self = this, counter = 0, index = 0;
let self = this, counter = 0;
this.device.open();
this.device.interfaces.forEach(function(iface){
(function(iface){
Expand All @@ -125,10 +117,10 @@ USB.prototype.open = function (callback){
}
iface.claim(); // must be called before using any endpoints of this interface.
iface.endpoints.filter(function(endpoint){
if(endpoint.direction == 'out' && !self.endpoint) {
if(endpoint.direction === 'out' && !self.endpoint) {
self.endpoint = endpoint;
}
if(endpoint.direction == 'in' && !self.deviceToPcEndpoint) {
if(endpoint.direction === 'in' && !self.deviceToPcEndpoint) {
self.deviceToPcEndpoint = endpoint;
}
});
Expand All @@ -154,6 +146,7 @@ USB.prototype.open = function (callback){
/**
* [function write]
* @param {[type]} data [description]
* @param {[type]} callback
* @return {[type]} [description]
*/
USB.prototype.write = function(data, callback){
Expand All @@ -169,7 +162,7 @@ USB.prototype.write = function(data, callback){
*/
USB.prototype.read = function(callback) {
this.deviceToPcEndpoint.transfer(64, (error,data) => callback(data));

return this;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/usb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"usb": "*"
"usb": "^2.5.0"
},
"keywords": [],
"author": "Lsong <song940@gmail.com> (https://lsong.org)",
Expand Down