我正在使用node-detection
在插入USB
设备时通知。但是我似乎无法知道如何知道它插入的端口,例如sdb1,sdc1等。有一种简单的方法吗?
答案 0 :(得分:3)
除了由resin.io的优秀人员制作的节点检测模块之外,您还可以使用drivelist模块。
驱动器模块列出所有连接的设备,包括安装点(路径)信息。此输出略有不同,具体取决于您的操作系统。查看驱动程序文档以获取更多信息。
这是一个快速示例,可以找到插入和安装可移动USB驱动器的时间,并获取安装路径。
在某些测试中,usb检测模块在插入设备时几乎立即触发,但设备安装需要一些时间。所以我使用setInterval来检查是否存在挂载点。找到挂载点后,我清除间隔。
// load modules
var usbDetect = require('usb-detection');
const drivelist = require('drivelist');
var checkUSBintervalID;
// This is the listener function for the 'add' event
function addUSBhandler() {
console.log('added event listener for usb ADD');
// Start interval to check if the USB is mounted
checkUSBintervalID = setInterval(function() {
drivelist.list(function(error, drives) {
console.log('listening for USB mount...');
if (error) { throw error; }
// iterate through all drives
for(var d = 0; d < drives.length; d++) {
// if the drive is a removable usb
if(drives[d].system == false) {
// if this drive is mounted
if(drives[d].mountpoints.length > 0) {
console.log('found removable USB');
// the drive is mounted
// do stuff here
// the path is at drives[d].mountpoints[0].path
clearInterval(checkUSBintervalID);
}
}
}
});
}, 1000);
}
// Add listener for when a USB is plugged in
usbDetect.on('add', addUSBhandler);
如果在安装USB之前插入和拔出USB,则不会考虑此答案。您可能需要添加超时或类似的东西。希望这有所帮助,祝你好运。
答案 1 :(得分:1)
我认为它没有使用节点检测
也许不是最好的方式,但这对我有用:
var shell = require('shelljs');
var watch = require('node-watch');
var tmpUsb;
Array.prototype.contains = function(needle){
for (var i=0; i<this.length; i++)
if (this[i] == needle) return true;
return false;
}
Array.prototype.diff = function(compare) {
return this.filter(function(elem) {return !compare.contains(elem);})
}
getUsb = function() {
this.usbJson = JSON.parse(shell.exec('lsblk --json', {silent:true}).stdout);
var dev = this.usbJson.blockdevices;
var devices = [];
dev.forEach(function(entry) {
entry.children.forEach(function(e) {
devices.push(e.mountpoint);
});
});
return devices;
}
tmpUsb = getUsb();
watch('/dev/disk/by-id', { recursive: true }, function(evt, name) {
var curr = getUsb();
var add = curr.diff(tmpUsb);
var rem = tmpUsb.diff(curr);
if(add.length > 0) {
console.log("ADD > " + add);
}
if(rem.length > 0) {
console.log("REM > " + rem);
}
tmpUsb = curr;
});
请在您的问题中添加您的输出内容。你尝试了什么代码......
答案 2 :(得分:0)
我不知道node.js绑定,但使用外部udevadm
工具会泄露信息:
$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[766928.886896] add /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
KERNEL[766928.888766] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
UDEV [766929.091194] add /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
KERNEL[766930.777996] add /module/usb_storage (module)
UDEV [766930.780203] add /module/usb_storage (module)
KERNEL[766930.781054] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
KERNEL[766930.781241] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766930.781370] add /bus/usb/drivers/usb-storage (drivers)
UDEV [766930.782320] add /bus/usb/drivers/usb-storage (drivers)
KERNEL[766931.092511] add /module/uas (module)
UDEV [766931.092584] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
KERNEL[766931.092608] add /bus/usb/drivers/uas (drivers)
UDEV [766931.093550] add /module/uas (module)
UDEV [766931.094000] add /bus/usb/drivers/uas (drivers)
UDEV [766931.094305] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
UDEV [766931.095737] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766931.807635] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766931.807770] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
KERNEL[766931.807901] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
KERNEL[766931.807995] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
KERNEL[766931.809543] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766931.809605] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766931.811736] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766931.812771] add /devices/virtual/bdi/8:16 (bdi)
UDEV [766931.815246] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
UDEV [766931.815908] add /devices/virtual/bdi/8:16 (bdi)
UDEV [766931.818228] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
UDEV [766931.819748] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766931.821506] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
UDEV [766931.821894] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766931.895701] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
KERNEL[766931.895807] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766931.895879] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766933.474661] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
UDEV [766933.566443] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766933.658413] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766942.802911] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
KERNEL[766942.804048] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766942.804163] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
KERNEL[766942.804246] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
KERNEL[766942.804338] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
KERNEL[766942.804426] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766942.804518] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
KERNEL[766942.804590] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
UDEV [766942.806838] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766942.807543] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
UDEV [766942.809181] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
UDEV [766942.809410] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
UDEV [766942.812382] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
UDEV [766942.812545] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766942.814711] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
UDEV [766942.815300] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
KERNEL[766942.816341] remove /devices/virtual/bdi/8:16 (bdi)
KERNEL[766942.816384] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
UDEV [766942.817716] remove /devices/virtual/bdi/8:16 (bdi)
UDEV [766942.819175] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766942.864803] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766942.864891] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
KERNEL[766942.865168] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
KERNEL[766942.866960] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
UDEV [766942.867670] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
UDEV [766942.868469] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
UDEV [766942.869291] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
UDEV [766942.870791] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
一些注意事项: