bacnet-objects 0.15.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
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
//! Trend Log's acquisition rows (#1354): Logging_Type's POLLED, TRIGGERED
//! and COV (#1480), Log_Interval's mode rules and switches (Clause 12.25.9),
//! the two COV rows, Trigger and the clock-alignment rows. The window is in
//! `crate::log_window_tests`, shared with the Event Log.

use super::*;
use crate::clock::ClockFrame;
use bacnet_types::constructed::LogDatum;
use bacnet_types::error::RemoteError;
use bacnet_types::primitives::{Date, Time};

fn read(log: &TrendLogObject, property: PropertyIdentifier) -> PropertyValue {
    log.read_property(property, None).unwrap()
}

fn write(
    log: &mut TrendLogObject,
    property: PropertyIdentifier,
    value: PropertyValue,
) -> Result<(), Error> {
    log.write_property(property, None, value, None)
}

fn assert_refused(result: Result<(), Error>, class: ErrorClass, code: ErrorCode) {
    assert!(
        matches!(result, Err(Error::Remote(RemoteError::Error { class: c, code: e }))
            if c == class && e == code),
        "expected {class:?} / {code:?}, got {result:?}"
    );
}

fn interval(log: &TrendLogObject) -> PropertyValue {
    read(log, PropertyIdentifier::LOG_INTERVAL)
}

fn record() -> BACnetLogRecord {
    BACnetLogRecord {
        date: Date {
            year: 126,
            month: 10,
            day: 4,
            day_of_week: 7,
        },
        time: Time {
            hour: 9,
            minute: 0,
            second: 0,
            hundredths: 0,
        },
        log_datum: LogDatum::RealValue(1.5),
        status_flags: None,
    }
}

#[test]
fn logging_type_takes_polled_triggered_and_cov() {
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    log.set_log_interval(500).unwrap();
    // A value outside BACnetLoggingType is one the object doesn't support,
    // so it gets the Clause 12.25.26 error, through the wire and the setter
    // alike.
    for raw in [3, 255] {
        assert_refused(
            write(
                &mut log,
                PropertyIdentifier::LOGGING_TYPE,
                PropertyValue::Enumerated(raw),
            ),
            ErrorClass::PROPERTY,
            ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
        );
        assert_refused(
            log.set_logging_type(LoggingType::from_raw(raw)),
            ErrorClass::PROPERTY,
            ErrorCode::OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED,
        );
    }
    assert_refused(
        write(
            &mut log,
            PropertyIdentifier::LOGGING_TYPE,
            PropertyValue::Unsigned(2),
        ),
        ErrorClass::PROPERTY,
        ErrorCode::INVALID_DATA_TYPE,
    );
    assert_eq!(logging_type(&log), LoggingType::POLLED);
    assert_eq!(interval(&log), PropertyValue::Unsigned(500));

    // COV, by the wire and by the setter, zeroes Log_Interval (Clause
    // 12.25.26), which stays writable (Table 12-29 footnote 3).
    write(
        &mut log,
        PropertyIdentifier::LOGGING_TYPE,
        PropertyValue::Enumerated(LoggingType::COV.to_raw()),
    )
    .unwrap();
    assert_eq!(logging_type(&log), LoggingType::COV);
    assert_eq!(interval(&log), PropertyValue::Unsigned(0));
    assert!(log.is_writable_property(PropertyIdentifier::LOG_INTERVAL));
    log.set_logging_type(LoggingType::POLLED).unwrap();
    log.set_log_interval(500).unwrap();
    log.set_logging_type(LoggingType::COV).unwrap();
    assert_eq!(logging_type(&log), LoggingType::COV);
    assert_eq!(interval(&log), PropertyValue::Unsigned(0));
    // A Trigger is for a TRIGGERED log only.
    assert_refused(
        log.trigger(),
        ErrorClass::PROPERTY,
        ErrorCode::NOT_CONFIGURED_FOR_TRIGGERED_LOGGING,
    );

    // TRIGGERED zeroes Log_Interval, which is then read-only.
    write(
        &mut log,
        PropertyIdentifier::LOGGING_TYPE,
        PropertyValue::Enumerated(2),
    )
    .unwrap();
    assert_eq!(interval(&log), PropertyValue::Unsigned(0));
    assert!(!log.is_writable_property(PropertyIdentifier::LOG_INTERVAL));
    for value in [PropertyValue::Unsigned(500), PropertyValue::Null] {
        assert_refused(
            write(&mut log, PropertyIdentifier::LOG_INTERVAL, value),
            ErrorClass::PROPERTY,
            ErrorCode::WRITE_ACCESS_DENIED,
        );
    }
    assert_refused(
        log.set_log_interval(500),
        ErrorClass::PROPERTY,
        ErrorCode::WRITE_ACCESS_DENIED,
    );

    // POLLED with a zero interval takes the default; a nonzero one stays.
    log.set_logging_type(LoggingType::POLLED).unwrap();
    assert!(log.is_writable_property(PropertyIdentifier::LOG_INTERVAL));
    assert_eq!(
        interval(&log),
        PropertyValue::Unsigned(DEFAULT_LOG_INTERVAL.into())
    );
    log.set_log_interval(250).unwrap();
    log.set_logging_type(LoggingType::POLLED).unwrap();
    assert_eq!(interval(&log), PropertyValue::Unsigned(250));
}

fn logging_type(log: &TrendLogObject) -> LoggingType {
    let PropertyValue::Enumerated(raw) = read(log, PropertyIdentifier::LOGGING_TYPE) else {
        panic!("Logging_Type is an enumeration");
    };
    LoggingType::from_raw(raw)
}

#[test]
fn log_interval_switches_between_polled_and_cov() {
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    // A new log is POLLED at zero: writing zero again changes nothing and
    // asks for no switch.
    write(
        &mut log,
        PropertyIdentifier::LOG_INTERVAL,
        PropertyValue::Unsigned(0),
    )
    .unwrap();
    log.set_log_interval(0).unwrap();
    assert_eq!(logging_type(&log), LoggingType::POLLED);
    write(
        &mut log,
        PropertyIdentifier::LOG_INTERVAL,
        PropertyValue::Unsigned(300),
    )
    .unwrap();
    // From nonzero to zero switches a POLLED log to COV (Clause 12.25.9),
    // by the wire and by the setter.
    write(
        &mut log,
        PropertyIdentifier::LOG_INTERVAL,
        PropertyValue::Unsigned(0),
    )
    .unwrap();
    assert_eq!(logging_type(&log), LoggingType::COV);
    assert_eq!(interval(&log), PropertyValue::Unsigned(0));
    // Zero again leaves it COV.
    log.set_log_interval(0).unwrap();
    assert_eq!(logging_type(&log), LoggingType::COV);
    // A nonzero interval switches a COV log back to POLLED at it.
    write(
        &mut log,
        PropertyIdentifier::LOG_INTERVAL,
        PropertyValue::Unsigned(700),
    )
    .unwrap();
    assert_eq!(logging_type(&log), LoggingType::POLLED);
    assert_eq!(interval(&log), PropertyValue::Unsigned(700));
    log.set_log_interval(0).unwrap();
    assert_eq!(logging_type(&log), LoggingType::COV);
    log.set_log_interval(40).unwrap();
    assert_eq!(logging_type(&log), LoggingType::POLLED);
    assert_eq!(interval(&log), PropertyValue::Unsigned(40));
    // Any other interval is taken, and so are the usual refusals.
    log.set_log_interval(1).unwrap();
    assert_eq!(interval(&log), PropertyValue::Unsigned(1));
    for (value, code) in [
        (
            PropertyValue::Unsigned(u64::from(u32::MAX) + 1),
            ErrorCode::VALUE_OUT_OF_RANGE,
        ),
        (PropertyValue::Boolean(true), ErrorCode::INVALID_DATA_TYPE),
    ] {
        assert_refused(
            write(&mut log, PropertyIdentifier::LOG_INTERVAL, value),
            ErrorClass::PROPERTY,
            code,
        );
    }
    assert_eq!(interval(&log), PropertyValue::Unsigned(1));

    // A Trend Log Multiple has no such switch: zero leaves it idle and
    // POLLED, and COV is out of its range (Clause 12.30.12).
    let mut multiple = TrendLogMultipleObject::new(1, "TLM-1", 8).unwrap();
    multiple.set_log_interval(300).unwrap();
    multiple.set_log_interval(0).unwrap();
    assert_eq!(
        multiple
            .read_property(PropertyIdentifier::LOG_INTERVAL, None)
            .unwrap(),
        PropertyValue::Unsigned(0)
    );
    assert_eq!(
        multiple
            .read_property(PropertyIdentifier::LOGGING_TYPE, None)
            .unwrap(),
        PropertyValue::Enumerated(0)
    );
    assert_refused(
        multiple.set_logging_type(LoggingType::COV),
        ErrorClass::PROPERTY,
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
}

#[test]
fn the_cov_rows_take_their_datatypes_and_ranges() {
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    let increment = PropertyIdentifier::CLIENT_COV_INCREMENT;
    let resubscription = PropertyIdentifier::COV_RESUBSCRIPTION_INTERVAL;
    // NULL, the referenced property's own criteria, by default.
    assert_eq!(read(&log, increment), PropertyValue::Null);
    assert_eq!(
        read(&log, resubscription),
        PropertyValue::Unsigned(DEFAULT_COV_RESUBSCRIPTION_INTERVAL.into())
    );
    write(&mut log, increment, PropertyValue::Real(0.5)).unwrap();
    assert_eq!(read(&log, increment), PropertyValue::Real(0.5));
    write(&mut log, increment, PropertyValue::Null).unwrap();
    assert_eq!(read(&log, increment), PropertyValue::Null);
    log.set_client_cov_increment(Some(2.0)).unwrap();
    assert_eq!(read(&log, increment), PropertyValue::Real(2.0));
    for value in [-1.0, f32::NAN, f32::INFINITY] {
        assert_refused(
            write(&mut log, increment, PropertyValue::Real(value)),
            ErrorClass::PROPERTY,
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_refused(
            log.set_client_cov_increment(Some(value)),
            ErrorClass::PROPERTY,
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
    }
    for value in [PropertyValue::Unsigned(1), PropertyValue::Double(1.0)] {
        assert_refused(
            write(&mut log, increment, value),
            ErrorClass::PROPERTY,
            ErrorCode::INVALID_DATA_TYPE,
        );
    }
    assert_eq!(read(&log, increment), PropertyValue::Real(2.0));

    write(&mut log, resubscription, PropertyValue::Unsigned(60)).unwrap();
    assert_eq!(read(&log, resubscription), PropertyValue::Unsigned(60));
    log.set_cov_resubscription_interval(90).unwrap();
    assert_eq!(read(&log, resubscription), PropertyValue::Unsigned(90));
    // Clause 12.25.10 keeps it nonzero.
    for (value, code) in [
        (PropertyValue::Unsigned(0), ErrorCode::VALUE_OUT_OF_RANGE),
        (
            PropertyValue::Unsigned(u64::from(u32::MAX) + 1),
            ErrorCode::VALUE_OUT_OF_RANGE,
        ),
        (PropertyValue::Null, ErrorCode::INVALID_DATA_TYPE),
    ] {
        assert_refused(
            write(&mut log, resubscription, value),
            ErrorClass::PROPERTY,
            code,
        );
    }
    assert_refused(
        log.set_cov_resubscription_interval(0),
        ErrorClass::PROPERTY,
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
    assert_eq!(read(&log, resubscription), PropertyValue::Unsigned(90));
    // Neither row is on a Trend Log Multiple (Table 12-35).
    let multiple = TrendLogMultipleObject::new(1, "TLM-1", 8).unwrap();
    for property in [increment, resubscription] {
        assert!(multiple.read_property(property, None).is_err());
    }
}

#[test]
fn trigger_asks_a_triggered_log_for_one_record() {
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    // Only a TRIGGERED log takes Trigger TRUE (Clause 12.25.29).
    assert_refused(
        write(
            &mut log,
            PropertyIdentifier::TRIGGER,
            PropertyValue::Boolean(true),
        ),
        ErrorClass::PROPERTY,
        ErrorCode::NOT_CONFIGURED_FOR_TRIGGERED_LOGGING,
    );
    assert_refused(
        log.trigger(),
        ErrorClass::PROPERTY,
        ErrorCode::NOT_CONFIGURED_FOR_TRIGGERED_LOGGING,
    );
    write(
        &mut log,
        PropertyIdentifier::TRIGGER,
        PropertyValue::Boolean(false),
    )
    .unwrap();

    log.set_logging_type(LoggingType::TRIGGERED).unwrap();
    write(
        &mut log,
        PropertyIdentifier::TRIGGER,
        PropertyValue::Boolean(true),
    )
    .unwrap();
    // FALSE doesn't take back an acquisition already asked for.
    write(
        &mut log,
        PropertyIdentifier::TRIGGER,
        PropertyValue::Boolean(false),
    )
    .unwrap();
    assert_eq!(
        read(&log, PropertyIdentifier::TRIGGER),
        PropertyValue::Boolean(true)
    );
    assert_refused(
        write(
            &mut log,
            PropertyIdentifier::TRIGGER,
            PropertyValue::Unsigned(1),
        ),
        ErrorClass::PROPERTY,
        ErrorCode::INVALID_DATA_TYPE,
    );
    // The record it asked for serves it.
    log.add_record(record()).unwrap();
    assert_eq!(
        read(&log, PropertyIdentifier::TRIGGER),
        PropertyValue::Boolean(false)
    );
    assert_eq!(log.records().len(), 1);

    // Leaving TRIGGERED drops a Trigger not yet served.
    log.trigger().unwrap();
    log.set_logging_type(LoggingType::POLLED).unwrap();
    assert_eq!(
        read(&log, PropertyIdentifier::TRIGGER),
        PropertyValue::Boolean(false)
    );
}

#[test]
fn a_record_the_log_ignores_still_serves_the_trigger() {
    struct Nine;
    impl ClockReader for Nine {
        fn read_clock(&self) -> Option<ClockFrame> {
            Some(ClockFrame {
                local_date: record().date,
                local_time: record().time,
                utc_offset: 0,
                daylight_savings_status: false,
            })
        }
    }
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    log.bind_clock_internal(Some(Arc::new(Nine)));
    log.set_logging_type(LoggingType::TRIGGERED).unwrap();
    let trigger = |log: &TrendLogObject| read(log, PropertyIdentifier::TRIGGER);

    // Enable FALSE: the record is ignored, but the acquisition was made.
    write(
        &mut log,
        PropertyIdentifier::LOG_ENABLE,
        PropertyValue::Boolean(false),
    )
    .unwrap();
    log.trigger().unwrap();
    log.add_record(record()).unwrap();
    assert_eq!(trigger(&log), PropertyValue::Boolean(false));
    assert!(
        log.records()
            .iter()
            .all(|r| matches!(r.log_datum, LogDatum::LogStatus(_)))
    );

    // Outside the window, the same.
    write(
        &mut log,
        PropertyIdentifier::LOG_ENABLE,
        PropertyValue::Boolean(true),
    )
    .unwrap();
    let (date, time) = (record().date, record().time);
    log.set_start_time(date, Time { hour: 10, ..time }).unwrap();
    log.trigger().unwrap();
    log.add_record(record()).unwrap();
    assert_eq!(trigger(&log), PropertyValue::Boolean(false));
    assert!(
        log.records()
            .iter()
            .all(|r| matches!(r.log_datum, LogDatum::LogStatus(_)))
    );
}

#[test]
fn alignment_rows_read_back_what_is_written() {
    let mut log = TrendLogObject::new(1, "TL-1", 8).unwrap();
    assert_eq!(
        read(&log, PropertyIdentifier::ALIGN_INTERVALS),
        PropertyValue::Boolean(false)
    );
    assert_eq!(
        read(&log, PropertyIdentifier::INTERVAL_OFFSET),
        PropertyValue::Unsigned(0)
    );
    write(
        &mut log,
        PropertyIdentifier::ALIGN_INTERVALS,
        PropertyValue::Boolean(true),
    )
    .unwrap();
    write(
        &mut log,
        PropertyIdentifier::INTERVAL_OFFSET,
        PropertyValue::Unsigned(31),
    )
    .unwrap();
    assert_eq!(
        read(&log, PropertyIdentifier::ALIGN_INTERVALS),
        PropertyValue::Boolean(true)
    );
    assert_eq!(
        read(&log, PropertyIdentifier::INTERVAL_OFFSET),
        PropertyValue::Unsigned(31)
    );
    log.set_align_intervals(false);
    log.set_interval_offset(7);
    assert_eq!(
        read(&log, PropertyIdentifier::ALIGN_INTERVALS),
        PropertyValue::Boolean(false)
    );
    assert_eq!(
        read(&log, PropertyIdentifier::INTERVAL_OFFSET),
        PropertyValue::Unsigned(7)
    );
    for (property, value) in [
        (
            PropertyIdentifier::ALIGN_INTERVALS,
            PropertyValue::Unsigned(1),
        ),
        (
            PropertyIdentifier::INTERVAL_OFFSET,
            PropertyValue::Boolean(true),
        ),
    ] {
        assert_refused(
            write(&mut log, property, value),
            ErrorClass::PROPERTY,
            ErrorCode::INVALID_DATA_TYPE,
        );
    }
}