1use std::{thread, time::Duration};
2
3use env_logger::{Builder, Env};
4use log::{debug, info};
5use serde::Serialize;
6use tether_agent::{PlugOptionsBuilder, TetherAgentOptionsBuilder};
7
8#[derive(Serialize)]
9#[serde(rename_all = "camelCase")]
10struct CustomStruct {
11 id: usize,
12 name: String,
13}
14fn main() {
15 println!("Rust Tether Agent publish example");
16
17 let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
18 builder.init();
19
20 debug!("Debugging is enabled; could be verbose");
21
22 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
23 .build()
24 .expect("failed to connect Tether");
25 let (role, id, _) = tether_agent.description();
26 info!("Created agent OK: {}, {}", role, id);
27
28 let empty_message_output = PlugOptionsBuilder::create_output("nothing")
29 .build(&mut tether_agent)
30 .expect("failed to create output");
31 let boolean_message_output = PlugOptionsBuilder::create_output("one")
32 .build(&mut tether_agent)
33 .expect("failed to create output");
34 let custom_output = PlugOptionsBuilder::create_output("two")
35 .topic(Some("custom/custom/two"))
36 .build(&mut tether_agent)
37 .expect("failed to create output");
38 let grouped_output_1 = PlugOptionsBuilder::create_output("one")
39 .id(Some("groupMessages"))
40 .build(&mut tether_agent)
41 .expect("failed to create output");
42 let grouped_output_2 = PlugOptionsBuilder::create_output("two")
43 .id(Some("groupMessages"))
44 .build(&mut tether_agent)
45 .expect("failed to create output");
46
47 for i in 1..=10 {
48 info!("#{i}: Sending empty message...");
49 tether_agent.publish(&empty_message_output, None).unwrap();
50
51 let bool = i % 2 == 0;
52 info!("#{i}: Sending boolean message...");
53 tether_agent
54 .publish(&boolean_message_output, Some(&[bool.into()]))
55 .unwrap();
56
57 info!("#{i}: Sending custom struct message...");
58 let custom_message = CustomStruct {
59 id: i,
60 name: "hello".into(),
61 };
62 tether_agent
63 .encode_and_publish(&custom_output, custom_message)
64 .unwrap();
65
66 info!("#{i}: Sending grouped messages...");
67 tether_agent.publish(&grouped_output_1, None).unwrap();
68 tether_agent.publish(&grouped_output_2, None).unwrap();
69
70 thread::sleep(Duration::from_millis(1000))
71 }
72}