wifi-densepose-mat 0.3.2

Mass Casualty Assessment Tool - WiFi-based disaster survivor detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Survivor entity representing a detected human in a disaster zone.

use chrono::{DateTime, Utc};
use uuid::Uuid;

use super::{triage::TriageCalculator, Coordinates3D, ScanZoneId, TriageStatus, VitalSignsReading};

/// Unique identifier for a survivor
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SurvivorId(Uuid);

impl SurvivorId {
    /// Create a new random survivor ID
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    /// Create from an existing UUID
    pub fn from_uuid(uuid: Uuid) -> Self {
        Self(uuid)
    }

    /// Get the inner UUID
    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }
}

impl Default for SurvivorId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for SurvivorId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Current status of a survivor
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SurvivorStatus {
    /// Actively being tracked
    Active,
    /// Confirmed rescued
    Rescued,
    /// Lost signal, may need re-detection
    Lost,
    /// Confirmed deceased
    Deceased,
    /// Determined to be false positive
    FalsePositive,
}

/// Additional metadata about a survivor
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SurvivorMetadata {
    /// Estimated age category based on vital patterns
    pub estimated_age_category: Option<AgeCategory>,
    /// Notes from rescue team
    pub notes: Vec<String>,
    /// Tags for organization
    pub tags: Vec<String>,
    /// Assigned rescue team ID
    pub assigned_team: Option<String>,
}

/// Estimated age category based on vital sign patterns
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AgeCategory {
    /// Infant (0-2 years)
    Infant,
    /// Child (2-12 years)
    Child,
    /// Adult (12-65 years)
    Adult,
    /// Elderly (65+ years)
    Elderly,
    /// Cannot determine
    Unknown,
}

/// History of vital signs readings
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VitalSignsHistory {
    readings: Vec<VitalSignsReading>,
    max_history: usize,
}

impl VitalSignsHistory {
    /// Create a new history with specified max size
    pub fn new(max_history: usize) -> Self {
        Self {
            readings: Vec::with_capacity(max_history),
            max_history,
        }
    }

    /// Add a new reading
    pub fn add(&mut self, reading: VitalSignsReading) {
        if self.readings.len() >= self.max_history {
            self.readings.remove(0);
        }
        self.readings.push(reading);
    }

    /// Get the most recent reading
    pub fn latest(&self) -> Option<&VitalSignsReading> {
        self.readings.last()
    }

    /// Get all readings
    pub fn all(&self) -> &[VitalSignsReading] {
        &self.readings
    }

    /// Get the number of readings
    pub fn len(&self) -> usize {
        self.readings.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.readings.is_empty()
    }

    /// Calculate average confidence across readings
    pub fn average_confidence(&self) -> f64 {
        if self.readings.is_empty() {
            return 0.0;
        }
        let sum: f64 = self.readings.iter().map(|r| r.confidence.value()).sum();
        sum / self.readings.len() as f64
    }

    /// Check if vitals are deteriorating
    pub fn is_deteriorating(&self) -> bool {
        if self.readings.len() < 3 {
            return false;
        }

        let recent: Vec<_> = self.readings.iter().rev().take(3).collect();

        // Check breathing trend
        let breathing_declining =
            recent
                .windows(2)
                .all(|w| match (&w[0].breathing, &w[1].breathing) {
                    (Some(a), Some(b)) => a.rate_bpm < b.rate_bpm,
                    _ => false,
                });

        // Check confidence trend
        let confidence_declining = recent
            .windows(2)
            .all(|w| w[0].confidence.value() < w[1].confidence.value());

        breathing_declining || confidence_declining
    }
}

/// A detected survivor in the disaster zone
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Survivor {
    id: SurvivorId,
    zone_id: ScanZoneId,
    first_detected: DateTime<Utc>,
    last_updated: DateTime<Utc>,
    location: Option<Coordinates3D>,
    vital_signs: VitalSignsHistory,
    triage_status: TriageStatus,
    status: SurvivorStatus,
    confidence: f64,
    metadata: SurvivorMetadata,
    alert_sent: bool,
}

impl Survivor {
    /// Create a new survivor from initial detection
    pub fn new(
        zone_id: ScanZoneId,
        initial_vitals: VitalSignsReading,
        location: Option<Coordinates3D>,
    ) -> Self {
        let now = Utc::now();
        let confidence = initial_vitals.confidence.value();
        let triage_status = TriageCalculator::calculate(&initial_vitals);

        let mut vital_signs = VitalSignsHistory::new(100);
        vital_signs.add(initial_vitals);

        Self {
            id: SurvivorId::new(),
            zone_id,
            first_detected: now,
            last_updated: now,
            location,
            vital_signs,
            triage_status,
            status: SurvivorStatus::Active,
            confidence,
            metadata: SurvivorMetadata::default(),
            alert_sent: false,
        }
    }

    /// Get the survivor ID
    pub fn id(&self) -> &SurvivorId {
        &self.id
    }

    /// Get the zone ID where survivor was detected
    pub fn zone_id(&self) -> &ScanZoneId {
        &self.zone_id
    }

    /// Get the first detection time
    pub fn first_detected(&self) -> &DateTime<Utc> {
        &self.first_detected
    }

    /// Get the last update time
    pub fn last_updated(&self) -> &DateTime<Utc> {
        &self.last_updated
    }

    /// Get the estimated location
    pub fn location(&self) -> Option<&Coordinates3D> {
        self.location.as_ref()
    }

    /// Get the vital signs history
    pub fn vital_signs(&self) -> &VitalSignsHistory {
        &self.vital_signs
    }

    /// Get the current triage status
    pub fn triage_status(&self) -> &TriageStatus {
        &self.triage_status
    }

    /// Get the current status
    pub fn status(&self) -> &SurvivorStatus {
        &self.status
    }

    /// Get the confidence score
    pub fn confidence(&self) -> f64 {
        self.confidence
    }

    /// Get the metadata
    pub fn metadata(&self) -> &SurvivorMetadata {
        &self.metadata
    }

    /// Get mutable metadata
    pub fn metadata_mut(&mut self) -> &mut SurvivorMetadata {
        &mut self.metadata
    }

    /// Update with new vital signs reading
    pub fn update_vitals(&mut self, reading: VitalSignsReading) {
        let previous_triage = self.triage_status.clone();
        self.vital_signs.add(reading.clone());
        self.confidence = self.vital_signs.average_confidence();
        self.triage_status = TriageCalculator::calculate(&reading);
        self.last_updated = Utc::now();

        // Log triage change for audit
        if previous_triage != self.triage_status {
            tracing::info!(
                survivor_id = %self.id,
                previous = ?previous_triage,
                current = ?self.triage_status,
                "Triage status changed"
            );
        }
    }

    /// Update the location estimate
    pub fn update_location(&mut self, location: Coordinates3D) {
        self.location = Some(location);
        self.last_updated = Utc::now();
    }

    /// Mark as rescued
    pub fn mark_rescued(&mut self) {
        self.status = SurvivorStatus::Rescued;
        self.last_updated = Utc::now();
        tracing::info!(survivor_id = %self.id, "Survivor marked as rescued");
    }

    /// Mark as lost (signal lost)
    pub fn mark_lost(&mut self) {
        self.status = SurvivorStatus::Lost;
        self.last_updated = Utc::now();
    }

    /// Mark as deceased
    pub fn mark_deceased(&mut self) {
        self.status = SurvivorStatus::Deceased;
        self.triage_status = TriageStatus::Deceased;
        self.last_updated = Utc::now();
    }

    /// Mark as false positive
    pub fn mark_false_positive(&mut self) {
        self.status = SurvivorStatus::FalsePositive;
        self.last_updated = Utc::now();
    }

    /// Check if survivor should generate an alert
    pub fn should_alert(&self) -> bool {
        if self.alert_sent {
            return false;
        }

        // Alert for high-priority survivors
        matches!(
            self.triage_status,
            TriageStatus::Immediate | TriageStatus::Delayed
        ) && self.confidence >= 0.5
    }

    /// Mark that alert was sent
    pub fn mark_alert_sent(&mut self) {
        self.alert_sent = true;
    }

    /// Check if vitals are deteriorating (needs priority upgrade)
    pub fn is_deteriorating(&self) -> bool {
        self.vital_signs.is_deteriorating()
    }

    /// Get time since last update
    pub fn time_since_update(&self) -> chrono::Duration {
        Utc::now() - self.last_updated
    }

    /// Check if survivor data is stale
    pub fn is_stale(&self, threshold_seconds: i64) -> bool {
        self.time_since_update().num_seconds() > threshold_seconds
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::{BreathingPattern, BreathingType, ConfidenceScore};

    fn create_test_vitals(confidence: f64) -> VitalSignsReading {
        VitalSignsReading {
            breathing: Some(BreathingPattern {
                rate_bpm: 16.0,
                amplitude: 0.8,
                regularity: 0.9,
                pattern_type: BreathingType::Normal,
            }),
            heartbeat: None,
            movement: Default::default(),
            timestamp: Utc::now(),
            confidence: ConfidenceScore::new(confidence),
        }
    }

    #[test]
    fn test_survivor_creation() {
        let zone_id = ScanZoneId::new();
        let vitals = create_test_vitals(0.8);
        let survivor = Survivor::new(zone_id.clone(), vitals, None);

        assert_eq!(survivor.zone_id(), &zone_id);
        assert!(survivor.confidence() >= 0.8);
        assert!(matches!(survivor.status(), SurvivorStatus::Active));
    }

    #[test]
    fn test_vital_signs_history() {
        let mut history = VitalSignsHistory::new(5);

        for i in 0..7 {
            history.add(create_test_vitals(0.5 + (i as f64 * 0.05)));
        }

        // Should only keep last 5
        assert_eq!(history.len(), 5);

        // Average should be based on last 5 readings
        assert!(history.average_confidence() > 0.5);
    }

    #[test]
    fn test_survivor_should_alert() {
        let zone_id = ScanZoneId::new();
        let vitals = create_test_vitals(0.8);
        let survivor = Survivor::new(zone_id, vitals, None);

        // Should alert if triage is Immediate or Delayed
        // Depends on triage calculation from vitals
        assert!(!survivor.alert_sent);
    }

    #[test]
    fn test_survivor_mark_rescued() {
        let zone_id = ScanZoneId::new();
        let vitals = create_test_vitals(0.8);
        let mut survivor = Survivor::new(zone_id, vitals, None);

        survivor.mark_rescued();
        assert!(matches!(survivor.status(), SurvivorStatus::Rescued));
    }
}