use core::fmt;
use super::{Cluster, ClusterId, DeviceType, EndptId, SemanticTag};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Endpoint<'a> {
pub id: EndptId,
pub device_types: &'a [DeviceType],
pub clusters: &'a [Cluster<'a>],
pub client_clusters: &'a [ClusterId],
pub unique_id: Option<&'a str>,
pub semantic_tags: &'a [SemanticTag<'a>],
}
impl<'a> Endpoint<'a> {
pub const fn new(
id: EndptId,
device_types: &'a [DeviceType],
clusters: &'a [Cluster<'a>],
) -> Self {
Self {
id,
device_types,
clusters,
client_clusters: &[],
unique_id: None,
semantic_tags: &[],
}
}
pub const fn new_with_clients(
id: EndptId,
device_types: &'a [DeviceType],
clusters: &'a [Cluster<'a>],
client_clusters: &'a [ClusterId],
) -> Self {
Self {
id,
device_types,
clusters,
client_clusters,
unique_id: None,
semantic_tags: &[],
}
}
pub const fn with_tags(self, semantic_tags: &'a [SemanticTag<'a>]) -> Self {
Self {
id: self.id,
device_types: self.device_types,
clusters: self.clusters,
client_clusters: self.client_clusters,
unique_id: self.unique_id,
semantic_tags,
}
}
pub const fn with_unique_id(self, unique_id: &'a str) -> Self {
Self {
id: self.id,
device_types: self.device_types,
clusters: self.clusters,
client_clusters: self.client_clusters,
unique_id: Some(unique_id),
semantic_tags: self.semantic_tags,
}
}
pub fn cluster(&self, id: ClusterId) -> Option<&Cluster<'a>> {
self.clusters.iter().find(|cluster| cluster.id == id)
}
}
impl core::fmt::Display for Endpoint<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "clusters:[")?;
let mut comma = "";
for cluster in self.clusters {
write!(f, "{} {{ {} }}", comma, cluster)?;
comma = ", ";
}
write!(f, "]")
}
}