-
Notifications
You must be signed in to change notification settings - Fork 31
/
udev.js
53 lines (44 loc) · 1.22 KB
/
udev.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
const EventEmitter = require('events').EventEmitter;
const udev = require('bindings')('udev');
const inherits = require('util').inherits
const Monitor = udev.Monitor;
inherits(Monitor, EventEmitter);
const getDetailedNodeChain = node => {
let result = [], extra;
while (node && node.hasOwnProperty('syspath')) {
extra = udev.getSysattrBySyspath(node.syspath);
for (let key in extra) {
if (extra.hasOwnProperty(key)) {
node[key] = extra[key];
}
}
result.push(node);
node = udev.getNodeParentBySyspath(node.syspath);
}
return result;
};
const getNodeDetailsSummary = node => {
let result = {},
nodes = getDetailedNodeChain(node),
key;
while (nodes.length != 0) {
node = nodes.pop();
for (let key in node) {
if (node.hasOwnProperty(key)) {
if (!result.hasOwnProperty(key)) {
result[key] = [];
}
result[key].push(node[key]);
}
}
}
return result;
};
module.exports = {
monitor: subsystem => new Monitor(subsystem),
list: udev.list,
getNodeParentBySyspath: udev.getNodeParentBySyspath,
getSysattrBySyspath: udev.getSysattrBySyspath,
getNodeChain: getDetailedNodeChain,
getNodeDetails: getNodeDetailsSummary
};