pub struct Listener<'a> { /* private fields */ }Expand description
An event listener for a Location.
Implementations§
Source§impl<'a> Listener<'a>
impl<'a> Listener<'a>
Sourcepub fn new<'b>(
location: &'b Location<'_>,
stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
) -> Listener<'b>
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.
Sourcepub async fn listen<EventHandler, EventHandlerFut, E>(
&'a mut self,
on_event: EventHandler,
) -> Result<(), E>
pub async fn listen<EventHandler, EventHandlerFut, E>( &'a mut self, on_event: EventHandler, ) -> Result<(), 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.
Sourcepub async fn send(&mut self, event: Event) -> Result<(), ApiError>
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");Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more