feat(protocol): Observation坐标字段可选化及协议v2扩展 - 将Observation中longitude/latitude/altitude_m/heading_deg改为Option类型, 支持无位置信息的观测数据 - 新增DeviceTelemetry结构体承载设备遥测扩展字段(serial, mac, model, rssi, speed, vertical_speed, pressure_altitude, geometric_altitude, height_agl, pilot_position等) - 新增LegacyObservation兼容结构体及From转换,保持旧接收器协议输入兼容 - PROTOCOL_VERSION从1升至2 - 新增coordinates()辅助方法统一提取有效坐标 - validate()适配Option类型,允许坐标字段同时缺失 - Store新增position_generation机制追踪位置可用性变化,确保合并帧间的 位置丢失仍能正确断开轨迹 - geofence-map-adapter过滤无效坐标避免渲染异常 - 新增appearance.css外观主题系统,支持blue/graphite/white三套配色 ```
172 lines
5.8 KiB
Rust
172 lines
5.8 KiB
Rust
//! Two-dimensional WGS84 monitoring areas. No flight-control semantics.
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
|
|
pub enum Geometry {
|
|
Circle { center: [f64; 2], radius_m: f64 },
|
|
Polygon { vertices: Vec<[f64; 2]> },
|
|
}
|
|
fn coordinate(p: &[f64; 2]) -> bool {
|
|
p[0].is_finite()
|
|
&& p[1].is_finite()
|
|
&& (-180.0..=180.0).contains(&p[0])
|
|
&& (-90.0..=90.0).contains(&p[1])
|
|
}
|
|
fn cross(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> f64 {
|
|
(b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
|
|
}
|
|
fn segment(a: [f64; 2], b: [f64; 2], p: [f64; 2]) -> bool {
|
|
cross(a, b, p).abs() <= 1e-10
|
|
&& p[0] >= a[0].min(b[0]) - 1e-10
|
|
&& p[0] <= a[0].max(b[0]) + 1e-10
|
|
&& p[1] >= a[1].min(b[1]) - 1e-10
|
|
&& p[1] <= a[1].max(b[1]) + 1e-10
|
|
}
|
|
fn intersects(a: [f64; 2], b: [f64; 2], c: [f64; 2], d: [f64; 2]) -> bool {
|
|
segment(a, b, c)
|
|
|| segment(a, b, d)
|
|
|| segment(c, d, a)
|
|
|| segment(c, d, b)
|
|
|| (cross(a, b, c) * cross(a, b, d) < 0.0 && cross(c, d, a) * cross(c, d, b) < 0.0)
|
|
}
|
|
impl Geometry {
|
|
pub fn valid(&self) -> bool {
|
|
match self {
|
|
Self::Circle { center, radius_m } => {
|
|
coordinate(center) && radius_m.is_finite() && (1.0..=1_000_000.0).contains(radius_m)
|
|
}
|
|
Self::Polygon { vertices: v } => {
|
|
if !(3..=128).contains(&v.len()) || !v.iter().all(coordinate) {
|
|
return false;
|
|
}
|
|
let min = v.iter().map(|p| p[0]).fold(f64::INFINITY, f64::min);
|
|
let max = v.iter().map(|p| p[0]).fold(f64::NEG_INFINITY, f64::max);
|
|
// Reject ambiguous dateline polygons rather than drawing the opposite hemisphere.
|
|
if max - min >= 180.0 {
|
|
return false;
|
|
}
|
|
let mut area = 0.0;
|
|
for i in 0..v.len() {
|
|
let j = (i + 1) % v.len();
|
|
if v[i] == v[j] {
|
|
return false;
|
|
}
|
|
area += v[i][0] * v[j][1] - v[j][0] * v[i][1];
|
|
for k in i + 1..v.len() {
|
|
let l = (k + 1) % v.len();
|
|
if j != k && l != i && intersects(v[i], v[j], v[k], v[l]) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
area.abs() > 1e-10
|
|
}
|
|
}
|
|
}
|
|
/// Boundaries are inside. Invalid coordinates never match.
|
|
pub fn contains(&self, point: [f64; 2]) -> bool {
|
|
if !coordinate(&point) {
|
|
return false;
|
|
}
|
|
match self {
|
|
Self::Circle { center, radius_m } => {
|
|
let a = center[1].to_radians();
|
|
let b = point[1].to_radians();
|
|
let h = ((b - a) / 2.0).sin().powi(2)
|
|
+ a.cos() * b.cos() * ((point[0] - center[0]).to_radians() / 2.0).sin().powi(2);
|
|
6_371_008.8 * 2.0 * h.clamp(0.0, 1.0).sqrt().asin() <= *radius_m + 1e-6
|
|
}
|
|
Self::Polygon { vertices: v } => {
|
|
let mut inside = false;
|
|
for i in 0..v.len() {
|
|
let a = v[i];
|
|
let b = v[(i + 1) % v.len()];
|
|
if segment(a, b, point) {
|
|
return true;
|
|
}
|
|
if (a[1] > point[1]) != (b[1] > point[1])
|
|
&& point[0] < (b[0] - a[0]) * (point[1] - a[1]) / (b[1] - a[1]) + a[0]
|
|
{
|
|
inside = !inside;
|
|
}
|
|
}
|
|
inside
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Fence {
|
|
pub id: String,
|
|
pub name: String,
|
|
pub enabled: bool,
|
|
pub geometry: Geometry,
|
|
}
|
|
impl Fence {
|
|
pub fn valid(&self) -> bool {
|
|
crate::target_lists::valid_target_id(&self.id)
|
|
&& !self.name.trim().is_empty()
|
|
&& self.name.chars().count() <= 40
|
|
&& !self.name.chars().any(char::is_control)
|
|
&& self.geometry.valid()
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
pub struct Snapshot {
|
|
pub revision: u64,
|
|
pub entries: Vec<Fence>,
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Mutation {
|
|
pub fence: Fence,
|
|
pub expected_revision: u64,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct Alert {
|
|
pub id: u64,
|
|
pub occurred_at_ms: u64,
|
|
pub event_type: String,
|
|
pub target_id: String,
|
|
pub fence_id: Option<String>,
|
|
pub fence_name: Option<String>,
|
|
pub longitude: Option<f64>,
|
|
pub latitude: Option<f64>,
|
|
pub list_kind: String,
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn circle_uses_metres_and_handles_dateline() {
|
|
let g = Geometry::Circle {
|
|
center: [179.999, 0.0],
|
|
radius_m: 300.0,
|
|
};
|
|
assert!(g.valid());
|
|
assert!(g.contains([-179.999, 0.0]));
|
|
assert!(!g.contains([179.99, 0.0]));
|
|
assert!(!g.contains([f64::NAN, 0.0]));
|
|
}
|
|
#[test]
|
|
fn polygon_boundary_concavity_and_validation() {
|
|
let g = Geometry::Polygon {
|
|
vertices: vec![[0.0, 0.0], [2.0, 0.0], [1.0, 1.0], [2.0, 2.0], [0.0, 2.0]],
|
|
};
|
|
assert!(g.valid());
|
|
assert!(g.contains([0.0, 1.0]));
|
|
assert!(g.contains([0.5, 1.0]));
|
|
assert!(!g.contains([1.8, 1.0]));
|
|
for vertices in [
|
|
vec![[0.0, 0.0]; 3],
|
|
vec![[0.0, 0.0], [2.0, 2.0], [0.0, 2.0], [2.0, 0.0]],
|
|
vec![[179.0, 0.0], [-179.0, 0.0], [179.0, 1.0]],
|
|
] {
|
|
assert!(!Geometry::Polygon { vertices }.valid());
|
|
}
|
|
}
|
|
}
|