use alec::{Classifier, Context, Decoder, Encoder, RawData};
fn main() {
println!("=== ALEC Simple Sensor Example ===\n");
let mut encoder = Encoder::new();
let mut decoder = Decoder::new();
let classifier = Classifier::default();
let mut context = Context::new();
let temperatures = vec![
20.0, 20.1, 20.2, 20.1, 20.3, 20.2, 20.4, 20.3, 20.5, 20.4, 20.3, 20.2, 20.1, 20.0, 20.1, 35.0, 20.5, 20.4, 20.3, 20.2, ];
println!(
"{:<5} {:<10} {:<15} {:<8} {:<10}",
"N°", "Temp (°C)", "Priority", "Size", "Transmit?"
);
println!("{}", "-".repeat(55));
let mut total_raw_size = 0;
let mut total_encoded_size = 0;
let mut transmitted_count = 0;
for (i, &temp) in temperatures.iter().enumerate() {
let data = RawData::new(temp, i as u64);
let classification = classifier.classify(&data, &context);
let message = encoder.encode(&data, &classification, &context);
total_raw_size += data.raw_size();
let msg_size = message.len();
let should_transmit = classification.priority.should_transmit();
if should_transmit {
total_encoded_size += msg_size;
transmitted_count += 1;
let decoded = decoder.decode(&message, &context).unwrap();
assert!((decoded.value - temp).abs() < 0.1, "Decode mismatch!");
}
context.observe(&data);
let transmit_str = if should_transmit { "✓" } else { "✗" };
println!(
"{:<5} {:<10.1} {:<15} {:<8} {:<10}",
i + 1,
temp,
format!("{:?}", classification.priority),
if should_transmit {
msg_size.to_string()
} else {
"-".to_string()
},
transmit_str
);
}
println!("{}", "-".repeat(55));
println!("\n=== Statistics ===\n");
println!("Total measurements: {}", temperatures.len());
println!("Transmitted: {}", transmitted_count);
println!(
"Suppressed (P4/P5): {}",
temperatures.len() - transmitted_count
);
println!();
println!("Raw data size: {} bytes", total_raw_size);
println!("Encoded size: {} bytes", total_encoded_size);
println!(
"Compression ratio: {:.1}%",
(1.0 - total_encoded_size as f64 / total_raw_size as f64) * 100.0
);
println!();
println!("Context patterns: {}", context.pattern_count());
println!("Context version: {}", context.version());
println!("Context memory: {} bytes", context.memory_usage());
}