```
feat(realtime): 多源观测追踪与启动配置文件支持 - 协议版本升至 3,Target 新增 sources 字段保留各接收器独立观测 - Store 改为按接收器维护 SourceObservation,基于 received_at_ms 独立执行 TTL 过期,过期后自动 refresh 选取最新有效位置 - refresh() 按时间排序源观测,优先选取有坐标的最近记录 - rid-shared 新增 SourceObservation 结构体 - Observation 验证内联 target_id 规则,长度不超过 192 - DeviceTelemetry 扩展 raw_identity、raw_position、 coordinate_system、position_unverified、relative_height_m - 项目级 config.json 支持版本化配置与 sqlite.busy_timeout_ms, 优先级:环境变量 > config.json > 旧版 maps/config.json - web-worker 传递 sources 与 position_source 至前端 - 首页标题更新为"凌空智巡无人机监测系统" ```
This commit is contained in:
+122
-45
@@ -1,12 +1,12 @@
|
||||
//! Bounded latest-state store, independent of SQLite and archive generation.
|
||||
use rid_shared::{Frame, Metrics, Observation, PROTOCOL_VERSION, Target};
|
||||
use rid_shared::{Frame, Metrics, Observation, PROTOCOL_VERSION, SourceObservation, Target};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
type ObservationListener = Box<dyn Fn(&Observation, u64) + Send + Sync>;
|
||||
|
||||
struct Track {
|
||||
target: Target,
|
||||
sources: HashMap<String, (u64, u64)>,
|
||||
sources: HashMap<String, SourceObservation>,
|
||||
}
|
||||
|
||||
pub struct Store {
|
||||
@@ -19,6 +19,30 @@ pub struct Store {
|
||||
metrics: Metrics,
|
||||
}
|
||||
|
||||
fn refresh(track: &mut Track) {
|
||||
let mut sources: Vec<_> = track.sources.values().cloned().collect();
|
||||
sources.sort_by(|a, b| {
|
||||
a.received_at_ms
|
||||
.cmp(&b.received_at_ms)
|
||||
.then_with(|| a.observation.receiver_id.cmp(&b.observation.receiver_id))
|
||||
});
|
||||
let selected = sources
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|s| s.observation.coordinates().is_some())
|
||||
.or_else(|| sources.last())
|
||||
.expect("nonempty source set");
|
||||
let old = &track.target.observation;
|
||||
if old.coordinates().is_some() != selected.observation.coordinates().is_some()
|
||||
|| old.receiver_id != selected.observation.receiver_id
|
||||
{
|
||||
track.target.position_generation += 1;
|
||||
}
|
||||
track.target.observation = selected.observation.clone();
|
||||
track.target.received_at_ms = sources.iter().map(|s| s.received_at_ms).max().unwrap_or(0);
|
||||
track.target.sources = sources;
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub fn new(capacity: usize, journal_capacity: usize, ttl_ms: u64, epoch: u64) -> Self {
|
||||
assert!(capacity > 0 && journal_capacity > 0 && ttl_ms > 0);
|
||||
@@ -49,62 +73,58 @@ impl Store {
|
||||
self.changes.push_back((self.metrics.sequence, id));
|
||||
}
|
||||
|
||||
/// Multiple receivers use arrival order; no unverified sensor-time fusion is implied.
|
||||
/// Select the latest still-valid position, retaining independent source observations.
|
||||
pub fn apply(&mut self, observation: Observation, now_ms: u64) -> bool {
|
||||
if !observation.validate() {
|
||||
self.metrics.rejected += 1;
|
||||
return false;
|
||||
}
|
||||
let id = observation.target_id.clone();
|
||||
if let Some(track) = self.tracks.get_mut(&id) {
|
||||
if let Some(&(epoch, sequence)) = track.sources.get(&observation.receiver_id) {
|
||||
if observation.source_epoch < epoch
|
||||
|| (observation.source_epoch == epoch && observation.sequence <= sequence)
|
||||
{
|
||||
self.metrics.stale += 1;
|
||||
return false;
|
||||
}
|
||||
} else if track.sources.len() >= 8 {
|
||||
self.metrics.rejected += 1;
|
||||
return false;
|
||||
}
|
||||
track.sources.insert(
|
||||
observation.receiver_id.clone(),
|
||||
(observation.source_epoch, observation.sequence),
|
||||
);
|
||||
let position_generation = track.target.position_generation
|
||||
+ u64::from(
|
||||
track.target.observation.coordinates().is_some()
|
||||
!= observation.coordinates().is_some(),
|
||||
);
|
||||
track.target = Target {
|
||||
position_generation,
|
||||
observation,
|
||||
received_at_ms: now_ms,
|
||||
};
|
||||
} else {
|
||||
if !self.tracks.contains_key(&id) {
|
||||
if self.tracks.len() >= self.metrics.capacity {
|
||||
self.metrics.rejected += 1;
|
||||
return false;
|
||||
}
|
||||
let sources = HashMap::from([(
|
||||
observation.receiver_id.clone(),
|
||||
(observation.source_epoch, observation.sequence),
|
||||
)]);
|
||||
self.tracks.insert(
|
||||
id.clone(),
|
||||
Track {
|
||||
target: Target {
|
||||
position_generation: 0,
|
||||
observation,
|
||||
observation: observation.clone(),
|
||||
received_at_ms: now_ms,
|
||||
position_generation: 0,
|
||||
sources: vec![],
|
||||
},
|
||||
sources,
|
||||
sources: HashMap::new(),
|
||||
},
|
||||
);
|
||||
}
|
||||
let track = self.tracks.get_mut(&id).unwrap();
|
||||
if let Some(old) = track.sources.get(&observation.receiver_id) {
|
||||
let old = &old.observation;
|
||||
if observation.source_epoch < old.source_epoch
|
||||
|| (observation.source_epoch == old.source_epoch
|
||||
&& observation.sequence <= old.sequence)
|
||||
{
|
||||
self.metrics.stale += 1;
|
||||
return false;
|
||||
}
|
||||
} else if track.sources.len() >= 32 {
|
||||
self.metrics.rejected += 1;
|
||||
return false;
|
||||
}
|
||||
track.sources.insert(
|
||||
observation.receiver_id.clone(),
|
||||
SourceObservation {
|
||||
observation,
|
||||
received_at_ms: now_ms,
|
||||
},
|
||||
);
|
||||
track
|
||||
.sources
|
||||
.retain(|_, s| now_ms.saturating_sub(s.received_at_ms) < self.ttl_ms);
|
||||
refresh(track);
|
||||
if let Some(observer) = &self.observer {
|
||||
observer(&self.tracks[&id].target.observation, now_ms);
|
||||
observer(&track.target.observation, now_ms);
|
||||
}
|
||||
self.metrics.accepted += 1;
|
||||
self.changed(id);
|
||||
@@ -112,17 +132,31 @@ impl Store {
|
||||
}
|
||||
|
||||
pub fn expire(&mut self, now_ms: u64) {
|
||||
let expired: Vec<String> = self
|
||||
.tracks
|
||||
.iter()
|
||||
.filter(|(_, t)| now_ms.saturating_sub(t.target.received_at_ms) >= self.ttl_ms)
|
||||
.map(|(id, _)| id.clone())
|
||||
.collect();
|
||||
for id in expired {
|
||||
let mut changed = Vec::new();
|
||||
let mut removed = Vec::new();
|
||||
for (id, track) in &mut self.tracks {
|
||||
let count = track.sources.len();
|
||||
track
|
||||
.sources
|
||||
.retain(|_, s| now_ms.saturating_sub(s.received_at_ms) < self.ttl_ms);
|
||||
if track.sources.is_empty() {
|
||||
removed.push(id.clone());
|
||||
} else if track.sources.len() != count {
|
||||
refresh(track);
|
||||
if let Some(observer) = &self.observer {
|
||||
observer(&track.target.observation, now_ms);
|
||||
}
|
||||
changed.push(id.clone());
|
||||
}
|
||||
}
|
||||
for id in removed {
|
||||
self.tracks.remove(&id);
|
||||
self.metrics.expired += 1;
|
||||
self.changed(id);
|
||||
}
|
||||
for id in changed {
|
||||
self.changed(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frame(&self, since: Option<u64>, now_ms: u64) -> Frame {
|
||||
@@ -176,6 +210,49 @@ impl Store {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn independent_sources_select_valid_position_fallback_expire_and_break_trails() {
|
||||
let mut store = Store::new(10, 100, 30, 1);
|
||||
let mut a = sample("same", 1);
|
||||
a.receiver_id = "a".into();
|
||||
let mut b = a.clone();
|
||||
b.receiver_id = "b".into();
|
||||
b.longitude = Some(121.);
|
||||
assert!(store.apply(a.clone(), 0));
|
||||
assert!(store.apply(b.clone(), 5));
|
||||
let frame = store.frame(None, 5);
|
||||
assert_eq!(frame.targets.len(), 1);
|
||||
assert_eq!(frame.targets[0].sources.len(), 2);
|
||||
assert_eq!(frame.targets[0].observation.receiver_id, "b");
|
||||
let generation = frame.targets[0].position_generation;
|
||||
a.sequence = 2;
|
||||
a.longitude = None;
|
||||
a.latitude = None;
|
||||
assert!(store.apply(a.clone(), 10));
|
||||
assert_eq!(
|
||||
store.frame(None, 10).targets[0].observation.receiver_id,
|
||||
"b"
|
||||
);
|
||||
b.sequence = 2;
|
||||
b.longitude = None;
|
||||
b.latitude = None;
|
||||
assert!(store.apply(b, 11));
|
||||
assert!(
|
||||
store.frame(None, 11).targets[0]
|
||||
.observation
|
||||
.coordinates()
|
||||
.is_none()
|
||||
);
|
||||
a.sequence = 3;
|
||||
a.longitude = Some(120.);
|
||||
a.latitude = Some(30.);
|
||||
assert!(store.apply(a, 15));
|
||||
assert!(store.frame(None, 15).targets[0].position_generation > generation);
|
||||
store.expire(41);
|
||||
assert_eq!(store.frame(None, 41).targets[0].sources.len(), 1);
|
||||
store.expire(45);
|
||||
assert!(store.frame(None, 45).targets.is_empty());
|
||||
}
|
||||
fn sample(id: &str, sequence: u64) -> Observation {
|
||||
Observation {
|
||||
target_id: id.into(),
|
||||
|
||||
Reference in New Issue
Block a user