Skip to main content

basic/
basic.rs

1// Simple example showing how to call into the generated bindings.
2// Build and run from repository root:
3//   cargo run --example basic
4//
5// Run it from the workspace root with:
6//   cargo run -p azure-iot-rs --example basic
7
8use azure_iot_rs::MessageBody;
9use azure_iot_rs::{IoTHubMessageDispositionResult, IotHubModuleClient, ModuleEventCallback};
10
11struct PrintModuleEvents;
12
13impl ModuleEventCallback for PrintModuleEvents {
14    fn on_message(&mut self, msg: azure_iot_rs::IotHubMessage) -> IoTHubMessageDispositionResult {
15        match msg.body() {
16            MessageBody::Text(s) => println!("Received message text: {s}"),
17            MessageBody::Binary(b) => {
18                println!("Received message binary ({:?} {} bytes)", b, b.len())
19            }
20        }
21        IoTHubMessageDispositionResult::Accepted
22    }
23
24    fn on_module_twin(&mut self, state: azure_iot_rs::IotHubDeviceTwinUpdateState, data: &[u8]) {
25        println!(
26            "Received twin update ({state}): {}",
27            String::from_utf8_lossy(data)
28        );
29    }
30
31    fn on_confirmation(&mut self, _status: Result<(), azure_iot_rs::IotError>) {}
32}
33
34fn main() {
35    println!("azure-iot-rs example CLI — starting client...");
36
37    let mut client = IotHubModuleClient::try_new(PrintModuleEvents).unwrap();
38
39    println!("Client initialized. Entering work loop (Ctrl+C to exit)...");
40    client.do_work();
41}