1use babbel_json::{
10 BufferDestination, BufferSource, Node, Numeric, ParserConfig,
11 embedded::{ArrayBuilder, ObjectBuilder, config, memory, sensor},
12 parse_with_config, stringify,
13};
14
15fn main() {
16 println!("=== Embedded IoT Sensor Example ===\n");
17
18 println!("1. Simple Sensor Reading:");
20 let temp_reading = sensor::simple_reading("temp_sensor_01", 23.5, 1699876543);
21 print_json("Temperature Reading", &temp_reading);
22
23 println!("\n2. Multi-Value Sensor Reading:");
25 let env_values = vec![
26 ("temperature", 23.5),
27 ("humidity", 45.2),
28 ("pressure", 1013.25),
29 ];
30 let env_reading = sensor::multi_reading("env_sensor_01", &env_values, 1699876543);
31 print_json("Environmental Reading", &env_reading);
32
33 println!("\n3. Batched Sensor Readings:");
35 let readings = vec![
36 sensor::simple_reading("temp_01", 23.5, 1699876543),
37 sensor::simple_reading("temp_01", 23.7, 1699876563),
38 sensor::simple_reading("temp_01", 23.4, 1699876583),
39 ];
40 let batch = sensor::batch_readings("temp_01", readings);
41 print_json("Batch of Readings", &batch);
42
43 println!("\n4. Memory-Efficient Construction:");
45 let reading = ObjectBuilder::with_capacity(4)
46 .add_str("device", "motion_01")
47 .add_bool("detected", true)
48 .add_i64("timestamp", 1699876543)
49 .add_u32("count", 5)
50 .build();
51
52 let size = memory::estimate_node_size(&reading);
53 println!(" Estimated memory: {} bytes", size);
54 print_json("Motion Detection", &reading);
55
56 println!("\n5. Device Configuration:");
58 let device_config = config::simple()
59 .add_str("wifi_ssid", "IoT_Network")
60 .add_str("mqtt_broker", "192.168.1.100")
61 .add_i32("sample_interval_ms", 5000)
62 .add_i32("transmit_interval_ms", 60000)
63 .add_bool("deep_sleep_enabled", true)
64 .add_f64("temperature_threshold", 30.0)
65 .build();
66
67 print_json("Device Config", &device_config);
68
69 println!("\n Reading config values:");
71 if let Some(ssid) = config::get_string(&device_config, "wifi_ssid") {
72 println!(" WiFi SSID: {}", ssid);
73 }
74 if let Some(interval) = config::get_i32(&device_config, "sample_interval_ms") {
75 println!(" Sample Interval: {}ms", interval);
76 }
77 if let Some(deep_sleep) = config::get_bool(&device_config, "deep_sleep_enabled") {
78 println!(" Deep Sleep: {}", deep_sleep);
79 }
80
81 println!("\n6. Parsing with Memory Limits:");
83 let json_config = r#"{
84 "device_id": "sensor_01",
85 "location": "room_A",
86 "enabled": true,
87 "thresholds": {
88 "min": 15.0,
89 "max": 30.0
90 }
91 }"#;
92
93 let config = ParserConfig::strict();
94 let mut source = BufferSource::new(json_config.as_bytes());
95
96 match parse_with_config(&mut source, &config) {
97 Ok(parsed) => {
98 println!(" ✓ Parsed configuration successfully");
99
100 if let Some(device_id) = config::get_string(&parsed, "device_id") {
101 println!(" Device ID: {}", device_id);
102 }
103
104 if let Some(thresholds) = parsed.get("thresholds") {
105 if let Some(min) = config::get_f64(thresholds, "min") {
106 println!(" Min Threshold: {}", min);
107 }
108 if let Some(max) = config::get_f64(thresholds, "max") {
109 println!(" Max Threshold: {}", max);
110 }
111 }
112 }
113 Err(e) => println!(" ✗ Parse error: {}", e),
114 }
115
116 println!("\n7. Sensor Data Array:");
118 let sensor_array = ArrayBuilder::with_capacity(5)
119 .add_f64(23.5)
120 .add_f64(23.7)
121 .add_f64(23.4)
122 .add_f64(23.6)
123 .add_f64(23.8)
124 .build();
125
126 print_json("Temperature History", &sensor_array);
127
128 println!("\n8. Complex Sensor Payload:");
130 let mut status_map = std::collections::HashMap::new();
131 status_map.insert("battery".to_string(), Node::Number(Numeric::UInt32(87)));
132 status_map.insert(
133 "signal_strength".to_string(),
134 Node::Number(Numeric::Int32(-65)),
135 );
136 status_map.insert("uptime".to_string(), Node::Number(Numeric::UInt32(3600)));
137
138 let payload = ObjectBuilder::new()
139 .add_str("device", "multi_sensor_01")
140 .add_i64("timestamp", 1699876543)
141 .add_object("status", status_map)
142 .add_node("reading", sensor::simple_reading("temp", 24.1, 1699876543))
143 .build();
144
145 let size = memory::estimate_node_size(&payload);
146 println!(" Payload size: {} bytes", size);
147 print_json("Complex Payload", &payload);
148
149 println!("\n9. Memory Requirements Analysis:");
151 println!(" Node size: {} bytes", memory::node_size());
152 println!(" Numeric size: {} bytes", memory::numeric_size());
153
154 let simple = sensor::simple_reading("test", 1.0, 0);
155 println!(
156 " Simple reading: {} bytes",
157 memory::estimate_node_size(&simple)
158 );
159
160 let multi = sensor::multi_reading("test", &[("a", 1.0), ("b", 2.0)], 0);
161 println!(
162 " Multi reading: {} bytes",
163 memory::estimate_node_size(&multi)
164 );
165
166 println!("\n=== Memory Optimization Tips ===");
167 println!("1. Use with_capacity() when size is known");
168 println!("2. Reuse buffers when possible");
169 println!("3. Use ParserConfig::strict() for constrained devices");
170 println!("4. Batch readings to reduce overhead");
171 println!("5. Choose appropriate numeric types (i32 vs i64)");
172}
173
174fn print_json(label: &str, node: &Node) {
175 let mut dest = BufferDestination::new();
176 if stringify(node, &mut dest).is_ok() {
177 println!(" {}: {}", label, dest.to_string());
178 }
179}