Rust SDK — Examples¶
Basic Event¶
use phytrace_sdk::{UdmEvent, EventType, SourceType};
use phytrace_sdk::models::domains::*;
fn main() {
let event = UdmEvent::new(SourceType::Amr)
.with_event_type(EventType::TelemetryPeriodic)
.with_identity(IdentityDomain {
source_id: Some("robot-001".to_string()),
fleet_id: Some("warehouse-fleet".to_string()),
..Default::default()
})
.with_location(LocationDomain {
position_2d: Some(Position2D { x: 10.5, y: 20.3 }),
heading_rad: Some(1.57),
..Default::default()
});
println!("{}", event.to_json_pretty().unwrap());
}
Agent Quickstart¶
use phytrace_sdk::{PhyTraceAgent, PhyTraceConfig, SourceType};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = PhyTraceConfig::new("robot-001")
.with_endpoint("https://api.phycloud.io/v1/events")
.with_api_key("your-api-key")
.with_source_type(SourceType::Amr);
let agent = PhyTraceAgent::from_config(config).await?;
agent.start().await?;
agent.emit()
.position_2d(10.0, 20.0)
.heading(1.57)
.speed(2.5)
.battery_soc(85.0)
.send()
.await?;
agent.stop().await?;
Ok(())
}
Offline Buffering¶
use phytrace_sdk::{PhyTraceConfig, PhyTraceAgent, SourceType};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = PhyTraceConfig::new("robot-001")
.with_endpoint("https://api.phycloud.io/v1/events")
.with_source_type(SourceType::Amr)
.with_buffer_path("/tmp/phytrace-buffer")
.with_buffer_max_size_mb(512);
let agent = PhyTraceAgent::from_config(config).await?;
agent.start().await?;
// Events are buffered locally if the endpoint is unreachable
// and automatically flushed when connectivity is restored.
for _ in 0..1000 {
agent.emit()
.position_2d(10.0, 20.0)
.speed(1.5)
.send()
.await?;
}
agent.stop().await?;
Ok(())
}
Custom Transport¶
use phytrace_sdk::transport::{Transport, SendResult, BatchSendResult};
use phytrace_sdk::{UdmEvent, Error};
use async_trait::async_trait;
struct LoggingTransport;
#[async_trait]
impl Transport for LoggingTransport {
async fn send(&self, event: &UdmEvent) -> Result<SendResult, Error> {
println!("Sending event: {}", event.event_id);
Ok(SendResult::success())
}
async fn send_batch(&self, events: &[UdmEvent]) -> Result<BatchSendResult, Error> {
println!("Sending {} events", events.len());
Ok(BatchSendResult::all_success(events.len()))
}
}
See the full examples in the repository.