Initial commit
This commit is contained in:
commit
dccb1ccf9f
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# ---> Rust
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "linux_battery"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
32
README.md
Normal file
32
README.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# linux_battery
|
||||||
|
|
||||||
|
A battery parser using linux' `power_supply` class.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
### debug
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cargo build
|
||||||
|
```
|
||||||
|
|
||||||
|
### release
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cargo build --release
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cargo doc
|
||||||
|
```
|
||||||
|
|
||||||
|
The built documentation can be found at `target/doc/`.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cargo build --example bat0
|
||||||
|
cargo run --example bat0
|
||||||
|
```
|
11
examples/bat0.rs
Normal file
11
examples/bat0.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use linux_battery;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut battery = linux_battery::Battery::new("BAT0");
|
||||||
|
let mut msg = format!("Status: {}", battery.state());
|
||||||
|
match battery.capacity() {
|
||||||
|
Some(cap) => msg = format!("{}\tCapacity: {} %", msg, cap),
|
||||||
|
None => {},
|
||||||
|
}
|
||||||
|
println!("{}", msg);
|
||||||
|
}
|
78
src/lib.rs
Normal file
78
src/lib.rs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
/// Battery parser using linux' `power_supply` class
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct Battery {
|
||||||
|
state_file: String,
|
||||||
|
capacity_file: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Battery {
|
||||||
|
/// Returns a battery representing object.
|
||||||
|
/// # Arguments:
|
||||||
|
/// * `name` - The name of the battery as it is represented in the sys file system.
|
||||||
|
/// (`/sys/class/power_supply/<name>`)
|
||||||
|
/// # Example:
|
||||||
|
/// ```
|
||||||
|
/// mod battery;
|
||||||
|
/// use battery::Battery;
|
||||||
|
/// let mut battery = Battery::new("BAT0");
|
||||||
|
/// ```
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn new(name: &str) -> Battery {
|
||||||
|
let path: String = format!("/sys/class/power_supply/{}/", name);
|
||||||
|
Battery {
|
||||||
|
state_file: format!("{}status", path),
|
||||||
|
capacity_file: format!("{}capacity", path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current state of the battery.
|
||||||
|
/// Return values:
|
||||||
|
/// * `"Unknown"`
|
||||||
|
/// * `"Charging"`
|
||||||
|
/// * `"Discharging"`
|
||||||
|
/// * `"Not_Charging"`
|
||||||
|
/// * `"Full"`
|
||||||
|
/// # Example:
|
||||||
|
/// ```
|
||||||
|
/// println!("Battery state: {}", battery.state());
|
||||||
|
/// ```
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn state(&mut self) -> String {
|
||||||
|
match Battery::read_file(&self.state_file) {
|
||||||
|
Ok(status) => String::from(status.trim()),
|
||||||
|
Err(_) => String::from("Unknown"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current capacity of the battery [%].
|
||||||
|
/// # Example:
|
||||||
|
/// ```
|
||||||
|
/// match battery.capacity() {
|
||||||
|
/// Some(cap) => println!("{} %", cap),
|
||||||
|
/// None => {},
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn capacity(&mut self) ->Option<u8> {
|
||||||
|
match Battery::read_file(&self.capacity_file) {
|
||||||
|
Ok(capacity) => Some(capacity.trim().parse::<u8>().unwrap_or(0)),
|
||||||
|
Err(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_file(name: &str) -> Result<String, io::Error> {
|
||||||
|
let f = std::fs::File::open(name);
|
||||||
|
let mut f = match f {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
let mut line = String::new();
|
||||||
|
match f.read_to_string(&mut line) {
|
||||||
|
Ok(_) => Ok(line),
|
||||||
|
Err(e) =>Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user