diff --git a/examples/README.md b/examples/README.md index 3af89f8..d925508 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 +``` diff --git a/examples/hvac-cli.rs b/examples/hvac-cli.rs new file mode 100644 index 0000000..0dbb84c --- /dev/null +++ b/examples/hvac-cli.rs @@ -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); + } + } +}