Listener

Struct Listener 

Source
pub struct Listener<'a> { /* private fields */ }
Expand description

An event listener for a Location.

Implementations§

Source§

impl<'a> Listener<'a>

Source

pub fn new<'b>( location: &'b Location<'_>, stream: WebSocketStream<MaybeTlsStream<TcpStream>>, ) -> Listener<'b>

Create a brand new event listener for a location.

This generally accepts a callback defined by the caller, which is triggered whenever an event is triggered by Ring.

Source

pub async fn listen<EventHandler, EventHandlerFut, E>( &'a mut self, on_event: EventHandler, ) -> Result<(), E>
where EventHandler: Fn(Event, &'a Location<'a>, Arc<Mutex<&'a mut Connection>>) -> EventHandlerFut, EventHandlerFut: Future<Output = Result<bool, E>>,

Listen for events in a particular location.

§Example
use ring_client::Client;

use ring_client::authentication::Credentials;
use ring_client::OperatingSystem;

let client = Client::new("Home Automation", "mock-system-id", OperatingSystem::Ios);

// For brevity, a Refresh Token is being used here. However, the client can also
// be authenticated using a username and password.
//
// See `Client::login` for more information.
let refresh_token = Credentials::RefreshToken("".to_string());

client.login(refresh_token)
     .await
     .expect("Logging in with a valid refresh token should not fail");

let locations = client.get_locations()
     .await
     .expect("Getting locations should not fail");

let location = locations
     .first()
     .expect("There should be at least one location");

let mut listener = location.get_listener()
     .await
     .expect("Creating a listener should not fail");

// Listen for events in the location and react to them using the provided closure.
listener.listen::<_, _, ()>(|event, location, mut connection| async move {
    // Connection can be used to send commands to the Ring API.
    println!("New event: {:#?}", event);

    // The connection argument can be used to send events back to Ring in
    // response to the event.

    // Return true or false to indicate whether the listener should continue listening
    Ok(true)
})
.await;
§Errors

Returns the error from the event handler if it returns an error when called.

Source

pub async fn send(&mut self, event: Event) -> Result<(), ApiError>

Send an event to Ring.

§Errors

Returns an error if the connection is closed.

§Example
use serde_json::json;
use ring_client::Client;

use ring_client::authentication::Credentials;
use ring_client::location::{Event, Message};
use ring_client::OperatingSystem;

let client = Client::new("Home Automation", "mock-system-id", OperatingSystem::Ios);

// For brevity, a Refresh Token is being used here. However, the client can also
// be authenticated using a username and password.
//
// See `Client::login` for more information.
let refresh_token = Credentials::RefreshToken("".to_string());

client.login(refresh_token)
     .await
     .expect("Logging in with a valid refresh token should not fail");

let locations = client.get_locations()
     .await
     .expect("Getting locations should not fail");

let location = locations
     .first()
     .expect("There should be at least one location");

let listener = location.get_listener().await;

location.get_listener()
    .await
    .expect("Creating a listener should not fail")
    .send(
        Event::new(
            Message::DataUpdate(json!({}))
        )
    )
    .await
    .expect("Sending an event should not fail");
Source

pub async fn close(self)

Close the underlying connection to Ring.

Trait Implementations§

Source§

impl<'a> Debug for Listener<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for Listener<'a>

§

impl<'a> !RefUnwindSafe for Listener<'a>

§

impl<'a> Send for Listener<'a>

§

impl<'a> Sync for Listener<'a>

§

impl<'a> Unpin for Listener<'a>

§

impl<'a> !UnwindSafe for Listener<'a>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T