diff --git a/src/client.rs b/src/client.rs index 5b61d21..a546fbb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +use crate::dtmi::Dtmi; use crate::message::Message; #[cfg(feature = "direct-methods")] use crate::DirectMethodResponse; @@ -91,6 +92,7 @@ impl IoTHubClient { device_id, token_source, root_ca: None, + pnp_model_id: None, } } @@ -207,6 +209,7 @@ pub struct IoTHubClientBuilder { device_id: String, token_source: TS, root_ca: Option, + pnp_model_id: Option, } impl IoTHubClientBuilder { @@ -219,12 +222,18 @@ impl IoTHubClientBuilder { device_id, token_source, root_ca, + pnp_model_id, } = self; #[cfg(not(feature = "https-transport"))] - let transport = - ClientTransport::new(&hub_name, device_id.clone(), token_source.clone(), root_ca) - .await?; + let transport = ClientTransport::new( + &hub_name, + device_id.clone(), + token_source.clone(), + root_ca, + pnp_model_id, + ) + .await?; #[cfg(feature = "https-transport")] let transport = ClientTransport::new(&hub_name, device_id.clone(), token_source.clone()).await?; @@ -243,4 +252,13 @@ impl IoTHubClientBuilder { self.root_ca = Some(root_ca); Ok(self) } + + /// Provide a PNP model ID + /// + /// Is only use with MQTT transport + pub fn pnp_model_id(mut self, pnp_model_id: impl AsRef) -> crate::Result { + let dtmi = pnp_model_id.as_ref().parse()?; + self.pnp_model_id = Some(dtmi); + Ok(self) + } } diff --git a/src/mqtt_transport.rs b/src/mqtt_transport.rs index 14fd238..f6ea3e0 100644 --- a/src/mqtt_transport.rs +++ b/src/mqtt_transport.rs @@ -24,6 +24,7 @@ use tokio_native_tls::{TlsConnector, TlsStream}; use async_trait::async_trait; +use crate::dtmi::Dtmi; use crate::message::Message; #[cfg(any( feature = "direct-methods", @@ -204,11 +205,19 @@ impl MqttTransport { device_id: String, token_source: TS, root_ca: Option, + pnp_model_id: Option, ) -> crate::Result where TS: TokenSource + Send + Sync + 'static, { - let user_name = format!("{}/{}/?api-version=2018-06-30", hub_name, device_id); + let user_name = if let Some(Dtmi(model_id)) = pnp_model_id { + format!( + "{}/{}/?api-version=2020-09-30&model-id={}", + hub_name, device_id, model_id + ) + } else { + format!("{}/{}/?api-version=2018-06-30", hub_name, device_id) + }; let expiry = Utc::now() + Duration::days(1); trace!("Generating token that will expire at {}", expiry);