获取已连接设备的USB端口在Node中的表示方法

4
我正在使用node-detection来通知我何时插入USB设备。然而,我似乎找不到如何知道它插入了哪个端口,例如sdb1,sdc1等。有没有简单的方法可以做到这一点?

你是否在npmjs上使用node-usb-detection?它基于usb(npmjs)。很难获得端口。我也在寻找解决方案。如果我找到了解决方案,我会发布的。 - Johan Hoeksma
3个回答

4
你可以使用drivelist模块,除此之外还需要使用由resin.io团队开发的node-detection模块。
drivelist模块列出所有连接的设备,包括挂载点(路径)信息。这个输出因操作系统而异。请查看drivelist文档以获取更多信息。
以下是一个快速示例,用于查找何时插入和挂载可移动USB驱动器,并获取挂载路径。
通过一些测试,当设备插入时,usb-detection模块几乎瞬间触发,但是设备需要时间来挂载。因此,我使用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的情况。你可能需要添加超时或类似的东西。希望能有所帮助,祝好运。

2

我认为它在节点检测方面无法正常工作。

也许不是最好的方法,但这对我起到了作用:

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;
});

请在您的问题中说明您的输出结果是什么。您尝试了哪些代码...

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)

一些注意事项:

  • 一个USB存储设备上可能有多个分区,或者它可能直接有一个文件系统而没有分区。
  • 你可能不需要专门监视USB设备,只需监视任何块设备即可。
  • 这并没有进行任何过滤,你会得到很多无关的事件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接