Struct Bridge

Source
pub struct Bridge { /* private fields */ }
Expand description

A bridge with IP address and username.

Implementations§

Source§

impl Bridge

Source

pub fn new<S>(ip_address: IpAddr, username: S) -> Self
where S: Into<String>,

Creates a new bridge.

§Examples

Create a bridge with an already registered user:

use huelib::Bridge;
use std::net::{IpAddr, Ipv4Addr};

let ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
let bridge = Bridge::new(ip, "username");
Examples found in repository?
examples/get_all_lights.rs (line 13)
5fn main() {
6    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
7    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
8
9    // Register a new user.
10    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
11
12    // Create a new bridge.
13    let bridge = Bridge::new(bridge_ip, username);
14
15    // Print out every light that is connected to the bridge.
16    let lights = bridge.get_all_lights().unwrap();
17    println!("{:?}", lights);
18}
More examples
Hide additional examples
examples/delete_light.rs (line 13)
5fn main() {
6    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
7    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
8
9    // Register a new user.
10    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
11
12    // Create a new bridge.
13    let bridge = Bridge::new(bridge_ip, username);
14
15    // Deletes the light with the id 1.
16    match bridge.delete_light("1") {
17        Ok(_) => println!("Deleted light"),
18        Err(e) => println!("Failed to delete light: {}", e),
19    };
20}
examples/set_light_state.rs (line 14)
6fn main() {
7    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
8    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
9
10    // Register a new user.
11    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
12
13    // Create a new bridge.
14    let bridge = Bridge::new(bridge_ip, username);
15
16    // Creates a new light modifier to turn on the light, set the saturation to 10 and decrement
17    // the brightness by 40.
18    let light_modifier = light::StateModifier::new()
19        .with_on(true)
20        .with_saturation(Adjust::Override(10))
21        .with_alert(Alert::Select)
22        .with_brightness(Adjust::Decrement(40));
23
24    // Modify the attributes declared in `light_modifier` on the light with the id 1.
25    let response = bridge.set_light_state("1", &light_modifier).unwrap();
26    println!("{:?}", response);
27}
Source

pub fn username(&self) -> &str

Returns the name of the user that is connected to the bridge.

Source

pub fn ip_address(&self) -> &IpAddr

Returns the IP address of the bridge.

Source

pub fn set_config(&self, modifier: &Modifier) -> Result<Vec<Response<Modified>>>

Modifies the configuration of the bridge.

Source

pub fn get_config(&self) -> Result<Config>

Returns the configuration of the bridge.

Source

pub fn set_light_attribute<S>( &self, id: S, modifier: &AttributeModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies attributes of a light.

Source

pub fn set_light_state<S>( &self, id: S, modifier: &StateModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies the state of a light.

Examples found in repository?
examples/set_light_state.rs (line 25)
6fn main() {
7    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
8    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
9
10    // Register a new user.
11    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
12
13    // Create a new bridge.
14    let bridge = Bridge::new(bridge_ip, username);
15
16    // Creates a new light modifier to turn on the light, set the saturation to 10 and decrement
17    // the brightness by 40.
18    let light_modifier = light::StateModifier::new()
19        .with_on(true)
20        .with_saturation(Adjust::Override(10))
21        .with_alert(Alert::Select)
22        .with_brightness(Adjust::Decrement(40));
23
24    // Modify the attributes declared in `light_modifier` on the light with the id 1.
25    let response = bridge.set_light_state("1", &light_modifier).unwrap();
26    println!("{:?}", response);
27}
Source

pub fn get_light<S>(&self, id: S) -> Result<Light>
where S: Into<String>,

Returns a light.

Source

pub fn get_all_lights(&self) -> Result<Vec<Light>>

Returns all lights that are connected to the bridge.

Examples found in repository?
examples/get_all_lights.rs (line 16)
5fn main() {
6    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
7    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
8
9    // Register a new user.
10    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
11
12    // Create a new bridge.
13    let bridge = Bridge::new(bridge_ip, username);
14
15    // Print out every light that is connected to the bridge.
16    let lights = bridge.get_all_lights().unwrap();
17    println!("{:?}", lights);
18}
Source

pub fn search_new_lights(&self, scanner: &Scanner) -> Result<()>

Starts searching for new lights.

The bridge will open the network for 40 seconds. The overall search might take longer since the configuration of new devices can take longer. If many devices are found the command will have to be issued a second time after discovery time has elapsed. If the command is received again during search the search will continue for at least an additional 40 seconds.

When the search has finished, new lights will be available using the get_new_lights function.

Source

pub fn get_new_lights(&self) -> Result<Scan>

Returns discovered lights.

Source

pub fn delete_light<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a light from the bridge.

Examples found in repository?
examples/delete_light.rs (line 16)
5fn main() {
6    // Discover bridges in the local network and save the first IP address as `bridge_ip`.
7    let bridge_ip = bridge::discover_nupnp().unwrap().pop().unwrap();
8
9    // Register a new user.
10    let username = bridge::register_user(bridge_ip, "huelib-rs example").unwrap();
11
12    // Create a new bridge.
13    let bridge = Bridge::new(bridge_ip, username);
14
15    // Deletes the light with the id 1.
16    match bridge.delete_light("1") {
17        Ok(_) => println!("Deleted light"),
18        Err(e) => println!("Failed to delete light: {}", e),
19    };
20}
Source

pub fn create_group(&self, creator: &Creator) -> Result<String>

Creates a new group.

Source

pub fn set_group_attribute<S>( &self, id: S, modifier: &AttributeModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies attributes of a group.

Source

pub fn set_group_state<S>( &self, id: S, modifier: &StateModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies the state of a group.

Source

pub fn get_group<S>(&self, id: S) -> Result<Group>
where S: Into<String>,

Returns a group.

Source

pub fn get_all_groups(&self) -> Result<Vec<Group>>

Returns all groups.

Source

pub fn delete_group<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a group from the bridge.

Source

pub fn create_scene(&self, creator: &Creator) -> Result<String>

Creates a new scene.

Source

pub fn set_scene<S>( &self, id: S, modifier: &Modifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies the state and attributes of a scene.

Source

pub fn get_scene<S>(&self, id: S) -> Result<Scene>
where S: Into<String>,

Returns a scene.

Source

pub fn get_all_scenes(&self) -> Result<Vec<Scene>>

Returns all scenes.

Source

pub fn delete_scene<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a scene.

Source

pub fn get_capabilities(&self) -> Result<Capabilities>

Returns the capabilities of resources.

Source

pub fn create_schedule(&self, creator: &Creator) -> Result<String>

Creates a new schedule and returns the identifier.

Source

pub fn set_schedule<S>( &self, id: S, modifier: &Modifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies attributes of a schedule.

Source

pub fn get_schedule<S>(&self, id: S) -> Result<Schedule>
where S: Into<String>,

Returns a schedule.

Source

pub fn get_all_schedules(&self) -> Result<Vec<Schedule>>

Returns all schedules.

Source

pub fn delete_schedule<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a schedule.

Creates a new resourcelink and returns the identifier.

Modifies attributes of a resourcelink.

Returns a resourcelink.

Returns all resourcelinks.

Deletes a resourcelink.

Source

pub fn set_sensor_attribute<S>( &self, id: S, modifier: &AttributeModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies attributes of a sensor.

Source

pub fn set_sensor_state<S>( &self, id: S, modifier: &StateModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies the state of a sensor.

Source

pub fn set_sensor_config<S>( &self, id: S, modifier: &ConfigModifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies the configuration of a sensor.

Source

pub fn get_sensor<S>(&self, id: S) -> Result<Sensor>
where S: Into<String>,

Returns a sensor.

Source

pub fn get_all_sensors(&self) -> Result<Vec<Sensor>>

Returns all sensors that are connected to the bridge.

Source

pub fn search_new_sensors(&self, scanner: &Scanner) -> Result<()>

Starts searching for new sensors.

The bridge will open the network for 40 seconds. The overall search might take longer since the configuration of new devices can take longer. If many devices are found the command will have to be issued a second time after discovery time has elapsed. If the command is received again during search the search will continue for at least an additional 40 seconds.

When the search has finished, new sensors will be available using the get_new_sensors function.

Source

pub fn get_new_sensors(&self) -> Result<Scan>

Returns discovered sensors.

Source

pub fn delete_sensor<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a sensor from the bridge.

Source

pub fn create_rule(&self, creator: &Creator) -> Result<String>

Creates a new rule.

Source

pub fn set_rule<S>( &self, id: S, modifier: &Modifier, ) -> Result<Vec<Response<Modified>>>
where S: Into<String>,

Modifies attributes of a rule.

Source

pub fn get_rule<S>(&self, id: S) -> Result<Rule>
where S: Into<String>,

Returns a rule.

Source

pub fn get_all_rules(&self) -> Result<Vec<Rule>>

Returns all rules.

Source

pub fn delete_rule<S>(&self, id: S) -> Result<()>
where S: Into<String>,

Deletes a rule.

Trait Implementations§

Source§

impl Clone for Bridge

Source§

fn clone(&self) -> Bridge

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bridge

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Bridge

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Bridge

Source§

fn eq(&self, other: &Bridge) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Bridge

Source§

impl StructuralPartialEq for Bridge

Auto Trait Implementations§

§

impl Freeze for Bridge

§

impl RefUnwindSafe for Bridge

§

impl Send for Bridge

§

impl Sync for Bridge

§

impl Unpin for Bridge

§

impl UnwindSafe for Bridge

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.