mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-08 21:18:31 -04:00
parent
fd1f72e1d0
commit
2360bd4a3f
3 changed files with 47 additions and 0 deletions
|
@ -35,6 +35,10 @@ async fn main() -> nut_client::Result<()> {
|
||||||
for (name, description) in conn.list_ups().await? {
|
for (name, description) in conn.list_ups().await? {
|
||||||
println!("\t- Name: {}", name);
|
println!("\t- Name: {}", name);
|
||||||
println!("\t Description: {}", description);
|
println!("\t Description: {}", description);
|
||||||
|
println!(
|
||||||
|
"\t Number of logins: {}",
|
||||||
|
conn.get_num_logins(&name).await?
|
||||||
|
);
|
||||||
|
|
||||||
// Get list of mutable variables
|
// Get list of mutable variables
|
||||||
let mutable_vars = conn.list_mutable_vars(&name).await?;
|
let mutable_vars = conn.list_mutable_vars(&name).await?;
|
||||||
|
|
|
@ -34,6 +34,7 @@ fn main() -> nut_client::Result<()> {
|
||||||
for (name, description) in conn.list_ups()? {
|
for (name, description) in conn.list_ups()? {
|
||||||
println!("\t- Name: {}", name);
|
println!("\t- Name: {}", name);
|
||||||
println!("\t Description: {}", description);
|
println!("\t Description: {}", description);
|
||||||
|
println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
|
||||||
|
|
||||||
// Get list of mutable variables
|
// Get list of mutable variables
|
||||||
let mutable_vars = conn.list_mutable_vars(&name)?;
|
let mutable_vars = conn.list_mutable_vars(&name)?;
|
||||||
|
|
|
@ -92,6 +92,10 @@ pub enum Response {
|
||||||
///
|
///
|
||||||
/// Params: (variable description)
|
/// Params: (variable description)
|
||||||
Desc(String),
|
Desc(String),
|
||||||
|
/// A NUMLOGINS response.
|
||||||
|
///
|
||||||
|
/// Params (number of logins)
|
||||||
|
NumLogins(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
|
@ -301,6 +305,28 @@ impl Response {
|
||||||
}?;
|
}?;
|
||||||
Ok(Response::Desc(desc))
|
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::<i32>().map_err(|_| {
|
||||||
|
ClientError::from(NutError::Generic(
|
||||||
|
"Invalid NUMLOGINS number in response".into(),
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
Ok(Response::NumLogins(num))
|
||||||
|
}
|
||||||
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
|
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -380,6 +406,14 @@ impl Response {
|
||||||
Err(NutError::UnexpectedResponse.into())
|
Err(NutError::UnexpectedResponse.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn expect_numlogins(&self) -> crate::Result<i32> {
|
||||||
|
if let Self::NumLogins(num) = &self {
|
||||||
|
Ok(*num)
|
||||||
|
} else {
|
||||||
|
Err(NutError::UnexpectedResponse.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A macro for implementing `LIST` commands.
|
/// A macro for implementing `LIST` commands.
|
||||||
|
@ -645,6 +679,14 @@ implement_get_commands! {
|
||||||
{ |row: Response| row.expect_cmddesc() },
|
{ |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! {
|
implement_simple_commands! {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue