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.phyware.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¶
When PhyCloud cannot be reached, the agent writes events to a local buffer and forwards them once it can.
- Signed first. Events are signed before they are buffered. Anyone who edits a buffered event (
FileBufferunits in the buffer directory) breaks its signature (or its Merkle inclusion proof), and PhyCloud flags it. - Batches stay whole. Each send batch is buffered as one unit, so a Merkle-signed batch stays together.
- Strict order. Units are forwarded in the order they were written. A unit is sent only after every older one has been delivered, and newer events queue behind buffered ones rather than overtaking them. PhyCloud then sees the original sequence order, and its gap detection reports a late batch as
late, not as a replay. - Nothing blocks the queue. Events PhyCloud refuses outright (validation errors), and unreadable units, are moved to the buffer's
rejected/folder. Events PhyCloud already has (Duplicate event, after a lost response) count as delivered.
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.phyware.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.