1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
5pub struct GeoPosition {
6 pub lat: f64,
8
9 pub lon: f64,
11
12 #[serde(skip_serializing_if = "Option::is_none")]
14 pub alt: Option<f64>,
15}
16
17impl GeoPosition {
18 pub fn new(lat: f64, lon: f64) -> Self {
19 Self {
20 lat,
21 lon,
22 alt: None,
23 }
24 }
25
26 pub fn with_altitude(mut self, alt: f64) -> Self {
27 self.alt = Some(alt);
28 self
29 }
30
31 pub fn distance_to(&self, other: &GeoPosition) -> f64 {
33 let r = 6371000.0; let lat1 = self.lat.to_radians();
35 let lat2 = other.lat.to_radians();
36 let delta_lat = (other.lat - self.lat).to_radians();
37 let delta_lon = (other.lon - self.lon).to_radians();
38
39 let a = (delta_lat / 2.0).sin().powi(2)
40 + lat1.cos() * lat2.cos() * (delta_lon / 2.0).sin().powi(2);
41 let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
42
43 r * c
44 }
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
49pub enum HierarchyLevel {
50 Platform = 0,
52 Squad = 1,
54 Platoon = 2,
56 Company = 3,
58}
59
60impl HierarchyLevel {
61 pub fn parent(&self) -> Option<HierarchyLevel> {
63 match self {
64 HierarchyLevel::Platform => Some(HierarchyLevel::Squad),
65 HierarchyLevel::Squad => Some(HierarchyLevel::Platoon),
66 HierarchyLevel::Platoon => Some(HierarchyLevel::Company),
67 HierarchyLevel::Company => None,
68 }
69 }
70
71 pub fn child(&self) -> Option<HierarchyLevel> {
73 match self {
74 HierarchyLevel::Platform => None,
75 HierarchyLevel::Squad => Some(HierarchyLevel::Platform),
76 HierarchyLevel::Platoon => Some(HierarchyLevel::Squad),
77 HierarchyLevel::Company => Some(HierarchyLevel::Platoon),
78 }
79 }
80
81 pub fn can_be_parent_of(&self, child: &HierarchyLevel) -> bool {
84 self > child
85 }
86}
87
88impl std::fmt::Display for HierarchyLevel {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 match self {
91 HierarchyLevel::Platform => write!(f, "Platform"),
92 HierarchyLevel::Squad => write!(f, "Squad"),
93 HierarchyLevel::Platoon => write!(f, "Platoon"),
94 HierarchyLevel::Company => write!(f, "Company"),
95 }
96 }
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct GeographicBeacon {
102 pub node_id: String,
104
105 pub position: GeoPosition,
107
108 pub geohash: String,
110
111 pub hierarchy_level: HierarchyLevel,
113
114 #[serde(default)]
116 pub capabilities: Vec<String>,
117
118 pub operational: bool,
120
121 pub timestamp: u64,
123
124 #[serde(skip_serializing_if = "Option::is_none")]
126 pub mobility: Option<super::config::NodeMobility>,
127
128 #[serde(default)]
130 pub can_parent: bool,
131
132 #[serde(default)]
134 pub parent_priority: u8,
135
136 #[serde(skip_serializing_if = "Option::is_none")]
138 pub resources: Option<super::config::NodeResources>,
139
140 #[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
142 pub metadata: std::collections::HashMap<String, String>,
143}
144
145impl GeographicBeacon {
146 pub fn new(node_id: String, position: GeoPosition, hierarchy_level: HierarchyLevel) -> Self {
148 use crate::geohash::encode;
149
150 let geohash =
151 encode(position.lon, position.lat, 7).unwrap_or_else(|_| String::from("invalid"));
152
153 let timestamp = std::time::SystemTime::now()
154 .duration_since(std::time::UNIX_EPOCH)
155 .map(|d| d.as_secs())
156 .unwrap_or(0);
157
158 Self {
159 node_id,
160 position,
161 geohash,
162 hierarchy_level,
163 capabilities: Vec::new(),
164 operational: true,
165 timestamp,
166 mobility: None,
167 can_parent: false,
168 parent_priority: 0,
169 resources: None,
170 metadata: std::collections::HashMap::new(),
171 }
172 }
173
174 pub fn update_timestamp(&mut self) {
176 self.timestamp = std::time::SystemTime::now()
177 .duration_since(std::time::UNIX_EPOCH)
178 .map(|d| d.as_secs())
179 .unwrap_or(0);
180 }
181
182 pub fn add_capability(&mut self, capability: String) {
184 if !self.capabilities.contains(&capability) {
185 self.capabilities.push(capability);
186 }
187 }
188
189 pub fn has_capability(&self, capability: &str) -> bool {
191 self.capabilities.iter().any(|c| c == capability)
192 }
193
194 pub fn age_seconds(&self) -> u64 {
196 std::time::SystemTime::now()
197 .duration_since(std::time::UNIX_EPOCH)
198 .map(|d| d.as_secs())
199 .unwrap_or(0)
200 .saturating_sub(self.timestamp)
201 }
202
203 pub fn is_expired(&self, ttl_seconds: u64) -> bool {
205 self.age_seconds() > ttl_seconds
206 }
207}
208
209#[cfg(test)]
210mod tests {
211 use super::*;
212
213 #[test]
214 fn test_geo_position_distance() {
215 let pos1 = GeoPosition::new(37.7749, -122.4194); let pos2 = GeoPosition::new(34.0522, -118.2437); let distance = pos1.distance_to(&pos2);
220
221 assert!(distance > 550_000.0 && distance < 570_000.0);
223 }
224
225 #[test]
226 fn test_hierarchy_level_parent_child() {
227 assert_eq!(
228 HierarchyLevel::Platform.parent(),
229 Some(HierarchyLevel::Squad)
230 );
231 assert_eq!(
232 HierarchyLevel::Squad.parent(),
233 Some(HierarchyLevel::Platoon)
234 );
235 assert_eq!(
236 HierarchyLevel::Platoon.parent(),
237 Some(HierarchyLevel::Company)
238 );
239 assert_eq!(HierarchyLevel::Company.parent(), None);
240
241 assert_eq!(HierarchyLevel::Platform.child(), None);
242 assert_eq!(
243 HierarchyLevel::Squad.child(),
244 Some(HierarchyLevel::Platform)
245 );
246 assert_eq!(HierarchyLevel::Platoon.child(), Some(HierarchyLevel::Squad));
247 assert_eq!(
248 HierarchyLevel::Company.child(),
249 Some(HierarchyLevel::Platoon)
250 );
251 }
252
253 #[test]
254 fn test_geographic_beacon_creation() {
255 let position = GeoPosition::new(37.7749, -122.4194);
256 let beacon = GeographicBeacon::new(
257 "test-node-1".to_string(),
258 position,
259 HierarchyLevel::Platform,
260 );
261
262 assert_eq!(beacon.node_id, "test-node-1");
263 assert_eq!(beacon.position.lat, 37.7749);
264 assert_eq!(beacon.hierarchy_level, HierarchyLevel::Platform);
265 assert!(beacon.operational);
266 assert!(!beacon.geohash.is_empty());
267 }
268
269 #[test]
270 fn test_beacon_capabilities() {
271 let position = GeoPosition::new(37.7749, -122.4194);
272 let mut beacon =
273 GeographicBeacon::new("test-node".to_string(), position, HierarchyLevel::Squad);
274
275 beacon.add_capability("ai-inference".to_string());
276 beacon.add_capability("sensor-fusion".to_string());
277
278 assert!(beacon.has_capability("ai-inference"));
279 assert!(beacon.has_capability("sensor-fusion"));
280 assert!(!beacon.has_capability("non-existent"));
281 }
282
283 #[test]
284 fn test_beacon_serialization() {
285 let position = GeoPosition::new(37.7749, -122.4194);
286 let beacon =
287 GeographicBeacon::new("test-node".to_string(), position, HierarchyLevel::Platform);
288
289 let json = serde_json::to_string(&beacon).unwrap();
291 assert!(json.contains("test-node"));
292 assert!(json.contains("37.7749"));
293
294 let deserialized: GeographicBeacon = serde_json::from_str(&json).unwrap();
296 assert_eq!(deserialized.node_id, beacon.node_id);
297 assert_eq!(deserialized.position.lat, beacon.position.lat);
298 }
299}