periphera
v1.2.13
Published
Periphera: A sleek and efficient Node.js library for real-time monitoring of serial and HID devices. Seamlessly track the addition and removal of peripherals with ease, ensuring your applications stay in sync with connected hardware. Perfect for developer
Downloads
42
Maintainers
Readme
Periphera
Periphera is a library for monitoring serial and HID devices for changes. It provides an elegant API to work with devices, allowing you to find devices by various properties and monitor device changes efficiently.
Installation
Install the library using npm:
npm install periphera
Usage
Importing the Library
First, import the necessary components from the library:
import { DeviceChangeEvent } from "periphera";
import { DeviceManager } from "periphera";
import { DeviceMonitor } from "periphera";
Creating a DeviceMonitor Instance
Create an instance of DeviceMonitor
to start monitoring devices. You can specify the monitoring interval and whether to filter out ghost devices (devices with undefined manufacturers).
const monitor = new DeviceMonitor(2000, true);
Creating a DeviceManager Instance
Create an instance of DeviceManager
using the DeviceMonitor
instance.
const manager = new DeviceManager(monitor);
Initializing the DeviceMonitor
Call the initialize
method on DeviceMonitor
to set up devices and start monitoring.
await monitor.initialize();
Listening to Device Changes
You can listen to device change events (added or removed) using the deviceMonitor.on
method:
monitor.on('deviceChange', (event: DeviceChangeEvent) => {
console.log(`Device ${event.type}:`, event.deviceType, event.device);
});
Finding Devices by Properties
The DeviceManager
class provides methods to find devices by various properties. You can find devices by a specific property in a case-insensitive and partial match manner.
Find Devices by Property
To find devices by a specific property:
const devicesByManufacturer = manager.findDeviceBy('manufacturer', 'ftdi');
console.log('Devices by Manufacturer:', devicesByManufacturer);
Full Example
Here is a complete example of how to use the DeviceMonitor
and DeviceManager
classes to monitor devices and find devices by specific properties:
import { DeviceChangeEvent } from "periphera";
import { DeviceManager } from "periphera";
import { DeviceMonitor } from "periphera";
const monitor = new DeviceMonitor(2000, true);
const manager = new DeviceManager(monitor);
monitor.on('deviceChange', (event: DeviceChangeEvent) => {
console.log(`Device ${event.type}:`, event.deviceType, event.device);
});
monitor.initialize().then(() => {
const devicesByManufacturer = manager.findDeviceBy('manufacturer', 'ftdi');
console.log('Devices by Manufacturer:', devicesByManufacturer);
}).catch(err => {
console.error('Error initializing devices:', err);
});
API Reference
DeviceMonitor
Constructor
new DeviceMonitor(monitoringInterval?: number, filterGhostDevices?: boolean);
monitoringInterval
(optional): The interval (in milliseconds) at which to scan for device changes. Default is 2000ms.filterGhostDevices
(optional): Boolean to enable or disable filtering of ghost devices (devices with undefined manufacturers). Default is false.
Methods
initialize(): Promise<void>
: Initializes the device lists and starts monitoring for changes.on(event: DeviceEventNames, listener: (event: DeviceChangeEvent) => void): this
: Adds an event listener for the specified event.getSerialDevices(): SerialInfo
: Gets the current list of monitored serial devices.getHIDDevices(): HIDInfo
: Gets the current list of monitored HID devices.stopMonitoring(): void
: Stops the device monitoring.
DeviceManager
Constructor
new DeviceManager(deviceMonitor: DeviceMonitor);
deviceMonitor
: An instance ofDeviceMonitor
.
Methods
findDeviceBy(property: keyof DeviceInfo, value: string): DeviceInfo[]
: Finds devices by a given property and value (case-insensitive and partial match).
License
This project is licensed under the MIT License - see the LICENSE file for details.