pub enum Connection {
Tcp(TcpConnection),
}
Expand description
A blocking NUT client connection.
Variants§
Tcp(TcpConnection)
A TCP connection.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn new(config: &Config) -> Result<Self>
pub fn new(config: &Config) -> Result<Self>
Initializes a connection to a NUT server (upsd).
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn close(self) -> Result<()>
pub fn close(self) -> Result<()>
Gracefully closes the connection.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Source§impl Connection
impl Connection
Sourcepub fn list_ups(&mut self) -> Result<Vec<(String, String)>>
pub fn list_ups(&mut self) -> Result<Vec<(String, String)>>
Queries a list of UPS devices.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn list_clients(&mut self, ups_name: &str) -> Result<Vec<String>>
pub fn list_clients(&mut self, ups_name: &str) -> Result<Vec<String>>
Queries the list of client IP addresses connected to the given device.
Sourcepub fn list_vars(&mut self, ups_name: &str) -> Result<Vec<Variable>>
pub fn list_vars(&mut self, ups_name: &str) -> Result<Vec<Variable>>
Queries the list of variables for a UPS device.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn list_mutable_vars(&mut self, ups_name: &str) -> Result<Vec<Variable>>
pub fn list_mutable_vars(&mut self, ups_name: &str) -> Result<Vec<Variable>>
Queries the list of mutable variables for a UPS device.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn list_commands(&mut self, ups_name: &str) -> Result<Vec<String>>
pub fn list_commands(&mut self, ups_name: &str) -> Result<Vec<String>>
Queries the list of commands available for the given device.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn list_var_range(
&mut self,
ups_name: &str,
variable: &str,
) -> Result<Vec<VariableRange>>
pub fn list_var_range( &mut self, ups_name: &str, variable: &str, ) -> Result<Vec<VariableRange>>
Queries the possible ranges of a UPS variable.
Source§impl Connection
impl Connection
Sourcepub fn get_var(&mut self, ups_name: &str, variable: &str) -> Result<Variable>
pub fn get_var(&mut self, ups_name: &str, variable: &str) -> Result<Variable>
Queries one variable for a UPS device.
Sourcepub fn get_var_description(
&mut self,
ups_name: &str,
variable: &str,
) -> Result<String>
pub fn get_var_description( &mut self, ups_name: &str, variable: &str, ) -> Result<String>
Queries the description of a UPS variable.
Sourcepub fn get_var_type(
&mut self,
ups_name: &str,
variable: &str,
) -> Result<VariableDefinition>
pub fn get_var_type( &mut self, ups_name: &str, variable: &str, ) -> Result<VariableDefinition>
Queries the type of a UPS variable.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn get_command_description(
&mut self,
ups_name: &str,
variable: &str,
) -> Result<String>
pub fn get_command_description( &mut self, ups_name: &str, variable: &str, ) -> Result<String>
Queries the description of a UPS command.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn get_ups_description(&mut self, ups_name: &str) -> Result<String>
pub fn get_ups_description(&mut self, ups_name: &str) -> Result<String>
Queries the description of a UPS device.
Sourcepub fn get_num_logins(&mut self, ups_name: &str) -> Result<i32>
pub fn get_num_logins(&mut self, ups_name: &str) -> Result<i32>
Queries the number of logins to the specified UPS.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Source§impl Connection
impl Connection
Sourcepub fn get_network_version(&mut self) -> Result<String>
pub fn get_network_version(&mut self) -> Result<String>
Queries the network protocol version.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}
Sourcepub fn get_server_version(&mut self) -> Result<String>
pub fn get_server_version(&mut self) -> Result<String>
Queries the server NUT version.
Examples found in repository?
7fn main() -> nut_client::Result<()> {
8 let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
9 let port = env::var("NUT_PORT")
10 .ok()
11 .map(|s| s.parse::<u16>().ok())
12 .flatten()
13 .unwrap_or(3493);
14
15 let username = env::var("NUT_USER").ok();
16 let password = env::var("NUT_PASSWORD").ok();
17 let auth = username.map(|username| Auth::new(username, password));
18
19 let config = ConfigBuilder::new()
20 .with_host((host, port).try_into().unwrap_or_default())
21 .with_auth(auth)
22 .with_debug(false) // Turn this on for debugging network chatter
23 .build();
24
25 let mut conn = Connection::new(&config)?;
26
27 // Get server information
28 println!("NUT server:");
29 println!("\tVersion: {}", conn.get_server_version()?);
30 println!("\tNetwork Version: {}", conn.get_network_version()?);
31
32 // Print a list of all UPS devices
33 println!("Connected UPS devices:");
34 for (name, description) in conn.list_ups()? {
35 println!("\t- Name: {}", name);
36 println!("\t Description: {}", description);
37 println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
38
39 // Get list of mutable variables
40 let mutable_vars = conn.list_mutable_vars(&name)?;
41
42 // List UPS variables (key = val)
43 println!("\t Mutable Variables:");
44 for var in mutable_vars.iter() {
45 println!("\t\t- {}", var);
46 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
47 }
48
49 // List UPS immutable properties (key = val)
50 println!("\t Immutable Properties:");
51 for var in conn.list_vars(&name)? {
52 if mutable_vars.iter().any(|v| v.name() == var.name()) {
53 continue;
54 }
55 println!("\t\t- {}", var);
56 println!("\t\t {:?}", conn.get_var_type(&name, var.name())?);
57 }
58
59 // List UPS commands
60 println!("\t Commands:");
61 for cmd in conn.list_commands(&name)? {
62 let description = conn.get_command_description(&name, &cmd)?;
63 println!("\t\t- {} ({})", cmd, description);
64 }
65 }
66
67 // Gracefully shut down the connection using the `LOGOUT` command
68 conn.close()
69}