1use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct CameraFacts {
24 pub id: String,
26 pub name: String,
28 pub place: String,
30 pub agent: String,
32 pub session: String,
35 pub ingest: Ingest,
37 pub state: StreamState,
39 pub governor: GovernorFacts,
41 #[serde(default)]
43 pub tracks: Vec<TrackFacts>,
44 #[serde(default)]
46 pub watches: Vec<WatchFacts>,
47 #[serde(default)]
49 pub zones: Vec<ZoneFacts>,
50 #[serde(default = "on")]
52 pub on: bool,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub from: Option<String>,
57 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub passing: Option<String>,
61}
62
63fn on() -> bool {
64 true
65}
66
67impl CameraFacts {
68 pub fn new(
70 id: impl Into<String>,
71 name: impl Into<String>,
72 agent: impl Into<String>,
73 session: impl Into<String>,
74 ) -> Self {
75 let name = name.into();
76 Self {
77 id: id.into(),
78 place: name.to_lowercase(),
79 name,
80 agent: agent.into(),
81 session: session.into(),
82 ingest: Ingest::rtsp(1920, 1080, ""),
83 state: StreamState::Live,
84 governor: GovernorFacts::new(1.0, 0, Utc::now()),
85 tracks: Vec::new(),
86 watches: Vec::new(),
87 zones: Vec::new(),
88 on: true,
89 from: None,
90 passing: None,
91 }
92 }
93
94 #[must_use]
96 pub fn with_place(mut self, place: impl Into<String>) -> Self {
97 self.place = place.into();
98 self
99 }
100
101 #[must_use]
103 pub fn with_ingest(mut self, ingest: Ingest) -> Self {
104 self.ingest = ingest;
105 self
106 }
107
108 #[must_use]
110 pub fn with_state(mut self, state: StreamState) -> Self {
111 self.state = state;
112 self
113 }
114
115 #[must_use]
117 pub fn with_governor(mut self, governor: GovernorFacts) -> Self {
118 self.governor = governor;
119 self
120 }
121
122 #[must_use]
124 pub fn with_tracks(mut self, tracks: Vec<TrackFacts>) -> Self {
125 self.tracks = tracks;
126 self
127 }
128
129 #[must_use]
131 pub fn with_watches(mut self, watches: Vec<WatchFacts>) -> Self {
132 self.watches = watches;
133 self
134 }
135
136 #[must_use]
138 pub fn with_zones(mut self, zones: Vec<ZoneFacts>) -> Self {
139 self.zones = zones;
140 self
141 }
142
143 #[must_use]
145 pub fn switched(mut self, on: bool) -> Self {
146 self.on = on;
147 self
148 }
149
150 #[must_use]
152 pub fn from(mut self, from: impl Into<String>) -> Self {
153 self.from = Some(from.into());
154 self
155 }
156
157 #[must_use]
159 pub fn passing(mut self, what: impl Into<String>) -> Self {
160 self.passing = Some(what.into());
161 self
162 }
163}
164
165#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
167#[serde(rename_all = "camelCase")]
168pub struct Ingest {
169 pub kind: String,
171 pub width: u32,
173 pub height: u32,
175 #[serde(default)]
177 pub at: String,
178}
179
180impl Ingest {
181 pub fn rtsp(width: u32, height: u32, at: impl Into<String>) -> Self {
183 Self { kind: "RTSP".into(), width, height, at: at.into() }
184 }
185
186 pub fn this_computer(width: u32, height: u32) -> Self {
188 Self { kind: "This computer".into(), width, height, at: String::new() }
189 }
190}
191
192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
194#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
195pub enum StreamState {
196 Live,
198 Paused {
200 since: DateTime<Utc>,
202 },
203 Lost {
205 since: DateTime<Utc>,
207 retry_at: DateTime<Utc>,
209 attempt: u32,
211 error: String,
213 },
214}
215
216#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
219#[serde(rename_all = "camelCase")]
220pub struct GovernorFacts {
221 pub rate: f64,
223 pub set_rate: f64,
225 #[serde(default, skip_serializing_if = "Option::is_none")]
227 pub escalation: Option<EscalationFacts>,
228 pub frames_today: u64,
230 pub frames_limit: u64,
232 pub renews_at: DateTime<Utc>,
234 pub look_less_at: f64,
236 pub stop_asking_at: f64,
238 #[serde(default, skip_serializing_if = "Option::is_none")]
242 pub min_rate: Option<f64>,
243 #[serde(default)]
245 pub steps: Vec<GovernorStep>,
246}
247
248impl GovernorFacts {
249 pub fn new(rate: f64, frames_limit: u64, renews_at: DateTime<Utc>) -> Self {
252 Self {
253 rate,
254 set_rate: rate,
255 escalation: None,
256 frames_today: 0,
257 frames_limit,
258 renews_at,
259 look_less_at: 0.9,
260 stop_asking_at: 0.95,
261 min_rate: None,
262 steps: Vec::new(),
263 }
264 }
265
266 #[must_use]
268 pub fn with_escalation(mut self, escalation: EscalationFacts) -> Self {
269 self.escalation = Some(escalation);
270 self
271 }
272
273 #[must_use]
275 pub fn with_frames_today(mut self, frames: u64) -> Self {
276 self.frames_today = frames;
277 self
278 }
279
280 #[must_use]
282 pub fn with_rate(mut self, rate: f64) -> Self {
283 self.rate = rate;
284 self
285 }
286
287 #[must_use]
289 pub fn with_step(mut self, step: GovernorStep) -> Self {
290 self.steps.push(step);
291 self
292 }
293
294 pub fn used(&self) -> f64 {
296 if self.frames_limit == 0 {
297 return 0.0;
298 }
299 (self.frames_today as f64 / self.frames_limit as f64).min(1.0)
300 }
301
302 pub fn stepped_down(&self) -> bool {
304 self.steps.iter().any(|step| !matches!(step.kind, GovernorStepKind::Pause))
305 }
306}
307
308#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
310#[serde(rename_all = "camelCase")]
311pub struct EscalationFacts {
312 pub backend: String,
314 pub last_minute: u32,
316 pub max_per_minute: u32,
318 pub today: u32,
320 pub cost_today_micros: u64,
322 #[serde(default, skip_serializing_if = "Option::is_none")]
324 pub stopped_at: Option<DateTime<Utc>>,
325}
326
327#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
329#[serde(rename_all = "camelCase")]
330pub struct GovernorStep {
331 pub at: DateTime<Utc>,
333 pub kind: GovernorStepKind,
335 #[serde(default, skip_serializing_if = "Option::is_none")]
337 pub used: Option<f64>,
338}
339
340#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
342#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
343pub enum GovernorStepKind {
344 LookLessOften {
346 from: f64,
348 to: f64,
350 },
351 StopAsking,
353 Pause,
355}
356
357#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
359#[serde(rename_all = "camelCase")]
360pub struct TrackFacts {
361 pub id: u64,
363 pub label: String,
365 pub phase: TrackPhase,
367 #[serde(default, skip_serializing_if = "Option::is_none")]
369 pub finding: Option<Finding>,
370}
371
372#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
374#[serde(rename_all = "camelCase")]
375pub enum TrackPhase {
376 Entering,
378 InZone,
380 Leaving,
382 InView,
384}
385
386#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
388#[serde(rename_all = "camelCase")]
389pub struct Finding {
390 pub label: String,
392 pub score: f64,
394 pub frames: u32,
396}
397
398#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
400#[serde(rename_all = "camelCase")]
401pub struct WatchFacts {
402 pub id: String,
404 pub name: String,
406 pub label: String,
408 pub min_score: f64,
410 pub frames: u32,
412 #[serde(default, skip_serializing_if = "Option::is_none")]
414 pub zone: Option<String>,
415 pub cooldown_secs: u32,
417 pub woke_today: u32,
419 #[serde(default, skip_serializing_if = "Option::is_none")]
422 pub wakes_for: Option<String>,
423}
424
425#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
427#[serde(rename_all = "camelCase")]
428pub struct ZoneFacts {
429 pub id: String,
431 pub name: String,
433 pub corners: [f64; 4],
435}
436
437#[derive(Debug, Clone, PartialEq)]
440pub struct CameraFrame {
441 pub bytes: Vec<u8>,
443 pub mime_type: String,
445 pub width: u32,
447 pub height: u32,
449 pub at: DateTime<Utc>,
451 pub latency_ms: u32,
453 pub overlays: Vec<Overlay>,
455}
456
457impl CameraFrame {
458 pub fn new(
460 bytes: Vec<u8>,
461 mime_type: impl Into<String>,
462 width: u32,
463 height: u32,
464 at: DateTime<Utc>,
465 ) -> Self {
466 Self {
467 bytes,
468 mime_type: mime_type.into(),
469 width,
470 height,
471 at,
472 latency_ms: 0,
473 overlays: Vec::new(),
474 }
475 }
476
477 #[must_use]
479 pub fn with_overlays(mut self, overlays: Vec<Overlay>) -> Self {
480 self.overlays = overlays;
481 self
482 }
483
484 #[must_use]
486 pub fn with_latency_ms(mut self, latency_ms: u32) -> Self {
487 self.latency_ms = latency_ms;
488 self
489 }
490}
491
492#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
494#[serde(rename_all = "camelCase")]
495pub struct Overlay {
496 pub label: String,
498 #[serde(default, skip_serializing_if = "Option::is_none")]
500 pub bbox: Option<[f64; 4]>,
501 #[serde(default, skip_serializing_if = "Option::is_none")]
503 pub score: Option<f64>,
504 #[serde(default, skip_serializing_if = "Option::is_none")]
506 pub track: Option<u64>,
507 #[serde(default, skip_serializing_if = "Vec::is_empty")]
509 pub outline: Vec<[f64; 2]>,
510 #[serde(default, skip_serializing_if = "Option::is_none")]
512 pub measurement: Option<Measurement>,
513 #[serde(default, skip_serializing_if = "Option::is_none")]
515 pub frames: Option<u32>,
516 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
518 pub blurred: bool,
519}
520
521impl Overlay {
522 pub fn boxed(label: impl Into<String>, bbox: [f64; 4]) -> Self {
524 Self {
525 label: label.into(),
526 bbox: Some(bbox),
527 score: None,
528 track: None,
529 outline: Vec::new(),
530 measurement: None,
531 frames: None,
532 blurred: false,
533 }
534 }
535
536 #[must_use]
538 pub fn on_track(mut self, track: u64) -> Self {
539 self.track = Some(track);
540 self
541 }
542
543 #[must_use]
545 pub fn scored(mut self, score: f64) -> Self {
546 self.score = Some(score);
547 self
548 }
549
550 #[must_use]
552 pub fn outlined(mut self, outline: Vec<[f64; 2]>) -> Self {
553 self.outline = outline;
554 self
555 }
556
557 #[must_use]
559 pub fn measured(mut self, measurement: Measurement) -> Self {
560 self.measurement = Some(measurement);
561 self
562 }
563
564 #[must_use]
566 pub fn for_frames(mut self, frames: u32) -> Self {
567 self.frames = Some(frames);
568 self
569 }
570
571 #[must_use]
573 pub fn blurred(mut self) -> Self {
574 self.blurred = true;
575 self
576 }
577}
578
579#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
581#[serde(rename_all = "camelCase")]
582pub struct Measurement {
583 pub from: [f64; 2],
585 pub to: [f64; 2],
587 pub millimetres: f64,
589}
590
591#[derive(Debug, Clone, PartialEq)]
593pub struct HeldFrame {
594 pub digest: String,
596 pub frame: CameraFrame,
598}