use bacnet_types::enums::{
EscalatorFault, EscalatorMode, EscalatorOperationDirection, ObjectType, PropertyIdentifier,
};
use bacnet_types::error::Error;
use bacnet_types::primitives::{ObjectIdentifier, PropertyValue, StatusFlags};
use std::{borrow::Cow, collections::HashSet};
use crate::common::{self, read_common_properties};
use crate::traits::BACnetObject;
pub struct ElevatorGroupObject {
oid: ObjectIdentifier,
name: String,
description: String,
group_id: u64,
group_members: Vec<ObjectIdentifier>,
group_mode: u32,
landing_calls: u64,
landing_call_control: u32,
status_flags: StatusFlags,
out_of_service: bool,
reliability: u32,
}
impl ElevatorGroupObject {
pub fn new(instance: u32, name: impl Into<String>) -> Result<Self, Error> {
let oid = ObjectIdentifier::new(ObjectType::ELEVATOR_GROUP, instance)?;
Ok(Self {
oid,
name: name.into(),
description: String::new(),
group_id: 0,
group_members: Vec::new(),
group_mode: 0, landing_calls: 0,
landing_call_control: 0,
status_flags: StatusFlags::empty(),
out_of_service: false,
reliability: 0,
})
}
pub fn add_member(&mut self, oid: ObjectIdentifier) {
self.group_members.push(oid);
}
}
impl BACnetObject for ElevatorGroupObject {
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::ELEVATOR_GROUP.to_raw(),
)),
p if p == PropertyIdentifier::GROUP_ID => Ok(PropertyValue::Unsigned(self.group_id)),
p if p == PropertyIdentifier::GROUP_MEMBERS => {
let items: Vec<PropertyValue> = self
.group_members
.iter()
.map(|oid| PropertyValue::ObjectIdentifier(*oid))
.collect();
Ok(PropertyValue::List(items))
}
p if p == PropertyIdentifier::GROUP_MODE => {
Ok(PropertyValue::Enumerated(self.group_mode))
}
p if p == PropertyIdentifier::LANDING_CALLS => {
Ok(PropertyValue::Unsigned(self.landing_calls))
}
p if p == PropertyIdentifier::LANDING_CALL_CONTROL => {
Ok(PropertyValue::Enumerated(self.landing_call_control))
}
_ => Err(common::unknown_property_error()),
}
}
fn write_property(
&mut self,
property: PropertyIdentifier,
_array_index: Option<u32>,
value: PropertyValue,
_priority: Option<u8>,
) -> Result<(), 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;
}
match property {
p if p == PropertyIdentifier::GROUP_ID => {
if let PropertyValue::Unsigned(v) = value {
self.group_id = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::GROUP_MODE => {
if let PropertyValue::Enumerated(v) = value {
self.group_mode = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::LANDING_CALL_CONTROL => {
if let PropertyValue::Enumerated(v) = value {
self.landing_call_control = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
_ => 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::GROUP_ID,
PropertyIdentifier::GROUP_MEMBERS,
PropertyIdentifier::GROUP_MODE,
PropertyIdentifier::LANDING_CALLS,
PropertyIdentifier::LANDING_CALL_CONTROL,
PropertyIdentifier::STATUS_FLAGS,
PropertyIdentifier::OUT_OF_SERVICE,
PropertyIdentifier::RELIABILITY,
];
Cow::Borrowed(PROPS)
}
}
pub struct EscalatorObject {
oid: ObjectIdentifier,
name: String,
description: String,
escalator_mode: EscalatorMode,
fault_signals: Vec<EscalatorFault>,
energy_meter: f32,
energy_meter_ref: Vec<u8>,
power_mode: bool,
operation_direction: EscalatorOperationDirection,
passenger_alarm: bool,
status_flags: StatusFlags,
out_of_service: bool,
reliability: u32,
}
impl EscalatorObject {
pub fn new(instance: u32, name: impl Into<String>) -> Result<Self, Error> {
let oid = ObjectIdentifier::new(ObjectType::ESCALATOR, instance)?;
Ok(Self {
oid,
name: name.into(),
description: String::new(),
escalator_mode: EscalatorMode::UNKNOWN,
fault_signals: Vec::new(),
energy_meter: 0.0,
energy_meter_ref: Vec::new(),
power_mode: false,
operation_direction: EscalatorOperationDirection::UNKNOWN,
passenger_alarm: false,
status_flags: StatusFlags::empty(),
out_of_service: false,
reliability: 0,
})
}
}
impl BACnetObject for EscalatorObject {
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::ESCALATOR.to_raw()))
}
p if p == PropertyIdentifier::ESCALATOR_MODE => {
Ok(PropertyValue::Enumerated(self.escalator_mode.to_raw()))
}
p if p == PropertyIdentifier::FAULT_SIGNALS => {
let items: Vec<PropertyValue> = self
.fault_signals
.iter()
.map(|v| PropertyValue::Enumerated(v.to_raw()))
.collect();
Ok(PropertyValue::List(items))
}
p if p == PropertyIdentifier::ENERGY_METER => {
Ok(PropertyValue::Real(self.energy_meter))
}
p if p == PropertyIdentifier::ENERGY_METER_REF => {
Ok(PropertyValue::OctetString(self.energy_meter_ref.clone()))
}
p if p == PropertyIdentifier::POWER_MODE => Ok(PropertyValue::Boolean(self.power_mode)),
p if p == PropertyIdentifier::OPERATION_DIRECTION => {
Ok(PropertyValue::Enumerated(self.operation_direction.to_raw()))
}
p if p == PropertyIdentifier::PASSENGER_ALARM => {
Ok(PropertyValue::Boolean(self.passenger_alarm))
}
_ => Err(common::unknown_property_error()),
}
}
fn write_property(
&mut self,
property: PropertyIdentifier,
_array_index: Option<u32>,
value: PropertyValue,
_priority: Option<u8>,
) -> Result<(), 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;
}
match property {
p if p == PropertyIdentifier::POWER_MODE => {
if let PropertyValue::Boolean(v) = value {
self.power_mode = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::ESCALATOR_MODE => {
if let PropertyValue::Enumerated(v) = value {
let named = EscalatorMode::ALL_NAMED
.iter()
.any(|&(_, value)| value.to_raw() == v);
if !(named || (1024..=65535).contains(&v)) {
return Err(common::value_out_of_range_error());
}
self.escalator_mode = EscalatorMode::from_raw(v);
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::OPERATION_DIRECTION => {
if let PropertyValue::Enumerated(v) = value {
let named = EscalatorOperationDirection::ALL_NAMED
.iter()
.any(|&(_, value)| value.to_raw() == v);
if !(named || (1024..=65535).contains(&v)) {
return Err(common::value_out_of_range_error());
}
self.operation_direction = EscalatorOperationDirection::from_raw(v);
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::ENERGY_METER => {
if let PropertyValue::Real(v) = value {
if !v.is_finite() {
return Err(common::value_out_of_range_error());
}
self.energy_meter = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::FAULT_SIGNALS => {
let values = match value {
PropertyValue::Enumerated(v) => vec![PropertyValue::Enumerated(v)],
PropertyValue::List(values) => values,
_ => return Err(common::invalid_data_type_error()),
};
let mut faults = Vec::with_capacity(values.len());
let mut seen = HashSet::with_capacity(values.len());
for value in values {
let PropertyValue::Enumerated(raw) = value else {
return Err(common::invalid_data_type_error());
};
let named = EscalatorFault::ALL_NAMED
.iter()
.any(|&(_, value)| value.to_raw() == raw);
if !(named || (1024..=65535).contains(&raw)) {
return Err(common::value_out_of_range_error());
}
let fault = EscalatorFault::from_raw(raw);
if !seen.insert(fault) {
return Err(common::value_out_of_range_error());
}
faults.push(fault);
}
self.fault_signals = faults;
Ok(())
}
p if p == PropertyIdentifier::PASSENGER_ALARM => {
if let PropertyValue::Boolean(v) = value {
self.passenger_alarm = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
_ => 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::ESCALATOR_MODE,
PropertyIdentifier::FAULT_SIGNALS,
PropertyIdentifier::ENERGY_METER,
PropertyIdentifier::ENERGY_METER_REF,
PropertyIdentifier::POWER_MODE,
PropertyIdentifier::OPERATION_DIRECTION,
PropertyIdentifier::PASSENGER_ALARM,
PropertyIdentifier::STATUS_FLAGS,
PropertyIdentifier::OUT_OF_SERVICE,
PropertyIdentifier::RELIABILITY,
];
Cow::Borrowed(PROPS)
}
fn is_writable_property(&self, property: PropertyIdentifier) -> bool {
matches!(
property,
PropertyIdentifier::DESCRIPTION
| PropertyIdentifier::OUT_OF_SERVICE
| PropertyIdentifier::POWER_MODE
| PropertyIdentifier::OPERATION_DIRECTION
| PropertyIdentifier::ESCALATOR_MODE
| PropertyIdentifier::ENERGY_METER
| PropertyIdentifier::FAULT_SIGNALS
| PropertyIdentifier::PASSENGER_ALARM
)
}
}
pub struct LiftObject {
oid: ObjectIdentifier,
name: String,
description: String,
tracking_value: u64,
car_position: u64,
car_moving_direction: u32,
car_door_status: Vec<u64>,
car_load: u64,
landing_doors: u64,
floor_text: Vec<String>,
energy_meter: f32,
status_flags: StatusFlags,
out_of_service: bool,
reliability: u32,
}
impl LiftObject {
pub fn new(instance: u32, name: impl Into<String>, num_floors: usize) -> Result<Self, Error> {
let oid = ObjectIdentifier::new(ObjectType::LIFT, instance)?;
let floor_text = (1..=num_floors).map(|i| format!("Floor {i}")).collect();
Ok(Self {
oid,
name: name.into(),
description: String::new(),
tracking_value: 1,
car_position: 1,
car_moving_direction: 1, car_door_status: Vec::new(),
car_load: 0,
landing_doors: num_floors as u64,
floor_text,
energy_meter: 0.0,
status_flags: StatusFlags::empty(),
out_of_service: false,
reliability: 0,
})
}
}
impl BACnetObject for LiftObject {
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::LIFT.to_raw()))
}
p if p == PropertyIdentifier::TRACKING_VALUE => {
Ok(PropertyValue::Unsigned(self.tracking_value))
}
p if p == PropertyIdentifier::CAR_POSITION => {
Ok(PropertyValue::Unsigned(self.car_position))
}
p if p == PropertyIdentifier::CAR_MOVING_DIRECTION => {
Ok(PropertyValue::Enumerated(self.car_moving_direction))
}
p if p == PropertyIdentifier::CAR_DOOR_STATUS => {
let items: Vec<PropertyValue> = self
.car_door_status
.iter()
.map(|v| PropertyValue::Unsigned(*v))
.collect();
Ok(PropertyValue::List(items))
}
p if p == PropertyIdentifier::CAR_LOAD => Ok(PropertyValue::Unsigned(self.car_load)),
p if p == PropertyIdentifier::LANDING_DOOR_STATUS => {
Ok(PropertyValue::Unsigned(self.landing_doors))
}
p if p == PropertyIdentifier::FLOOR_TEXT => {
let items: Vec<PropertyValue> = self
.floor_text
.iter()
.map(|s| PropertyValue::CharacterString(s.clone()))
.collect();
Ok(PropertyValue::List(items))
}
p if p == PropertyIdentifier::ENERGY_METER => {
Ok(PropertyValue::Real(self.energy_meter))
}
p if p == PropertyIdentifier::FLOOR_NUMBER => {
Ok(PropertyValue::Unsigned(self.tracking_value))
}
_ => Err(common::unknown_property_error()),
}
}
fn write_property(
&mut self,
property: PropertyIdentifier,
_array_index: Option<u32>,
value: PropertyValue,
_priority: Option<u8>,
) -> Result<(), 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;
}
match property {
p if p == PropertyIdentifier::TRACKING_VALUE => {
if let PropertyValue::Unsigned(v) = value {
self.tracking_value = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::CAR_POSITION => {
if let PropertyValue::Unsigned(v) = value {
self.car_position = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::CAR_MOVING_DIRECTION => {
if let PropertyValue::Enumerated(v) = value {
if v > 3 {
return Err(common::value_out_of_range_error());
}
self.car_moving_direction = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
p if p == PropertyIdentifier::CAR_LOAD => {
if let PropertyValue::Unsigned(v) = value {
if v > 100 {
return Err(common::value_out_of_range_error());
}
self.car_load = v;
Ok(())
} else {
Err(common::invalid_data_type_error())
}
}
_ => 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::TRACKING_VALUE,
PropertyIdentifier::CAR_POSITION,
PropertyIdentifier::CAR_MOVING_DIRECTION,
PropertyIdentifier::CAR_DOOR_STATUS,
PropertyIdentifier::CAR_LOAD,
PropertyIdentifier::LANDING_DOOR_STATUS,
PropertyIdentifier::FLOOR_TEXT,
PropertyIdentifier::ENERGY_METER,
PropertyIdentifier::STATUS_FLAGS,
PropertyIdentifier::OUT_OF_SERVICE,
PropertyIdentifier::RELIABILITY,
];
Cow::Borrowed(PROPS)
}
}
#[cfg(test)]
mod tests;