bacnet-objects 0.11.0

BACnet object model: traits, database, and standard object types
Documentation
//! Lighting Output (type 54) and Binary Lighting Output (type 55) objects per
//! ASHRAE 135-2020 Clauses 12.54 and 12.55.

use bacnet_types::enums::{ObjectType, PropertyIdentifier};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue, StatusFlags};
use std::borrow::Cow;

use crate::common::{
    self, read_common_properties, read_priority_array, write_priority_array,
    write_priority_array_direct,
};
use crate::traits::BACnetObject;

// ---------------------------------------------------------------------------
// LightingOutput (type 54)
// ---------------------------------------------------------------------------

/// BACnet Lighting Output object.
///
/// Commandable output with a 16-level priority array controlling a
/// floating-point present-value (0.0 to 100.0 percent).
pub struct LightingOutputObject {
    oid: ObjectIdentifier,
    name: String,
    description: String,
    present_value: f32,
    tracking_value: f32,
    /// Stored as opaque OctetString for now (BACnetLightingCommand encoding).
    lighting_command: Vec<u8>,
    lighting_command_default_priority: u32,
    /// LightingInProgress enumeration: 0=idle, 1=fade-active, 2=ramp-active, 3=not-controlled, etc.
    in_progress: u32,
    blink_warn_enable: bool,
    egress_time: u32,
    egress_active: bool,
    out_of_service: bool,
    status_flags: StatusFlags,
    /// Reliability: 0 = NO_FAULT_DETECTED.
    reliability: u32,
    priority_array: [Option<f32>; 16],
    relinquish_default: f32,
}

impl LightingOutputObject {
    /// Create a new Lighting Output object.
    pub fn new(instance: u32, name: impl Into<String>) -> Result<Self, Error> {
        let oid = ObjectIdentifier::new(ObjectType::LIGHTING_OUTPUT, instance)?;
        Ok(Self {
            oid,
            name: name.into(),
            description: String::new(),
            present_value: 0.0,
            tracking_value: 0.0,
            lighting_command: Vec::new(),
            lighting_command_default_priority: 16,
            in_progress: 0, // idle
            blink_warn_enable: false,
            egress_time: 0,
            egress_active: false,
            out_of_service: false,
            status_flags: StatusFlags::empty(),
            reliability: 0,
            priority_array: [None; 16],
            relinquish_default: 0.0,
        })
    }

    /// Set the description string.
    pub fn set_description(&mut self, desc: impl Into<String>) {
        self.description = desc.into();
    }

    /// Recalculate present-value from the priority array.
    fn recalculate_present_value(&mut self) {
        self.present_value =
            common::recalculate_from_priority_array(&self.priority_array, self.relinquish_default);
    }

    /// Set the Relinquish_Default (#270).
    ///
    /// Validated the same way a commanded Present_Value is (finite Real
    /// within the 0..=100 light level); after the store, Present_Value is
    /// resolved anew from the priority array so an empty array falls back to
    /// the new default immediately.
    pub fn set_relinquish_default(&mut self, value: f32) -> Result<(), Error> {
        if !value.is_finite() || !(0.0..=100.0).contains(&value) {
            return Err(common::value_out_of_range_error());
        }
        self.relinquish_default = value;
        self.recalculate_present_value();
        Ok(())
    }
}

impl BACnetObject for LightingOutputObject {
    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> {
        if let Some(result) = read_common_properties!(self, property, array_index) {
            return result;
        }
        match property {
            p if p == PropertyIdentifier::OBJECT_TYPE => Ok(PropertyValue::Enumerated(
                ObjectType::LIGHTING_OUTPUT.to_raw(),
            )),
            p if p == PropertyIdentifier::PRESENT_VALUE => {
                Ok(PropertyValue::Real(self.present_value))
            }
            p if p == PropertyIdentifier::TRACKING_VALUE => {
                Ok(PropertyValue::Real(self.tracking_value))
            }
            p if p == PropertyIdentifier::LIGHTING_COMMAND => {
                Ok(PropertyValue::OctetString(self.lighting_command.clone()))
            }
            p if p == PropertyIdentifier::LIGHTING_COMMAND_DEFAULT_PRIORITY => Ok(
                PropertyValue::Unsigned(self.lighting_command_default_priority as u64),
            ),
            p if p == PropertyIdentifier::IN_PROGRESS => {
                Ok(PropertyValue::Enumerated(self.in_progress))
            }
            p if p == PropertyIdentifier::BLINK_WARN_ENABLE => {
                Ok(PropertyValue::Boolean(self.blink_warn_enable))
            }
            p if p == PropertyIdentifier::EGRESS_TIME => {
                Ok(PropertyValue::Unsigned(self.egress_time as u64))
            }
            p if p == PropertyIdentifier::EGRESS_ACTIVE => {
                Ok(PropertyValue::Boolean(self.egress_active))
            }
            p if p == PropertyIdentifier::PRIORITY_ARRAY => {
                read_priority_array!(self, array_index, PropertyValue::Real)
            }
            p if p == PropertyIdentifier::RELINQUISH_DEFAULT => {
                Ok(PropertyValue::Real(self.relinquish_default))
            }
            p if p == PropertyIdentifier::DEFAULT_FADE_TIME => Ok(PropertyValue::Unsigned(0)),
            _ => Err(common::unknown_property_error()),
        }
    }

    fn write_property(
        &mut self,
        property: PropertyIdentifier,
        array_index: Option<u32>,
        value: PropertyValue,
        priority: Option<u8>,
    ) -> Result<(), Error> {
        // Direct writes to PRIORITY_ARRAY[index]
        write_priority_array_direct!(self, property, array_index, value, |v| {
            match v {
                PropertyValue::Real(f) => {
                    if !(0.0..=100.0).contains(&f) {
                        Err(common::value_out_of_range_error())
                    } else {
                        Ok(f)
                    }
                }
                _ => Err(common::invalid_data_type_error()),
            }
        });

        // PRESENT_VALUE — commandable via priority array
        if property == PropertyIdentifier::PRESENT_VALUE {
            return write_priority_array!(self, value, priority, |v| {
                match v {
                    PropertyValue::Real(f) => {
                        if !(0.0..=100.0).contains(&f) {
                            Err(common::value_out_of_range_error())
                        } else {
                            Ok(f)
                        }
                    }
                    _ => Err(common::invalid_data_type_error()),
                }
            });
        }

        // LIGHTING_COMMAND — stored as opaque bytes
        if property == PropertyIdentifier::LIGHTING_COMMAND {
            if let PropertyValue::OctetString(data) = value {
                self.lighting_command = data;
                return Ok(());
            }
            return Err(common::invalid_data_type_error());
        }

        // LIGHTING_COMMAND_DEFAULT_PRIORITY
        if property == PropertyIdentifier::LIGHTING_COMMAND_DEFAULT_PRIORITY {
            if let PropertyValue::Unsigned(v) = value {
                if !(1..=16).contains(&v) {
                    return Err(common::value_out_of_range_error());
                }
                self.lighting_command_default_priority = v as u32;
                return Ok(());
            }
            return Err(common::invalid_data_type_error());
        }

        // RELINQUISH_DEFAULT — writable per Table 12-64 (R; the standard
        // permits writability), validated the same way a commanded
        // Present_Value is by the shared setter.
        if property == PropertyIdentifier::RELINQUISH_DEFAULT {
            if let PropertyValue::Real(f) = value {
                return self.set_relinquish_default(f);
            }
            return Err(common::invalid_data_type_error());
        }

        // BLINK_WARN_ENABLE
        if property == PropertyIdentifier::BLINK_WARN_ENABLE {
            if let PropertyValue::Boolean(v) = value {
                self.blink_warn_enable = v;
                return Ok(());
            }
            return Err(common::invalid_data_type_error());
        }

        // EGRESS_TIME
        if property == PropertyIdentifier::EGRESS_TIME {
            if let PropertyValue::Unsigned(v) = value {
                self.egress_time = common::u64_to_u32(v)?;
                return Ok(());
            }
            return Err(common::invalid_data_type_error());
        }

        if let Some(result) =
            common::write_out_of_service(&mut self.out_of_service, property, &value)
        {
            return result;
        }
        if let Some(result) = common::write_description(&mut self.description, property, &value) {
            return result;
        }
        Err(common::write_access_denied_error())
    }

    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        static PROPS: &[PropertyIdentifier] = &[
            PropertyIdentifier::OBJECT_IDENTIFIER,
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::DESCRIPTION,
            PropertyIdentifier::OBJECT_TYPE,
            PropertyIdentifier::PRESENT_VALUE,
            PropertyIdentifier::TRACKING_VALUE,
            PropertyIdentifier::LIGHTING_COMMAND,
            PropertyIdentifier::LIGHTING_COMMAND_DEFAULT_PRIORITY,
            PropertyIdentifier::IN_PROGRESS,
            PropertyIdentifier::BLINK_WARN_ENABLE,
            PropertyIdentifier::EGRESS_TIME,
            PropertyIdentifier::EGRESS_ACTIVE,
            PropertyIdentifier::STATUS_FLAGS,
            PropertyIdentifier::OUT_OF_SERVICE,
            PropertyIdentifier::RELIABILITY,
            PropertyIdentifier::PRIORITY_ARRAY,
            PropertyIdentifier::RELINQUISH_DEFAULT,
        ];
        Cow::Borrowed(PROPS)
    }

    fn supports_cov(&self) -> bool {
        true
    }

    fn is_writable_property(&self, property: PropertyIdentifier) -> bool {
        // Mirrors the LightingOutputObject `write_property` arms so the PICS
        // and runtime dispatch share one truth source.
        matches!(
            property,
            PropertyIdentifier::PRIORITY_ARRAY
                | PropertyIdentifier::PRESENT_VALUE
                | PropertyIdentifier::RELINQUISH_DEFAULT
                | PropertyIdentifier::LIGHTING_COMMAND
                | PropertyIdentifier::LIGHTING_COMMAND_DEFAULT_PRIORITY
                | PropertyIdentifier::BLINK_WARN_ENABLE
                | PropertyIdentifier::EGRESS_TIME
                | PropertyIdentifier::OUT_OF_SERVICE
                | PropertyIdentifier::DESCRIPTION
        )
    }
}

mod binary;
pub use binary::BinaryLightingOutputObject;

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests;