Skip to main content

bacnet_objects/
cov_change.rs

1//! The change test of COV reporting (Clause 13.1), shared by the server's
2//! COV notifications and a Trend Log that logs by COV (#1480).
3//!
4//! A numeric value reports when it has moved by at least the increment in
5//! use; with no increment, or one of zero or less, any change reports. Every
6//! other value reports when it differs at all. Which increment is in use is
7//! [`effective_increment`]'s rule: one the client supplied wins, otherwise
8//! only a numeric Present_Value takes the object's COV_Increment (Table 13-1
9//! for the objects it lists, Table 13-1a for any other property).
10
11use bacnet_types::enums::PropertyIdentifier;
12use bacnet_types::primitives::PropertyValue;
13
14use crate::traits::BACnetObject;
15
16/// Whether `value`, read from `property` (at `index`) of `object`, is a
17/// numeric quantity the increment applies to: a REAL, Double, Unsigned or
18/// INTEGER that is not a whole array or an array's size.
19pub fn is_numeric(
20    object: &dyn BACnetObject,
21    property: PropertyIdentifier,
22    index: Option<u32>,
23    value: &PropertyValue,
24) -> bool {
25    let array = object.is_array_property(property);
26    !(array && (index.is_none() || index == Some(0)))
27        && matches!(
28            value,
29            PropertyValue::Real(_)
30                | PropertyValue::Double(_)
31                | PropertyValue::Signed(_)
32                | PropertyValue::Unsigned(_)
33        )
34}
35
36/// The increment in use: an `explicit` one (a SubscribeCOVProperty
37/// increment, or a Trend Log's Client_COV_Increment) wins; otherwise only a
38/// numeric Present_Value inherits the object's current COV_Increment.
39///
40/// The explicit increment is a REAL on the wire; the object's own may be
41/// wider (a Large Analog Value's Double), so the result is an `f64`.
42pub fn effective_increment(
43    object: &dyn BACnetObject,
44    property: PropertyIdentifier,
45    numeric: bool,
46    explicit: Option<f32>,
47) -> Option<f64> {
48    let explicit = explicit.map(f64::from);
49    if numeric && property == PropertyIdentifier::PRESENT_VALUE {
50        explicit.or_else(|| object.cov_increment())
51    } else {
52        explicit
53    }
54}
55
56/// Whether `current` is a change from `previous` worth reporting. A
57/// `numeric` pair of the same datatype is held to `increment`; a
58/// non-finite REAL or Double reports when its bits differ. Anything else
59/// reports when [`same_value`] says the two differ.
60pub fn reports_change(
61    current: &PropertyValue,
62    previous: &PropertyValue,
63    increment: Option<f64>,
64    numeric: bool,
65) -> bool {
66    if !numeric {
67        return !same_value(current, previous);
68    }
69    use PropertyValue::*;
70    match (current, previous) {
71        (Unsigned(a), Unsigned(b)) => integer_delta(a.abs_diff(*b), increment),
72        (Signed(a), Signed(b)) => integer_delta(u64::from(a.abs_diff(*b)), increment),
73        (Real(a), Real(b)) => {
74            if !a.is_finite() || !b.is_finite() {
75                a.to_bits() != b.to_bits()
76            } else {
77                float_delta((f64::from(*a) - f64::from(*b)).abs(), increment)
78            }
79        }
80        (Double(a), Double(b)) => {
81            if !a.is_finite() || !b.is_finite() {
82                a.to_bits() != b.to_bits()
83            } else {
84                float_delta((a - b).abs(), increment)
85            }
86        }
87        _ => !same_value(current, previous),
88    }
89}
90
91/// Value equality for change detection: REALs and Doubles compare by their
92/// bits (so NaN equals itself and 0.0 differs from -0.0), lists element by
93/// element, everything else as `==` does.
94pub fn same_value(a: &PropertyValue, b: &PropertyValue) -> bool {
95    use PropertyValue::*;
96    match (a, b) {
97        (Real(a), Real(b)) => a.to_bits() == b.to_bits(),
98        (Double(a), Double(b)) => a.to_bits() == b.to_bits(),
99        (List(a), List(b)) => a.len() == b.len() && a.iter().zip(b).all(|(a, b)| same_value(a, b)),
100        _ => a == b,
101    }
102}
103
104fn integer_delta(delta: u64, increment: Option<f64>) -> bool {
105    match increment {
106        None => delta != 0,
107        Some(i) if i <= 0.0 => delta != 0,
108        Some(i) if i.is_nan() || i >= 18_446_744_073_709_551_616.0 => false,
109        Some(i) => delta >= crate::float::ceil(i) as u64,
110    }
111}
112
113fn float_delta(delta: f64, increment: Option<f64>) -> bool {
114    match increment {
115        None => delta != 0.0,
116        Some(i) if i <= 0.0 => delta != 0.0,
117        Some(i) if !i.is_finite() => false,
118        Some(i) => delta >= i,
119    }
120}