mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-09 05:28:31 -04:00
Generify commands in blocking/tokio (#24)
This commit is contained in:
parent
65f25fb958
commit
6e82736808
5 changed files with 211 additions and 166 deletions
|
@ -3,7 +3,7 @@ use std::net::{SocketAddr, TcpStream};
|
|||
|
||||
use crate::blocking::stream::ConnectionStream;
|
||||
use crate::cmd::{Command, Response};
|
||||
use crate::{ClientError, Config, Host, NutError, Variable};
|
||||
use crate::{ClientError, Config, Host, NutError};
|
||||
|
||||
mod stream;
|
||||
|
||||
|
@ -20,41 +20,6 @@ impl Connection {
|
|||
Host::Tcp(host) => Ok(Self::Tcp(TcpConnection::new(config.clone(), &host.addr)?)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Queries a list of UPS devices.
|
||||
pub fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
|
||||
match self {
|
||||
Self::Tcp(conn) => conn.list_ups(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
Self::Tcp(conn) => Ok(conn
|
||||
.list_vars(ups_name)?
|
||||
.into_iter()
|
||||
.map(|(key, val)| Variable::parse(key.as_str(), val))
|
||||
.collect()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Queries one variable for a UPS device.
|
||||
pub fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<Variable> {
|
||||
match self {
|
||||
Self::Tcp(conn) => {
|
||||
let var = conn.get_var(ups_name, variable)?;
|
||||
Ok(Variable::parse(var.0.as_str(), var.1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A blocking TCP NUT client connection.
|
||||
|
@ -129,7 +94,8 @@ impl TcpConnection {
|
|||
self.stream = self.stream.upgrade_ssl(sess)?;
|
||||
|
||||
// Send a test command
|
||||
self.get_network_version()?;
|
||||
self.write_cmd(Command::NetworkVersion)?;
|
||||
self.read_plain_response()?;
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
@ -154,44 +120,7 @@ impl TcpConnection {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
|
||||
let query = &["UPS"];
|
||||
self.write_cmd(Command::List(query))?;
|
||||
|
||||
let list = self.read_list(query)?;
|
||||
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(Command::List(query))?;
|
||||
|
||||
let list = self.read_list(query)?;
|
||||
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(Command::List(query))?;
|
||||
|
||||
let list = self.read_list(query)?;
|
||||
list.into_iter().map(|row| row.expect_var()).collect()
|
||||
}
|
||||
|
||||
fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> {
|
||||
let query = &["VAR", ups_name, variable];
|
||||
self.write_cmd(Command::Get(query))?;
|
||||
|
||||
self.read_response()?.expect_var()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get_network_version(&mut self) -> crate::Result<String> {
|
||||
self.write_cmd(Command::NetworkVersion)?;
|
||||
self.read_plain_response()
|
||||
}
|
||||
|
||||
fn write_cmd(&mut self, line: Command) -> crate::Result<()> {
|
||||
pub(crate) fn write_cmd(&mut self, line: Command) -> crate::Result<()> {
|
||||
let line = format!("{}\n", line);
|
||||
if self.config.debug {
|
||||
eprint!("DEBUG -> {}", line);
|
||||
|
@ -219,19 +148,19 @@ impl TcpConnection {
|
|||
Ok(args)
|
||||
}
|
||||
|
||||
fn read_response(&mut self) -> crate::Result<Response> {
|
||||
pub(crate) fn read_response(&mut self) -> crate::Result<Response> {
|
||||
let mut reader = BufReader::new(&mut self.stream);
|
||||
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||
Response::from_args(args)
|
||||
}
|
||||
|
||||
fn read_plain_response(&mut self) -> crate::Result<String> {
|
||||
pub(crate) fn read_plain_response(&mut self) -> crate::Result<String> {
|
||||
let mut reader = BufReader::new(&mut self.stream);
|
||||
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||
Ok(args.join(" "))
|
||||
}
|
||||
|
||||
fn read_list(&mut self, query: &[&str]) -> crate::Result<Vec<Response>> {
|
||||
pub(crate) fn read_list(&mut self, query: &[&str]) -> crate::Result<Vec<Response>> {
|
||||
let mut reader = BufReader::new(&mut self.stream);
|
||||
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue