bacnet-objects 0.9.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use super::super::*;
use crate::event::LimitEnable;
use bacnet_types::enums::EventState;

// --- AnalogInput ---

#[test]
fn ai_read_present_value() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap(); // 62 = degrees-fahrenheit
    ai.set_present_value(72.5);
    let val = ai
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(72.5));
}

#[test]
fn ai_read_units() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai.read_property(PropertyIdentifier::UNITS, None).unwrap();
    assert_eq!(val, PropertyValue::Enumerated(62));
}

#[test]
fn ai_write_present_value_denied_when_in_service() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(99.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ai_write_present_value_allowed_when_out_of_service() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(99.0),
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(99.0));
}

#[test]
fn ai_read_unknown_property() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.read_property(PropertyIdentifier::PRIORITY_ARRAY, None);
    assert!(result.is_err());
}

// --- AnalogOutput ---

#[test]
fn ao_write_with_priority() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();

    // Write at priority 8
    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(8),
    )
    .unwrap();

    let val = ao
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(50.0));

    // Priority array at index 8 should have the value
    let slot = ao
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(8))
        .unwrap();
    assert_eq!(slot, PropertyValue::Real(50.0));

    // Priority array at index 1 should be Null
    let slot = ao
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(1))
        .unwrap();
    assert_eq!(slot, PropertyValue::Null);
}

#[test]
fn ao_relinquish_falls_to_default() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();

    // Write at priority 16 (lowest)
    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(75.0),
        Some(16),
    )
    .unwrap();
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(75.0)
    );

    // Relinquish (write Null)
    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Null,
        Some(16),
    )
    .unwrap();

    // Should fall back to relinquish-default (0.0)
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(0.0)
    );
}

#[test]
fn ao_higher_priority_wins() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();

    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(10.0),
        Some(16),
    )
    .unwrap();
    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(90.0),
        Some(8),
    )
    .unwrap();

    // Priority 8 wins over 16
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(90.0)
    );
}

// --- Intrinsic Reporting ---

#[test]
fn ai_read_event_state_default_normal() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai
        .read_property(PropertyIdentifier::EVENT_STATE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Enumerated(EventState::NORMAL.to_raw()));
}

#[test]
fn ai_read_write_high_limit() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::HIGH_LIMIT,
        None,
        PropertyValue::Real(85.0),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::HIGH_LIMIT, None)
            .unwrap(),
        PropertyValue::Real(85.0)
    );
}

#[test]
fn ai_read_write_low_limit() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::LOW_LIMIT,
        None,
        PropertyValue::Real(15.0),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::LOW_LIMIT, None)
            .unwrap(),
        PropertyValue::Real(15.0)
    );
}

#[test]
fn ai_read_write_deadband() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(2.5),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::DEADBAND, None)
            .unwrap(),
        PropertyValue::Real(2.5)
    );
}

#[test]
fn ai_deadband_reject_negative() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(-1.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ai_read_write_limit_enable() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let enable_both = LimitEnable::BOTH.to_bits();
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![enable_both],
        },
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::LIMIT_ENABLE, None)
        .unwrap();
    if let PropertyValue::BitString { data, .. } = val {
        let le = LimitEnable::from_bits(data[0]);
        assert!(le.low_limit_enable);
        assert!(le.high_limit_enable);
    } else {
        panic!("Expected BitString");
    }
}

#[test]
fn ai_intrinsic_reporting_triggers_on_present_value_change() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    // Configure: high=80, low=20, deadband=2, both limits enabled
    ai.write_property(
        PropertyIdentifier::HIGH_LIMIT,
        None,
        PropertyValue::Real(80.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::LOW_LIMIT,
        None,
        PropertyValue::Real(20.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(2.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::BOTH.to_bits()],
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0x07 << 5], // all transitions enabled
        },
        None,
    )
    .unwrap();

    // Normal value — no transition
    ai.set_present_value(50.0);
    assert!(ai.evaluate_intrinsic_reporting().is_none());

    // Go above high limit
    ai.set_present_value(81.0);
    let change = ai.evaluate_intrinsic_reporting().unwrap();
    assert_eq!(change.from, EventState::NORMAL);
    assert_eq!(change.to, EventState::HIGH_LIMIT);

    // Verify event_state property reads correctly
    assert_eq!(
        ai.read_property(PropertyIdentifier::EVENT_STATE, None)
            .unwrap(),
        PropertyValue::Enumerated(EventState::HIGH_LIMIT.to_raw())
    );

    // Drop below deadband threshold → back to NORMAL
    ai.set_present_value(77.0);
    let change = ai.evaluate_intrinsic_reporting().unwrap();
    assert_eq!(change.to, EventState::NORMAL);
}

#[test]
fn ao_intrinsic_reporting_after_priority_write() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    ao.write_property(
        PropertyIdentifier::HIGH_LIMIT,
        None,
        PropertyValue::Real(80.0),
        None,
    )
    .unwrap();
    ao.write_property(
        PropertyIdentifier::LOW_LIMIT,
        None,
        PropertyValue::Real(20.0),
        None,
    )
    .unwrap();
    ao.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(2.0),
        None,
    )
    .unwrap();
    ao.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::BOTH.to_bits()],
        },
        None,
    )
    .unwrap();
    ao.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0x07 << 5], // all transitions enabled
        },
        None,
    )
    .unwrap();

    // Write a high value via priority array
    ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(85.0),
        Some(8),
    )
    .unwrap();
    let change = ao.evaluate_intrinsic_reporting().unwrap();
    assert_eq!(change.to, EventState::HIGH_LIMIT);
}

#[test]
fn ai_read_reliability_default() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai
        .read_property(PropertyIdentifier::RELIABILITY, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Enumerated(0)); // NO_FAULT_DETECTED
}

#[test]
fn ai_description_read_write() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    // Default description is empty
    let val = ai
        .read_property(PropertyIdentifier::DESCRIPTION, None)
        .unwrap();
    assert_eq!(val, PropertyValue::CharacterString(String::new()));
    // Write a description
    ai.write_property(
        PropertyIdentifier::DESCRIPTION,
        None,
        PropertyValue::CharacterString("Zone temperature sensor".into()),
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::DESCRIPTION, None)
        .unwrap();
    assert_eq!(
        val,
        PropertyValue::CharacterString("Zone temperature sensor".into())
    );
}

#[test]
fn ai_set_description_convenience() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.set_description("Supply air temperature");
    assert_eq!(
        ai.read_property(PropertyIdentifier::DESCRIPTION, None)
            .unwrap(),
        PropertyValue::CharacterString("Supply air temperature".into())
    );
}

#[test]
fn ai_description_in_property_list() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    assert!(ai
        .property_list()
        .contains(&PropertyIdentifier::DESCRIPTION));
}

#[test]
fn ao_description_read_write() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    ao.write_property(
        PropertyIdentifier::DESCRIPTION,
        None,
        PropertyValue::CharacterString("Chilled water valve".into()),
        None,
    )
    .unwrap();
    assert_eq!(
        ao.read_property(PropertyIdentifier::DESCRIPTION, None)
            .unwrap(),
        PropertyValue::CharacterString("Chilled water valve".into())
    );
}

#[test]
fn ao_description_in_property_list() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    assert!(ao
        .property_list()
        .contains(&PropertyIdentifier::DESCRIPTION));
}

#[test]
fn ao_read_reliability_default() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    let val = ao
        .read_property(PropertyIdentifier::RELIABILITY, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Enumerated(0)); // NO_FAULT_DETECTED
}

// --- Priority array bounds tests ---

#[test]
fn ao_priority_array_index_zero_returns_size() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    let val = ao
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(0))
        .unwrap();
    assert_eq!(val, PropertyValue::Unsigned(16));
}

#[test]
fn ao_priority_array_index_out_of_bounds() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Index 17 is out of bounds (valid: 0-16)
    let result = ao.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(17));
    assert!(result.is_err());
}

#[test]
fn ao_priority_array_index_far_out_of_bounds() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Large index well beyond valid range
    let result = ao.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(100));
    assert!(result.is_err());
}

#[test]
fn ao_priority_array_index_u32_max_out_of_bounds() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    let result = ao.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(u32::MAX));
    assert!(result.is_err());
}

// --- WriteProperty with invalid priority tests ---

#[test]
fn ao_write_with_priority_zero_rejected() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Priority 0 is invalid (valid range is 1-16)
    let result = ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(0),
    );
    assert!(result.is_err());
}

#[test]
fn ao_write_with_priority_17_rejected() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Priority 17 is invalid (valid range is 1-16)
    let result = ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(17),
    );
    assert!(result.is_err());
}

#[test]
fn ao_write_with_priority_255_rejected() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Priority 255 is invalid
    let result = ao.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(50.0),
        Some(255),
    );
    assert!(result.is_err());
}

#[test]
fn ao_write_with_all_valid_priorities() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // All priorities 1 through 16 should succeed
    for prio in 1..=16u8 {
        ao.write_property(
            PropertyIdentifier::PRESENT_VALUE,
            None,
            PropertyValue::Real(prio as f32),
            Some(prio),
        )
        .unwrap();
    }
    // Present value should be the highest priority (priority 1)
    let val = ao
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(1.0));
}

#[test]
fn ao_priority_array_read_all_slots_none_by_default() {
    let ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Read entire array (no index)
    let val = ao
        .read_property(PropertyIdentifier::PRIORITY_ARRAY, None)
        .unwrap();
    if let PropertyValue::List(elements) = val {
        assert_eq!(elements.len(), 16);
        for elem in &elements {
            assert_eq!(elem, &PropertyValue::Null);
        }
    } else {
        panic!("Expected List for priority array without index");
    }
}

// --- Direct PRIORITY_ARRAY writes ---

#[test]
fn ao_direct_priority_array_write_value() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Write directly to PRIORITY_ARRAY[5]
    ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        Some(5),
        PropertyValue::Real(42.0),
        None,
    )
    .unwrap();
    // present_value should reflect the written value
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(42.0)
    );
    // Slot 5 should have the value
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(5))
            .unwrap(),
        PropertyValue::Real(42.0)
    );
}

#[test]
fn ao_direct_priority_array_relinquish() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Write a value at priority 5
    ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        Some(5),
        PropertyValue::Real(42.0),
        None,
    )
    .unwrap();
    // Relinquish with Null
    ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        Some(5),
        PropertyValue::Null,
        None,
    )
    .unwrap();
    // Should fall back to relinquish default (0.0)
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRESENT_VALUE, None)
            .unwrap(),
        PropertyValue::Real(0.0)
    );
    assert_eq!(
        ao.read_property(PropertyIdentifier::PRIORITY_ARRAY, Some(5))
            .unwrap(),
        PropertyValue::Null
    );
}

#[test]
fn ao_direct_priority_array_no_index_error() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    // Writing PRIORITY_ARRAY without array_index should error
    let result = ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        None,
        PropertyValue::Real(42.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ao_direct_priority_array_index_zero_error() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    let result = ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        Some(0),
        PropertyValue::Real(42.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ao_direct_priority_array_index_17_error() {
    let mut ao = AnalogOutputObject::new(1, "AO-1", 62).unwrap();
    let result = ao.write_property(
        PropertyIdentifier::PRIORITY_ARRAY,
        Some(17),
        PropertyValue::Real(42.0),
        None,
    );
    assert!(result.is_err());
}