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-01 15:44:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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()),
|
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()),
|
2025-10-02 18:39:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StateVarInstance {
|
|
|
|
|
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()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
*modified = Utc::now();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|