-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement UTransport for MQTT v5 (#4)
* Initial impl of send func * Better implementation of send message, still missing topic split * Initial update to 1.5.8 spec * Fix clippy warnings * Update cargo to reflect current rust toolchain version * Address some PR comments * ran cargo fmt * First step towards removing hard coded protobuf field number values * Update properties > attributes fn and add more tests * Fix clippy warnings * Add unit tests for lib * Fix lib tests, add skeleton for transport tests * add remaining tests, add examples, update readme * Fix library to properly handle wildcard subscriptions without duplication of messages * remove dead_code macro from upclient implementation * resolved remaining unwraps that could lead to panics * fixed clippy warnings, updated rust version to address test pipeline error * Remove unecessary println statements * Add reference to examples to show how to use UPClientMqtt * Fix free subscription id bug * Use channels rather than blocking callback thread for handling listeners * removed async-std from cargo.toml * remove subscriber_example comment * refactored code to remove indentations and other minor improvements * added newlines to test_cases to try to make inputs more readable * added check to confirm when test is expected to hit an error * Update up-rust to 0.1.0 * Fix tests and add uattributes version to mqtt props * cargo fmt fix * Improve logging for mqtt props to attributes
- Loading branch information
Showing
7 changed files
with
2,193 additions
and
29 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
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,61 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
use std::{str::FromStr, time::SystemTime}; | ||
|
||
use up_client_mqtt5_rust::{MqttConfig, UPClientMqtt, UPClientMqttType}; | ||
use up_rust::{UMessageBuilder, UPayloadFormat, UStatus, UTransport, UUri, UUID}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), UStatus> { | ||
let config = MqttConfig { | ||
mqtt_hostname: "localhost".to_string(), | ||
mqtt_port: "1883".to_string(), | ||
max_buffered_messages: 100, | ||
max_subscriptions: 100, | ||
session_expiry_interval: 3600, | ||
ssl_options: None, | ||
}; | ||
|
||
let client = UPClientMqtt::new( | ||
config, | ||
UUID::build(), | ||
"Vehicle_B".to_string(), | ||
UPClientMqttType::Device, | ||
) | ||
.await?; | ||
|
||
let source = | ||
UUri::from_str("//Vehicle_B/A8000/2/8A50").expect("Failed to create source filter"); | ||
|
||
loop { | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
let current_time = SystemTime::now() | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs(); | ||
let message = UMessageBuilder::publish(source.clone()) | ||
.build_with_payload( | ||
current_time.to_string(), | ||
UPayloadFormat::UPAYLOAD_FORMAT_TEXT, | ||
) | ||
.expect("Failed to build message"); | ||
|
||
println!( | ||
"Sending message: {} to source: {}", | ||
current_time, | ||
source.to_uri(false) | ||
); | ||
client.send(message).await?; | ||
} | ||
} |
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,72 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
use std::{ | ||
str::{self, FromStr}, | ||
sync::Arc, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
use up_client_mqtt5_rust::{MqttConfig, UPClientMqtt, UPClientMqttType}; | ||
use up_rust::{UListener, UMessage, UStatus, UTransport, UUri, UUID}; | ||
|
||
const WILDCARD_ENTITY_ID: u32 = 0x0000_FFFF; | ||
const WILDCARD_ENTITY_VERSION: u32 = 0x0000_00FF; | ||
const WILDCARD_RESOURCE_ID: u32 = 0x0000_FFFF; | ||
|
||
struct PrintlnListener {} | ||
|
||
#[async_trait] | ||
impl UListener for PrintlnListener { | ||
async fn on_receive(&self, message: UMessage) { | ||
let msg_payload = message.payload.unwrap(); | ||
let msg_str: &str = str::from_utf8(&msg_payload).unwrap(); | ||
println!("Received message: {msg_str}"); | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), UStatus> { | ||
let config = MqttConfig { | ||
mqtt_hostname: "localhost".to_string(), | ||
mqtt_port: "1883".to_string(), | ||
max_buffered_messages: 100, | ||
max_subscriptions: 100, | ||
session_expiry_interval: 3600, | ||
ssl_options: None, | ||
}; | ||
|
||
let client = UPClientMqtt::new( | ||
config, | ||
UUID::build(), | ||
"Vehicle_A".to_string(), | ||
UPClientMqttType::Device, | ||
) | ||
.await?; | ||
|
||
let listener = Arc::new(PrintlnListener {}); | ||
let source_filter = UUri::from_str(&format!( | ||
"//Vehicle_B/{WILDCARD_ENTITY_ID:X}/{WILDCARD_ENTITY_VERSION:X}/{WILDCARD_RESOURCE_ID:X}" | ||
)) | ||
.expect("Failed to create source filter"); | ||
|
||
println!("Subscribing to: {}", source_filter.to_uri(false)); | ||
|
||
client | ||
.register_listener(&source_filter, None, listener.clone()) | ||
.await?; | ||
|
||
loop { | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
} | ||
} |
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,2 +1,2 @@ | ||
[toolchain] | ||
channel = "1.76" | ||
channel = "1.77" |
Oops, something went wrong.