diff --git a/nut-client/examples/async.rs b/nut-client/examples/async.rs index 7dcc948..a004b27 100644 --- a/nut-client/examples/async.rs +++ b/nut-client/examples/async.rs @@ -36,9 +36,21 @@ async fn main() -> nut_client::Result<()> { println!("\t- Name: {}", name); println!("\t Description: {}", description); + // Get list of mutable variables + let mutable_vars = conn.list_mutable_variables(&name).await?; + // List UPS variables (key = val) - println!("\t Variables:"); + println!("\t Mutable Variables:"); + for var in mutable_vars.iter() { + println!("\t\t- {}", var); + } + + // List UPS immutable properties (key = val) + println!("\t Immutable Properties:"); for var in conn.list_vars(&name).await? { + if mutable_vars.iter().any(|v| v.name() == var.name()) { + continue; + } println!("\t\t- {}", var); } diff --git a/nut-client/examples/blocking.rs b/nut-client/examples/blocking.rs index 2bfaa0e..f7492ff 100644 --- a/nut-client/examples/blocking.rs +++ b/nut-client/examples/blocking.rs @@ -35,9 +35,21 @@ fn main() -> nut_client::Result<()> { println!("\t- Name: {}", name); println!("\t Description: {}", description); + // Get list of mutable variables + let mutable_vars = conn.list_mutable_variables(&name)?; + // List UPS variables (key = val) - println!("\t Variables:"); + println!("\t Mutable Variables:"); + for var in mutable_vars.iter() { + println!("\t\t- {}", var); + } + + // List UPS immutable properties (key = val) + println!("\t Immutable Properties:"); for var in conn.list_vars(&name)? { + if mutable_vars.iter().any(|v| v.name() == var.name()) { + continue; + } println!("\t\t- {}", var); } diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index 6af9834..d314bbb 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -84,6 +84,10 @@ pub enum Response { /// /// Params: (command description) CmdDesc(String), + /// A mutable variable (RW) response. + /// + /// Params: (var name, var value) + Rw(String, String), } impl Response { @@ -170,6 +174,30 @@ impl Response { }?; Ok(Response::Var(var_name, var_value)) } + "RW" => { + let _var_device = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified RW device name in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + let var_name = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified RW name in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + let var_value = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified RW value in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + Ok(Response::Rw(var_name, var_value)) + } "UPS" => { let name = if args.is_empty() { Err(ClientError::from(NutError::Generic( @@ -277,6 +305,14 @@ impl Response { } } + pub fn expect_rw(&self) -> crate::Result { + if let Self::Rw(name, value) = &self { + Ok(Variable::parse(name, value.to_owned())) + } else { + Err(NutError::UnexpectedResponse.into()) + } + } + pub fn expect_ups(&self) -> crate::Result<(String, String)> { if let Self::Ups(name, description) = &self { Ok((name.to_owned(), description.to_owned())) @@ -532,6 +568,14 @@ implement_list_commands! { ) } + /// Queries the list of mutable variables for a UPS device. + pub fn list_mutable_variables(ups_name: &str) -> Vec { + ( + { &["RW", ups_name] }, + { |row: Response| row.expect_rw() }, + ) + } + /// Queries the list of commands available for the given device. pub fn list_commands(ups_name: &str) -> Vec { ( diff --git a/nut-client/src/var.rs b/nut-client/src/var.rs index b39a8ca..7401dda 100644 --- a/nut-client/src/var.rs +++ b/nut-client/src/var.rs @@ -30,7 +30,7 @@ pub mod key { /// Well-known variables for NUT UPS devices. /// /// List retrieved from: https://networkupstools.org/docs/user-manual.chunked/apcs01.html -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum Variable { /// Device model. DeviceModel(String), @@ -123,7 +123,7 @@ impl fmt::Display for Variable { } /// NUT device type. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum DeviceType { /// UPS (Uninterruptible Power Supply) Ups,