use super::AccumulatorObject;
use std::borrow::Cow;
use bacnet_types::enums::PropertyIdentifier as P;
use crate::property_metadata::{
PropertyConformance::{Optional, RequiredRead},
PropertyMetadata,
PropertyWriteCapability::{Always, ReadOnly},
};
const ACCUMULATOR_BASE: &[PropertyMetadata] = &[
PropertyMetadata::new(P::OBJECT_IDENTIFIER, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::OBJECT_NAME, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::DESCRIPTION, Optional, None, Always),
PropertyMetadata::new(P::OBJECT_TYPE, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::PRESENT_VALUE, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::MAX_PRES_VALUE, RequiredRead, None, Always),
PropertyMetadata::new(P::SCALE, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::PRESCALE, Optional, None, ReadOnly),
PropertyMetadata::new(P::PULSE_RATE, Optional, None, Always),
PropertyMetadata::new(P::UNITS, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::LIMIT_MONITORING_INTERVAL, Optional, None, Always),
PropertyMetadata::new(P::STATUS_FLAGS, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::EVENT_STATE, RequiredRead, None, ReadOnly),
PropertyMetadata::new(P::OUT_OF_SERVICE, RequiredRead, None, Always),
PropertyMetadata::new(P::RELIABILITY, Optional, None, ReadOnly),
PropertyMetadata::new(P::VALUE_BEFORE_CHANGE, Optional, None, ReadOnly),
PropertyMetadata::new(P::VALUE_SET, Optional, None, ReadOnly),
PropertyMetadata::new(P::PROPERTY_LIST, RequiredRead, None, ReadOnly),
];
pub(super) fn for_accumulator_object(object: &AccumulatorObject) -> Cow<'_, [PropertyMetadata]> {
if object.has_prescale() {
Cow::Borrowed(ACCUMULATOR_BASE)
} else {
Cow::Owned(
ACCUMULATOR_BASE
.iter()
.filter(|row| row.property_identifier != P::PRESCALE)
.copied()
.collect(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::property_metadata::PropertyWriteCapability;
use crate::traits::BACnetObject;
use bacnet_types::enums::{ErrorClass, ErrorCode};
use bacnet_types::error::Error;
use bacnet_types::primitives::PropertyValue;
use std::collections::HashSet;
fn assert_error(error: Error, expected: ErrorCode) {
assert!(
matches!(error, Error::Protocol { class, code }
if class == ErrorClass::PROPERTY.to_raw() as u32
&& code == expected.to_raw() as u32),
"expected {expected:?}, got {error:?}"
);
}
fn assert_exact_sets(object: &dyn BACnetObject, all: &[P], required: &[P]) {
let metadata = object.property_metadata();
assert_eq!(metadata.len(), all.len() + 1);
assert_eq!(object.property_list().as_ref(), all);
assert_eq!(object.required_properties().as_ref(), required);
assert_eq!(
metadata
.iter()
.map(|row| row.property_identifier)
.collect::<HashSet<_>>()
.len(),
metadata.len()
);
assert!(!object.is_createable());
assert!(object.is_deleteable());
assert!(object.supports_cov());
for row in metadata.iter() {
assert_eq!(row.presence_condition, None);
let expected = if required.contains(&row.property_identifier) {
RequiredRead
} else {
Optional
};
assert_eq!(row.conformance, expected, "{:?}", row.property_identifier);
object.read_property(row.property_identifier, None).unwrap();
}
}
fn assert_indexed_property_list(object: &dyn BACnetObject, all: &[P]) {
let wire: Vec<_> = all
.iter()
.filter(|&&p| !matches!(p, P::OBJECT_IDENTIFIER | P::OBJECT_NAME | P::OBJECT_TYPE))
.map(|p| PropertyValue::Enumerated(p.to_raw()))
.collect();
assert!(object.is_array_property(P::PROPERTY_LIST));
assert_eq!(
object.read_property(P::PROPERTY_LIST, None).unwrap(),
PropertyValue::List(wire.clone())
);
assert_eq!(
object.read_property(P::PROPERTY_LIST, Some(0)).unwrap(),
PropertyValue::Unsigned(wire.len() as u64)
);
for (index, value) in wire.iter().enumerate() {
assert_eq!(
object
.read_property(P::PROPERTY_LIST, Some(index as u32 + 1))
.unwrap(),
*value
);
}
for index in [wire.len() as u32 + 1, u32::MAX] {
assert_error(
object
.read_property(P::PROPERTY_LIST, Some(index))
.unwrap_err(),
ErrorCode::INVALID_ARRAY_INDEX,
);
}
}
#[test]
fn property_metadata_accumulator_exact_sets_readable_rows_and_indexed_list() {
let mut object = AccumulatorObject::new(1, "ACC-1", 95).unwrap();
let without_prescale = AccumulatorObject::new(2, "ACC-2", 95).unwrap();
object.set_prescale(bacnet_types::constructed::BACnetPrescale {
multiplier: 1,
modulo_divide: 100,
});
let all = [
P::OBJECT_IDENTIFIER,
P::OBJECT_NAME,
P::DESCRIPTION,
P::OBJECT_TYPE,
P::PRESENT_VALUE,
P::MAX_PRES_VALUE,
P::SCALE,
P::PRESCALE,
P::PULSE_RATE,
P::UNITS,
P::LIMIT_MONITORING_INTERVAL,
P::STATUS_FLAGS,
P::EVENT_STATE,
P::OUT_OF_SERVICE,
P::RELIABILITY,
P::VALUE_BEFORE_CHANGE,
P::VALUE_SET,
];
let required = [
P::OBJECT_IDENTIFIER,
P::OBJECT_NAME,
P::OBJECT_TYPE,
P::PRESENT_VALUE,
P::MAX_PRES_VALUE,
P::SCALE,
P::UNITS,
P::STATUS_FLAGS,
P::EVENT_STATE,
P::OUT_OF_SERVICE,
P::PROPERTY_LIST,
];
assert_exact_sets(&object, &all, &required);
assert_indexed_property_list(&object, &all);
let all_without: Vec<_> = all.into_iter().filter(|p| *p != P::PRESCALE).collect();
assert_exact_sets(&without_prescale, &all_without, &required);
assert_indexed_property_list(&without_prescale, &all_without);
assert_eq!(
object.read_property(P::PRESENT_VALUE, None).unwrap(),
PropertyValue::Unsigned(0)
);
assert_eq!(
object.read_property(P::SCALE, None).unwrap(),
PropertyValue::ApplicationData(vec![0x0C, 0x3F, 0x80, 0x00, 0x00])
);
assert_eq!(
object.read_property(P::PRESCALE, None).unwrap(),
PropertyValue::ApplicationData(vec![0x09, 0x01, 0x19, 0x64])
);
assert!(!object.is_array_property(P::SCALE));
assert!(!object.is_array_property(P::PRESCALE));
assert!(!object.is_array_property(P::PRESENT_VALUE));
}
#[test]
fn property_metadata_accumulator_write_capabilities_match_dispatch() {
let writable = [
P::DESCRIPTION,
P::OUT_OF_SERVICE,
P::MAX_PRES_VALUE,
P::PULSE_RATE,
P::LIMIT_MONITORING_INTERVAL,
];
for out_of_service in [false, true] {
let mut object = AccumulatorObject::new(1, "ACC-1", 95).unwrap();
object
.write_property(
P::OUT_OF_SERVICE,
None,
PropertyValue::Boolean(out_of_service),
None,
)
.unwrap();
let original = object.property_metadata().into_owned();
for row in &original {
let p = row.property_identifier;
let capability = if writable.contains(&p) {
PropertyWriteCapability::Always
} else {
PropertyWriteCapability::ReadOnly
};
assert_eq!(row.write_capability, capability, "{p:?}");
assert_eq!(
object.is_writable_property(p),
capability.is_writable(),
"{p:?}"
);
let value = object.read_property(p, None).unwrap();
let result = object.write_property(p, None, value, None);
if capability.is_writable() {
result.unwrap();
} else {
assert_error(result.unwrap_err(), ErrorCode::WRITE_ACCESS_DENIED);
}
}
assert!(!object.is_writable_property(P::OBJECT_NAME));
assert_error(
object
.write_property(
P::OBJECT_NAME,
None,
PropertyValue::CharacterString("renamed".into()),
None,
)
.unwrap_err(),
ErrorCode::WRITE_ACCESS_DENIED,
);
assert!(!object.is_writable_property(P::PRESENT_VALUE));
assert_error(
object
.write_property(P::PRESENT_VALUE, None, PropertyValue::Unsigned(10), None)
.unwrap_err(),
ErrorCode::WRITE_ACCESS_DENIED,
);
assert_eq!(
object.read_property(P::PRESENT_VALUE, None).unwrap(),
PropertyValue::Unsigned(0)
);
assert_eq!(object.property_metadata().as_ref(), original);
}
}
#[test]
fn property_metadata_accumulator_writes_store_verbatim_with_range_gates() {
for out_of_service in [false, true] {
let mut object = AccumulatorObject::new(1, "ACC-1", 95).unwrap();
object.set_prescale(bacnet_types::constructed::BACnetPrescale {
multiplier: 1,
modulo_divide: 100,
});
object
.write_property(
P::OUT_OF_SERVICE,
None,
PropertyValue::Boolean(out_of_service),
None,
)
.unwrap();
object
.write_property(P::MAX_PRES_VALUE, None, PropertyValue::Unsigned(1000), None)
.unwrap();
assert_eq!(
object.read_property(P::MAX_PRES_VALUE, None).unwrap(),
PropertyValue::Unsigned(1000)
);
object
.write_property(P::PULSE_RATE, None, PropertyValue::Real(2.5), None)
.unwrap();
assert_eq!(
object.read_property(P::PULSE_RATE, None).unwrap(),
PropertyValue::Real(2.5)
);
object
.write_property(
P::LIMIT_MONITORING_INTERVAL,
None,
PropertyValue::Unsigned(60),
None,
)
.unwrap();
assert_eq!(
object
.read_property(P::LIMIT_MONITORING_INTERVAL, None)
.unwrap(),
PropertyValue::Unsigned(60)
);
for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
assert_error(
object
.write_property(P::PULSE_RATE, None, PropertyValue::Real(value), None)
.unwrap_err(),
ErrorCode::VALUE_OUT_OF_RANGE,
);
}
assert_eq!(
object.read_property(P::PULSE_RATE, None).unwrap(),
PropertyValue::Real(2.5)
);
assert_error(
object
.write_property(
P::LIMIT_MONITORING_INTERVAL,
None,
PropertyValue::Unsigned(u64::from(u32::MAX) + 1),
None,
)
.unwrap_err(),
ErrorCode::VALUE_OUT_OF_RANGE,
);
for (p, value) in [
(P::MAX_PRES_VALUE, PropertyValue::Real(1.0)),
(P::PULSE_RATE, PropertyValue::Unsigned(1)),
(P::LIMIT_MONITORING_INTERVAL, PropertyValue::Enumerated(60)),
(P::DESCRIPTION, PropertyValue::Unsigned(1)),
(P::OUT_OF_SERVICE, PropertyValue::Unsigned(1)),
] {
assert_error(
object.write_property(p, None, value, None).unwrap_err(),
ErrorCode::INVALID_DATA_TYPE,
);
}
for p in [
P::SCALE,
P::PRESCALE,
P::VALUE_BEFORE_CHANGE,
P::VALUE_SET,
P::STATUS_FLAGS,
P::EVENT_STATE,
P::RELIABILITY,
P::UNITS,
] {
let value = object.read_property(p, None).unwrap();
assert_error(
object.write_property(p, None, value, None).unwrap_err(),
ErrorCode::WRITE_ACCESS_DENIED,
);
assert!(!object.is_writable_property(p));
}
}
}
#[test]
fn property_metadata_accumulator_unserved_rows_stay_unknown() {
fn assert_unserved(object: &mut dyn BACnetObject, p: P) {
assert!(!object.is_writable_property(p));
assert_error(
object.read_property(p, None).unwrap_err(),
ErrorCode::UNKNOWN_PROPERTY,
);
assert_error(
object
.write_property(p, None, PropertyValue::Null, None)
.unwrap_err(),
ErrorCode::UNKNOWN_PROPERTY,
);
}
let mut acc = AccumulatorObject::new(1, "ACC-1", 95).unwrap();
assert_unserved(&mut acc, P::DEVICE_TYPE);
assert_unserved(&mut acc, P::VALUE_CHANGE_TIME);
assert_unserved(&mut acc, P::COUNT);
assert_unserved(&mut acc, P::PRESCALE);
}
}