init
This commit is contained in:
9
examples/simple_producer/Cargo.toml
Normal file
9
examples/simple_producer/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
[package]
|
||||
name = "simple_producer"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
server = { path = "../../server" }
|
||||
tonic = "0.12.3"
|
||||
tokio = { version = "1.40.0", features = ["rt-multi-thread"] }
|
||||
64
examples/simple_producer/src/main.rs
Normal file
64
examples/simple_producer/src/main.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use server::core::telemetry_service_client::TelemetryServiceClient;
|
||||
use server::core::telemetry_value::Value;
|
||||
use server::core::{TelemetryDataType, TelemetryDefinitionRequest, TelemetryItem, TelemetryValue, Timestamp};
|
||||
use std::f32::consts::TAU;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::time::Instant;
|
||||
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
|
||||
use tonic::codegen::tokio_stream::StreamExt;
|
||||
use tonic::Request;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut client = TelemetryServiceClient::connect("http://[::1]:50051").await?;
|
||||
|
||||
let request = Request::new(TelemetryDefinitionRequest {
|
||||
name: "simple_producer/sin".into(),
|
||||
data_type: TelemetryDataType::Float32.into(),
|
||||
});
|
||||
|
||||
let response = client.new_telemetry(request).await?.into_inner();
|
||||
|
||||
println!("Response={:?}", response);
|
||||
let Some(uuid) = response.uuid else { return Err("UUID Missing".into()); };
|
||||
|
||||
let (tx, rx) = mpsc::channel(128);
|
||||
|
||||
let mut response_stream = client.insert_telemetry(ReceiverStream::new(rx)).await?.into_inner();
|
||||
|
||||
let mut next_time = Instant::now();
|
||||
let mut index = 0;
|
||||
loop {
|
||||
next_time += Duration::from_millis(100);
|
||||
index += 10;
|
||||
tokio::time::sleep_until(next_time).await;
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
|
||||
tx.send(TelemetryItem {
|
||||
uuid: Some(uuid.clone()),
|
||||
value: Some(TelemetryValue {
|
||||
value: Some(Value::Float32(
|
||||
(TAU * (index as f32) / (1000.0_f32)).sin()
|
||||
)),
|
||||
}),
|
||||
timestamp: Some(Timestamp {
|
||||
secs: now.as_secs(),
|
||||
nanos: now.subsec_nanos(),
|
||||
}),
|
||||
}).await?;
|
||||
if let Some(response) = response_stream.next().await {
|
||||
match response {
|
||||
Ok(_) => {
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Error");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Stream closed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user