-
Notifications
You must be signed in to change notification settings - Fork 3
/
barcode-scanner.js
99 lines (92 loc) · 3.39 KB
/
barcode-scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var Emitter = require('extended-emitter');
var arrays = require('async-arrays');
var drivers = {};
var listers = {};
switch(process.platform){
case 'darwin':
if(!drivers.HID) drivers.HID = require('node-hid');
if(!listers.HID) listers.HID = function(cb){
//console.log('***', drivers.HID.devices())
var devices = drivers.HID.devices().filter(function(device){
var isApple = device.product &&
(device.product.indexOf('Apple ') !== -1);
var isMikeyDriver = device.product === 'Apple Mikey HID Driver';
return !(isMikeyDriver || isApple || device.product === 'Keyboard Backlight');
});
if(cb) cb(devices);
};
if(!drivers.serial) drivers.serial = {SerialPort:require('serialport')};
if(!listers.serial) listers.serial = function(cb){
drivers.serial.SerialPort.list().then(function(ports){
//*
ports = ports.filter(function(device){
var result = device.path === '/dev/cu.Bluetooth-Incoming-Port';
return result;
}); // */
cb(ports);
}).catch(function(err){
});
}
var SerialPort = drivers.serial.SerialPort;
break;
case 'linux':
if(!drivers.HID) drivers.HID = require('node-hid');
if(!listers.HID) listers.HID = function(cb){
var devices = drivers.HID.devices();
if(cb) cb(devices);
};
if(!drivers.serial) drivers.serial = {SerialPort:require('serialport')};
if(!listers.serial) listers.serial = function(cb){
drivers.serial.SerialPort.list().then(function(ports){
/*
ports = ports.filter(function(device){
var result = device.path !== '/dev/cu.Bluetooth-Incoming-Port';
return result;
}); // */
cb(ports);
}).catch(function(err){
});
}
var SerialPort = drivers.serial.SerialPort;
break;
default : throw new Error('Unsupported platform: '+process.platform);
}
module.exports.listen = function(device, handler){
if(!!device.serial){
var serialDevice = new drivers.serial.SerialPort(device.serial);
serialDevice.on('data', function(data){
handler(data.toString().trim());
});
return;
}else{
if(device.vendor && device.product){
var HIDDevice = new drivers.HID.HID(device.vendor, device.product);
HIDDevice.on('data', function(data){
handler(data.toString());
});
}else{
var HIDDevice = new drivers.HID.HID(device.hid || device.path);
HIDDevice.on('data', function(data){
handler(data.toString());
});
}
}
}
module.exports.devices = function(type, callback){
if(typeof type == 'function' && !callback){
callback = type;
var results = {};
arrays.forEachEmission(Object.keys(drivers), function(key, index, done){
listers[key](function(list){
results[key] = list;
done();
});
}, function(){
callback(results);
})
}else{
listers[type](function(list){
callback(list);
});
}
};