Initial working implementation (MVP)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
use rmon_common::signal::{SignalId, SignalInfo, Sample};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub struct SignalHistory {
|
||||
pub info: SignalInfo,
|
||||
pub samples: VecDeque<Sample>,
|
||||
pub max_samples: usize,
|
||||
}
|
||||
|
||||
impl SignalHistory {
|
||||
pub fn new(info: SignalInfo) -> Self {
|
||||
Self {
|
||||
info,
|
||||
samples: VecDeque::new(),
|
||||
max_samples: 1000, // Default for display
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, sample: Sample) {
|
||||
self.samples.push_back(sample);
|
||||
if self.samples.len() > self.max_samples {
|
||||
self.samples.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SessionState {
|
||||
pub connected: bool,
|
||||
pub signals: HashMap<SignalId, Arc<Mutex<SignalHistory>>>,
|
||||
}
|
||||
|
||||
impl SessionState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
connected: false,
|
||||
signals: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rmon_common::signal::SourceType;
|
||||
|
||||
#[test]
|
||||
fn test_signal_history_push() {
|
||||
let info = SignalInfo {
|
||||
id: "test://1".to_string(),
|
||||
label: "L".to_string(),
|
||||
unit: "U".to_string(),
|
||||
scale: 1.0,
|
||||
offset: 0.0,
|
||||
sampling_period: None,
|
||||
source_type: SourceType::Csv,
|
||||
path: vec![],
|
||||
};
|
||||
let mut history = SignalHistory::new(info);
|
||||
history.max_samples = 2;
|
||||
|
||||
history.push(Sample { timestamp: 1, value: 1.0 });
|
||||
history.push(Sample { timestamp: 2, value: 2.0 });
|
||||
history.push(Sample { timestamp: 3, value: 3.0 });
|
||||
|
||||
assert_eq!(history.samples.len(), 2);
|
||||
assert_eq!(history.samples[0].timestamp, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_state_init() {
|
||||
let state = SessionState::new();
|
||||
assert!(!state.connected);
|
||||
assert!(state.signals.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user