From 2360bd4a3f00142740bd7dd3e7e01a3a35866ea8 Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sun, 1 Aug 2021 01:19:31 -0400 Subject: [PATCH] Implement GET NUMLOGINS Fixes #11 --- nut-client/examples/async.rs | 4 ++++ nut-client/examples/blocking.rs | 1 + nut-client/src/cmd.rs | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/nut-client/examples/async.rs b/nut-client/examples/async.rs index 704b710..16a95e4 100644 --- a/nut-client/examples/async.rs +++ b/nut-client/examples/async.rs @@ -35,6 +35,10 @@ async fn main() -> nut_client::Result<()> { for (name, description) in conn.list_ups().await? { println!("\t- Name: {}", name); println!("\t Description: {}", description); + println!( + "\t Number of logins: {}", + conn.get_num_logins(&name).await? + ); // Get list of mutable variables let mutable_vars = conn.list_mutable_vars(&name).await?; diff --git a/nut-client/examples/blocking.rs b/nut-client/examples/blocking.rs index 7fbf037..c8924a6 100644 --- a/nut-client/examples/blocking.rs +++ b/nut-client/examples/blocking.rs @@ -34,6 +34,7 @@ fn main() -> nut_client::Result<()> { for (name, description) in conn.list_ups()? { println!("\t- Name: {}", name); println!("\t Description: {}", description); + println!("\t Number of logins: {}", conn.get_num_logins(&name)?); // Get list of mutable variables let mutable_vars = conn.list_mutable_vars(&name)?; diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index 199a859..e97daf1 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -92,6 +92,10 @@ pub enum Response { /// /// Params: (variable description) Desc(String), + /// A NUMLOGINS response. + /// + /// Params (number of logins) + NumLogins(i32), } impl Response { @@ -301,6 +305,28 @@ impl Response { }?; Ok(Response::Desc(desc)) } + "NUMLOGINS" => { + let _device = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified NUMLOGINS device in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + let num = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified NUMLOGINS number in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + let num = num.parse::().map_err(|_| { + ClientError::from(NutError::Generic( + "Invalid NUMLOGINS number in response".into(), + )) + })?; + Ok(Response::NumLogins(num)) + } _ => Err(NutError::UnknownResponseType(cmd_name).into()), } } @@ -380,6 +406,14 @@ impl Response { Err(NutError::UnexpectedResponse.into()) } } + + pub fn expect_numlogins(&self) -> crate::Result { + if let Self::NumLogins(num) = &self { + Ok(*num) + } else { + Err(NutError::UnexpectedResponse.into()) + } + } } /// A macro for implementing `LIST` commands. @@ -645,6 +679,14 @@ implement_get_commands! { { |row: Response| row.expect_cmddesc() }, ) } + + /// Queries the number of logins to the specified UPS. + pub fn get_num_logins(ups_name: &str) -> i32 { + ( + { &["NUMLOGINS", ups_name] }, + { |row: Response| row.expect_numlogins() }, + ) + } } implement_simple_commands! {