rupsc: Implement listing clients

This commit is contained in:
Aram 🍐 2021-07-31 03:44:45 -04:00
parent 52afe6bbd1
commit abd38f0a99
4 changed files with 66 additions and 1 deletions

View file

@ -28,6 +28,13 @@ impl Connection {
}
}
/// Queries a list of client IP addresses connected to the given device.
pub fn list_clients(&mut self, ups_name: &str) -> crate::Result<Vec<String>> {
match self {
Self::Tcp(conn) => conn.list_clients(ups_name),
}
}
/// Queries the list of variables for a UPS device.
pub fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<Variable>> {
match self {
@ -103,6 +110,18 @@ impl TcpConnection {
list.into_iter().map(|row| row.expect_ups()).collect()
}
fn list_clients(&mut self, ups_name: &str) -> crate::Result<Vec<String>> {
let query = &["CLIENT", ups_name];
Self::write_cmd(
&mut self.tcp_stream,
Command::List(query),
self.config.debug,
)?;
let list = Self::read_list(&mut self.tcp_stream, query, self.config.debug)?;
list.into_iter().map(|row| row.expect_client()).collect()
}
fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
let query = &["VAR", ups_name];
Self::write_cmd(

View file

@ -52,9 +52,17 @@ pub enum Response {
/// Marks the end of a list response.
EndList(String),
/// A variable (VAR) response.
///
/// Params: (var name, var value)
Var(String, String),
/// A UPS (UPS) response.
///
/// Params: (device name, device description)
Ups(String, String),
/// A client (CLIENT) response.
///
/// Params: (client IP)
Client(String),
}
impl Response {
@ -157,6 +165,23 @@ impl Response {
}?;
Ok(Response::Ups(name, description))
}
"CLIENT" => {
let _device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CLIENT device in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let ip_address = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CLIENT IP in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
Ok(Response::Client(ip_address))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
}
}
@ -196,4 +221,12 @@ impl Response {
Err(NutError::UnexpectedResponse.into())
}
}
pub fn expect_client(&self) -> crate::Result<String> {
if let Self::Client(client_ip) = &self {
Ok(client_ip.to_owned())
} else {
Err(NutError::UnexpectedResponse.into())
}
}
}

View file

@ -43,6 +43,19 @@ pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
Ok(())
}
pub fn list_clients(server: UpsdName, debug: bool) -> anyhow::Result<()> {
let ups_name = server
.upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
let mut conn = connect(server, debug)?;
for client_ip in conn.list_clients(ups_name)? {
println!("{}", client_ip);
}
Ok(())
}
fn connect(server: UpsdName, debug: bool) -> anyhow::Result<Connection> {
let host = server.try_into()?;
let config = nut_client::ConfigBuilder::new()

View file

@ -73,7 +73,7 @@ fn main() -> anyhow::Result<()> {
}
if args.is_present("clients") {
todo!("listing clients")
return cmd::list_clients(server, debug);
}
// Fallback: prints one variable (or all of them)