node.js - 如何在 raspbian/ubuntu 下从 nodejs 检测 SD 卡插入和 USB HDD 插入?
问题描述
我想要节点 js 的两件事:
- 检测 SD 卡插入 SD 卡读卡器并获取安装点(和可用空间)
- 检测 USB HDD 何时插入 USB 端口,并获取安装点(和可用空间)
我正在使用 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); } );
解决方案
推荐阅读
- sql - SQL-Server 在另一个表中向下钻取(分组依据)
- session - JSF Web 应用程序超时问题
- python - 在矢量化循环实现中使用 Pandas 提高速度和效率
- python - pandas:沿轴 = 1 的 DataFrame 的低级连接
- qt - 子窗口中的 QFileDialog 关闭应用程序
- regex - 如何在 python 中提取 URL 的某个部分?
- sql-server - 将 json 字符串保存到 SQL Server
- matlab - 类似于 R 的极坐标直方图
- r - 另一个“有限的'xlim'值”问题
- c++ - 将 HEX 转换为可打印的字符串/字符