airtouch5 0.2.0

A library for communicating with AirTouch 5 air conditioning system control consoles
Documentation
//! Show the current zone status of an AirTouch 5 controller.
//!
//! Usage:
//!     `status <ip>`
//!
//! Fetches the status of zones from the AirTouch 5 controller at `ip`, and
//! prints them.

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

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

const USAGE_STR: &str = "Usage: status <ip>";
static UNNAMED_ZONE: LazyLock<String> = LazyLock::new(|| "<unknown>".to_string());

#[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 non 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 AC unit status
    let acs = controller
        .ac_status()
        .await
        .expect("could not get AC status");
    for (_, ac) in acs.acs {
        println!("{}", ac);
    }
    println!();

    // fetch the list of zone names
    let names = controller
        .zone_names()
        .await
        .expect("could not get zone names");

    // find the longest zone name
    let w = names.by_index().map(|(_, z)| z.len()).max().unwrap_or(0);

    // fetch the current zone status
    let status = controller
        .zone_status()
        .await
        .expect("could not get zone status");

    for (idx, s) in status.zones {
        println!(
            "{:>w$}: {}",
            names.zones.get(&idx).unwrap_or(&UNNAMED_ZONE),
            s
        );
    }
}