@mcesystems/usb-device-listener 1.0.62 → 1.0.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,8 +32,20 @@ npm install
|
|
|
32
32
|
|
|
33
33
|
## Quick Start
|
|
34
34
|
|
|
35
|
+
Examples below use `@mcesystems/tool-debug` for formatted logs.
|
|
36
|
+
|
|
35
37
|
```javascript
|
|
36
|
-
import usbListener from
|
|
38
|
+
import usbListener from "usb-device-listener";
|
|
39
|
+
import {
|
|
40
|
+
logDataObject,
|
|
41
|
+
logErrorObject,
|
|
42
|
+
logHeader,
|
|
43
|
+
logNamespace,
|
|
44
|
+
setLogLevel
|
|
45
|
+
} from "@mcesystems/tool-debug";
|
|
46
|
+
|
|
47
|
+
logNamespace("usb");
|
|
48
|
+
setLogLevel("debug");
|
|
37
49
|
|
|
38
50
|
// Define configuration
|
|
39
51
|
const config = {
|
|
@@ -50,31 +62,57 @@ const config = {
|
|
|
50
62
|
listenOnlyHubs: []
|
|
51
63
|
};
|
|
52
64
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
function handleDeviceAdd(device) {
|
|
66
|
+
logDataObject("Device connected", {
|
|
67
|
+
locationInfo: device.locationInfo,
|
|
68
|
+
vid: device.vid.toString(16).toUpperCase().padStart(4, "0"),
|
|
69
|
+
pid: device.pid.toString(16).toUpperCase().padStart(4, "0"),
|
|
70
|
+
logicalPort: device.logicalPort ?? "<unmapped>"
|
|
71
|
+
});
|
|
72
|
+
}
|
|
59
73
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
74
|
+
function handleDeviceRemove(device) {
|
|
75
|
+
logDataObject("Device disconnected", {
|
|
76
|
+
locationInfo: device.locationInfo,
|
|
77
|
+
logicalPort: device.logicalPort ?? "<unmapped>"
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Register event handlers
|
|
82
|
+
usbListener.onDeviceAdd(handleDeviceAdd);
|
|
83
|
+
usbListener.onDeviceRemove(handleDeviceRemove);
|
|
63
84
|
|
|
64
85
|
// Start listening
|
|
65
86
|
try {
|
|
66
87
|
usbListener.startListening(config);
|
|
67
|
-
|
|
88
|
+
logHeader("Listening for USB events");
|
|
68
89
|
} catch (error) {
|
|
69
|
-
|
|
90
|
+
logErrorObject(error, "Failed to start");
|
|
70
91
|
}
|
|
71
92
|
|
|
72
93
|
// Graceful shutdown
|
|
73
94
|
process.on('SIGINT', () => {
|
|
95
|
+
logHeader("Stopping USB listener");
|
|
74
96
|
usbListener.stopListening();
|
|
75
97
|
process.exit(0);
|
|
76
98
|
});
|
|
77
99
|
```
|
|
100
|
+
Example output:
|
|
101
|
+
```
|
|
102
|
+
usb:info ================================================================================
|
|
103
|
+
usb:info Listening for USB events
|
|
104
|
+
usb:info ================================================================================
|
|
105
|
+
usb:detail ================================================================================
|
|
106
|
+
usb:detail *Device connected* handleDeviceAdd
|
|
107
|
+
usb:detail ================================================================================
|
|
108
|
+
usb:detail
|
|
109
|
+
usb:detail # locationInfo: Port_#0005.Hub_#0002
|
|
110
|
+
usb:detail # vid: 04E8
|
|
111
|
+
usb:detail # pid: 6860
|
|
112
|
+
usb:detail # logicalPort: 1
|
|
113
|
+
usb:detail
|
|
114
|
+
usb:detail ================================================================================
|
|
115
|
+
```
|
|
78
116
|
|
|
79
117
|
## API Reference
|
|
80
118
|
|
|
@@ -134,10 +172,15 @@ Register callback for device connection events.
|
|
|
134
172
|
|
|
135
173
|
**Example:**
|
|
136
174
|
```javascript
|
|
175
|
+
import { logDataObject } from "@mcesystems/tool-debug";
|
|
176
|
+
|
|
137
177
|
usbListener.onDeviceAdd((device) => {
|
|
138
|
-
const vidHex = device.vid.toString(16).toUpperCase().padStart(4,
|
|
139
|
-
const pidHex = device.pid.toString(16).toUpperCase().padStart(4,
|
|
140
|
-
|
|
178
|
+
const vidHex = device.vid.toString(16).toUpperCase().padStart(4, "0");
|
|
179
|
+
const pidHex = device.pid.toString(16).toUpperCase().padStart(4, "0");
|
|
180
|
+
logDataObject("Device connected", {
|
|
181
|
+
device: `${vidHex}:${pidHex}`,
|
|
182
|
+
port: device.logicalPort ?? "<unmapped>"
|
|
183
|
+
});
|
|
141
184
|
});
|
|
142
185
|
```
|
|
143
186
|
|
|
@@ -147,8 +190,12 @@ Register callback for device disconnection events. Device info format same as `o
|
|
|
147
190
|
|
|
148
191
|
**Example:**
|
|
149
192
|
```javascript
|
|
193
|
+
import { logDataObject } from "@mcesystems/tool-debug";
|
|
194
|
+
|
|
150
195
|
usbListener.onDeviceRemove((device) => {
|
|
151
|
-
|
|
196
|
+
logDataObject("Device disconnected", {
|
|
197
|
+
port: device.logicalPort ?? "<unmapped>"
|
|
198
|
+
});
|
|
152
199
|
});
|
|
153
200
|
```
|
|
154
201
|
|
|
@@ -160,9 +207,15 @@ Get list of all currently connected USB devices.
|
|
|
160
207
|
|
|
161
208
|
**Example:**
|
|
162
209
|
```javascript
|
|
210
|
+
import { logDataObject } from "@mcesystems/tool-debug";
|
|
211
|
+
|
|
163
212
|
const devices = usbListener.listDevices();
|
|
164
|
-
devices.forEach(device => {
|
|
165
|
-
|
|
213
|
+
devices.forEach((device) => {
|
|
214
|
+
logDataObject("Device", {
|
|
215
|
+
locationInfo: device.locationInfo,
|
|
216
|
+
vid: device.vid.toString(16).toUpperCase().padStart(4, "0"),
|
|
217
|
+
pid: device.pid.toString(16).toUpperCase().padStart(4, "0")
|
|
218
|
+
});
|
|
166
219
|
});
|
|
167
220
|
```
|
|
168
221
|
|