Mountain/ApplicationState/State/FeatureState/OutputChannels/
OutputChannelState.rs1use std::{
34 collections::HashMap,
35 sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::OutputChannelStateDTO::OutputChannelStateDTO;
41
42#[derive(Clone)]
44pub struct OutputChannelState {
45 pub OutputChannels:Arc<StandardMutex<HashMap<String, OutputChannelStateDTO>>>,
47}
48
49impl Default for OutputChannelState {
50 fn default() -> Self {
51 debug!("[OutputChannelState] Initializing default output channel state...");
52
53 Self { OutputChannels:Arc::new(StandardMutex::new(HashMap::new())) }
54 }
55}
56
57impl OutputChannelState {
58 pub fn GetAll(&self) -> HashMap<String, OutputChannelStateDTO> {
60 self.OutputChannels.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
61 }
62
63 pub fn Get(&self, id:&str) -> Option<OutputChannelStateDTO> {
65 self.OutputChannels.lock().ok().and_then(|guard| guard.get(id).cloned())
66 }
67
68 pub fn AddOrUpdate(&self, id:String, channel:OutputChannelStateDTO) {
70 if let Ok(mut guard) = self.OutputChannels.lock() {
71 guard.insert(id, channel);
72 debug!("[OutputChannelState] Output channel added/updated");
73 }
74 }
75
76 pub fn Remove(&self, id:&str) {
78 if let Ok(mut guard) = self.OutputChannels.lock() {
79 guard.remove(id);
80 debug!("[OutputChannelState] Output channel removed: {}", id);
81 }
82 }
83
84 pub fn Clear(&self) {
86 if let Ok(mut guard) = self.OutputChannels.lock() {
87 guard.clear();
88 debug!("[OutputChannelState] All output channels cleared");
89 }
90 }
91
92 pub fn Count(&self) -> usize { self.OutputChannels.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
94
95 pub fn Contains(&self, id:&str) -> bool {
97 self.OutputChannels
98 .lock()
99 .ok()
100 .map(|guard| guard.contains_key(id))
101 .unwrap_or(false)
102 }
103}