oxvif 0.11.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
use super::{FloatRange, xml_escape, xml_str};
use crate::error::OnvifError;
use crate::soap::{SoapError, XmlNode};

// ── ImagingSettings ───────────────────────────────────────────────────────────

/// Image quality settings returned by `GetImagingSettings`.
///
/// All numeric fields use the device's native range (commonly 0–100).
/// `None` means the device did not report that field.
///
/// Pass a modified copy to
/// [`set_imaging_settings`](crate::client::OnvifClient::set_imaging_settings).
#[derive(Debug, Clone, Default)]
pub struct ImagingSettings {
    /// Image brightness level.
    pub brightness: Option<f32>,
    /// Color saturation level.
    pub color_saturation: Option<f32>,
    /// Image contrast level.
    pub contrast: Option<f32>,
    /// Image sharpness level.
    pub sharpness: Option<f32>,
    /// IR cut filter mode: `"ON"`, `"OFF"`, or `"AUTO"`.
    pub ir_cut_filter: Option<String>,
    /// White balance mode: `"AUTO"` or `"MANUAL"`.
    pub white_balance_mode: Option<String>,
    /// Manual white-balance red-channel gain (device-native range).
    /// Only meaningful when `white_balance_mode == "MANUAL"`.
    pub wb_cr_gain: Option<f32>,
    /// Manual white-balance blue-channel gain (device-native range).
    /// Only meaningful when `white_balance_mode == "MANUAL"`.
    pub wb_cb_gain: Option<f32>,
    /// Exposure mode: `"AUTO"` or `"MANUAL"`.
    pub exposure_mode: Option<String>,
    /// Auto-exposure priority: `"FrameRate"` or `"LowNoise"`.
    pub exposure_priority: Option<String>,
    /// Manual exposure time in seconds. Only meaningful when
    /// `exposure_mode == "MANUAL"`.
    pub exposure_time: Option<f32>,
    /// Manual sensor gain in dB. Only meaningful when
    /// `exposure_mode == "MANUAL"`.
    pub exposure_gain: Option<f32>,
    /// Manual iris value (F-number). Only meaningful when
    /// `exposure_mode == "MANUAL"`.
    pub exposure_iris: Option<f32>,
    /// Backlight compensation mode: `"OFF"` or `"ON"`.
    pub backlight_compensation: Option<String>,
    /// Autofocus mode: `"AUTO"`, `"MANUAL"`, or `"OnePush"`.
    pub focus_mode: Option<String>,
    /// Default focus speed for autofocus operations (device-native range).
    pub focus_default_speed: Option<f32>,
    /// Near-end focus limit (device-native units). Constrains autofocus search.
    pub focus_near_limit: Option<f32>,
    /// Far-end focus limit (device-native units). Constrains autofocus search.
    pub focus_far_limit: Option<f32>,
    /// Wide dynamic range mode: `"OFF"` or `"ON"`.
    pub wide_dynamic_range_mode: Option<String>,
    /// Wide dynamic range intensity level (device-native range, typically 0–100).
    pub wide_dynamic_range_level: Option<f32>,
    /// Image stabilization mode: `"OFF"`, `"ON"`, or `"Extended"`.
    pub image_stabilization_mode: Option<String>,
    /// Tone compensation mode: `"OFF"`, `"ON"`, or `"Auto"`.
    pub tone_compensation_mode: Option<String>,
}

impl ImagingSettings {
    /// Parse from a `GetImagingSettingsResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let s = resp
            .child("ImagingSettings")
            .ok_or_else(|| SoapError::missing("ImagingSettings"))?;

        let parse_f32 = |child: &str| s.child(child).and_then(|n| n.text().parse::<f32>().ok());

        let parse_nested_f32 =
            |path: &[&str]| s.path(path).and_then(|n| n.text().parse::<f32>().ok());
        let parse_nested_str = |path: &[&str]| {
            s.path(path)
                .map(|n| n.text().to_string())
                .filter(|v| !v.is_empty())
        };

        Ok(Self {
            brightness: parse_f32("Brightness"),
            color_saturation: parse_f32("ColorSaturation"),
            contrast: parse_f32("Contrast"),
            sharpness: parse_f32("Sharpness"),
            ir_cut_filter: xml_str(s, "IrCutFilter").filter(|v| !v.is_empty()),
            white_balance_mode: parse_nested_str(&["WhiteBalance", "Mode"]),
            wb_cr_gain: parse_nested_f32(&["WhiteBalance", "CrGain"]),
            wb_cb_gain: parse_nested_f32(&["WhiteBalance", "CbGain"]),
            exposure_mode: parse_nested_str(&["Exposure", "Mode"]),
            exposure_priority: parse_nested_str(&["Exposure", "Priority"]),
            exposure_time: parse_nested_f32(&["Exposure", "ExposureTime"]),
            exposure_gain: parse_nested_f32(&["Exposure", "Gain"]),
            exposure_iris: parse_nested_f32(&["Exposure", "Iris"]),
            backlight_compensation: parse_nested_str(&["BacklightCompensation", "Mode"]),
            focus_mode: parse_nested_str(&["Focus", "AutoFocusMode"]),
            focus_default_speed: parse_nested_f32(&["Focus", "DefaultSpeed"]),
            focus_near_limit: parse_nested_f32(&["Focus", "NearLimit"]),
            focus_far_limit: parse_nested_f32(&["Focus", "FarLimit"]),
            wide_dynamic_range_mode: parse_nested_str(&["WideDynamicRange", "Mode"]),
            wide_dynamic_range_level: parse_nested_f32(&["WideDynamicRange", "Level"]),
            image_stabilization_mode: parse_nested_str(&["ImageStabilization", "Mode"]),
            tone_compensation_mode: parse_nested_str(&["ToneCompensation", "Mode"]),
        })
    }

    /// Serialise to a `<timg:ImagingSettings>` XML fragment for
    /// `SetImagingSettings`.
    pub(crate) fn to_xml_body(&self) -> String {
        let mut out = String::from("<timg:ImagingSettings>");
        if let Some(v) = self.brightness {
            out.push_str(&format!("<tt:Brightness>{v}</tt:Brightness>"));
        }
        if let Some(v) = self.color_saturation {
            out.push_str(&format!("<tt:ColorSaturation>{v}</tt:ColorSaturation>"));
        }
        if let Some(v) = self.contrast {
            out.push_str(&format!("<tt:Contrast>{v}</tt:Contrast>"));
        }
        if let Some(v) = self.sharpness {
            out.push_str(&format!("<tt:Sharpness>{v}</tt:Sharpness>"));
        }
        if let Some(ref v) = self.ir_cut_filter {
            out.push_str(&format!(
                "<tt:IrCutFilter>{}</tt:IrCutFilter>",
                xml_escape(v)
            ));
        }
        // WhiteBalance20 sequence: Mode, CrGain, CbGain
        if self.white_balance_mode.is_some()
            || self.wb_cr_gain.is_some()
            || self.wb_cb_gain.is_some()
        {
            out.push_str("<tt:WhiteBalance>");
            if let Some(ref m) = self.white_balance_mode {
                out.push_str(&format!("<tt:Mode>{}</tt:Mode>", xml_escape(m)));
            }
            if let Some(v) = self.wb_cr_gain {
                out.push_str(&format!("<tt:CrGain>{v}</tt:CrGain>"));
            }
            if let Some(v) = self.wb_cb_gain {
                out.push_str(&format!("<tt:CbGain>{v}</tt:CbGain>"));
            }
            out.push_str("</tt:WhiteBalance>");
        }
        // Exposure20 sequence: Mode, Priority, Window, Min/MaxExposureTime,
        // Min/MaxGain, Min/MaxIris, ExposureTime, Gain, Iris. We model only
        // the fields a caller can set: Mode, Priority, ExposureTime, Gain, Iris.
        if self.exposure_mode.is_some()
            || self.exposure_priority.is_some()
            || self.exposure_time.is_some()
            || self.exposure_gain.is_some()
            || self.exposure_iris.is_some()
        {
            out.push_str("<tt:Exposure>");
            if let Some(ref m) = self.exposure_mode {
                out.push_str(&format!("<tt:Mode>{}</tt:Mode>", xml_escape(m)));
            }
            if let Some(ref p) = self.exposure_priority {
                out.push_str(&format!("<tt:Priority>{}</tt:Priority>", xml_escape(p)));
            }
            if let Some(v) = self.exposure_time {
                out.push_str(&format!("<tt:ExposureTime>{v}</tt:ExposureTime>"));
            }
            if let Some(v) = self.exposure_gain {
                out.push_str(&format!("<tt:Gain>{v}</tt:Gain>"));
            }
            if let Some(v) = self.exposure_iris {
                out.push_str(&format!("<tt:Iris>{v}</tt:Iris>"));
            }
            out.push_str("</tt:Exposure>");
        }
        if let Some(ref m) = self.backlight_compensation {
            out.push_str(&format!(
                "<tt:BacklightCompensation><tt:Mode>{}</tt:Mode></tt:BacklightCompensation>",
                xml_escape(m)
            ));
        }
        // FocusConfiguration20 sequence: AutoFocusMode, DefaultSpeed, NearLimit, FarLimit
        if self.focus_mode.is_some()
            || self.focus_default_speed.is_some()
            || self.focus_near_limit.is_some()
            || self.focus_far_limit.is_some()
        {
            out.push_str("<tt:Focus>");
            if let Some(ref m) = self.focus_mode {
                out.push_str(&format!(
                    "<tt:AutoFocusMode>{}</tt:AutoFocusMode>",
                    xml_escape(m)
                ));
            }
            if let Some(v) = self.focus_default_speed {
                out.push_str(&format!("<tt:DefaultSpeed>{v}</tt:DefaultSpeed>"));
            }
            if let Some(v) = self.focus_near_limit {
                out.push_str(&format!("<tt:NearLimit>{v}</tt:NearLimit>"));
            }
            if let Some(v) = self.focus_far_limit {
                out.push_str(&format!("<tt:FarLimit>{v}</tt:FarLimit>"));
            }
            out.push_str("</tt:Focus>");
        }
        if self.wide_dynamic_range_mode.is_some() || self.wide_dynamic_range_level.is_some() {
            out.push_str("<tt:WideDynamicRange>");
            if let Some(ref m) = self.wide_dynamic_range_mode {
                out.push_str(&format!("<tt:Mode>{}</tt:Mode>", xml_escape(m)));
            }
            if let Some(v) = self.wide_dynamic_range_level {
                out.push_str(&format!("<tt:Level>{v}</tt:Level>"));
            }
            out.push_str("</tt:WideDynamicRange>");
        }
        if let Some(ref m) = self.image_stabilization_mode {
            out.push_str(&format!(
                "<tt:ImageStabilization><tt:Mode>{}</tt:Mode></tt:ImageStabilization>",
                xml_escape(m)
            ));
        }
        if let Some(ref m) = self.tone_compensation_mode {
            out.push_str(&format!(
                "<tt:ToneCompensation><tt:Mode>{}</tt:Mode></tt:ToneCompensation>",
                xml_escape(m)
            ));
        }
        out.push_str("</timg:ImagingSettings>");
        out
    }
}

// ── ImagingOptions ────────────────────────────────────────────────────────────

/// Valid parameter ranges for `SetImagingSettings`, returned by `GetOptions`.
#[derive(Debug, Clone, Default)]
pub struct ImagingOptions {
    /// Valid brightness range.
    pub brightness: Option<FloatRange>,
    /// Valid color saturation range.
    pub color_saturation: Option<FloatRange>,
    /// Valid contrast range.
    pub contrast: Option<FloatRange>,
    /// Valid sharpness range.
    pub sharpness: Option<FloatRange>,
    /// Supported IR cut filter modes (e.g. `["ON", "OFF", "AUTO"]`).
    pub ir_cut_filter_modes: Vec<String>,
    /// Supported white balance modes (e.g. `["AUTO", "MANUAL"]`).
    pub white_balance_modes: Vec<String>,
    /// Supported exposure modes (e.g. `["AUTO", "MANUAL"]`).
    pub exposure_modes: Vec<String>,
    /// Valid exposure time range in seconds (`Exposure/ExposureTime` Min/Max).
    pub exposure_time_range: Option<FloatRange>,
    /// Valid gain range in dB (`Exposure/Gain` Min/Max).
    pub gain_range: Option<FloatRange>,
    /// Valid iris range in F-number (`Exposure/Iris` Min/Max).
    pub iris_range: Option<FloatRange>,
    /// Supported auto-focus modes (e.g. `["AUTO", "MANUAL"]`) from `Focus/AFModes`.
    pub focus_af_modes: Vec<String>,
    /// Valid auto-focus speed range (`Focus/AutoFocusSpeed` Min/Max).
    pub focus_speed_range: Option<FloatRange>,
    /// Valid wide dynamic range level range (`WideDynamicRange/Level` Min/Max).
    pub wdr_level_range: Option<FloatRange>,
    /// Supported wide dynamic range modes (e.g. `["ON", "OFF"]`) from `WideDynamicRange/Mode`.
    pub wdr_modes: Vec<String>,
    /// Supported backlight compensation modes from `BacklightCompensation/Mode`.
    pub backlight_compensation_modes: Vec<String>,
}

impl ImagingOptions {
    /// Parse from a `GetOptionsResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let opts = resp
            .child("ImagingOptions")
            .ok_or_else(|| SoapError::missing("ImagingOptions"))?;

        let parse_range = |child: &str| {
            opts.child(child).map(|n| FloatRange {
                min: n
                    .child("Min")
                    .and_then(|m| m.text().parse().ok())
                    .unwrap_or(0.0),
                max: n
                    .child("Max")
                    .and_then(|m| m.text().parse().ok())
                    .unwrap_or(0.0),
            })
        };

        let parse_nested_range = |parent: Option<&XmlNode>, child: &str| {
            parent.and_then(|p| p.child(child)).map(|n| FloatRange {
                min: n
                    .child("Min")
                    .and_then(|m| m.text().parse().ok())
                    .unwrap_or(0.0),
                max: n
                    .child("Max")
                    .and_then(|m| m.text().parse().ok())
                    .unwrap_or(0.0),
            })
        };

        // Exposure20 options report two FloatRange bounds per parameter:
        // `Min{X}` (allowed range for the configured *minimum*) and `Max{X}`
        // (for the *maximum*), each itself a `{Min,Max}` range. Collapse to a
        // single envelope `[Min{X}.Min, Max{X}.Max]`. Some GeoVisions ALSO emit
        // a legacy combined `{X}` element; the spec form wins, legacy is the
        // fallback only. (Looking up the legacy `{X}` alone is wrong on
        // spec-compliant cameras — they return the bounds under `Min{X}`/`Max{X}`
        // and the field would silently be `None`.)
        let parse_envelope_range = |parent: Option<&XmlNode>, base: &str| -> Option<FloatRange> {
            let p = parent?;
            let bound = |el: Option<&XmlNode>, tag: &str| {
                el.and_then(|x| x.child(tag))
                    .and_then(|m| m.text().parse::<f32>().ok())
            };
            let min_el = p.child(&format!("Min{base}"));
            let max_el = p.child(&format!("Max{base}"));
            if min_el.is_some() || max_el.is_some() {
                return Some(FloatRange {
                    min: bound(min_el, "Min").unwrap_or(0.0),
                    max: bound(max_el, "Max").unwrap_or(0.0),
                });
            }
            // Legacy combined `{X}` element fallback.
            p.child(base).map(|n| FloatRange {
                min: bound(Some(n), "Min").unwrap_or(0.0),
                max: bound(Some(n), "Max").unwrap_or(0.0),
            })
        };

        let exposure = opts.child("Exposure");
        let focus = opts.child("Focus");
        let wdr = opts.child("WideDynamicRange");
        let blc = opts.child("BacklightCompensation");

        Ok(Self {
            brightness: parse_range("Brightness"),
            color_saturation: parse_range("ColorSaturation"),
            contrast: parse_range("Contrast"),
            sharpness: parse_range("Sharpness"),
            ir_cut_filter_modes: opts
                .children_named("IrCutFilterModes")
                .map(|n| n.text().to_string())
                .collect(),
            white_balance_modes: opts
                .child("WhiteBalance")
                .map(|wb| {
                    wb.children_named("Mode")
                        .map(|n| n.text().to_string())
                        .collect()
                })
                .unwrap_or_default(),
            exposure_modes: exposure
                .map(|e| {
                    e.children_named("Mode")
                        .map(|n| n.text().to_string())
                        .collect()
                })
                .unwrap_or_default(),
            exposure_time_range: parse_envelope_range(exposure, "ExposureTime"),
            gain_range: parse_envelope_range(exposure, "Gain"),
            iris_range: parse_envelope_range(exposure, "Iris"),
            focus_af_modes: focus
                .map(|f| {
                    f.children_named("AutoFocusModes")
                        .map(|n| n.text().to_string())
                        .collect()
                })
                .unwrap_or_default(),
            focus_speed_range: parse_nested_range(focus, "DefaultSpeed"),
            wdr_level_range: parse_nested_range(wdr, "Level"),
            wdr_modes: wdr
                .map(|w| {
                    w.children_named("Mode")
                        .map(|n| n.text().to_string())
                        .collect()
                })
                .unwrap_or_default(),
            backlight_compensation_modes: blc
                .map(|b| {
                    b.children_named("Mode")
                        .map(|n| n.text().to_string())
                        .collect()
                })
                .unwrap_or_default(),
        })
    }
}

// ── ImagingStatus ─────────────────────────────────────────────────────────────

/// Current imaging / focus status returned by `imaging_get_status`.
#[derive(Debug, Clone, Default)]
pub struct ImagingStatus {
    /// Current focus position in the device's native range.
    pub focus_position: Option<f32>,
    /// Focus move state: `"IDLE"`, `"MOVING"`, or `"UNKNOWN"`.
    pub focus_move_status: String,
}

impl ImagingStatus {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let status = resp
            .child("Status")
            .ok_or_else(|| SoapError::missing("Status"))?;
        Ok(Self {
            focus_position: status
                .path(&["FocusStatus20", "Position"])
                .and_then(|n| n.text().parse().ok()),
            focus_move_status: status
                .path(&["FocusStatus20", "MoveStatus"])
                .map(|n| n.text().to_string())
                .unwrap_or_else(|| "UNKNOWN".to_string()),
        })
    }
}

// ── ImagingMoveOptions ────────────────────────────────────────────────────────

/// Valid focus movement ranges returned by `imaging_get_move_options`.
#[derive(Debug, Clone, Default)]
pub struct ImagingMoveOptions {
    /// Valid absolute focus position range.
    pub absolute_position_range: Option<FloatRange>,
    /// Valid absolute focus speed range.
    pub absolute_speed_range: Option<FloatRange>,
    /// Valid relative focus distance range.
    pub relative_distance_range: Option<FloatRange>,
    /// Valid relative focus speed range.
    pub relative_speed_range: Option<FloatRange>,
    /// Valid continuous focus speed range.
    pub continuous_speed_range: Option<FloatRange>,
}

impl ImagingMoveOptions {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let opts = resp
            .child("MoveOptions")
            .ok_or_else(|| SoapError::missing("MoveOptions"))?;

        let range = |parent: &str, child: &str| {
            opts.child(parent)
                .and_then(|p| p.child(child))
                .map(|n| FloatRange {
                    min: n
                        .child("Min")
                        .and_then(|m| m.text().parse().ok())
                        .unwrap_or(0.0),
                    max: n
                        .child("Max")
                        .and_then(|m| m.text().parse().ok())
                        .unwrap_or(0.0),
                })
        };

        Ok(Self {
            absolute_position_range: range("Absolute", "PositionSpace"),
            absolute_speed_range: range("Absolute", "SpeedSpace"),
            relative_distance_range: range("Relative", "DistanceSpace"),
            relative_speed_range: range("Relative", "SpeedSpace"),
            continuous_speed_range: range("Continuous", "SpeedSpace"),
        })
    }
}

// ── FocusMove ─────────────────────────────────────────────────────────────────

/// Focus movement command passed to `imaging_move`.
#[derive(Debug, Clone)]
pub enum FocusMove {
    /// Move focus to an absolute position.
    Absolute {
        /// Target focus position in the device's native range.
        position: f32,
        /// Movement speed. `None` uses the device default.
        speed: Option<f32>,
    },
    /// Move focus by a relative distance.
    Relative {
        /// Distance to move (positive = far, negative = near).
        distance: f32,
        /// Movement speed. `None` uses the device default.
        speed: Option<f32>,
    },
    /// Start continuous focus movement at a given speed.
    ///
    /// Call `imaging_stop` to halt.
    Continuous {
        /// Movement speed: positive = far, negative = near.
        speed: f32,
    },
}

impl FocusMove {
    pub(crate) fn to_xml_body(&self) -> String {
        match self {
            Self::Absolute { position, speed } => {
                let speed_el = speed
                    .map(|s| format!("<timg:Speed>{s}</timg:Speed>"))
                    .unwrap_or_default();
                format!(
                    "<timg:Absolute>\
                       <timg:Position>{position}</timg:Position>\
                       {speed_el}\
                     </timg:Absolute>"
                )
            }
            Self::Relative { distance, speed } => {
                let speed_el = speed
                    .map(|s| format!("<timg:Speed>{s}</timg:Speed>"))
                    .unwrap_or_default();
                format!(
                    "<timg:Relative>\
                       <timg:Distance>{distance}</timg:Distance>\
                       {speed_el}\
                     </timg:Relative>"
                )
            }
            Self::Continuous { speed } => {
                format!("<timg:Continuous><timg:Speed>{speed}</timg:Speed></timg:Continuous>")
            }
        }
    }
}