use std::borrow::Cow;
use std::collections::VecDeque;
use std::sync::Arc;
use bacnet_types::constructed::{BACnetDeviceObjectPropertyReference, BACnetLogRecord};
use bacnet_types::enums::{ErrorClass, ErrorCode, ObjectType, PropertyIdentifier};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue, StatusFlags};
use crate::clock::ClockReader;
use crate::common::{self, read_property_list_property};
use crate::log_buffer::{LogRecordBuffer, LogRecordIdentity, LogRecordProfile};
use crate::log_lifecycle::{LogLifecycle, LogLifecycleSnapshot};
use crate::traits::{BACnetObject, WritePropertyRollback};
pub struct TrendLogObject {
oid: ObjectIdentifier,
name: String,
description: String,
log_enable: bool,
log_interval: u32,
stop_when_full: bool,
buffer_size: u32,
log_buffer: LogRecordBuffer,
out_of_service: bool,
reliability: u32,
status_flags: StatusFlags,
log_device_object_property: Option<BACnetDeviceObjectPropertyReference>,
logging_type: u32, clock: Option<Arc<dyn ClockReader>>,
}
impl TrendLogObject {
pub fn new(instance: u32, name: impl Into<String>, buffer_size: u32) -> Result<Self, Error> {
let oid = ObjectIdentifier::new(ObjectType::TREND_LOG, instance)?;
Ok(Self {
oid,
name: name.into(),
description: String::new(),
log_enable: true,
log_interval: 0,
stop_when_full: false,
buffer_size,
log_buffer: LogRecordBuffer::new(buffer_size),
out_of_service: false,
reliability: 0,
status_flags: StatusFlags::empty(),
log_device_object_property: None,
logging_type: 0,
clock: None,
})
}
pub fn add_record(&mut self, record: BACnetLogRecord) {
let _ = self.try_add_record_internal(record);
}
pub fn records(&self) -> &VecDeque<BACnetLogRecord> {
self.log_buffer.records()
}
pub fn clear(&mut self) {
self.log_buffer.clear();
}
pub fn set_description(&mut self, desc: impl Into<String>) {
self.description = desc.into();
}
pub fn set_log_device_object_property(
&mut self,
reference: Option<BACnetDeviceObjectPropertyReference>,
) {
self.log_device_object_property = reference;
}
pub fn set_logging_type(&mut self, logging_type: u32) {
self.logging_type = logging_type;
}
fn try_add_record_internal(&mut self, record: BACnetLogRecord) -> Result<(), Error> {
self.lifecycle().try_add_ordinary(record).map(|_| ())
}
fn lifecycle(&mut self) -> LogLifecycle<'_> {
LogLifecycle::new(
&mut self.log_buffer,
&mut self.log_enable,
&mut self.stop_when_full,
self.clock.as_ref(),
)
}
}
impl BACnetObject for TrendLogObject {
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> {
match property {
p if p == PropertyIdentifier::OBJECT_IDENTIFIER => {
Ok(PropertyValue::ObjectIdentifier(self.oid))
}
p if p == PropertyIdentifier::OBJECT_NAME => {
Ok(PropertyValue::CharacterString(self.name.clone()))
}
p if p == PropertyIdentifier::DESCRIPTION => {
Ok(PropertyValue::CharacterString(self.description.clone()))
}
p if p == PropertyIdentifier::OBJECT_TYPE => {
Ok(PropertyValue::Enumerated(ObjectType::TREND_LOG.to_raw()))
}
p if p == PropertyIdentifier::LOG_ENABLE => Ok(PropertyValue::Boolean(self.log_enable)),
p if p == PropertyIdentifier::LOG_INTERVAL => {
Ok(PropertyValue::Unsigned(self.log_interval as u64))
}
p if p == PropertyIdentifier::STOP_WHEN_FULL => {
Ok(PropertyValue::Boolean(self.stop_when_full))
}
p if p == PropertyIdentifier::BUFFER_SIZE => {
Ok(PropertyValue::Unsigned(self.buffer_size as u64))
}
p if p == PropertyIdentifier::RECORD_COUNT => {
Ok(PropertyValue::Unsigned(self.records().len() as u64))
}
p if p == PropertyIdentifier::TOTAL_RECORD_COUNT => Ok(PropertyValue::Unsigned(
self.log_buffer.total_record_count() as u64,
)),
p if p == PropertyIdentifier::STATUS_FLAGS => Ok(PropertyValue::BitString {
unused_bits: 4,
data: vec![self.status_flags.bits() << 4],
}),
p if p == PropertyIdentifier::EVENT_STATE => Ok(PropertyValue::Enumerated(0)),
p if p == PropertyIdentifier::RELIABILITY => {
Ok(PropertyValue::Enumerated(self.reliability))
}
p if p == PropertyIdentifier::OUT_OF_SERVICE => {
Ok(PropertyValue::Boolean(self.out_of_service))
}
p if p == PropertyIdentifier::LOG_BUFFER => {
Ok(self.log_buffer.project(LogRecordProfile::Trend))
}
p if p == PropertyIdentifier::LOGGING_TYPE => {
Ok(PropertyValue::Enumerated(self.logging_type))
}
p if p == PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY => {
match &self.log_device_object_property {
None => Ok(PropertyValue::Null),
Some(r) => Ok(PropertyValue::List(vec![
PropertyValue::ObjectIdentifier(r.object_identifier),
PropertyValue::Unsigned(r.property_identifier as u64),
match r.property_array_index {
Some(idx) => PropertyValue::Unsigned(idx as u64),
None => PropertyValue::Null,
},
match r.device_identifier {
Some(dev) => PropertyValue::ObjectIdentifier(dev),
None => PropertyValue::Null,
},
])),
}
}
p if p == PropertyIdentifier::PROPERTY_LIST => {
read_property_list_property(&self.property_list(), array_index)
}
_ => Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::UNKNOWN_PROPERTY.to_raw() as u32,
}),
}
}
fn write_property(
&mut self,
property: PropertyIdentifier,
_array_index: Option<u32>,
value: PropertyValue,
_priority: Option<u8>,
) -> Result<(), Error> {
if property == PropertyIdentifier::LOG_ENABLE {
if let PropertyValue::Boolean(v) = value {
return self.lifecycle().write_enable(v);
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::LOG_INTERVAL {
if let PropertyValue::Unsigned(v) = value {
self.log_interval = common::u64_to_u32(v)?;
return Ok(());
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::STOP_WHEN_FULL {
if let PropertyValue::Boolean(v) = value {
return self.lifecycle().write_stop_when_full(v);
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::RECORD_COUNT {
if let PropertyValue::Unsigned(0) = value {
return self.lifecycle().purge();
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::RELIABILITY {
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::WRITE_ACCESS_DENIED.to_raw() as u32,
});
}
if property == PropertyIdentifier::OUT_OF_SERVICE {
if let PropertyValue::Boolean(v) = value {
self.out_of_service = v;
return Ok(());
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::DESCRIPTION {
if let PropertyValue::CharacterString(s) = value {
self.description = s;
return Ok(());
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::WRITE_ACCESS_DENIED.to_raw() as u32,
})
}
fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
static PROPS: &[PropertyIdentifier] = &[
PropertyIdentifier::OBJECT_IDENTIFIER,
PropertyIdentifier::OBJECT_NAME,
PropertyIdentifier::DESCRIPTION,
PropertyIdentifier::OBJECT_TYPE,
PropertyIdentifier::LOG_ENABLE,
PropertyIdentifier::LOG_INTERVAL,
PropertyIdentifier::STOP_WHEN_FULL,
PropertyIdentifier::BUFFER_SIZE,
PropertyIdentifier::LOG_BUFFER,
PropertyIdentifier::RECORD_COUNT,
PropertyIdentifier::TOTAL_RECORD_COUNT,
PropertyIdentifier::STATUS_FLAGS,
PropertyIdentifier::EVENT_STATE,
PropertyIdentifier::RELIABILITY,
PropertyIdentifier::OUT_OF_SERVICE,
PropertyIdentifier::LOGGING_TYPE,
PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY,
];
Cow::Borrowed(PROPS)
}
fn bind_clock_internal(&mut self, clock: Option<Arc<dyn ClockReader>>) {
self.clock = clock;
}
fn is_writable_property(&self, property: PropertyIdentifier) -> bool {
matches!(
property,
PropertyIdentifier::LOG_ENABLE
| PropertyIdentifier::LOG_INTERVAL
| PropertyIdentifier::STOP_WHEN_FULL
| PropertyIdentifier::RECORD_COUNT
| PropertyIdentifier::OUT_OF_SERVICE
| PropertyIdentifier::DESCRIPTION
)
}
fn capture_write_property_rollback(
&mut self,
property: PropertyIdentifier,
value: &PropertyValue,
) -> Option<WritePropertyRollback> {
((property == PropertyIdentifier::RECORD_COUNT
&& matches!(value, PropertyValue::Unsigned(0)))
|| (matches!(
property,
PropertyIdentifier::LOG_ENABLE | PropertyIdentifier::STOP_WHEN_FULL
) && matches!(value, PropertyValue::Boolean(_))))
.then(|| {
WritePropertyRollback::new(LogLifecycleSnapshot::capture(
&self.log_buffer,
self.log_enable,
self.stop_when_full,
))
})
}
fn restore_write_property_rollback(
&mut self,
rollback: WritePropertyRollback,
) -> Result<(), Error> {
rollback.downcast::<LogLifecycleSnapshot>()?.restore(
&mut self.log_buffer,
&mut self.log_enable,
&mut self.stop_when_full,
);
Ok(())
}
fn log_record_identities_internal(&self) -> Option<Vec<LogRecordIdentity>> {
Some(self.log_buffer.identities())
}
fn add_trend_record(&mut self, record: BACnetLogRecord) {
self.add_record(record);
}
fn try_add_trend_record_internal(&mut self, record: BACnetLogRecord) -> Result<(), Error> {
self.try_add_record_internal(record)
}
}
pub struct TrendLogMultipleObject {
oid: ObjectIdentifier,
name: String,
description: String,
log_enable: bool,
log_interval: u32,
stop_when_full: bool,
buffer_size: u32,
log_buffer: LogRecordBuffer,
status_flags: StatusFlags,
log_device_object_property: Vec<BACnetDeviceObjectPropertyReference>,
logging_type: u32, out_of_service: bool,
reliability: u32,
clock: Option<Arc<dyn ClockReader>>,
}
impl TrendLogMultipleObject {
pub fn new(instance: u32, name: impl Into<String>, buffer_size: u32) -> Result<Self, Error> {
let oid = ObjectIdentifier::new(ObjectType::TREND_LOG_MULTIPLE, instance)?;
Ok(Self {
oid,
name: name.into(),
description: String::new(),
log_enable: true,
log_interval: 0,
stop_when_full: false,
buffer_size,
log_buffer: LogRecordBuffer::new(buffer_size),
status_flags: StatusFlags::empty(),
log_device_object_property: Vec::new(),
logging_type: 0,
out_of_service: false,
reliability: 0,
clock: None,
})
}
pub fn add_record(&mut self, record: BACnetLogRecord) {
let _ = self.try_add_record_internal(record);
}
pub fn add_property_reference(&mut self, reference: BACnetDeviceObjectPropertyReference) {
self.log_device_object_property.push(reference);
}
pub fn records(&self) -> &VecDeque<BACnetLogRecord> {
self.log_buffer.records()
}
pub fn clear(&mut self) {
self.log_buffer.clear();
}
pub fn set_description(&mut self, desc: impl Into<String>) {
self.description = desc.into();
}
pub fn set_logging_type(&mut self, logging_type: u32) {
self.logging_type = logging_type;
}
fn try_add_record_internal(&mut self, record: BACnetLogRecord) -> Result<(), Error> {
self.lifecycle().try_add_ordinary(record).map(|_| ())
}
fn lifecycle(&mut self) -> LogLifecycle<'_> {
LogLifecycle::new(
&mut self.log_buffer,
&mut self.log_enable,
&mut self.stop_when_full,
self.clock.as_ref(),
)
}
}
impl BACnetObject for TrendLogMultipleObject {
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> {
match property {
p if p == PropertyIdentifier::OBJECT_IDENTIFIER => {
Ok(PropertyValue::ObjectIdentifier(self.oid))
}
p if p == PropertyIdentifier::OBJECT_NAME => {
Ok(PropertyValue::CharacterString(self.name.clone()))
}
p if p == PropertyIdentifier::DESCRIPTION => {
Ok(PropertyValue::CharacterString(self.description.clone()))
}
p if p == PropertyIdentifier::OBJECT_TYPE => Ok(PropertyValue::Enumerated(
ObjectType::TREND_LOG_MULTIPLE.to_raw(),
)),
p if p == PropertyIdentifier::LOG_ENABLE => Ok(PropertyValue::Boolean(self.log_enable)),
p if p == PropertyIdentifier::LOG_INTERVAL => {
Ok(PropertyValue::Unsigned(self.log_interval as u64))
}
p if p == PropertyIdentifier::STOP_WHEN_FULL => {
Ok(PropertyValue::Boolean(self.stop_when_full))
}
p if p == PropertyIdentifier::BUFFER_SIZE => {
Ok(PropertyValue::Unsigned(self.buffer_size as u64))
}
p if p == PropertyIdentifier::RECORD_COUNT => {
Ok(PropertyValue::Unsigned(self.records().len() as u64))
}
p if p == PropertyIdentifier::TOTAL_RECORD_COUNT => Ok(PropertyValue::Unsigned(
self.log_buffer.total_record_count() as u64,
)),
p if p == PropertyIdentifier::STATUS_FLAGS => Ok(PropertyValue::BitString {
unused_bits: 4,
data: vec![self.status_flags.bits() << 4],
}),
p if p == PropertyIdentifier::EVENT_STATE => Ok(PropertyValue::Enumerated(0)),
p if p == PropertyIdentifier::OUT_OF_SERVICE => {
Ok(PropertyValue::Boolean(self.out_of_service))
}
p if p == PropertyIdentifier::RELIABILITY => {
Ok(PropertyValue::Enumerated(self.reliability))
}
p if p == PropertyIdentifier::LOG_BUFFER => {
Ok(self.log_buffer.project(LogRecordProfile::TrendMultiple))
}
p if p == PropertyIdentifier::LOGGING_TYPE => {
Ok(PropertyValue::Enumerated(self.logging_type))
}
p if p == PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY => {
let refs: Vec<PropertyValue> = self
.log_device_object_property
.iter()
.map(|r| {
PropertyValue::List(vec![
PropertyValue::ObjectIdentifier(r.object_identifier),
PropertyValue::Unsigned(r.property_identifier as u64),
match r.property_array_index {
Some(idx) => PropertyValue::Unsigned(idx as u64),
None => PropertyValue::Null,
},
match r.device_identifier {
Some(dev) => PropertyValue::ObjectIdentifier(dev),
None => PropertyValue::Null,
},
])
})
.collect();
Ok(PropertyValue::List(refs))
}
p if p == PropertyIdentifier::PROPERTY_LIST => {
read_property_list_property(&self.property_list(), array_index)
}
_ => Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::UNKNOWN_PROPERTY.to_raw() as u32,
}),
}
}
fn write_property(
&mut self,
property: PropertyIdentifier,
_array_index: Option<u32>,
value: PropertyValue,
_priority: Option<u8>,
) -> Result<(), Error> {
if property == PropertyIdentifier::LOG_ENABLE {
if let PropertyValue::Boolean(v) = value {
return self.lifecycle().write_enable(v);
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::LOG_INTERVAL {
if let PropertyValue::Unsigned(v) = value {
self.log_interval = common::u64_to_u32(v)?;
return Ok(());
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::STOP_WHEN_FULL {
if let PropertyValue::Boolean(v) = value {
return self.lifecycle().write_stop_when_full(v);
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::RECORD_COUNT {
if let PropertyValue::Unsigned(0) = value {
return self.lifecycle().purge();
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
if property == PropertyIdentifier::DESCRIPTION {
if let PropertyValue::CharacterString(s) = value {
self.description = s;
return Ok(());
}
return Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::INVALID_DATA_TYPE.to_raw() as u32,
});
}
Err(Error::Protocol {
class: ErrorClass::PROPERTY.to_raw() as u32,
code: ErrorCode::WRITE_ACCESS_DENIED.to_raw() as u32,
})
}
fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
static PROPS: &[PropertyIdentifier] = &[
PropertyIdentifier::OBJECT_IDENTIFIER,
PropertyIdentifier::OBJECT_NAME,
PropertyIdentifier::DESCRIPTION,
PropertyIdentifier::OBJECT_TYPE,
PropertyIdentifier::LOG_ENABLE,
PropertyIdentifier::LOG_INTERVAL,
PropertyIdentifier::STOP_WHEN_FULL,
PropertyIdentifier::BUFFER_SIZE,
PropertyIdentifier::LOG_BUFFER,
PropertyIdentifier::RECORD_COUNT,
PropertyIdentifier::TOTAL_RECORD_COUNT,
PropertyIdentifier::STATUS_FLAGS,
PropertyIdentifier::EVENT_STATE,
PropertyIdentifier::OUT_OF_SERVICE,
PropertyIdentifier::RELIABILITY,
PropertyIdentifier::LOGGING_TYPE,
PropertyIdentifier::LOG_DEVICE_OBJECT_PROPERTY,
];
Cow::Borrowed(PROPS)
}
fn bind_clock_internal(&mut self, clock: Option<Arc<dyn ClockReader>>) {
self.clock = clock;
}
fn is_writable_property(&self, property: PropertyIdentifier) -> bool {
matches!(
property,
PropertyIdentifier::LOG_ENABLE
| PropertyIdentifier::LOG_INTERVAL
| PropertyIdentifier::STOP_WHEN_FULL
| PropertyIdentifier::RECORD_COUNT
| PropertyIdentifier::DESCRIPTION
)
}
fn capture_write_property_rollback(
&mut self,
property: PropertyIdentifier,
value: &PropertyValue,
) -> Option<WritePropertyRollback> {
((property == PropertyIdentifier::RECORD_COUNT
&& matches!(value, PropertyValue::Unsigned(0)))
|| (matches!(
property,
PropertyIdentifier::LOG_ENABLE | PropertyIdentifier::STOP_WHEN_FULL
) && matches!(value, PropertyValue::Boolean(_))))
.then(|| {
WritePropertyRollback::new(LogLifecycleSnapshot::capture(
&self.log_buffer,
self.log_enable,
self.stop_when_full,
))
})
}
fn restore_write_property_rollback(
&mut self,
rollback: WritePropertyRollback,
) -> Result<(), Error> {
rollback.downcast::<LogLifecycleSnapshot>()?.restore(
&mut self.log_buffer,
&mut self.log_enable,
&mut self.stop_when_full,
);
Ok(())
}
fn log_record_identities_internal(&self) -> Option<Vec<LogRecordIdentity>> {
Some(self.log_buffer.identities())
}
fn add_trend_record(&mut self, record: BACnetLogRecord) {
self.add_record(record);
}
fn try_add_trend_record_internal(&mut self, record: BACnetLogRecord) -> Result<(), Error> {
self.try_add_record_internal(record)
}
}
#[cfg(test)]
mod log_record_tests;
#[cfg(test)]
mod tests;