首页 > 解决方案 > 如何在 raspbian/ubuntu 下从 nodejs 检测 SD 卡插入和 USB HDD 插入?

问题描述

我想要节点 js 的两件事:

我正在使用 raspbian/ubuntu linux,我希望有一个解决方案,我不需要从 setInterval 调用中轮询设备。

我现在正在使用“usb-detection”库,它检测 USB 设备何时插入,但它不检测 sd 卡插入。

我知道有一个“驱动列表”模块,但它需要不断地轮询它。有没有更好的办法?(因为 linux 知道安装新事物)。

或者我可以用其他方式问同样的问题,如何在不轮询“驱动器列表”的情况下检测何时将某些东西安装到 raspbian/ubuntu?

编辑:

同时我有另一个想法,如果我观察 /media 文件夹,系统会将连接的驱动器安装到那里。所以我可以观察文件系统。你怎么看?

const chokidar = require('chokidar');
var baseDirToWatch='/media';

var deviceList=[];

const driveAdded = function (path)
{
  if (path!==baseDirToWatch)
  {
    deviceList.push(path);
    console.log(deviceList);
  }
}

const driveRemoved= function (path)
{
  deviceList.indexOf(path) > -1 ? deviceList.splice(deviceList.indexOf(path), 1) : false
  console.log(deviceList);
}

const watcher = chokidar.watch(baseDirToWatch, {
  persistent: true,
  depth:1
});

watcher
  .on('addDir', path => { driveAdded(path); })
  .on('unlinkDir', path => { driveRemoved(path); } );

标签: node.jsdetectionmountsd-cardusb-drive

解决方案


推荐阅读