oxvif 0.10.0

Async Rust client library for the ONVIF IP camera protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Result types for a [`HealthCheck`](super::HealthCheck) run.

use std::fmt;
use std::time::Duration;

use serde::{Deserialize, Serialize};

/// Which area of the device a check exercises. Also drives report grouping order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Category {
    Connectivity,
    Time,
    Services,
    Media,
    Imaging,
    Ptz,
    Events,
    Network,
    Users,
    Coverage,
    Write,
}

impl Category {
    /// Human-readable group label.
    pub fn label(self) -> &'static str {
        match self {
            Category::Connectivity => "Connectivity",
            Category::Time => "Time",
            Category::Services => "Services",
            Category::Media => "Media",
            Category::Imaging => "Imaging",
            Category::Ptz => "PTZ",
            Category::Events => "Events",
            Category::Network => "Network",
            Category::Users => "Users",
            Category::Coverage => "Parse coverage",
            Category::Write => "Write round-trip",
        }
    }
}

/// Outcome of a single check.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "reason")]
pub enum CheckStatus {
    /// The check succeeded.
    Pass,
    /// The check succeeded but something is off (reason attached).
    Warn(String),
    /// The check failed (reason attached).
    Fail(String),
    /// The check did not run (reason attached, e.g. service not advertised).
    Skip(String),
}

impl CheckStatus {
    /// Short uppercase tag for table output.
    pub fn tag(&self) -> &'static str {
        match self {
            CheckStatus::Pass => "PASS",
            CheckStatus::Warn(_) => "WARN",
            CheckStatus::Fail(_) => "FAIL",
            CheckStatus::Skip(_) => "SKIP",
        }
    }
}

/// Result of one named check.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CheckResult {
    /// Stable identifier (e.g. `"get_profiles"`).
    pub id: String,
    /// Grouping category.
    pub category: Category,
    /// Pass / Warn / Fail / Skip.
    pub status: CheckStatus,
    /// Extra context (parsed values on success, error text otherwise).
    pub detail: String,
    /// Wall-clock time this check took.
    #[serde(with = "duration_ms", rename = "elapsed_ms")]
    pub elapsed: Duration,
}

mod duration_ms {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::time::Duration;

    pub fn serialize<S: Serializer>(d: &Duration, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_u128(d.as_millis())
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Duration, D::Error> {
        let ms = u64::deserialize(d)?;
        Ok(Duration::from_millis(ms))
    }
}

impl CheckResult {
    pub(crate) fn pass(
        id: impl Into<String>,
        category: Category,
        detail: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            category,
            status: CheckStatus::Pass,
            detail: detail.into(),
            elapsed: Duration::ZERO,
        }
    }

    pub(crate) fn warn(
        id: impl Into<String>,
        category: Category,
        reason: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            category,
            status: CheckStatus::Warn(reason.into()),
            detail: detail.into(),
            elapsed: Duration::ZERO,
        }
    }

    pub(crate) fn fail(
        id: impl Into<String>,
        category: Category,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            category,
            status: CheckStatus::Fail(reason.into()),
            detail: String::new(),
            elapsed: Duration::ZERO,
        }
    }

    pub(crate) fn skip(
        id: impl Into<String>,
        category: Category,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            category,
            status: CheckStatus::Skip(reason.into()),
            detail: String::new(),
            elapsed: Duration::ZERO,
        }
    }

    pub(crate) fn with_elapsed(mut self, elapsed: Duration) -> Self {
        self.elapsed = elapsed;
        self
    }
}

/// Per-profile conformance verdict derived from the check results.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProfileVerdict {
    /// All required checks passed.
    Conformant,
    /// Some required checks passed, others failed or were skipped.
    Partial,
    /// No required checks passed (profile likely unsupported).
    Unsupported,
}

impl ProfileVerdict {
    fn label(self) -> &'static str {
        match self {
            ProfileVerdict::Conformant => "conformant",
            ProfileVerdict::Partial => "partial",
            ProfileVerdict::Unsupported => "unsupported",
        }
    }
}

/// Conformance assessment for the ONVIF profiles, derived from check results.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProfileAssessment {
    /// Profile S (video streaming): verdict + non-passing required check ids.
    pub profile_s: (ProfileVerdict, Vec<String>),
    /// Profile T (advanced streaming / imaging / events).
    pub profile_t: (ProfileVerdict, Vec<String>),
    /// Profile G (recording / search / replay) — not exercised; informational.
    pub profile_g: (ProfileVerdict, Vec<String>),
}

/// The full result of a [`HealthCheck`](super::HealthCheck) run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HealthReport {
    /// Device URL the check ran against.
    pub target: String,
    /// Total wall-clock time for the whole run.
    #[serde(with = "duration_ms", rename = "total_elapsed_ms")]
    pub total_elapsed: Duration,
    /// Individual check results, ordered by category.
    pub checks: Vec<CheckResult>,
    /// Per-profile conformance verdicts.
    pub profiles: ProfileAssessment,
}

impl HealthReport {
    /// Number of checks with a given status discriminant.
    pub fn count(&self, want: fn(&CheckStatus) -> bool) -> usize {
        self.checks.iter().filter(|c| want(&c.status)).count()
    }

    /// `true` when no check failed (warnings and skips are tolerated).
    pub fn ok(&self) -> bool {
        !self
            .checks
            .iter()
            .any(|c| matches!(c.status, CheckStatus::Fail(_)))
    }

    /// Serialise the report as a compact single-line JSON string. Durations
    /// are emitted as integer milliseconds (fields suffixed `_ms`).
    pub fn to_json(&self) -> String {
        // serde_json on the fully-serializable HealthReport — infallible.
        serde_json::to_string(self).expect("HealthReport is fully serializable")
    }

    /// Serialise the report as pretty-printed JSON (indented, line-separated).
    pub fn to_json_pretty(&self) -> String {
        serde_json::to_string_pretty(self).expect("HealthReport is fully serializable")
    }

    /// Compare this report against an earlier baseline and report the
    /// differences relevant for regression tracking (e.g. between firmware
    /// versions).
    pub fn diff(&self, prev: &HealthReport) -> ReportDiff {
        ReportDiff::compute(prev, self)
    }
}

// ── ReportDiff ────────────────────────────────────────────────────────────────

/// Differences between two [`HealthReport`]s, surfacing the things that
/// matter for regression tracking.
///
/// Computed via [`HealthReport::diff`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReportDiff {
    /// Checks that were not failing in `prev` but are failing now.
    pub flipped_to_fail: Vec<String>,
    /// Checks that were failing in `prev` but are not failing now.
    pub flipped_to_pass: Vec<String>,
    /// Check ids present in the new report but not in the baseline.
    pub new_checks: Vec<String>,
    /// Check ids present in the baseline but missing from the new report.
    pub removed_checks: Vec<String>,
    /// Checks whose runtime grew significantly: now > 2 × prev AND
    /// the delta exceeds 100 ms (filters millisecond-scale noise).
    /// Tuples are `(id, prev_ms, now_ms)`.
    pub slowed: Vec<SlowedCheck>,
}

/// One entry in [`ReportDiff::slowed`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SlowedCheck {
    pub id: String,
    pub prev_ms: u128,
    pub now_ms: u128,
}

impl ReportDiff {
    /// `true` if nothing flipped, nothing changed set, and nothing slowed.
    pub fn is_empty(&self) -> bool {
        self.flipped_to_fail.is_empty()
            && self.flipped_to_pass.is_empty()
            && self.new_checks.is_empty()
            && self.removed_checks.is_empty()
            && self.slowed.is_empty()
    }

    fn compute(prev: &HealthReport, now: &HealthReport) -> Self {
        use std::collections::HashMap;
        let prev_by_id: HashMap<&str, &CheckResult> =
            prev.checks.iter().map(|c| (c.id.as_str(), c)).collect();
        let now_by_id: HashMap<&str, &CheckResult> =
            now.checks.iter().map(|c| (c.id.as_str(), c)).collect();

        let mut flipped_to_fail = Vec::new();
        let mut flipped_to_pass = Vec::new();
        let mut new_checks = Vec::new();
        let mut slowed = Vec::new();

        for c in &now.checks {
            match prev_by_id.get(c.id.as_str()) {
                None => new_checks.push(c.id.clone()),
                Some(p) => {
                    let was_fail = matches!(p.status, CheckStatus::Fail(_));
                    let is_fail = matches!(c.status, CheckStatus::Fail(_));
                    match (was_fail, is_fail) {
                        (false, true) => flipped_to_fail.push(c.id.clone()),
                        (true, false) => flipped_to_pass.push(c.id.clone()),
                        _ => {}
                    }
                    let prev_ms = p.elapsed.as_millis();
                    let now_ms = c.elapsed.as_millis();
                    if now_ms > prev_ms.saturating_mul(2) && now_ms.saturating_sub(prev_ms) > 100 {
                        slowed.push(SlowedCheck {
                            id: c.id.clone(),
                            prev_ms,
                            now_ms,
                        });
                    }
                }
            }
        }

        let removed_checks: Vec<String> = prev
            .checks
            .iter()
            .filter(|c| !now_by_id.contains_key(c.id.as_str()))
            .map(|c| c.id.clone())
            .collect();

        Self {
            flipped_to_fail,
            flipped_to_pass,
            new_checks,
            removed_checks,
            slowed,
        }
    }
}

impl fmt::Display for ReportDiff {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Diff vs baseline:")?;
        if self.is_empty() {
            writeln!(f, "  no changes")?;
            return Ok(());
        }
        if !self.flipped_to_fail.is_empty() {
            writeln!(f, "  flipped → FAIL: {}", self.flipped_to_fail.join(", "))?;
        }
        if !self.flipped_to_pass.is_empty() {
            writeln!(f, "  recovered    : {}", self.flipped_to_pass.join(", "))?;
        }
        if !self.new_checks.is_empty() {
            writeln!(f, "  new checks   : {}", self.new_checks.join(", "))?;
        }
        if !self.removed_checks.is_empty() {
            writeln!(f, "  removed      : {}", self.removed_checks.join(", "))?;
        }
        if !self.slowed.is_empty() {
            writeln!(f, "  slowed:")?;
            for s in &self.slowed {
                writeln!(f, "    {:<28} {:>5}ms → {:>5}ms", s.id, s.prev_ms, s.now_ms)?;
            }
        }
        Ok(())
    }
}

impl fmt::Display for HealthReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "ONVIF health check — {}", self.target)?;
        writeln!(
            f,
            "  {} pass · {} warn · {} fail · {} skip   ({} ms total)",
            self.count(|s| matches!(s, CheckStatus::Pass)),
            self.count(|s| matches!(s, CheckStatus::Warn(_))),
            self.count(|s| matches!(s, CheckStatus::Fail(_))),
            self.count(|s| matches!(s, CheckStatus::Skip(_))),
            self.total_elapsed.as_millis(),
        )?;
        let mut last: Option<Category> = None;
        for c in &self.checks {
            if last != Some(c.category) {
                writeln!(f, "\n  [{}]", c.category.label())?;
                last = Some(c.category);
            }
            let note = match &c.status {
                CheckStatus::Pass => c.detail.clone(),
                CheckStatus::Warn(r) | CheckStatus::Fail(r) | CheckStatus::Skip(r) => r.clone(),
            };
            writeln!(
                f,
                "    {:<4} {:<28} {:>5}ms  {}",
                c.status.tag(),
                c.id,
                c.elapsed.as_millis(),
                note,
            )?;
        }
        writeln!(f, "\n  Profiles:")?;
        for (name, (verdict, missing)) in [
            ("S", &self.profiles.profile_s),
            ("T", &self.profiles.profile_t),
            ("G", &self.profiles.profile_g),
        ] {
            let miss = if missing.is_empty() {
                String::new()
            } else {
                format!("  (missing: {})", missing.join(", "))
            };
            writeln!(f, "    Profile {name}: {}{miss}", verdict.label())?;
        }
        Ok(())
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn check(id: &str, status: CheckStatus, elapsed_ms: u64) -> CheckResult {
        let mut c = CheckResult::pass(id, Category::Media, "");
        c.status = status;
        c.elapsed = Duration::from_millis(elapsed_ms);
        c
    }

    fn assess() -> ProfileAssessment {
        ProfileAssessment {
            profile_s: (ProfileVerdict::Conformant, vec![]),
            profile_t: (ProfileVerdict::Conformant, vec![]),
            profile_g: (ProfileVerdict::Unsupported, vec!["recording".into()]),
        }
    }

    fn report(checks: Vec<CheckResult>) -> HealthReport {
        HealthReport {
            target: "http://test/onvif/device_service".into(),
            total_elapsed: Duration::from_millis(123),
            checks,
            profiles: assess(),
        }
    }

    #[test]
    fn diff_flags_pass_to_fail() {
        let prev = report(vec![check("a", CheckStatus::Pass, 10)]);
        let now = report(vec![check("a", CheckStatus::Fail("boom".into()), 10)]);
        let d = now.diff(&prev);
        assert_eq!(d.flipped_to_fail, vec!["a".to_string()]);
        assert!(d.flipped_to_pass.is_empty());
    }

    #[test]
    fn diff_flags_fail_to_pass() {
        let prev = report(vec![check("a", CheckStatus::Fail("x".into()), 10)]);
        let now = report(vec![check("a", CheckStatus::Pass, 10)]);
        let d = now.diff(&prev);
        assert_eq!(d.flipped_to_pass, vec!["a".to_string()]);
        assert!(d.flipped_to_fail.is_empty());
    }

    #[test]
    fn diff_flags_warn_is_not_flipped() {
        // Warn is not a fail, so warn → pass and pass → warn don't flip.
        let prev = report(vec![check("a", CheckStatus::Warn("slow".into()), 10)]);
        let now = report(vec![check("a", CheckStatus::Pass, 10)]);
        let d = now.diff(&prev);
        assert!(d.flipped_to_pass.is_empty());
        assert!(d.flipped_to_fail.is_empty());
    }

    #[test]
    fn diff_lists_added_and_removed_checks() {
        let prev = report(vec![
            check("a", CheckStatus::Pass, 1),
            check("b", CheckStatus::Pass, 1),
        ]);
        let now = report(vec![
            check("a", CheckStatus::Pass, 1),
            check("c", CheckStatus::Pass, 1),
        ]);
        let d = now.diff(&prev);
        assert_eq!(d.new_checks, vec!["c".to_string()]);
        assert_eq!(d.removed_checks, vec!["b".to_string()]);
    }

    #[test]
    fn diff_slowed_requires_doubled_and_delta_over_100ms() {
        // 10 → 30 ms (delta < 100 ms) → not slowed.
        let prev = report(vec![check("a", CheckStatus::Pass, 10)]);
        let now = report(vec![check("a", CheckStatus::Pass, 30)]);
        assert!(now.diff(&prev).slowed.is_empty());

        // 200 → 350 ms (less than 2×) → not slowed.
        let prev = report(vec![check("b", CheckStatus::Pass, 200)]);
        let now = report(vec![check("b", CheckStatus::Pass, 350)]);
        assert!(now.diff(&prev).slowed.is_empty());

        // 100 → 250 ms (2.5× AND delta 150 > 100) → slowed.
        let prev = report(vec![check("c", CheckStatus::Pass, 100)]);
        let now = report(vec![check("c", CheckStatus::Pass, 250)]);
        let d = now.diff(&prev);
        assert_eq!(d.slowed.len(), 1);
        assert_eq!(d.slowed[0].id, "c");
        assert_eq!(d.slowed[0].prev_ms, 100);
        assert_eq!(d.slowed[0].now_ms, 250);
    }

    #[test]
    fn diff_is_empty_when_nothing_changed() {
        let r = report(vec![check("a", CheckStatus::Pass, 5)]);
        assert!(r.diff(&r).is_empty());
    }

    #[test]
    fn report_round_trips_through_json() {
        let r = report(vec![
            check("a", CheckStatus::Pass, 12),
            check("b", CheckStatus::Fail("nope".into()), 7),
        ]);
        let json = r.to_json();
        let re: HealthReport = serde_json::from_str(&json).expect("json deserialises");
        assert_eq!(re.target, r.target);
        assert_eq!(re.checks.len(), r.checks.len());
        assert_eq!(re.checks[1].id, "b");
        assert!(matches!(re.checks[1].status, CheckStatus::Fail(_)));
        assert_eq!(re.checks[0].elapsed, Duration::from_millis(12));
        assert_eq!(re.total_elapsed, Duration::from_millis(123));
    }
}