airtouch5 0.2.0

A library for communicating with AirTouch 5 air conditioning system control consoles
Documentation
//! Show the status of an AirTouch 5 controller
//!
//! Usage:
//!     `info <ip>`

use std::{net::IpAddr, str::FromStr};

use airtouch5::AirTouch5;
use simplelog::TermLogger;

const USAGE_STR: &str = "Usage: info <ip>";

#[tokio::main(flavor = "current_thread")]
pub async fn main() {
    // log to console
    TermLogger::init(
        log::LevelFilter::Debug,
        simplelog::Config::default(),
        simplelog::TerminalMode::Mixed,
        simplelog::ColorChoice::Auto,
    )
    .expect("could not init logger");

    // connect to the given addess
    let addr = IpAddr::from_str(&std::env::args().nth(1).expect(USAGE_STR)).expect(USAGE_STR);
    let controller = AirTouch5::with_ipaddr(addr)
        .await
        .expect("could not connect");

    // fetch the console version
    let vers = controller
        .console_version()
        .await
        .expect("could not get console version");

    println!(
        "Airtouch 5 Console version{} {}{}",
        if vers.versions.len() > 1 { "s" } else { "" },
        vers.versions.join(", "),
        if vers.update_available {
            " (update available)"
        } else {
            ""
        }
    );

    // fetch the AC unit capabilities
    let caps = controller
        .ac_capabilities()
        .await
        .expect("could not get capabilities");

    // find the longest AC unit name
    let w = caps
        .by_index()
        .map(|(_, ac)| ac.name.len())
        .max()
        .unwrap_or(0);

    // print each unit's capabilities
    for (_, ac) in caps.by_index() {
        println!("{:#w$}", ac);
    }
}