Skip to content

Commit

Permalink
examples: add hvac-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
manio committed Sep 17, 2023
1 parent 0e02e62 commit 44bb0f7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,28 @@ mqtt://1.2.3.4:1883 is shown below:
```sh
cargo run --example mqtt-broadlink --features mqtt-broadlink -- -c 10.8.0.1 mqtt://1.2.3.4:1883
```
## HVAC client
This library includes an example HVAC/Air Conditioner client to show how to control supported devices.
The source can be found [here](hvac-cli.rs) and its usage is shown below:
```sh
USAGE:
hvac-cli MODE

MODE:
info show air conditioner state
on power ON air conditioner
off power OFF air conditioner
toggle toggle power state
```
Note: by default this client is autodiscovering all devices and it is trying to issue the command on all discovered devices.
An example of using this client to obtain information of the current state is show below:
```sh
cargo run --example hvac-cli -- info
```
73 changes: 73 additions & 0 deletions examples/hvac-cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use rbroadlink::{traits::DeviceTrait, Device};
use std::env;

#[derive(PartialEq)]
enum RunMode {
Help,
Info,
Toggle,
TurnOn,
TurnOff,
}

fn main() {
let argument = env::args().nth(1);
let run_mode = if let Some(arg) = argument {
match &arg[..] {
"info" => RunMode::Info,
"toggle" => RunMode::Toggle,
"on" => RunMode::TurnOn,
"off" => RunMode::TurnOff,
_ => RunMode::Help,
}
} else {
RunMode::Help
};

if run_mode == RunMode::Help {
println! {"No arguments given, possible choices:\n"};
println! {"info show air conditioner state"};
println! {"on power ON air conditioner"};
println! {"off power OFF air conditioner"};
println! {"toggle toggle power state"};
return;
};

println!(">>> autodiscovering broadlink devices...");
let discovered = Device::list(None).expect("Could not enumerate devices!");
for device in discovered {
println!(">>> device authentication ...");
let addr = device.get_info().address;
println!(">>> device at {} => {}", addr, device);

let hvac = match device {
Device::Hvac { hvac } => hvac,
_ => {
return;
}
};
if run_mode == RunMode::Info {
println!(">>> get_info");
let ac_info = hvac.get_info().unwrap();
println!("Current power state: {}", ac_info.power);
println!("Ambient temperature: {:.1}", ac_info.get_ambient_temp());
} else {
println!(">>> get_state");
let mut state = hvac.get_state().unwrap();
println!("Current state: {:?}", state);

// Setting desired mode according to command line argument
if run_mode == RunMode::Toggle {
state.power = !state.power;
} else if run_mode == RunMode::TurnOn {
state.power = true;
} else if run_mode == RunMode::TurnOff {
state.power = false;
}

println!(">>> set_state");
let response = hvac.set_state(&mut state).unwrap();
println!(">>> device response {:02x?}", response);
}
}
}

0 comments on commit 44bb0f7

Please sign in to comment.