This repository was archived by the owner on May 25, 2020. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,7 +55,7 @@ scanner.startScanning()
5555
5656### scanner = new UsbScanner(options)
5757
58- * Creates a new scanner using the options
58+ * Creates and opens a new scanner using the options
5959 * `vendorID` - *number* - the vendorID of the device
6060 * `productID` - *number* - the productID of the device
6161 * `path` - *string* the path of the devices
@@ -66,6 +66,10 @@ scanner.startScanning()
6666
6767* Starts listening for barcodes
6868
69+ ### scanner.stopScanning()
70+
71+ * Stops listening for barcodes and closes the HID device.
72+
6973### scanner.on('data', functions(data) {})
7074
7175* listen for the ` data ` event to receive the barcode
Original file line number Diff line number Diff line change @@ -6,23 +6,24 @@ const hidMap = require('./hidmap');
66class UsbScanner extends EventEmitter {
77
88 constructor ( options ) {
9+ options = options || { } ;
10+
911 let {
10- vendorID,
11- productID,
12- path,
12+ vendorID = undefined ,
13+ productID = undefined ,
14+ path = undefined ,
1315 vCardString = true ,
1416 vCardSeperator = '|'
1517 } = options ;
16-
18+
1719 super ( ) ;
1820
21+ this . vendorID = vendorID ;
22+ this . productID = productID ;
23+ this . path = path ;
1924 this . _vCardString = vCardString ;
2025 this . _vCardSeperator = vCardSeperator ;
2126
22- if ( path ) this . hid = new HID ( path ) ;
23- else if ( vendorID && productID ) this . hid = new HID ( vendorID , productID ) ;
24- else console . error ( 'Device cannot be found, please supply a path or VID & PID' ) ; // eslint-disable-line
25-
2627 this . _hidMap = hidMap . standard ;
2728 this . _hidMapShift = hidMap . shift ;
2829
@@ -36,6 +37,14 @@ class UsbScanner extends EventEmitter {
3637
3738
3839 startScanning ( ) {
40+ try {
41+ if ( this . path ) this . hid = new HID ( path ) ;
42+ else if ( this . vendorID && this . productID ) this . hid = new HID ( this . vendorID , this . productID ) ;
43+ else throw 'Device cannot be found, please supply a path or VID & PID' ;
44+ } catch ( error ) {
45+ this . emit ( 'error' , error ) ;
46+ }
47+
3948 let scanResult = [ ] ;
4049 let vCard = [ ] ;
4150
@@ -71,7 +80,14 @@ class UsbScanner extends EventEmitter {
7180 }
7281 }
7382 } ) ;
83+
84+ }
85+
86+ stopScanning ( ) {
87+ this . hid . removeAllListeners ( 'data' ) ;
88+ this . hid . close ( ) ;
7489 }
90+
7591}
7692
7793
Original file line number Diff line number Diff line change 11{
22 "name" : " @isirthijs/barcode-scanner" ,
3- "version" : " 1.1 .0" ,
3+ "version" : " 1.2 .0" ,
44 "description" : " A simple wedge barcode scanner module with support for the shift modifier key" ,
55 "main" : " index.js" ,
66 "scripts" : {
You can’t perform that action at this time.
0 commit comments