var HID = require('node-hid');
var _ = require('underscore')
var events = require('events');
var util = require("util");

/*
options:
{vendorId:Int, hidMap: Obj}
defaults:
{vendorId:1534, hidMap:{30: '1', 31: '2', 32: '3', 33: '4',34: '5',35: '6', 36: '7', 37: '8', 38: '9',39: '0',40: 'e'}}
*/
function usbScanner(options){
	var opts = options || {}
	this.init(opts);
	events.EventEmitter.call(this);
}

//enherit event emitter
util.inherits(usbScanner, events.EventEmitter);

function getDevices(){
	var devices = HID.devices()
	return devices
}

let usbDevice;

usbScanner.prototype.init = function(options){
	var vendorId =  options.vendorId || 1534
	var allDevices = getDevices();
	//hidMap defining keyboard code to coresponding string value
	this.hidMap = options.hidMap || {
	4:"A", 5:"B",6:"C",7:"D",8:"E",
	9:"F", 10:"G", 11:"H",12:"I",13:"J",
	14:"K",15:"L",16:"M",17:"N", 18:"O",
	19:"P",20:"Q",21:"R",22:"S",23:"T",
	24:"U",25:"V", 26:"W", 27:"X", 28:"Y",
	29:"Z",30: '1', 31: '2', 32: '3', 33: '4',
	34: '5',35: '6', 36: '7', 37: '8', 38: '9',
	// enter - barcode escape char
	39: '0',40: 'enter',44:" ",45:"-", 55:".", 56:"/",
	85:"*", 87:"+"
	};

	var scanner = _.find(allDevices, function(device) {
    	return (device.vendorId === vendorId)
	});

	var device = new HID.HID(scanner.path);
	usbDevice = device;
	// start waiting for scan events
	this.startScanning(device);
}

usbScanner.prototype.startScanning = function(device){
	var hidMap = this.hidMap
	//empty array for barcode bytes
	var bcodeBuff = [];
	//string variable to hold barcode string
	var aBarcode = "";
	//event emitter for when newCode is read from scanner
	var getCode = function(code){
		this.emit("data", code)
	}.bind(this)

	device.on("data", function(chunk) {
    //second byte of buffer is all that contains data
    if (hidMap[chunk[2]]) {
        //if not bcodeBuff escape char (40)
        if (chunk[2] !== 40) {
            bcodeBuff.push(hidMap[chunk[2]])
        } else {
        //revieved escape code, join bCodebuff array and
            aBarcode = bcodeBuff.join("")
            bcodeBuff = []
            //emit newCode event
           	getCode(aBarcode)
        }
    }
});
}

usbScanner.prototype.stopScanning = function() {
	usbDevice.close();
}

module.exports = {usbScanner:usbScanner, getDevices:getDevices}

@@ -647,16 +647,61 @@
 %7C%7C 1534%0A
+%09var devicePath = options.devicePath %7C%7C null%0A
 %09var all
@@ -1200,16 +1200,17 @@
 %22+%22%0A%09%7D;%0A
+%09
 %0A%09var sc
@@ -1216,16 +1216,50 @@
 canner =
+ devicePath ? %7Bpath: devicePath%7D :
  _.find(

add device path option