Enum Connection

Source
pub enum Connection {
    Tcp(TcpConnection),
}
Expand description

An async NUT client connection.

Variants§

§

Tcp(TcpConnection)

A TCP connection.

Implementations§

Source§

impl Connection

Source

pub async fn new(config: &Config) -> Result<Self>

Initializes a connection to a NUT server (upsd).

Examples found in repository?
examples/async.rs (line 26)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn close(self) -> Result<()>

Gracefully closes the connection.

Examples found in repository?
examples/async.rs (line 72)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source§

impl Connection

Source

pub async fn list_ups(&mut self) -> Result<Vec<(String, String)>>

Queries a list of UPS devices.

Examples found in repository?
examples/async.rs (line 35)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn list_clients(&mut self, ups_name: &str) -> Result<Vec<String>>

Queries the list of client IP addresses connected to the given device.

Source

pub async fn list_vars(&mut self, ups_name: &str) -> Result<Vec<Variable>>

Queries the list of variables for a UPS device.

Examples found in repository?
examples/async.rs (line 55)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async 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?
examples/async.rs (line 44)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async 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?
examples/async.rs (line 65)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn list_var_range( &mut self, ups_name: &str, variable: &str, ) -> Result<Vec<VariableRange>>

Queries the possible ranges of a UPS variable.

Source

pub async fn list_var_enum( &mut self, ups_name: &str, variable: &str, ) -> Result<Vec<String>>

Queries the possible enum values of a UPS variable.

Source§

impl Connection

Source

pub async fn get_var( &mut self, ups_name: &str, variable: &str, ) -> Result<Variable>

Queries one variable for a UPS device.

Source

pub async fn get_var_description( &mut self, ups_name: &str, variable: &str, ) -> Result<String>

Queries the description of a UPS variable.

Source

pub async fn get_var_type( &mut self, ups_name: &str, variable: &str, ) -> Result<VariableDefinition>

Queries the type of a UPS variable.

Examples found in repository?
examples/async.rs (line 50)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn get_command_description( &mut self, ups_name: &str, variable: &str, ) -> Result<String>

Queries the description of a UPS command.

Examples found in repository?
examples/async.rs (line 66)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn get_ups_description(&mut self, ups_name: &str) -> Result<String>

Queries the description of a UPS device.

Source

pub async fn get_num_logins(&mut self, ups_name: &str) -> Result<i32>

Queries the number of logins to the specified UPS.

Examples found in repository?
examples/async.rs (line 40)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source§

impl Connection

Source

pub async fn get_network_version(&mut self) -> Result<String>

Queries the network protocol version.

Examples found in repository?
examples/async.rs (line 31)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}
Source

pub async fn get_server_version(&mut self) -> Result<String>

Queries the server NUT version.

Examples found in repository?
examples/async.rs (line 30)
8async fn main() -> nut_client::Result<()> {
9    let host = env::var("NUT_HOST").unwrap_or_else(|_| "localhost".into());
10    let port = env::var("NUT_PORT")
11        .ok()
12        .map(|s| s.parse::<u16>().ok())
13        .flatten()
14        .unwrap_or(3493);
15
16    let username = env::var("NUT_USER").ok();
17    let password = env::var("NUT_PASSWORD").ok();
18    let auth = username.map(|username| Auth::new(username, password));
19
20    let config = ConfigBuilder::new()
21        .with_host((host, port).try_into().unwrap_or_default())
22        .with_auth(auth)
23        .with_debug(false) // Turn this on for debugging network chatter
24        .build();
25
26    let mut conn = Connection::new(&config).await?;
27
28    // Get server information
29    println!("NUT server:");
30    println!("\tVersion: {}", conn.get_server_version().await?);
31    println!("\tNetwork Version: {}", conn.get_network_version().await?);
32
33    // Print a list of all UPS devices
34    println!("Connected UPS devices:");
35    for (name, description) in conn.list_ups().await? {
36        println!("\t- Name: {}", name);
37        println!("\t  Description: {}", description);
38        println!(
39            "\t  Number of logins: {}",
40            conn.get_num_logins(&name).await?
41        );
42
43        // Get list of mutable variables
44        let mutable_vars = conn.list_mutable_vars(&name).await?;
45
46        // List UPS variables (key = val)
47        println!("\t  Mutable Variables:");
48        for var in mutable_vars.iter() {
49            println!("\t\t- {}", var);
50            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
51        }
52
53        // List UPS immutable properties (key = val)
54        println!("\t  Immutable Properties:");
55        for var in conn.list_vars(&name).await? {
56            if mutable_vars.iter().any(|v| v.name() == var.name()) {
57                continue;
58            }
59            println!("\t\t- {}", var);
60            println!("\t\t  {:?}", conn.get_var_type(&name, var.name()).await?);
61        }
62
63        // List UPS commands
64        println!("\t  Commands:");
65        for cmd in conn.list_commands(&name).await? {
66            let description = conn.get_command_description(&name, &cmd).await?;
67            println!("\t\t- {} ({})", cmd, description);
68        }
69    }
70
71    // Gracefully shut down the connection using the `LOGOUT` command
72    conn.close().await
73}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.