use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CameraFacts {
pub id: String,
pub name: String,
pub place: String,
pub agent: String,
pub session: String,
pub ingest: Ingest,
pub state: StreamState,
pub governor: GovernorFacts,
#[serde(default)]
pub tracks: Vec<TrackFacts>,
#[serde(default)]
pub watches: Vec<WatchFacts>,
#[serde(default)]
pub zones: Vec<ZoneFacts>,
#[serde(default = "on")]
pub on: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub passing: Option<String>,
}
fn on() -> bool {
true
}
impl CameraFacts {
pub fn new(
id: impl Into<String>,
name: impl Into<String>,
agent: impl Into<String>,
session: impl Into<String>,
) -> Self {
let name = name.into();
Self {
id: id.into(),
place: name.to_lowercase(),
name,
agent: agent.into(),
session: session.into(),
ingest: Ingest::rtsp(1920, 1080, ""),
state: StreamState::Live,
governor: GovernorFacts::new(1.0, 0, Utc::now()),
tracks: Vec::new(),
watches: Vec::new(),
zones: Vec::new(),
on: true,
from: None,
passing: None,
}
}
#[must_use]
pub fn with_place(mut self, place: impl Into<String>) -> Self {
self.place = place.into();
self
}
#[must_use]
pub fn with_ingest(mut self, ingest: Ingest) -> Self {
self.ingest = ingest;
self
}
#[must_use]
pub fn with_state(mut self, state: StreamState) -> Self {
self.state = state;
self
}
#[must_use]
pub fn with_governor(mut self, governor: GovernorFacts) -> Self {
self.governor = governor;
self
}
#[must_use]
pub fn with_tracks(mut self, tracks: Vec<TrackFacts>) -> Self {
self.tracks = tracks;
self
}
#[must_use]
pub fn with_watches(mut self, watches: Vec<WatchFacts>) -> Self {
self.watches = watches;
self
}
#[must_use]
pub fn with_zones(mut self, zones: Vec<ZoneFacts>) -> Self {
self.zones = zones;
self
}
#[must_use]
pub fn switched(mut self, on: bool) -> Self {
self.on = on;
self
}
#[must_use]
pub fn from(mut self, from: impl Into<String>) -> Self {
self.from = Some(from.into());
self
}
#[must_use]
pub fn passing(mut self, what: impl Into<String>) -> Self {
self.passing = Some(what.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Ingest {
pub kind: String,
pub width: u32,
pub height: u32,
#[serde(default)]
pub at: String,
}
impl Ingest {
pub fn rtsp(width: u32, height: u32, at: impl Into<String>) -> Self {
Self { kind: "RTSP".into(), width, height, at: at.into() }
}
pub fn this_computer(width: u32, height: u32) -> Self {
Self { kind: "This computer".into(), width, height, at: String::new() }
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
pub enum StreamState {
Live,
Paused {
since: DateTime<Utc>,
},
Lost {
since: DateTime<Utc>,
retry_at: DateTime<Utc>,
attempt: u32,
error: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GovernorFacts {
pub rate: f64,
pub set_rate: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub escalation: Option<EscalationFacts>,
pub frames_today: u64,
pub frames_limit: u64,
pub renews_at: DateTime<Utc>,
pub look_less_at: f64,
pub stop_asking_at: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_rate: Option<f64>,
#[serde(default)]
pub steps: Vec<GovernorStep>,
}
impl GovernorFacts {
pub fn new(rate: f64, frames_limit: u64, renews_at: DateTime<Utc>) -> Self {
Self {
rate,
set_rate: rate,
escalation: None,
frames_today: 0,
frames_limit,
renews_at,
look_less_at: 0.9,
stop_asking_at: 0.95,
min_rate: None,
steps: Vec::new(),
}
}
#[must_use]
pub fn with_escalation(mut self, escalation: EscalationFacts) -> Self {
self.escalation = Some(escalation);
self
}
#[must_use]
pub fn with_frames_today(mut self, frames: u64) -> Self {
self.frames_today = frames;
self
}
#[must_use]
pub fn with_rate(mut self, rate: f64) -> Self {
self.rate = rate;
self
}
#[must_use]
pub fn with_step(mut self, step: GovernorStep) -> Self {
self.steps.push(step);
self
}
pub fn used(&self) -> f64 {
if self.frames_limit == 0 {
return 0.0;
}
(self.frames_today as f64 / self.frames_limit as f64).min(1.0)
}
pub fn stepped_down(&self) -> bool {
self.steps.iter().any(|step| !matches!(step.kind, GovernorStepKind::Pause))
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EscalationFacts {
pub backend: String,
pub last_minute: u32,
pub max_per_minute: u32,
pub today: u32,
pub cost_today_micros: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stopped_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GovernorStep {
pub at: DateTime<Utc>,
pub kind: GovernorStepKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub used: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
pub enum GovernorStepKind {
LookLessOften {
from: f64,
to: f64,
},
StopAsking,
Pause,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrackFacts {
pub id: u64,
pub label: String,
pub phase: TrackPhase,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub finding: Option<Finding>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TrackPhase {
Entering,
InZone,
Leaving,
InView,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Finding {
pub label: String,
pub score: f64,
pub frames: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WatchFacts {
pub id: String,
pub name: String,
pub label: String,
pub min_score: f64,
pub frames: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub zone: Option<String>,
pub cooldown_secs: u32,
pub woke_today: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wakes_for: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ZoneFacts {
pub id: String,
pub name: String,
pub corners: [f64; 4],
}
#[derive(Debug, Clone, PartialEq)]
pub struct CameraFrame {
pub bytes: Vec<u8>,
pub mime_type: String,
pub width: u32,
pub height: u32,
pub at: DateTime<Utc>,
pub latency_ms: u32,
pub overlays: Vec<Overlay>,
}
impl CameraFrame {
pub fn new(
bytes: Vec<u8>,
mime_type: impl Into<String>,
width: u32,
height: u32,
at: DateTime<Utc>,
) -> Self {
Self {
bytes,
mime_type: mime_type.into(),
width,
height,
at,
latency_ms: 0,
overlays: Vec::new(),
}
}
#[must_use]
pub fn with_overlays(mut self, overlays: Vec<Overlay>) -> Self {
self.overlays = overlays;
self
}
#[must_use]
pub fn with_latency_ms(mut self, latency_ms: u32) -> Self {
self.latency_ms = latency_ms;
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Overlay {
pub label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bbox: Option<[f64; 4]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub track: Option<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub outline: Vec<[f64; 2]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub measurement: Option<Measurement>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub frames: Option<u32>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub blurred: bool,
}
impl Overlay {
pub fn boxed(label: impl Into<String>, bbox: [f64; 4]) -> Self {
Self {
label: label.into(),
bbox: Some(bbox),
score: None,
track: None,
outline: Vec::new(),
measurement: None,
frames: None,
blurred: false,
}
}
#[must_use]
pub fn on_track(mut self, track: u64) -> Self {
self.track = Some(track);
self
}
#[must_use]
pub fn scored(mut self, score: f64) -> Self {
self.score = Some(score);
self
}
#[must_use]
pub fn outlined(mut self, outline: Vec<[f64; 2]>) -> Self {
self.outline = outline;
self
}
#[must_use]
pub fn measured(mut self, measurement: Measurement) -> Self {
self.measurement = Some(measurement);
self
}
#[must_use]
pub fn for_frames(mut self, frames: u32) -> Self {
self.frames = Some(frames);
self
}
#[must_use]
pub fn blurred(mut self) -> Self {
self.blurred = true;
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Measurement {
pub from: [f64; 2],
pub to: [f64; 2],
pub millimetres: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct HeldFrame {
pub digest: String,
pub frame: CameraFrame,
}