Files
pmomusic/pmoupnp/src/state_variables/instance_methods.rs

160 lines
5.0 KiB
Rust
Raw Normal View History

2025-10-02 18:39:32 +02:00
use std::fmt;
2025-09-16 21:07:44 +02:00
use chrono::{DateTime, Utc};
2025-10-03 21:19:14 +02:00
use std::sync::RwLock;
2025-09-16 21:07:44 +02:00
use xmltree::Element;
use crate::{
2025-10-03 21:19:14 +02:00
object_trait::{UpnpInstance, UpnpObject},
state_variables::{StateVarInstance, StateVariable, UpnpVariable},
variable_types::{StateValue, StateValueError, UpnpVarType},
UpnpObjectType, UpnpTyped, UpnpTypedInstance
2025-09-16 21:07:44 +02:00
};
impl UpnpVariable for StateVarInstance {
fn get_definition(&self) -> &StateVariable {
2025-10-02 18:39:32 +02:00
return &self.model;
2025-09-16 21:07:44 +02:00
}
}
2025-10-02 18:39:32 +02:00
impl UpnpObject for StateVarInstance {
2025-10-03 21:19:14 +02:00
fn to_xml_element(&self) -> Element {
self.get_definition().to_xml_element()
}
}
2025-10-02 18:39:32 +02:00
impl UpnpVarType for StateVarInstance {
fn as_state_var_type(&self) -> crate::variable_types::StateVarType {
self.get_definition().as_state_var_type()
2025-09-16 21:07:44 +02:00
}
}
2025-10-02 18:39:32 +02:00
impl UpnpInstance for StateVarInstance {
type Model = StateVariable;
fn new(from: &StateVariable) -> Self {
2025-09-16 21:07:44 +02:00
Self {
object: UpnpObjectType {
name: from.object.name.clone(),
object_type: "StateVarInstance".to_string(),
},
2025-10-02 18:39:32 +02:00
model: from.clone(),
value: RwLock::new(from.get_default()),
old_value: RwLock::new(from.get_default()),
last_modified: RwLock::new(Utc::now()),
last_notification: RwLock::new(Utc::now()),
service: RwLock::new(None),
2025-09-16 21:07:44 +02:00
}
}
2025-10-02 18:39:32 +02:00
}
impl UpnpTyped for StateVarInstance {
fn as_upnp_object_type(&self) -> &UpnpObjectType {
return &self.object;
2025-09-16 21:07:44 +02:00
}
2025-10-02 18:39:32 +02:00
}
impl UpnpTypedInstance for StateVarInstance {
2025-09-16 21:07:44 +02:00
2025-10-02 18:39:32 +02:00
fn get_model(&self) -> &Self::Model {
&self.model
}
}
impl fmt::Debug for StateVarInstance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StateVarInstance")
.field("object", &self.object)
.field("model", &self.model)
.field("value", &self.value)
.field("old_value", &self.old_value)
.field("last_modified", &self.last_modified)
.field("last_notification", &self.last_notification)
.finish()
}
}
impl Clone for StateVarInstance {
fn clone(&self) -> Self {
Self {
object: self.object.clone(),
model: self.model.clone(),
2025-10-03 21:19:14 +02:00
value: RwLock::new(self.value.read().unwrap().clone()),
old_value: RwLock::new(self.old_value.read().unwrap().clone()),
last_modified: RwLock::new(self.last_modified.read().unwrap().clone()),
last_notification: RwLock::new(self.last_notification.read().unwrap().clone()),
service: RwLock::new(self.service.read().unwrap().clone()),
2025-10-02 18:39:32 +02:00
}
}
}
impl StateVarInstance {
/// Enregistre le service parent pour cette variable.
///
/// Cette méthode doit être appelée depuis `ServiceInstance::new()` pour
/// permettre à la variable de notifier le service lorsqu'elle change.
///
/// # Arguments
///
/// * `service` - Arc vers le ServiceInstance parent
///
/// # Examples
///
/// ```rust,ignore
/// # use pmoupnp::services::ServiceInstance;
/// # use pmoupnp::state_variables::StateVarInstance;
/// # use std::sync::Arc;
/// let service_instance = Arc::new(ServiceInstance::new(&service));
/// let var_instance = Arc::new(StateVarInstance::new(&variable));
/// var_instance.register_service(Arc::downgrade(&service_instance));
/// ```
pub fn register_service(&self, service: std::sync::Weak<crate::services::ServiceInstance>) {
let mut svc = self.service.write().unwrap();
*svc = Some(service);
}
2025-10-02 18:39:32 +02:00
pub async fn set_value(&self, new_value: StateValue) -> Result<(), StateValueError> {
// Validation du type
if self.as_state_var_type() != new_value.as_state_var_type() {
return Err(StateValueError::TypeError(
"Value type mismatch".to_string()
));
}
2025-10-02 18:39:32 +02:00
// Mise à jour avec les locks
2025-10-03 21:19:14 +02:00
let mut old_val = self.old_value.write().unwrap();
let mut val = self.value.write().unwrap();
let mut modified = self.last_modified.write().unwrap();
2025-10-02 18:39:32 +02:00
*old_val = val.clone();
*val = new_value.clone();
2025-10-02 18:39:32 +02:00
*modified = Utc::now();
// Notifier le service parent si la variable envoie des événements
if self.is_sending_notification() {
// Relâcher les locks avant d'appeler le service
drop(val);
drop(old_val);
drop(modified);
if let Some(weak_service) = self.service.read().unwrap().as_ref() {
if let Some(service) = weak_service.upgrade() {
service.event_to_be_sent(self.get_name().to_string(), new_value.to_string());
}
}
}
2025-10-02 18:39:32 +02:00
Ok(())
}
2025-09-16 21:07:44 +02:00
/// Accès à la valeur
2025-10-02 18:39:32 +02:00
pub fn value(&self) -> StateValue {
2025-10-03 21:19:14 +02:00
self.value.read().unwrap().clone()
2025-09-16 21:07:44 +02:00
}
/// Accès au timestamp
pub fn last_modified(&self) -> DateTime<Utc> {
2025-10-03 21:19:14 +02:00
self.last_modified.read().unwrap().clone()
2025-09-16 21:07:44 +02:00
}
}