1use std::borrow::Cow;
4use std::collections::VecDeque;
5use std::sync::Arc;
6
7use bacnet_types::bitstring::EventTransitionBits;
8use bacnet_types::constructed::BACnetEventLogRecord;
9use bacnet_types::enums::{NotifyType, ObjectType, PropertyIdentifier, Reliability};
10use bacnet_types::error::Error;
11use bacnet_types::primitives::{Date, ObjectIdentifier, PropertyValue, StatusFlags, Time};
12
13use crate::clock::ClockReader;
14use crate::common::{self, read_common_properties};
15use crate::log_buffer::{
16 log_buffer_read_denied, LogBufferRecords, LogRecordBuffer, LogRecordIdentity,
17};
18use crate::log_lifecycle::LogLifecycle;
19use crate::log_reporting::{impl_buffer_ready_reporting, BufferReadyReporting};
20use crate::log_window::LogWindow;
21use crate::traits::BACnetObject;
22
23mod metadata;
24
25pub struct EventLogObject {
45 oid: ObjectIdentifier,
46 name: String,
47 description: String,
48 log_enable: bool,
49 stop_when_full: bool,
50 buffer_size: u32,
51 log_buffer: LogRecordBuffer<BACnetEventLogRecord>,
52 status_flags: StatusFlags,
53 reliability: Reliability,
54 window: LogWindow,
55 reporting: BufferReadyReporting,
56 log_received_notifications: bool,
57 clock: Option<Arc<dyn ClockReader>>,
58}
59
60impl EventLogObject {
61 pub fn new(instance: u32, name: impl Into<String>, buffer_size: u32) -> Result<Self, Error> {
63 let oid = ObjectIdentifier::new(ObjectType::EVENT_LOG, instance)?;
64 Ok(Self {
65 oid,
66 name: name.into(),
67 description: String::new(),
68 log_enable: true,
69 stop_when_full: false,
70 buffer_size,
71 log_buffer: LogRecordBuffer::new(buffer_size),
72 status_flags: StatusFlags::empty(),
73 reliability: Reliability::NO_FAULT_DETECTED,
74 window: LogWindow::default(),
75 reporting: BufferReadyReporting::default(),
76 log_received_notifications: false,
77 clock: None,
78 })
79 }
80
81 pub fn add_record(&mut self, record: BACnetEventLogRecord) -> Result<(), Error> {
93 self.lifecycle().try_add_ordinary(record).map(|_| ())
94 }
95
96 pub fn records(&self) -> &VecDeque<BACnetEventLogRecord> {
98 self.log_buffer.records()
99 }
100
101 pub fn clear(&mut self) {
103 self.log_buffer.clear();
104 }
105
106 pub fn set_description(&mut self, desc: impl Into<String>) {
108 self.description = desc.into();
109 }
110
111 pub fn set_start_time(&mut self, date: Date, time: Time) -> Result<(), Error> {
120 self.lifecycle()
121 .configure_window(PropertyIdentifier::START_TIME, (date, time))
122 }
123
124 pub fn set_stop_time(&mut self, date: Date, time: Time) -> Result<(), Error> {
128 self.lifecycle()
129 .configure_window(PropertyIdentifier::STOP_TIME, (date, time))
130 }
131
132 pub fn logs_received_notifications(&self) -> bool {
135 self.log_received_notifications
136 }
137
138 pub fn set_log_received_notifications(&mut self, log: bool) {
145 self.log_received_notifications = log;
146 }
147
148 pub fn set_notification_threshold(&mut self, threshold: u32) {
151 self.reporting.set_notification_threshold(threshold);
152 }
153
154 pub fn set_notification_class(&mut self, class: u32) {
157 self.reporting.set_notification_class(class);
158 }
159
160 pub fn set_event_enable(&mut self, enable: EventTransitionBits) {
163 self.reporting.set_event_enable(enable);
164 }
165
166 pub fn set_notify_type(&mut self, notify_type: NotifyType) {
168 self.reporting.set_notify_type(notify_type);
169 }
170
171 fn lifecycle(&mut self) -> LogLifecycle<'_, BACnetEventLogRecord> {
172 LogLifecycle::new(
173 &mut self.log_buffer,
174 &mut self.log_enable,
175 &mut self.stop_when_full,
176 self.clock.as_ref(),
177 &mut self.window,
178 &mut self.reporting,
179 )
180 }
181}
182
183impl BACnetObject for EventLogObject {
184 fn object_identifier(&self) -> ObjectIdentifier {
185 self.oid
186 }
187
188 fn object_name(&self) -> &str {
189 &self.name
190 }
191
192 fn read_property(
193 &self,
194 property: PropertyIdentifier,
195 array_index: Option<u32>,
196 ) -> Result<PropertyValue, Error> {
197 if let Some(result) =
200 read_common_properties!(self, property, array_index, no_out_of_service)
201 {
202 return result;
203 }
204 if let Some(value) = self.window.read(property) {
205 return Ok(value);
206 }
207 let total = self.log_buffer.total_record_count();
208 if let Some(result) = self.reporting.read(property, array_index, total) {
209 return result;
210 }
211 match property {
212 p if p == PropertyIdentifier::OBJECT_TYPE => {
213 Ok(PropertyValue::Enumerated(ObjectType::EVENT_LOG.to_raw()))
214 }
215 p if p == PropertyIdentifier::LOG_ENABLE => Ok(PropertyValue::Boolean(self.log_enable)),
216 p if p == PropertyIdentifier::STOP_WHEN_FULL => {
217 Ok(PropertyValue::Boolean(self.stop_when_full))
218 }
219 p if p == PropertyIdentifier::BUFFER_SIZE => {
220 Ok(PropertyValue::Unsigned(self.buffer_size as u64))
221 }
222 p if p == PropertyIdentifier::LOG_BUFFER => Err(log_buffer_read_denied()),
224 p if p == PropertyIdentifier::RECORD_COUNT => {
225 Ok(PropertyValue::Unsigned(self.records().len() as u64))
226 }
227 p if p == PropertyIdentifier::TOTAL_RECORD_COUNT => {
228 Ok(PropertyValue::Unsigned(u64::from(total)))
229 }
230 _ => Err(common::unknown_property_error()),
231 }
232 }
233
234 fn write_property(
235 &mut self,
236 property: PropertyIdentifier,
237 array_index: Option<u32>,
238 value: PropertyValue,
239 _priority: Option<u8>,
240 ) -> Result<(), Error> {
241 if property == PropertyIdentifier::LOG_ENABLE {
242 if let PropertyValue::Boolean(v) = value {
243 return self.lifecycle().write_enable(v);
244 }
245 return Err(common::invalid_data_type_error());
246 }
247 if let Some(result) = self.window.write(property, &value) {
248 result?;
250 self.lifecycle().refresh_window();
251 return Ok(());
252 }
253 if property == PropertyIdentifier::STOP_WHEN_FULL {
254 if let PropertyValue::Boolean(v) = value {
255 return self.lifecycle().write_stop_when_full(v);
256 }
257 return Err(common::invalid_data_type_error());
258 }
259 if property == PropertyIdentifier::RECORD_COUNT {
260 if let PropertyValue::Unsigned(0) = value {
261 return self.lifecycle().purge();
262 }
263 return Err(common::invalid_data_type_error());
264 }
265 let total = self.log_buffer.total_record_count();
266 if let Some(result) = self.reporting.write(property, array_index, &value, total) {
267 return result;
268 }
269 if let Some(result) = common::write_description(&mut self.description, property, &value) {
270 return result;
271 }
272 Err(crate::common::unhandled_write_error(
273 self.property_metadata().as_ref(),
274 property,
275 array_index,
276 ))
277 }
278
279 fn property_metadata(&self) -> Cow<'_, [crate::property_metadata::PropertyMetadata]> {
280 metadata::for_object(self)
281 }
282
283 fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
284 crate::property_metadata::property_list_from_metadata(self.property_metadata().as_ref())
285 }
286
287 fn bind_clock_internal(&mut self, clock: Option<Arc<dyn ClockReader>>) {
288 self.clock = clock;
289 }
290
291 fn log_record_identities_internal(&self) -> Option<Vec<LogRecordIdentity>> {
292 Some(self.log_buffer.identities())
293 }
294
295 fn log_buffer_internal(&self) -> Option<&dyn LogBufferRecords> {
296 Some(&self.log_buffer)
297 }
298
299 fn add_event_log_record(&mut self, record: BACnetEventLogRecord) -> Result<(), Error> {
300 self.add_record(record)
301 }
302
303 fn refresh_log_window_internal(&mut self) -> bool {
304 self.lifecycle().refresh_window()
305 }
306
307 fn logs_received_event_notifications_internal(&self) -> bool {
308 self.log_received_notifications
309 }
310
311 impl_buffer_ready_reporting!(reporting, log_buffer);
312}
313
314#[cfg(test)]
315mod tests;