Megatec UPS Control Library (Rust)
A Rust library for interfacing with Megatec protocol-compatible UPS (Uninterruptible Power Supply) devices via USB. This library provides a safe, high-level interface for monitoring and controlling UPS devices.
The library was created from scratch, initially in C, but rewritten in Rust. Through reverse engineering and in-depth analysis of the UPSilon 2000 program, I discovered how the program communicates with the UPS device.
This project is the only library in the world that has collected all the functionalities of the UPSilon 2000 program, making them open-source. The library supports the Mega(USB) protocol created by Mega System Technologies, Inc.
Features
- 🔌 USB device connection management
- 📊 Real-time UPS status monitoring
- 🔋 Battery testing capabilities
- 🔔 Beep control
- 📈 Rating information retrieval
- 🛑 Emergency shutdown control
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic example:
use ;
API Reference
Main Types
MegatecUps
Main structure for interacting with the UPS device.
let ups = new?;
UpsStatus
Structure containing UPS status information:
input_voltage: Input voltage (V)input_fault_voltage: Input fault voltage (V)output_voltage: Output voltage (V)output_current: Output current (%)input_frequency: Input frequency (Hz)battery_voltage: Battery voltage (V)temperature: Temperature (°C)
Key Methods
Device Information
get_name()- Get UPS nameget_rating()- Get UPS rating informationget_status()- Get UPS status with acknowledgmentget_status_no_ack()- Get UPS status without acknowledgment
Testing Functions
test()- Perform 10-second testtest_until_battery_low()- Test until battery is lowtest_with_time(minutes)- Test for specified durationabort_test()- Abort current test
Control Functions
switch_beep()- Toggle UPS beepshutdown()- Initiate UPS shutdown (1-minute delay)
Error Handling
The library uses a custom error type UpsError with the following variants:
Usb(UsbError)- USB communication errorsInvalidResponse- Invalid or unexpected device responseInvalidTime- Invalid time value for testing
Test Duration Calculation
The library includes a special algorithm for calculating test durations:
- 1-9 minutes: values 101-109
- 10-19 minutes: values 125-134
- 20-99 minutes: calculated using range-based formula
Building from Source
# Clone the repository
# Build the library
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.