use std::sync::mpsc;
use tonic::Request;
use liveplot::sink::{PlotCommand, PlotPoint};
use liveplot::{run_liveplot, LivePlotConfig};
pub mod sine {
pub mod v1 {
tonic::include_proto!("sine.v1");
}
}
use sine::v1::{sine_wave_client::SineWaveClient, SubscribeRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (tx, rx) = mpsc::channel::<PlotCommand>();
let ui_handle = std::thread::spawn(move || {
if let Err(e) = run_liveplot(rx, LivePlotConfig::default()) {
eprintln!("UI error: {e}");
}
});
let mut client = SineWaveClient::connect("http://127.0.0.1:50051").await?;
let mut stream = client
.subscribe(Request::new(SubscribeRequest {}))
.await?
.into_inner();
let _ = tx.send(PlotCommand::RegisterTrace {
id: 1,
name: "signal".into(),
info: None,
});
while let Some(sample) = stream.message().await? {
let t_s = (sample.timestamp_micros as f64) * 1e-6;
let cmd = PlotCommand::Point {
trace_id: 1,
point: PlotPoint {
x: t_s,
y: sample.value,
},
};
if tx.send(cmd).is_err() {
break;
}
}
let _ = ui_handle.join();
Ok(())
}