use clap::Parser; use fundamentum_sdk_mqtt::{ async_event_loop_listener, Device, SecurityBuilder, SecurityFileFetcher, }; use fundamentum_sdk_mqtt::{Client, ClientSettings, Message, QoS}; use tracing::info; #[derive(Debug, Parser)] #[clap(name = "config")] #[clap(about = "Dimonoff Inc. Config Example", long_about = None, version, propagate_version = true)] struct CliCore { /// Device's private_key #[clap(long, default_value = "./rsa_private.pem")] private_key: String, /// Device's project_id #[clap(long)] project_id: u64, /// Device's region_id #[clap(long)] region_id: u64, /// Device's registry_id #[clap(long)] registry_id: u64, /// Device's serial #[clap(long)] serial: String, /// Fundamentum's MQTT endpoint #[clap(long, default_value = "mqtts.fundamentum-iot-dev.com")] endpoint: String, } #[tokio::main] async fn main() -> Result<(), Box> { tracing_subscriber::fmt::init(); let args = CliCore::parse(); let settings = ClientSettings::new( SecurityBuilder::default() .project_id(args.project_id) .fetcher(SecurityFileFetcher::new_boxed(args.private_key)) .build()?, // Security strategy Device::new( args.serial, // serial args.project_id, // project's id args.region_id, // region's id args.registry_id, // registry's id ), args.endpoint, // uri endpointuse tokio::task::JoinHandle; None, // override mqtt configuration ); let (client, eventloop) = Client::new(settings).await?; let mut receiver = client.get_receiver(); // Subscribe on the config channel client.subscribe_config(QoS::AtLeastOnce).await?; tokio::try_join!( async move { // Run the mqtt client's event loop. async_event_loop_listener(eventloop).await?; Ok::<_, Box>(()) }, async move { // Handle messages from the config channel we subscribed to. loop { let message = receiver.recv().await?; if let Message::Config(value) = message { info!("Rx config: {:#?}", value); } } #[allow(unreachable_code)] Ok::<_, Box>(()) } )?; Ok(()) }