-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ⬆️ Specify edition in Cargo toml * Add MQTTReadingsSensor * Integrate MQTTSender into main
- Loading branch information
1 parent
e6e8ffd
commit ea12ee1
Showing
5 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
[package] | ||
name = "room-monitor" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1.0.75" | ||
base64 = "0.21.5" | ||
env_logger = "0.10.1" | ||
log = "0.4.20" | ||
mockall = "0.11.4" | ||
paho-mqtt = "0.12.3" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0.108" | ||
tokio = { version = "1.34.0", features = ["sync"] } | ||
|
||
[target.'cfg(target_os = "linux")'.dependencies] | ||
bme280 = "0.4.4" | ||
linux-embedded-hal = "0.4.0-alpha.2" | ||
linux-embedded-hal = "0.4.0-alpha.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use super::ReadingsSender; | ||
use paho_mqtt as mqtt; | ||
use serde_json; | ||
|
||
pub struct MQTTReadingsSender { | ||
client: mqtt::Client, | ||
connection_opts: mqtt::ConnectOptions, | ||
topic: String, | ||
} | ||
|
||
impl MQTTReadingsSender { | ||
pub fn new( | ||
server_uri: &str, | ||
client_id: &str, | ||
topic: &str, | ||
ca_file: &str, | ||
key_store_file: &str, | ||
private_key_file: &str, | ||
) -> anyhow::Result<Self> { | ||
match Self::create_mqtt_client(server_uri, client_id) { | ||
Ok(client) => { | ||
match Self::get_connection_opts(ca_file, key_store_file, private_key_file) { | ||
Ok(connection_opts) => Ok(Self { | ||
client, | ||
connection_opts, | ||
topic: topic.to_string(), | ||
}), | ||
Err(e) => Err(anyhow::anyhow!( | ||
"Failed to create MQTT connection options: {}", | ||
e | ||
)), | ||
} | ||
} | ||
Err(e) => Err(anyhow::anyhow!("Failed to create MQTT client: {}", e)), | ||
} | ||
} | ||
|
||
fn create_mqtt_client(uri: &str, client_id: &str) -> Result<mqtt::Client, mqtt::Error> { | ||
let opts = mqtt::CreateOptionsBuilder::new_v3() | ||
.server_uri(uri) | ||
.client_id(client_id) | ||
.finalize(); | ||
|
||
mqtt::Client::new(opts) | ||
} | ||
|
||
pub fn get_connection_opts( | ||
ca_file: &str, | ||
key_store_file: &str, | ||
private_key_file: &str, | ||
) -> Result<mqtt::ConnectOptions, mqtt::Error> { | ||
let ssl_opts = mqtt::SslOptionsBuilder::new() | ||
.trust_store(ca_file)? | ||
.key_store(key_store_file)? | ||
.private_key(private_key_file)? | ||
.finalize(); | ||
|
||
Ok(mqtt::ConnectOptionsBuilder::new() | ||
.ssl_options(ssl_opts) | ||
.clean_session(true) | ||
.finalize()) | ||
} | ||
} | ||
|
||
impl ReadingsSender for MQTTReadingsSender { | ||
fn send_readings(&self, readings: &crate::driver::Readings) -> anyhow::Result<()> { | ||
let payload = serde_json::to_string(readings).unwrap(); | ||
|
||
let msg = mqtt::MessageBuilder::new() | ||
.topic(&self.topic) | ||
.payload(payload) | ||
.qos(1) | ||
.finalize(); | ||
|
||
match self.client.connect(self.connection_opts.clone()) { | ||
Ok(_) => { | ||
if let Err(e) = self.client.publish(msg) { | ||
log::warn!("Failed to publish readings to MQTT broker: {}", e); | ||
} | ||
|
||
if let Err(e) = self.client.disconnect(None) { | ||
log::warn!( | ||
"Failed to disconnect from MQTT broker after sending readings: {}", | ||
e | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
Err(e) => Err(anyhow::anyhow!("Failed to connect to MQTT broker: {}", e)), | ||
} | ||
} | ||
} |