From cf0f058c7abb87479fb663ab630e740ee4bbc4f8 Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sun, 1 Aug 2021 01:23:38 -0400 Subject: [PATCH] Implement GET UPSDESC Fixes #12 --- nut-client/src/cmd.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index e97daf1..879d397 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 UPS description (UPSDESC) response. + /// + /// Params: (UPS description) + UpsDesc(String), /// A mutable variable (RW) response. /// /// Params: (var name, var value) @@ -281,6 +285,23 @@ impl Response { }?; Ok(Response::CmdDesc(desc)) } + "UPSDESC" => { + let _device = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified UPSDESC device in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + let desc = if args.is_empty() { + Err(ClientError::from(NutError::Generic( + "Unspecified UPSDESC description in response".into(), + ))) + } else { + Ok(args.remove(0)) + }?; + Ok(Response::UpsDesc(desc)) + } "DESC" => { let _device = if args.is_empty() { Err(ClientError::from(NutError::Generic( @@ -399,6 +420,14 @@ impl Response { } } + pub fn expect_upsdesc(&self) -> crate::Result { + if let Self::UpsDesc(description) = &self { + Ok(description.to_owned()) + } else { + Err(NutError::UnexpectedResponse.into()) + } + } + pub fn expect_desc(&self) -> crate::Result { if let Self::Desc(description) = &self { Ok(description.to_owned()) @@ -680,6 +709,14 @@ implement_get_commands! { ) } + /// Queries the description of a UPS device. + pub fn get_ups_description(ups_name: &str) -> String { + ( + { &["UPSDESC", ups_name] }, + { |row: Response| row.expect_upsdesc() }, + ) + } + /// Queries the number of logins to the specified UPS. pub fn get_num_logins(ups_name: &str) -> i32 { (