bacnet-objects 0.13.0

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
//! The Start_Time / Stop_Time window on each log object (#1235, #1353),
//! against a clock the test sets: a record outside it is ignored, an opening
//! or closing while Enable is TRUE is logged at a write and at the next look
//! (the look the trend poller makes on each pass), and a value that names
//! no moment is refused. Trend Log Multiple's own cases are in
//! `trend::multiple_options_tests`; the poller's passes in
//! `database::trend_poll`.

use std::sync::{Arc, Mutex};

use bacnet_encoding::constructed::{
    decode_event_log_record, decode_log_multiple_record, decode_log_record,
};
use bacnet_types::bitstring::LogStatus;
use bacnet_types::calendar::SpecificDate;
use bacnet_types::constructed::{
    BACnetEventLogRecord, BACnetLogMultipleRecord, BACnetLogRecord, EventLogDatum, LogData,
    LogDatum, LogValue,
};
use bacnet_types::enums::{ErrorClass, ErrorCode, PropertyIdentifier as P};
use bacnet_types::error::Error;
use bacnet_types::primitives::{Date, PropertyValue, Time};
use bytes::BytesMut;

use crate::clock::{ClockFrame, ClockReader, UNSPECIFIED_DATETIME};
use crate::event_log::EventLogObject;
use crate::traits::BACnetObject;
use crate::trend::{TrendLogMultipleObject, TrendLogObject};

struct Clock(Mutex<Option<ClockFrame>>);

impl ClockReader for Clock {
    fn read_clock(&self) -> Option<ClockFrame> {
        *self.0.lock().unwrap()
    }
}

/// `hour`:`minute` on 2026-10-03, its weekday filled in.
fn at(hour: u8, minute: u8) -> (Date, Time) {
    (
        SpecificDate::new(2026, 10, 3).unwrap().to_date(),
        Time {
            hour,
            minute,
            second: 0,
            hundredths: 0,
        },
    )
}

fn datetime((date, time): (Date, Time)) -> PropertyValue {
    PropertyValue::List(vec![PropertyValue::Date(date), PropertyValue::Time(time)])
}

fn assert_refused(result: Result<(), Error>, code: ErrorCode) {
    assert!(
        matches!(result, Err(Error::Protocol { class, code: c })
            if class == ErrorClass::PROPERTY.to_raw() as u32 && c == code.to_raw() as u32),
        "expected PROPERTY / {code:?}, got {result:?}"
    );
}

/// What a test looks for in a record: a log status, or an ordinary record.
#[derive(Debug, PartialEq)]
enum Seen {
    Status(LogStatus),
    Record,
}

const DISABLED: Seen = Seen::Status(LogStatus::LOG_DISABLED);
const ENABLED: Seen = Seen::Status(LogStatus::empty());

#[derive(Debug, Clone, Copy)]
enum Kind {
    Trend,
    Event,
    Multiple,
}

const KINDS: [Kind; 3] = [Kind::Trend, Kind::Event, Kind::Multiple];

/// A log of each kind as its own type, so its local setters can be called.
enum Object {
    Trend(TrendLogObject),
    Event(EventLogObject),
    Multiple(TrendLogMultipleObject),
}

/// One log of `kind` bound to a clock reading 09:00.
struct Log {
    kind: Kind,
    object: Object,
    clock: Arc<Clock>,
}

impl Log {
    /// A log whose window is open at both ends, as built.
    fn new(kind: Kind) -> Self {
        let object = match kind {
            Kind::Trend => Object::Trend(TrendLogObject::new(1, "TL-1", 16).unwrap()),
            Kind::Event => Object::Event(EventLogObject::new(1, "EL-1", 16).unwrap()),
            Kind::Multiple => {
                Object::Multiple(TrendLogMultipleObject::new(1, "TLM-1", 16).unwrap())
            }
        };
        let mut log = Self {
            kind,
            object,
            clock: Arc::new(Clock(Mutex::new(None))),
        };
        log.set_clock(at(9, 0));
        let clock = log.clock.clone();
        log.object_mut().bind_clock_internal(Some(clock));
        log
    }

    /// A log whose window the local setters gave, or their refusal.
    fn configured(kind: Kind, start: (Date, Time), stop: (Date, Time)) -> Result<Self, Error> {
        let mut log = Self::new(kind);
        log.set_end(P::START_TIME, start)?;
        log.set_end(P::STOP_TIME, stop)?;
        Ok(log)
    }

    /// Set Start_Time or Stop_Time through the object's local setter.
    fn set_end(&mut self, property: P, (date, time): (Date, Time)) -> Result<(), Error> {
        let start = property == P::START_TIME;
        match &mut self.object {
            Object::Trend(log) if start => log.set_start_time(date, time),
            Object::Trend(log) => log.set_stop_time(date, time),
            Object::Event(log) if start => log.set_start_time(date, time),
            Object::Event(log) => log.set_stop_time(date, time),
            Object::Multiple(log) if start => log.set_start_time(date, time),
            Object::Multiple(log) => log.set_stop_time(date, time),
        }
    }

    fn object(&self) -> &dyn BACnetObject {
        match &self.object {
            Object::Trend(log) => log,
            Object::Event(log) => log,
            Object::Multiple(log) => log,
        }
    }

    fn object_mut(&mut self) -> &mut dyn BACnetObject {
        match &mut self.object {
            Object::Trend(log) => log,
            Object::Event(log) => log,
            Object::Multiple(log) => log,
        }
    }

    fn set_clock(&self, (local_date, local_time): (Date, Time)) {
        *self.clock.0.lock().unwrap() = Some(ClockFrame {
            local_date,
            local_time,
            utc_offset: 0,
            daylight_savings_status: false,
        });
    }

    /// Take the clock away, so no look can tell the time.
    fn clear_clock(&self) {
        *self.clock.0.lock().unwrap() = None;
    }

    fn write(&mut self, property: P, value: PropertyValue) -> Result<(), Error> {
        self.object_mut()
            .write_property(property, None, value, None)
    }

    fn read(&self, property: P) -> PropertyValue {
        self.object().read_property(property, None).unwrap()
    }

    /// The look the poller's pass makes; whether it logged a change.
    fn pass(&mut self) -> bool {
        self.object_mut().refresh_log_window_internal()
    }

    /// Offer an ordinary record taken at `(date, time)`.
    fn add(&mut self, (date, time): (Date, Time)) {
        let kind = self.kind;
        let object = self.object_mut();
        match kind {
            Kind::Trend => object.add_trend_record(BACnetLogRecord {
                date,
                time,
                log_datum: LogDatum::RealValue(1.5),
                status_flags: None,
            }),
            Kind::Event => object.add_event_log_record(BACnetEventLogRecord {
                date,
                time,
                log_datum: EventLogDatum::TimeChange(1.5),
            }),
            Kind::Multiple => object.add_trend_multiple_record(BACnetLogMultipleRecord {
                date,
                time,
                log_data: LogData::Values(vec![LogValue::RealValue(1.5)]),
            }),
        }
        .unwrap();
    }

    /// The resident records, as ReadRange serves them.
    fn seen(&self) -> Vec<Seen> {
        let records = self.object().log_buffer_internal().unwrap();
        (0..records.record_count())
            .map(|index| {
                let mut bytes = BytesMut::new();
                records.encode_record(index, &mut bytes);
                let status = match self.kind {
                    Kind::Trend => match decode_log_record(&bytes, 0).unwrap().0.log_datum {
                        LogDatum::LogStatus(status) => Some(status),
                        _ => None,
                    },
                    Kind::Event => match decode_event_log_record(&bytes, 0).unwrap().0.log_datum {
                        EventLogDatum::LogStatus(status) => Some(status),
                        _ => None,
                    },
                    Kind::Multiple => {
                        match decode_log_multiple_record(&bytes, 0).unwrap().0.log_data {
                            LogData::LogStatus(status) => Some(status),
                            _ => None,
                        }
                    }
                };
                status.map_or(Seen::Record, Seen::Status)
            })
            .collect()
    }
}

#[test]
fn records_outside_the_window_are_ignored_and_each_change_is_logged() {
    for kind in KINDS {
        let mut log = Log::new(kind);
        // Opening at 10:00 shuts the window at 09:00: logged at the write.
        log.write(P::START_TIME, datetime(at(10, 0))).unwrap();
        log.write(P::STOP_TIME, datetime(at(11, 0))).unwrap();
        assert_eq!(log.read(P::START_TIME), datetime(at(10, 0)), "{kind:?}");
        assert_eq!(log.read(P::STOP_TIME), datetime(at(11, 0)), "{kind:?}");
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
        // A record outside the window is ignored, and Enable stays TRUE.
        log.add(at(9, 30));
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
        assert_eq!(log.read(P::LOG_ENABLE), PropertyValue::Boolean(true));

        // Start_Time reached: the next pass logs the opening, once.
        log.set_clock(at(10, 0));
        assert!(log.pass(), "{kind:?}");
        assert!(!log.pass(), "{kind:?}");
        log.add(at(10, 30));
        assert_eq!(log.seen(), [DISABLED, ENABLED, Seen::Record], "{kind:?}");

        // Stop_Time reached: the next pass logs the closing, and a record
        // taken from then on is ignored.
        log.set_clock(at(11, 0));
        assert!(log.pass(), "{kind:?}");
        log.add(at(11, 0));
        assert_eq!(log.seen()[3..], [DISABLED], "{kind:?}");

        // A write that opens the window logs it at once, and one that shuts
        // it again too.
        log.write(P::STOP_TIME, datetime(UNSPECIFIED_DATETIME))
            .unwrap();
        log.add(at(11, 0));
        assert_eq!(log.seen()[4..], [ENABLED, Seen::Record], "{kind:?}");
        log.write(P::START_TIME, datetime(at(12, 0))).unwrap();
        assert_eq!(log.seen()[6..], [DISABLED], "{kind:?}");
        assert!(!log.pass(), "{kind:?}");
    }
}

#[test]
fn while_enable_is_false_the_window_moves_without_a_record() {
    for kind in KINDS {
        let mut log = Log::new(kind);
        log.write(P::START_TIME, datetime(at(10, 0))).unwrap();
        log.write(P::LOG_ENABLE, PropertyValue::Boolean(false))
            .unwrap();
        // While shut, the Enable change keeps logging off: no record.
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
        log.set_clock(at(10, 0));
        assert!(!log.pass(), "{kind:?}");
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
        // Enable written TRUE inside the window logs a clear status.
        log.write(P::LOG_ENABLE, PropertyValue::Boolean(true))
            .unwrap();
        assert_eq!(log.seen(), [DISABLED, ENABLED], "{kind:?}");
        // A purge outside the window carries LOG_DISABLED.
        log.write(P::STOP_TIME, datetime(at(10, 0))).unwrap();
        log.write(P::RECORD_COUNT, PropertyValue::Unsigned(0))
            .unwrap();
        assert_eq!(
            log.seen(),
            [Seen::Status(
                LogStatus::BUFFER_PURGED | LogStatus::LOG_DISABLED
            )],
            "{kind:?}"
        );
    }
}

#[test]
fn window_values_that_name_no_moment_are_value_out_of_range() {
    let (date, time) = at(10, 0);
    let partly = [
        (
            Date {
                year: Date::UNSPECIFIED,
                ..date
            },
            time,
        ),
        (Date { day: 32, ..date }, time),
        (
            date,
            Time {
                minute: Time::UNSPECIFIED,
                ..time
            },
        ),
        (date, Time { hour: 24, ..time }),
        (date, UNSPECIFIED_DATETIME.1),
        (UNSPECIFIED_DATETIME.0, time),
    ];
    for kind in KINDS {
        let mut log = Log::new(kind);
        for value in partly {
            for property in [P::START_TIME, P::STOP_TIME] {
                assert_refused(
                    log.write(property, datetime(value)),
                    ErrorCode::VALUE_OUT_OF_RANGE,
                );
            }
            // The local setters refuse it too.
            for (start, stop) in [(value, UNSPECIFIED_DATETIME), (UNSPECIFIED_DATETIME, value)] {
                assert_refused(
                    Log::configured(kind, start, stop).map(|_| ()),
                    ErrorCode::VALUE_OUT_OF_RANGE,
                );
            }
        }
        assert_refused(
            log.write(P::START_TIME, PropertyValue::Date(date)),
            ErrorCode::INVALID_DATA_TYPE,
        );
        // Nothing changed and nothing was logged.
        for property in [P::START_TIME, P::STOP_TIME] {
            assert_eq!(
                log.read(property),
                datetime(UNSPECIFIED_DATETIME),
                "{kind:?}"
            );
        }
        assert!(log.seen().is_empty(), "{kind:?}");
        // Unspecified seconds and hundredths count as zero.
        let loose = Time {
            second: Time::UNSPECIFIED,
            hundredths: Time::UNSPECIFIED,
            ..time
        };
        log.write(P::START_TIME, datetime((date, loose))).unwrap();
        assert_eq!(log.read(P::START_TIME), datetime((date, loose)));
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
    }
}

#[test]
fn a_local_window_is_configuration_and_its_opening_is_logged() {
    for kind in KINDS {
        // Set before the first look: noting the shut window logs nothing.
        let mut log = Log::configured(kind, at(10, 0), at(11, 0)).unwrap();
        assert!(!log.pass(), "{kind:?}");
        log.add(at(9, 0));
        assert!(log.seen().is_empty(), "{kind:?}");
        log.set_clock(at(10, 0));
        assert!(log.pass(), "{kind:?}");
        log.set_clock(at(11, 0));
        assert!(log.pass(), "{kind:?}");
        assert_eq!(log.seen(), [ENABLED, DISABLED], "{kind:?}");
    }
}

#[test]
fn a_window_opened_at_both_ends_logs_its_opening_at_the_write() {
    for kind in KINDS {
        // With a clock, the write that leaves both ends open logs it.
        let mut log = Log::new(kind);
        log.write(P::START_TIME, datetime(at(10, 0))).unwrap();
        log.write(P::START_TIME, datetime(UNSPECIFIED_DATETIME))
            .unwrap();
        assert_eq!(log.seen(), [DISABLED, ENABLED], "{kind:?}");
        assert!(!log.pass(), "{kind:?}");
    }
}

#[test]
fn a_window_opened_at_both_ends_without_a_clock_logs_it_at_the_next_pass() {
    for kind in KINDS {
        // No valid clock at the write: the first pass with one logs it.
        let mut log = Log::new(kind);
        log.write(P::START_TIME, datetime(at(10, 0))).unwrap();
        log.clear_clock();
        log.write(P::START_TIME, datetime(UNSPECIFIED_DATETIME))
            .unwrap();
        assert!(!log.pass(), "{kind:?}");
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
        log.set_clock(at(9, 30));
        assert!(log.pass(), "{kind:?}");
        assert_eq!(log.seen(), [DISABLED, ENABLED], "{kind:?}");
    }
}

#[test]
fn a_client_write_after_a_local_setter_logs_the_change() {
    for kind in KINDS {
        // The setter notes the window open at 09:00 without a record, so the
        // client's write that shuts it is logged with no pass between.
        let mut log = Log::new(kind);
        log.set_end(P::STOP_TIME, at(10, 0)).unwrap();
        assert!(log.seen().is_empty(), "{kind:?}");
        log.write(P::STOP_TIME, datetime(at(8, 0))).unwrap();
        assert_eq!(log.seen(), [DISABLED], "{kind:?}");
    }
}