[][src]Struct azure_iot_sdk::client::IoTHubClient

pub struct IoTHubClient { /* fields omitted */ }

Client for communicating with IoT hub

Methods

impl IoTHubClient[src]

pub async fn with_device_key(
    hub: String,
    device_id: String,
    key: String
) -> Self
[src]

Create a new IoT Hub device client using the device's primary key

Arguments

  • hub - The IoT hub resource name
  • device_id - The registered device to connect as
  • key - The primary or secondary key for this device

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::with_device_key(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "TheAccessKey".into()).await;
}

pub async fn from_connection_string<'_>(connection_string: &'_ str) -> Self[src]

Create a new IoT Hub device client using the device's connection string

Arguments

  • connection_string - The connection string for this device and iot hub

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::from_connection_string(
        "HostName=iothubname.azure-devices.net;DeviceId=MyDeviceId;SharedAccessKey=TheAccessKey").await;
}

pub async fn new(hub_name: String, device_id: String, sas: String) -> Self[src]

Create a new IoT Hub device client using a shared access signature

Arguments

  • hub_name - The IoT hub resource name
  • device_id - The registered device to connect as
  • sas - The shared access signature for this device to connect with

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::new(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "SharedAccessSignature sr=iothubname.azure-devices.net%2Fdevices%2MyDeviceId&sig=vn0%2BgyIUKgaBhEU0ypyOhJ0gPK5fSY1TKdvcJ1HxhnQ%3D&se=1587123309".into()).await;
}

pub async fn send_message<'_>(&'_ mut self, message: Message)[src]

Send a device to cloud message for this device to the IoT Hub

#Example

use azure_iot_sdk::client::IoTHubClient;
use azure_iot_sdk::message::Message;
use tokio::time;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::with_device_key(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "TheAccessKey".into()).await;

    let mut interval = time::interval(time::Duration::from_secs(1));
    let mut count: u32 = 0;

    loop {
        interval.tick().await;

        let msg = Message::builder()
            .set_body(format!("Message #{}", count).as_bytes().to_vec())
            .set_message_id(format!("{}-t", count))
            .build();

        client.send_message(msg).await;

        count += 1;
    }
}

pub async fn on_message<'_, T>(&'_ mut self, handler: T) where
    T: Fn(Message) + Send + 'static, 
[src]

Define the cloud to device message handler

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::with_device_key(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "TheAccessKey".into()).await;

    client
       .on_message(|msg| {
           println!("Received message {:?}", msg);
       })
       .await;
}

pub async fn on_direct_method<'_, T>(&'_ mut self, handler: T) where
    T: Fn(String, Message) -> i32 + Send + 'static, 
[src]

Define the message handler for direct method invocation

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::with_device_key(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "TheAccessKey".into()).await;

    client
       .on_direct_method(|method_name, msg| {
            println!("Received direct method {} {}", method_name, std::str::from_utf8(&msg.body).unwrap());
            0
        })
        .await;
}

pub async fn on_twin_update<'_, T>(&'_ mut self, handler: T) where
    T: Fn(Message) + Send + 'static, 
[src]

Define the cloud to device message handler

Example

use azure_iot_sdk::client::IoTHubClient;

#[tokio::main]
async fn main() {
    let mut client = IoTHubClient::with_device_key(
        "iothubname.azure-devices.net".into(),
        "MyDeviceId".into(),
        "TheAccessKey".into()).await;

    client
       .on_twin_update(|msg| {
           println!("Received message {:?}", msg);
       })
       .await;
}

Trait Implementations

impl Clone for IoTHubClient[src]

impl Debug for IoTHubClient[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.