use crate::{manifest::SignalId, value::Value};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Confidence(f64);
impl Confidence {
pub const CERTAIN: Confidence = Confidence(1.0);
pub fn new(v: f64) -> Self {
Confidence(v.clamp(0.0, 1.0))
}
pub fn get(self) -> f64 {
self.0
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Signal {
pub id: SignalId,
pub value: Value,
pub confidence: Confidence,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Observation {
pub frame_id: u64,
pub captured_at_ms: u64,
pub signals: Vec<Signal>,
}
impl Observation {
pub fn get(&self, id: &str) -> Option<&Signal> {
self.signals.iter().find(|s| s.id.as_str() == id)
}
pub fn weakest_confidence(&self) -> Confidence {
self.signals
.iter()
.map(|s| s.confidence)
.fold(Confidence::CERTAIN, |a, b| if b < a { b } else { a })
}
pub fn age_ms(&self, now_ms: u64) -> u64 {
now_ms.saturating_sub(self.captured_at_ms)
}
}