use std::io::Read;
use sml_kit::io::{FrameReader, ReadError};
use sml_kit::{Obis, readout};
fn main() -> std::io::Result<()> {
let source: Box<dyn Read> = match std::env::args().nth(1) {
Some(path) => Box::new(std::fs::File::open(path)?),
None => Box::new(std::io::stdin()),
};
for frame in FrameReader::new(source) {
let frame = match frame {
Ok(frame) => frame,
Err(ReadError::Frame(e)) => {
eprintln!("skipped a damaged frame: {e}");
continue;
}
Err(ReadError::Io(e))
if matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock) =>
{
continue;
}
Err(ReadError::Io(e)) => return Err(e),
};
match readout(&frame) {
Ok(list) => {
let show = |obis| list.quantity(obis).map_or_else(|| "-".to_owned(), |q| q.to_string());
println!(
"{} A+ {} A- {} P {}",
list.server_id,
show(Obis::ENERGY_IMPORT),
show(Obis::ENERGY_EXPORT),
show(Obis::POWER)
);
}
Err(e) => eprintln!("no readings: {e} (try `cargo run --example decode` on a capture)"),
}
}
Ok(())
}