Implement LIST RW

Fixes #17
This commit is contained in:
Aram 🍐 2021-08-01 00:41:28 -04:00
parent b6e3a96aa1
commit 0e18b57624
4 changed files with 72 additions and 4 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<Variable> {
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<Variable> {
(
{ &["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<String> {
(

View file

@ -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,