Struct tether_agent::TetherAgent
source · pub struct TetherAgent { /* private fields */ }Implementations§
source§impl TetherAgent
impl TetherAgent
pub fn is_connected(&self) -> bool
sourcepub fn description(&self) -> (&str, &str)
pub fn description(&self) -> (&str, &str)
Returns the Agent Role and ID (group)
Examples found in repository?
examples/publish.rs (line 23)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn broker_uri(&self) -> &str
pub fn broker_uri(&self) -> &str
Return the URI (protocol, IP address, port, path) that was used to connect to the MQTT broker
pub fn set_role(&mut self, role: &str)
pub fn set_id(&mut self, id: &str)
sourcepub fn new(role: &str, id: Option<&str>, broker_host: Option<IpAddr>) -> Self
pub fn new(role: &str, id: Option<&str>, broker_host: Option<IpAddr>) -> Self
Examples found in repository?
examples/publish.rs (line 22)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}More examples
examples/subscribe.rs (line 15)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
println!("Rust Tether Agent subscribe example");
let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", Some("example"), None);
agent.connect();
let input_one = agent.create_input_plug("one", None, None).unwrap();
let input_two = agent.create_input_plug("two", None, None).unwrap();
info!("Checking messages every 1s, 10x...");
for i in 1..10 {
info!("#{i}: Checking for messages...");
if let Some((plug_name, message)) = agent.check_messages() {
if &input_one.name == plug_name.as_str() {
println!(
"******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_one.name,
message.topic(),
message.payload().len()
);
}
if &input_two.name == plug_name.as_str() {
println!(
"******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_two.name,
message.topic(),
message.payload().len()
);
}
}
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn connect(&self)
pub fn connect(&self)
Examples found in repository?
examples/publish.rs (line 26)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}More examples
examples/subscribe.rs (line 17)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
println!("Rust Tether Agent subscribe example");
let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", Some("example"), None);
agent.connect();
let input_one = agent.create_input_plug("one", None, None).unwrap();
let input_two = agent.create_input_plug("two", None, None).unwrap();
info!("Checking messages every 1s, 10x...");
for i in 1..10 {
info!("#{i}: Checking for messages...");
if let Some((plug_name, message)) = agent.check_messages() {
if &input_one.name == plug_name.as_str() {
println!(
"******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_one.name,
message.topic(),
message.payload().len()
);
}
if &input_two.name == plug_name.as_str() {
println!(
"******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_two.name,
message.topic(),
message.payload().len()
);
}
}
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn create_input_plug(
&self,
name: &str,
qos: Option<i32>,
override_topic: Option<&str>
) -> Result<PlugDefinition, ()>
pub fn create_input_plug( &self, name: &str, qos: Option<i32>, override_topic: Option<&str> ) -> Result<PlugDefinition, ()>
Examples found in repository?
examples/subscribe.rs (line 19)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
println!("Rust Tether Agent subscribe example");
let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", Some("example"), None);
agent.connect();
let input_one = agent.create_input_plug("one", None, None).unwrap();
let input_two = agent.create_input_plug("two", None, None).unwrap();
info!("Checking messages every 1s, 10x...");
for i in 1..10 {
info!("#{i}: Checking for messages...");
if let Some((plug_name, message)) = agent.check_messages() {
if &input_one.name == plug_name.as_str() {
println!(
"******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_one.name,
message.topic(),
message.payload().len()
);
}
if &input_two.name == plug_name.as_str() {
println!(
"******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_two.name,
message.topic(),
message.payload().len()
);
}
}
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn create_output_plug(
&self,
name: &str,
qos: Option<i32>,
override_topic: Option<&str>
) -> Result<PlugDefinition, ()>
pub fn create_output_plug( &self, name: &str, qos: Option<i32>, override_topic: Option<&str> ) -> Result<PlugDefinition, ()>
Examples found in repository?
examples/publish.rs (line 29)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn check_messages(&self) -> Option<(String, Message)>
pub fn check_messages(&self) -> Option<(String, Message)>
If a message is waiting return Plug Name, Message (String, Message)
Examples found in repository?
examples/subscribe.rs (line 26)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
println!("Rust Tether Agent subscribe example");
let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", Some("example"), None);
agent.connect();
let input_one = agent.create_input_plug("one", None, None).unwrap();
let input_two = agent.create_input_plug("two", None, None).unwrap();
info!("Checking messages every 1s, 10x...");
for i in 1..10 {
info!("#{i}: Checking for messages...");
if let Some((plug_name, message)) = agent.check_messages() {
if &input_one.name == plug_name.as_str() {
println!(
"******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_one.name,
message.topic(),
message.payload().len()
);
}
if &input_two.name == plug_name.as_str() {
println!(
"******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
input_two.name,
message.topic(),
message.payload().len()
);
}
}
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn publish(
&self,
plug: &PlugDefinition,
payload: Option<&[u8]>
) -> Result<(), ()>
pub fn publish( &self, plug: &PlugDefinition, payload: Option<&[u8]> ) -> Result<(), ()>
Given a plug definition and a raw (u8 buffer) payload, generate a message on an appropriate topic and with the QOS specified in the Plug Definition
Examples found in repository?
examples/publish.rs (line 38)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}sourcepub fn encode_and_publish<T: Serialize>(
&self,
plug: &PlugDefinition,
data: T
) -> Result<(), ()>
pub fn encode_and_publish<T: Serialize>( &self, plug: &PlugDefinition, data: T ) -> Result<(), ()>
Similar to publish but serializes the data automatically before sending
Examples found in repository?
examples/publish.rs (line 51)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
println!("Rust Tether Agent publish example");
let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
builder.init();
debug!("Debugging is enabled; could be verbose");
let agent = TetherAgent::new("RustDemoAgent", None, None);
let (role, id) = agent.description();
info!("Created agent OK: {}, {}", role, id);
agent.connect();
let empty_message_output = agent
.create_output_plug("emptyMessage", None, None)
.unwrap();
let boolean_message_output = agent
.create_output_plug("booleanMessage", None, None)
.unwrap();
let custom_output = agent.create_output_plug("custom", None, None).unwrap();
for i in 1..10 {
info!("#{i}: Sending empty message...");
agent.publish(&empty_message_output, None).unwrap();
let bool = i % 2 == 0;
info!("#{i}: Sending boolean message...");
agent
.publish(&boolean_message_output, Some(&[bool.into()]))
.unwrap();
let custom_message = CustomStruct {
foo: "hello".into(),
bar: 0.42,
};
agent
.encode_and_publish(&custom_output, custom_message)
.unwrap();
thread::sleep(Duration::from_millis(1000))
}
}Auto Trait Implementations§
impl !RefUnwindSafe for TetherAgent
impl Send for TetherAgent
impl Sync for TetherAgent
impl Unpin for TetherAgent
impl !UnwindSafe for TetherAgent
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more