pub struct TetherAgent { /* private fields */ }

Implementations§

source§

impl TetherAgent

source

pub fn is_connected(&self) -> bool

source

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))
    }
}
source

pub fn broker_uri(&self) -> &str

Return the URI (protocol, IP address, port, path) that was used to connect to the MQTT broker

source

pub fn set_role(&mut self, role: &str)

source

pub fn set_id(&mut self, id: &str)

source

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
Hide additional 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))
    }
}
source

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
Hide additional 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))
    }
}
source

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))
    }
}
source

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))
    }
}
source

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))
    }
}
source

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))
    }
}
source

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§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.