Skip to content

Commit d48891a

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 6bdfa93 + 06f3466 commit d48891a

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

.github/workflows/rust.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Piotr Bednarski
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)