mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
Merge branch 'master' into withmergeresolve
This commit is contained in:
commit
10da97a631
4 changed files with 329 additions and 140 deletions
|
|
@ -1,6 +1,4 @@
|
|||
// src/sync_logic.rs
|
||||
|
||||
use chrono::{DateTime, Local, Timelike, Utc};
|
||||
use chrono::{DateTime, Local, Timelike, Utc};
|
||||
use regex::Captures;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
|
|
@ -43,6 +41,8 @@ pub struct LtcState {
|
|||
pub free_count: u32,
|
||||
/// Stores the last up-to-20 raw offset measurements in ms.
|
||||
pub offset_history: VecDeque<i64>,
|
||||
/// Stores the last up-to-20 timecode Δ measurements in ms.
|
||||
pub clock_delta_history: VecDeque<i64>,
|
||||
pub last_match_status: String,
|
||||
pub last_match_check: i64,
|
||||
}
|
||||
|
|
@ -54,12 +54,13 @@ impl LtcState {
|
|||
lock_count: 0,
|
||||
free_count: 0,
|
||||
offset_history: VecDeque::with_capacity(20),
|
||||
clock_delta_history: VecDeque::with_capacity(20),
|
||||
last_match_status: "UNKNOWN".into(),
|
||||
last_match_check: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Record one measured offset in ms, maintaining a sliding window of up to 20 samples.
|
||||
/// Record one measured jitter offset in ms.
|
||||
pub fn record_offset(&mut self, offset_ms: i64) {
|
||||
if self.offset_history.len() == 20 {
|
||||
self.offset_history.pop_front();
|
||||
|
|
@ -67,12 +68,25 @@ impl LtcState {
|
|||
self.offset_history.push_back(offset_ms);
|
||||
}
|
||||
|
||||
/// Clear all stored offset measurements (e.g. on FREE-run).
|
||||
/// Record one timecode Δ in ms.
|
||||
pub fn record_clock_delta(&mut self, delta_ms: i64) {
|
||||
if self.clock_delta_history.len() == 20 {
|
||||
self.clock_delta_history.pop_front();
|
||||
}
|
||||
self.clock_delta_history.push_back(delta_ms);
|
||||
}
|
||||
|
||||
/// Clear all stored jitter measurements.
|
||||
pub fn clear_offsets(&mut self) {
|
||||
self.offset_history.clear();
|
||||
}
|
||||
|
||||
/// Update LOCK/FREE counts, clear offsets on FREE, and refresh timecode-match every 5 s.
|
||||
/// Clear all stored timecode Δ measurements.
|
||||
pub fn clear_clock_deltas(&mut self) {
|
||||
self.clock_delta_history.clear();
|
||||
}
|
||||
|
||||
/// Update LOCK/FREE counts and timecode-match status every 5 s.
|
||||
pub fn update(&mut self, frame: LtcFrame) {
|
||||
match frame.status.as_str() {
|
||||
"LOCK" => {
|
||||
|
|
@ -81,36 +95,38 @@ impl LtcState {
|
|||
"FREE" => {
|
||||
self.free_count += 1;
|
||||
self.clear_offsets();
|
||||
self.clear_clock_deltas();
|
||||
self.last_match_status = "UNKNOWN".into();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Every 5 seconds, recompute whether HH:MM:SS matches local time
|
||||
// Recompute timecode-match every 5 seconds
|
||||
let now_secs = Utc::now().timestamp();
|
||||
if now_secs - self.last_match_check >= 5 {
|
||||
self.last_match_status = if frame.matches_system_time() {
|
||||
"IN SYNC".into()
|
||||
self.last_match_status = if let Some(frame) = &self.latest {
|
||||
if frame.matches_system_time() { "IN SYNC" } else { "OUT OF SYNC" }
|
||||
} else {
|
||||
"OUT OF SYNC".into()
|
||||
};
|
||||
"UNKNOWN"
|
||||
}
|
||||
.into();
|
||||
self.last_match_check = now_secs;
|
||||
}
|
||||
|
||||
self.latest = Some(frame);
|
||||
}
|
||||
|
||||
/// Average jitter over the stored history, in milliseconds.
|
||||
/// Average jitter over stored history, in ms.
|
||||
pub fn average_jitter(&self) -> i64 {
|
||||
if self.offset_history.is_empty() {
|
||||
0
|
||||
} else {
|
||||
let sum: i64 = self.offset_history.iter().sum();
|
||||
sum / (self.offset_history.len() as i64)
|
||||
sum / self.offset_history.len() as i64
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert that average jitter into frames (rounded).
|
||||
/// Convert average jitter into frames (rounded).
|
||||
pub fn average_frames(&self) -> i64 {
|
||||
if let Some(frame) = &self.latest {
|
||||
let ms_per_frame = 1000.0 / frame.frame_rate;
|
||||
|
|
@ -120,17 +136,27 @@ impl LtcState {
|
|||
}
|
||||
}
|
||||
|
||||
/// Average timecode Δ over stored history, in ms.
|
||||
pub fn average_clock_delta(&self) -> i64 {
|
||||
if self.clock_delta_history.is_empty() {
|
||||
0
|
||||
} else {
|
||||
let sum: i64 = self.clock_delta_history.iter().sum();
|
||||
sum / self.clock_delta_history.len() as i64
|
||||
}
|
||||
}
|
||||
|
||||
/// Percentage of samples seen in LOCK state versus total.
|
||||
pub fn lock_ratio(&self) -> f64 {
|
||||
let total = self.lock_count + self.free_count;
|
||||
if total == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(self.lock_count as f64 / total as f64) * 100.0
|
||||
self.lock_count as f64 / total as f64 * 100.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the last computed timecode‐match status ("IN SYNC", "OUT OF SYNC", or "UNKNOWN").
|
||||
/// Get timecode-match status.
|
||||
pub fn timecode_match(&self) -> &str {
|
||||
&self.last_match_status
|
||||
}
|
||||
|
|
@ -202,5 +228,4 @@ mod tests {
|
|||
assert_eq!(state.offset_history.len(), 20);
|
||||
assert_eq!(*state.offset_history.front().unwrap(), 5); // 0-4 are pushed out
|
||||
assert_eq!(*state.offset_history.back().unwrap(), 24);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue