|
| 1 | +# Megatec UPS Control Library (Rust) |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- 🔌 USB device connection management |
| 12 | +- 📊 Real-time UPS status monitoring |
| 13 | +- 🔋 Battery testing capabilities |
| 14 | +- 🔔 Beep control |
| 15 | +- 📈 Rating information retrieval |
| 16 | +- 🛑 Emergency shutdown control |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Add this to your `Cargo.toml`: |
| 21 | + |
| 22 | +```toml |
| 23 | +[dependencies] |
| 24 | +megatec-ups-control = "0.1.0" |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +Basic example: |
| 30 | + |
| 31 | +```rust |
| 32 | +use megatec_ups_control::{MegatecUps, Result, UpsStatus}; |
| 33 | + |
| 34 | +fn main() -> Result<()> { |
| 35 | + // Create a new UPS connection |
| 36 | + // Replace these with your actual vendor and product IDs |
| 37 | + let ups = match MegatecUps::new(0x0001, 0x0000) { |
| 38 | + Ok(ups) => { |
| 39 | + println!("Successfully connected to UPS device"); |
| 40 | + ups |
| 41 | + } |
| 42 | + Err(e) => { |
| 43 | + println!("Failed to connect to UPS device: {:?}", e); |
| 44 | + return Err(e); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + // Get UPS name |
| 49 | + let name: String = ups.get_name()?; |
| 50 | + println!("UPS Name: {}", name); |
| 51 | + |
| 52 | + // Get UPS status |
| 53 | + let status: UpsStatus = ups.get_status()?; |
| 54 | + println!("UPS Status:"); |
| 55 | + println!(" Input Voltage: {} V", status.input_voltage); |
| 56 | + println!(" Input Fault Voltage: {} V", status.input_fault_voltage); |
| 57 | + println!(" Output Voltage: {} V", status.output_voltage); |
| 58 | + println!(" Output Current: {}%", status.output_current); |
| 59 | + println!(" Input Frequency: {} Hz", status.input_frequency); |
| 60 | + println!(" Battery Voltage: {} V", status.battery_voltage); |
| 61 | + println!(" Temperature: {} °C", status.temperature); |
| 62 | + |
| 63 | + // Perform a 10-second test |
| 64 | + println!("Performing 10-second test..."); |
| 65 | + ups.test()?; |
| 66 | + |
| 67 | + Ok(()) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## API Reference |
| 72 | + |
| 73 | +### Main Types |
| 74 | + |
| 75 | +#### `MegatecUps` |
| 76 | +Main structure for interacting with the UPS device. |
| 77 | + |
| 78 | +```rust |
| 79 | +let ups = MegatecUps::new(vendor_id, product_id)?; |
| 80 | +``` |
| 81 | + |
| 82 | +#### `UpsStatus` |
| 83 | +Structure containing UPS status information: |
| 84 | +- `input_voltage`: Input voltage (V) |
| 85 | +- `input_fault_voltage`: Input fault voltage (V) |
| 86 | +- `output_voltage`: Output voltage (V) |
| 87 | +- `output_current`: Output current (%) |
| 88 | +- `input_frequency`: Input frequency (Hz) |
| 89 | +- `battery_voltage`: Battery voltage (V) |
| 90 | +- `temperature`: Temperature (°C) |
| 91 | + |
| 92 | +### Key Methods |
| 93 | + |
| 94 | +#### Device Information |
| 95 | +- `get_name()` - Get UPS name |
| 96 | +- `get_rating()` - Get UPS rating information |
| 97 | +- `get_status()` - Get UPS status with acknowledgment |
| 98 | +- `get_status_no_ack()` - Get UPS status without acknowledgment |
| 99 | + |
| 100 | +#### Testing Functions |
| 101 | +- `test()` - Perform 10-second test |
| 102 | +- `test_until_battery_low()` - Test until battery is low |
| 103 | +- `test_with_time(minutes)` - Test for specified duration |
| 104 | +- `abort_test()` - Abort current test |
| 105 | + |
| 106 | +#### Control Functions |
| 107 | +- `switch_beep()` - Toggle UPS beep |
| 108 | +- `shutdown()` - Initiate UPS shutdown (1-minute delay) |
| 109 | + |
| 110 | +## Error Handling |
| 111 | + |
| 112 | +The library uses a custom error type `UpsError` with the following variants: |
| 113 | +- `Usb(UsbError)` - USB communication errors |
| 114 | +- `InvalidResponse` - Invalid or unexpected device response |
| 115 | +- `InvalidTime` - Invalid time value for testing |
| 116 | + |
| 117 | +## Test Duration Calculation |
| 118 | + |
| 119 | +The library includes a special algorithm for calculating test durations: |
| 120 | +- 1-9 minutes: values 101-109 |
| 121 | +- 10-19 minutes: values 125-134 |
| 122 | +- 20-99 minutes: calculated using range-based formula |
| 123 | + |
| 124 | +## Building from Source |
| 125 | + |
| 126 | +```bash |
| 127 | +# Clone the repository |
| 128 | +git clone https://github.com/piotrmaciejbednarski/megatec-ups-control |
| 129 | +cd megatec-ups-control |
| 130 | + |
| 131 | +# Build the library |
| 132 | +cargo build --release |
| 133 | +``` |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments