bacnet-objects 0.8.1

BACnet object model: traits, database, and standard object types
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
//! Averaging (type 18) object per ASHRAE 135-2020 Clause 12.4.
//!
//! Computes running statistics (min, max, average) over sampled values from
//! a referenced object property.

use bacnet_types::constructed::BACnetObjectPropertyReference;
use bacnet_types::enums::{ObjectType, PropertyIdentifier};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue, StatusFlags};
use std::borrow::Cow;

use crate::common::{self, read_common_properties};
use crate::traits::BACnetObject;

/// BACnet Averaging object (type 18).
///
/// Accumulates sample values and computes min/max/average statistics.
/// The `present_value` property reflects the current average.
pub struct AveragingObject {
    oid: ObjectIdentifier,
    name: String,
    description: String,
    present_value: f32,
    minimum_value: f32,
    maximum_value: f32,
    average_value: f32,
    attempted_samples: u32,
    valid_samples: u32,
    object_property_reference: Option<BACnetObjectPropertyReference>,
    status_flags: StatusFlags,
    out_of_service: bool,
    reliability: u32,
}

impl AveragingObject {
    pub fn new(instance: u32, name: impl Into<String>) -> Result<Self, Error> {
        let oid = ObjectIdentifier::new(ObjectType::AVERAGING, instance)?;
        Ok(Self {
            oid,
            name: name.into(),
            description: String::new(),
            present_value: 0.0,
            minimum_value: f32::MAX,
            maximum_value: f32::MIN,
            average_value: 0.0,
            attempted_samples: 0,
            valid_samples: 0,
            object_property_reference: None,
            status_flags: StatusFlags::empty(),
            out_of_service: false,
            reliability: 0,
        })
    }

    /// Add a sample value, updating min/max/average and counts.
    pub fn add_sample(&mut self, value: f32) {
        self.attempted_samples += 1;
        self.valid_samples += 1;

        if value < self.minimum_value {
            self.minimum_value = value;
        }
        if value > self.maximum_value {
            self.maximum_value = value;
        }

        // Running average: avg = avg_prev + (value - avg_prev) / n
        self.average_value += (value - self.average_value) / self.valid_samples as f32;
        self.present_value = self.average_value;
    }

    /// Set the object property reference (the property being averaged).
    pub fn set_object_property_reference(
        &mut self,
        reference: Option<BACnetObjectPropertyReference>,
    ) {
        self.object_property_reference = reference;
    }

    /// Set the description string.
    pub fn set_description(&mut self, desc: impl Into<String>) {
        self.description = desc.into();
    }
}

impl BACnetObject for AveragingObject {
    fn object_identifier(&self) -> ObjectIdentifier {
        self.oid
    }

    fn object_name(&self) -> &str {
        &self.name
    }

    fn read_property(
        &self,
        property: PropertyIdentifier,
        array_index: Option<u32>,
    ) -> Result<PropertyValue, Error> {
        if let Some(result) = read_common_properties!(self, property, array_index) {
            return result;
        }
        match property {
            p if p == PropertyIdentifier::OBJECT_TYPE => {
                Ok(PropertyValue::Enumerated(ObjectType::AVERAGING.to_raw()))
            }
            p if p == PropertyIdentifier::PRESENT_VALUE => {
                Ok(PropertyValue::Real(self.present_value))
            }
            p if p == PropertyIdentifier::MINIMUM_VALUE => {
                if self.valid_samples == 0 {
                    Ok(PropertyValue::Real(0.0))
                } else {
                    Ok(PropertyValue::Real(self.minimum_value))
                }
            }
            p if p == PropertyIdentifier::MAXIMUM_VALUE => {
                if self.valid_samples == 0 {
                    Ok(PropertyValue::Real(0.0))
                } else {
                    Ok(PropertyValue::Real(self.maximum_value))
                }
            }
            p if p == PropertyIdentifier::AVERAGE_VALUE => {
                Ok(PropertyValue::Real(self.average_value))
            }
            p if p == PropertyIdentifier::ATTEMPTED_SAMPLES => {
                Ok(PropertyValue::Unsigned(self.attempted_samples as u64))
            }
            p if p == PropertyIdentifier::VALID_SAMPLES => {
                Ok(PropertyValue::Unsigned(self.valid_samples as u64))
            }
            p if p == PropertyIdentifier::OBJECT_PROPERTY_REFERENCE => {
                match &self.object_property_reference {
                    None => Ok(PropertyValue::Null),
                    Some(r) => {
                        let mut fields = vec![
                            PropertyValue::ObjectIdentifier(r.object_identifier),
                            PropertyValue::Unsigned(r.property_identifier as u64),
                        ];
                        if let Some(idx) = r.property_array_index {
                            fields.push(PropertyValue::Unsigned(idx as u64));
                        }
                        Ok(PropertyValue::List(fields))
                    }
                }
            }
            p if p == PropertyIdentifier::EVENT_STATE => Ok(PropertyValue::Enumerated(0)),
            _ => Err(common::unknown_property_error()),
        }
    }

    fn write_property(
        &mut self,
        property: PropertyIdentifier,
        _array_index: Option<u32>,
        value: PropertyValue,
        _priority: Option<u8>,
    ) -> Result<(), Error> {
        if let Some(result) = common::write_description(&mut self.description, property, &value) {
            return result;
        }
        if let Some(result) =
            common::write_out_of_service(&mut self.out_of_service, property, &value)
        {
            return result;
        }
        if property == PropertyIdentifier::OBJECT_PROPERTY_REFERENCE {
            match value {
                PropertyValue::Null => {
                    self.object_property_reference = None;
                    return Ok(());
                }
                PropertyValue::List(ref items) if items.len() >= 2 => {
                    if let (PropertyValue::ObjectIdentifier(oid), PropertyValue::Unsigned(prop)) =
                        (&items[0], &items[1])
                    {
                        let array_index = if items.len() > 2 {
                            if let PropertyValue::Unsigned(idx) = &items[2] {
                                Some(*idx as u32)
                            } else {
                                None
                            }
                        } else {
                            None
                        };
                        self.object_property_reference = Some(if let Some(idx) = array_index {
                            BACnetObjectPropertyReference::new_indexed(*oid, *prop as u32, idx)
                        } else {
                            BACnetObjectPropertyReference::new(*oid, *prop as u32)
                        });
                        return Ok(());
                    }
                    return Err(common::invalid_data_type_error());
                }
                _ => return Err(common::invalid_data_type_error()),
            }
        }
        Err(common::write_access_denied_error())
    }

    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        static PROPS: &[PropertyIdentifier] = &[
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::DESCRIPTION,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PRESENT_VALUE,
            PropertyIdentifier::MINIMUM_VALUE,
            PropertyIdentifier::MAXIMUM_VALUE,
            PropertyIdentifier::AVERAGE_VALUE,
            PropertyIdentifier::ATTEMPTED_SAMPLES,
            PropertyIdentifier::VALID_SAMPLES,
            PropertyIdentifier::OBJECT_PROPERTY_REFERENCE,
            PropertyIdentifier::STATUS_FLAGS,
            PropertyIdentifier::OUT_OF_SERVICE,
            PropertyIdentifier::RELIABILITY,
            PropertyIdentifier::EVENT_STATE,
        ];
        Cow::Borrowed(PROPS)
    }
}

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

    #[test]
    fn averaging_create() {
        let avg = AveragingObject::new(1, "AVG-1").unwrap();
        assert_eq!(
            avg.read_property(PropertyIdentifier::OBJECT_NAME, None)
                .unwrap(),
            PropertyValue::CharacterString("AVG-1".into())
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::OBJECT_TYPE, None)
                .unwrap(),
            PropertyValue::Enumerated(ObjectType::AVERAGING.to_raw())
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::PRESENT_VALUE, None)
                .unwrap(),
            PropertyValue::Real(0.0)
        );
    }

    #[test]
    fn averaging_add_samples() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        avg.add_sample(10.0);
        avg.add_sample(20.0);
        avg.add_sample(30.0);

        assert_eq!(
            avg.read_property(PropertyIdentifier::ATTEMPTED_SAMPLES, None)
                .unwrap(),
            PropertyValue::Unsigned(3)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::VALID_SAMPLES, None)
                .unwrap(),
            PropertyValue::Unsigned(3)
        );
    }

    #[test]
    fn averaging_min_max() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        avg.add_sample(15.0);
        avg.add_sample(5.0);
        avg.add_sample(25.0);

        assert_eq!(
            avg.read_property(PropertyIdentifier::MINIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(5.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::MAXIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(25.0)
        );
    }

    #[test]
    fn averaging_average_value() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        avg.add_sample(10.0);
        avg.add_sample(20.0);
        avg.add_sample(30.0);

        let val = avg
            .read_property(PropertyIdentifier::AVERAGE_VALUE, None)
            .unwrap();
        if let PropertyValue::Real(v) = val {
            assert!((v - 20.0).abs() < 0.001);
        } else {
            panic!("Expected Real");
        }

        // present_value should equal average_value
        let pv = avg
            .read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap();
        assert_eq!(pv, val);
    }

    #[test]
    fn averaging_no_samples_defaults() {
        let avg = AveragingObject::new(1, "AVG-1").unwrap();
        // Before any samples, min/max return 0.0
        assert_eq!(
            avg.read_property(PropertyIdentifier::MINIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(0.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::MAXIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(0.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::AVERAGE_VALUE, None)
                .unwrap(),
            PropertyValue::Real(0.0)
        );
    }

    #[test]
    fn averaging_property_list() {
        let avg = AveragingObject::new(1, "AVG-1").unwrap();
        let props = avg.property_list();
        assert!(props.contains(&PropertyIdentifier::PRESENT_VALUE));
        assert!(props.contains(&PropertyIdentifier::MINIMUM_VALUE));
        assert!(props.contains(&PropertyIdentifier::MAXIMUM_VALUE));
        assert!(props.contains(&PropertyIdentifier::AVERAGE_VALUE));
        assert!(props.contains(&PropertyIdentifier::ATTEMPTED_SAMPLES));
        assert!(props.contains(&PropertyIdentifier::VALID_SAMPLES));
        assert!(props.contains(&PropertyIdentifier::OBJECT_PROPERTY_REFERENCE));
        assert!(props.contains(&PropertyIdentifier::STATUS_FLAGS));
        assert!(props.contains(&PropertyIdentifier::OUT_OF_SERVICE));
        assert!(props.contains(&PropertyIdentifier::RELIABILITY));
    }

    #[test]
    fn averaging_object_property_reference_default_null() {
        let avg = AveragingObject::new(1, "AVG-1").unwrap();
        assert_eq!(
            avg.read_property(PropertyIdentifier::OBJECT_PROPERTY_REFERENCE, None)
                .unwrap(),
            PropertyValue::Null
        );
    }

    #[test]
    fn averaging_set_object_property_reference() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 5).unwrap();
        let pv_raw = PropertyIdentifier::PRESENT_VALUE.to_raw();
        avg.set_object_property_reference(Some(BACnetObjectPropertyReference::new(oid, pv_raw)));

        let val = avg
            .read_property(PropertyIdentifier::OBJECT_PROPERTY_REFERENCE, None)
            .unwrap();
        assert_eq!(
            val,
            PropertyValue::List(vec![
                PropertyValue::ObjectIdentifier(oid),
                PropertyValue::Unsigned(pv_raw as u64),
            ])
        );
    }

    #[test]
    fn averaging_write_object_property_reference() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 3).unwrap();
        let pv_raw = PropertyIdentifier::PRESENT_VALUE.to_raw();

        avg.write_property(
            PropertyIdentifier::OBJECT_PROPERTY_REFERENCE,
            None,
            PropertyValue::List(vec![
                PropertyValue::ObjectIdentifier(oid),
                PropertyValue::Unsigned(pv_raw as u64),
            ]),
            None,
        )
        .unwrap();

        assert_eq!(
            avg.read_property(PropertyIdentifier::OBJECT_PROPERTY_REFERENCE, None)
                .unwrap(),
            PropertyValue::List(vec![
                PropertyValue::ObjectIdentifier(oid),
                PropertyValue::Unsigned(pv_raw as u64),
            ])
        );
    }

    #[test]
    fn averaging_write_null_clears_reference() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        let oid = ObjectIdentifier::new(ObjectType::ANALOG_INPUT, 1).unwrap();
        avg.set_object_property_reference(Some(BACnetObjectPropertyReference::new(
            oid,
            PropertyIdentifier::PRESENT_VALUE.to_raw(),
        )));

        avg.write_property(
            PropertyIdentifier::OBJECT_PROPERTY_REFERENCE,
            None,
            PropertyValue::Null,
            None,
        )
        .unwrap();

        assert_eq!(
            avg.read_property(PropertyIdentifier::OBJECT_PROPERTY_REFERENCE, None)
                .unwrap(),
            PropertyValue::Null
        );
    }

    #[test]
    fn averaging_write_present_value_denied() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        let result = avg.write_property(
            PropertyIdentifier::PRESENT_VALUE,
            None,
            PropertyValue::Real(42.0),
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn averaging_description_read_write() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        assert_eq!(
            avg.read_property(PropertyIdentifier::DESCRIPTION, None)
                .unwrap(),
            PropertyValue::CharacterString(String::new())
        );
        avg.write_property(
            PropertyIdentifier::DESCRIPTION,
            None,
            PropertyValue::CharacterString("Zone temperature averaging".into()),
            None,
        )
        .unwrap();
        assert_eq!(
            avg.read_property(PropertyIdentifier::DESCRIPTION, None)
                .unwrap(),
            PropertyValue::CharacterString("Zone temperature averaging".into())
        );
    }

    #[test]
    fn averaging_single_sample() {
        let mut avg = AveragingObject::new(1, "AVG-1").unwrap();
        avg.add_sample(42.0);

        assert_eq!(
            avg.read_property(PropertyIdentifier::MINIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(42.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::MAXIMUM_VALUE, None)
                .unwrap(),
            PropertyValue::Real(42.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::AVERAGE_VALUE, None)
                .unwrap(),
            PropertyValue::Real(42.0)
        );
        assert_eq!(
            avg.read_property(PropertyIdentifier::PRESENT_VALUE, None)
                .unwrap(),
            PropertyValue::Real(42.0)
        );
    }
}