scout_rs 0.9.0

A Rust client for the Scout API that allows uploading events and images to the Scout database
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use native_db::{native_db, ToKey};
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};

// Re-export all unchanged models from v2
pub use super::v2::{
    Action, AncestorLocal, Device, DevicePrettyLocation, DeviceType, Event, EventLocal, Heartbeat,
    Herd, Layer, MediaType, Operator, OperatorLocal, Plan, PlanInsert, PlanType, ResponseScout,
    ResponseScoutStatus, Session, SessionLocal, Syncable, Tag, TagLocal, TagObservationType, Zone,
};

// ===== CONNECTIVITY V3 WITH FREQUENCY, BANDWIDTH, AND ASSOCIATED STATION =====
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[native_model(id = 15, version = 3)]
#[native_db]
pub struct ConnectivityLocal {
    pub id: Option<i64>,
    #[primary_key]
    pub id_local: Option<String>,
    #[secondary_key]
    pub session_id: Option<i64>,
    #[secondary_key]
    pub device_id: Option<i64>,
    #[secondary_key]
    pub ancestor_id_local: Option<String>,
    pub inserted_at: Option<String>,
    pub timestamp_start: String,
    pub signal: f64,
    pub noise: f64,
    pub altitude: f64,
    pub heading: f64,
    pub location: Option<String>,
    pub h14_index: String,
    pub h13_index: String,
    pub h12_index: String,
    pub h11_index: String,
    // FIELD FROM V2
    pub battery_percentage: Option<f32>,
    // NEW FIELDS IN V3
    pub frequency_hz: Option<f32>,
    pub bandwidth_hz: Option<f32>,
    pub associated_station: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Connectivity {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i64>,
    pub session_id: Option<i64>,
    pub device_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inserted_at: Option<String>,
    pub timestamp_start: String,
    pub signal: f64,
    pub noise: f64,
    pub altitude: f64,
    pub heading: f64,
    pub location: Option<String>,
    pub h14_index: String,
    pub h13_index: String,
    pub h12_index: String,
    pub h11_index: String,
    // FIELD FROM V2
    pub battery_percentage: Option<f32>,
    // NEW FIELDS IN V3
    pub frequency_hz: Option<f32>,
    pub bandwidth_hz: Option<f32>,
    pub associated_station: Option<String>,
}

impl Default for ConnectivityLocal {
    fn default() -> Self {
        Self {
            id: None,
            id_local: None,
            session_id: None,
            device_id: None,
            ancestor_id_local: None,
            inserted_at: None,
            timestamp_start: String::new(),
            signal: 0.0,
            noise: 0.0,
            altitude: 0.0,
            heading: 0.0,
            location: None,
            h14_index: String::new(),
            h13_index: String::new(),
            h12_index: String::new(),
            h11_index: String::new(),
            battery_percentage: None,
            frequency_hz: None,
            bandwidth_hz: None,
            associated_station: None,
        }
    }
}

impl super::v1::Syncable for ConnectivityLocal {
    fn id(&self) -> Option<i64> {
        self.id
    }

    fn set_id(&mut self, id: i64) {
        self.id = Some(id);
    }

    fn id_local(&self) -> Option<String> {
        self.id_local.clone()
    }

    fn set_id_local(&mut self, id_local: String) {
        self.id_local = Some(id_local);
    }
}

impl super::v1::AncestorLocal for ConnectivityLocal {
    fn ancestor_id_local(&self) -> Option<String> {
        self.ancestor_id_local.clone()
    }

    fn set_ancestor_id_local(&mut self, ancestor_id_local: String) {
        self.ancestor_id_local = Some(ancestor_id_local);
    }
}

impl From<ConnectivityLocal> for Connectivity {
    fn from(local: ConnectivityLocal) -> Self {
        Connectivity {
            id: local.id,
            session_id: local.session_id,
            device_id: local.device_id,
            inserted_at: local.inserted_at,
            timestamp_start: local.timestamp_start,
            signal: local.signal,
            noise: local.noise,
            altitude: local.altitude,
            heading: local.heading,
            location: local.location,
            h14_index: local.h14_index,
            h13_index: local.h13_index,
            h12_index: local.h12_index,
            h11_index: local.h11_index,
            battery_percentage: local.battery_percentage,
            frequency_hz: local.frequency_hz,
            bandwidth_hz: local.bandwidth_hz,
            associated_station: local.associated_station,
        }
    }
}

impl From<Connectivity> for ConnectivityLocal {
    fn from(connectivity: Connectivity) -> Self {
        ConnectivityLocal {
            id: connectivity.id,
            id_local: None, // API structs don't have id_local
            session_id: connectivity.session_id,
            device_id: connectivity.device_id,
            ancestor_id_local: None, // API structs don't have ancestor_id_local
            inserted_at: connectivity.inserted_at,
            timestamp_start: connectivity.timestamp_start,
            signal: connectivity.signal,
            noise: connectivity.noise,
            altitude: connectivity.altitude,
            heading: connectivity.heading,
            location: connectivity.location,
            h14_index: connectivity.h14_index,
            h13_index: connectivity.h13_index,
            h12_index: connectivity.h12_index,
            h11_index: connectivity.h11_index,
            battery_percentage: connectivity.battery_percentage,
            frequency_hz: connectivity.frequency_hz,
            bandwidth_hz: connectivity.bandwidth_hz,
            associated_station: connectivity.associated_station,
        }
    }
}

impl Connectivity {
    pub fn new(
        session_id: Option<i64>,
        device_id: Option<i64>,
        timestamp_start: u64,
        signal: f64,
        noise: f64,
        altitude: f64,
        heading: f64,
        location: String,
        h14_index: String,
        h13_index: String,
        h12_index: String,
        h11_index: String,
        battery_percentage: Option<f32>,
        frequency_hz: Option<f32>,
        bandwidth_hz: Option<f32>,
        associated_station: Option<String>,
    ) -> Self {
        use chrono::{DateTime, Utc};
        let timestamp_start_str = DateTime::from_timestamp(timestamp_start as i64, 0)
            .unwrap_or_else(|| DateTime::<Utc>::from_timestamp(0, 0).unwrap())
            .to_rfc3339();

        Self {
            id: None,
            session_id,
            device_id,
            inserted_at: None,
            timestamp_start: timestamp_start_str,
            signal,
            noise,
            altitude,
            heading,
            location: Some(location),
            h14_index,
            h13_index,
            h12_index,
            h11_index,
            battery_percentage,
            frequency_hz,
            bandwidth_hz,
            associated_station,
        }
    }
}

impl ConnectivityLocal {
    pub fn new(
        session_id: Option<i64>,
        device_id: Option<i64>,
        timestamp_start: u64,
        signal: f64,
        noise: f64,
        altitude: f64,
        heading: f64,
        location: String,
        h14_index: String,
        h13_index: String,
        h12_index: String,
        h11_index: String,
        battery_percentage: Option<f32>,
        frequency_hz: Option<f32>,
        bandwidth_hz: Option<f32>,
        associated_station: Option<String>,
    ) -> Self {
        use chrono::{DateTime, Utc};
        let timestamp_start_str = DateTime::from_timestamp(timestamp_start as i64, 0)
            .unwrap_or_else(|| DateTime::<Utc>::from_timestamp(0, 0).unwrap())
            .to_rfc3339();

        Self {
            id: None,
            id_local: None,
            session_id,
            device_id,
            ancestor_id_local: None,
            inserted_at: None,
            timestamp_start: timestamp_start_str,
            signal,
            noise,
            altitude,
            heading,
            location: Some(location),
            h14_index,
            h13_index,
            h12_index,
            h11_index,
            battery_percentage,
            frequency_hz,
            bandwidth_hz,
            associated_station,
        }
    }
}

// ===== MIGRATION FROM V2 TO V3 =====
impl From<super::v2::ConnectivityLocal> for ConnectivityLocal {
    fn from(v2: super::v2::ConnectivityLocal) -> Self {
        Self {
            id: v2.id,
            id_local: v2.id_local,
            session_id: v2.session_id,
            device_id: v2.device_id,
            ancestor_id_local: v2.ancestor_id_local,
            inserted_at: v2.inserted_at,
            timestamp_start: v2.timestamp_start,
            signal: v2.signal,
            noise: v2.noise,
            altitude: v2.altitude,
            heading: v2.heading,
            location: v2.location,
            h14_index: v2.h14_index,
            h13_index: v2.h13_index,
            h12_index: v2.h12_index,
            h11_index: v2.h11_index,
            battery_percentage: v2.battery_percentage,
            // Default for new fields in v3
            frequency_hz: None,
            bandwidth_hz: None,
            associated_station: None,
        }
    }
}

impl From<super::v2::Connectivity> for Connectivity {
    fn from(v2: super::v2::Connectivity) -> Self {
        Self {
            id: v2.id,
            session_id: v2.session_id,
            device_id: v2.device_id,
            inserted_at: v2.inserted_at,
            timestamp_start: v2.timestamp_start,
            signal: v2.signal,
            noise: v2.noise,
            altitude: v2.altitude,
            heading: v2.heading,
            location: v2.location,
            h14_index: v2.h14_index,
            h13_index: v2.h13_index,
            h12_index: v2.h12_index,
            h11_index: v2.h11_index,
            battery_percentage: v2.battery_percentage,
            // Default for new fields in v3
            frequency_hz: None,
            bandwidth_hz: None,
            associated_station: None,
        }
    }
}

// ===== ARTIFACT V3 WITH UPDATED SCHEMA =====
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[native_model(id = 19, version = 1)]
#[native_db]
pub struct ArtifactLocal {
    pub id: Option<i64>,
    #[primary_key]
    pub id_local: Option<String>,
    #[secondary_key]
    pub ancestor_id_local: Option<String>,
    pub created_at: Option<String>,
    pub file_path: String,
    #[secondary_key]
    pub session_id: Option<i64>,
    pub timestamp_observation: Option<String>,
    pub modality: Option<String>,
    pub device_id: i64,
    pub updated_at: Option<String>,
    pub timestamp_observation_end: String,
    pub has_uploaded_file_to_storage: bool,
    pub upload_url: Option<String>,
    pub upload_url_generated_at: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Artifact {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    pub file_path: String,
    pub session_id: Option<i64>,
    pub timestamp_observation: Option<String>,
    pub modality: Option<String>,
    pub device_id: i64,
    pub updated_at: Option<String>,
    pub timestamp_observation_end: String,
}

impl Default for ArtifactLocal {
    fn default() -> Self {
        use chrono::Utc;
        Self {
            id: None,
            id_local: None,
            ancestor_id_local: None,
            created_at: None,
            file_path: String::new(),
            session_id: None,
            timestamp_observation: None,
            modality: None,
            device_id: 0,
            updated_at: None,
            timestamp_observation_end: Utc::now().to_rfc3339(),
            has_uploaded_file_to_storage: false,
            upload_url: None,
            upload_url_generated_at: None,
        }
    }
}

impl super::v1::Syncable for ArtifactLocal {
    fn id(&self) -> Option<i64> {
        self.id
    }

    fn set_id(&mut self, id: i64) {
        self.id = Some(id);
    }

    fn id_local(&self) -> Option<String> {
        self.id_local.clone()
    }

    fn set_id_local(&mut self, id_local: String) {
        self.id_local = Some(id_local);
    }
}

impl super::v1::AncestorLocal for ArtifactLocal {
    fn ancestor_id_local(&self) -> Option<String> {
        self.ancestor_id_local.clone()
    }

    fn set_ancestor_id_local(&mut self, ancestor_id_local: String) {
        self.ancestor_id_local = Some(ancestor_id_local);
    }
}

impl From<ArtifactLocal> for Artifact {
    fn from(local: ArtifactLocal) -> Self {
        Artifact {
            id: local.id,
            created_at: local.created_at,
            file_path: local.file_path,
            session_id: local.session_id,
            timestamp_observation: local.timestamp_observation,
            modality: local.modality,
            device_id: local.device_id,
            updated_at: local.updated_at,
            timestamp_observation_end: local.timestamp_observation_end,
        }
    }
}

impl From<Artifact> for ArtifactLocal {
    fn from(artifact: Artifact) -> Self {
        ArtifactLocal {
            id: artifact.id,
            id_local: None,          // API structs don't have id_local
            ancestor_id_local: None, // API structs don't have ancestor_id_local
            created_at: artifact.created_at,
            file_path: artifact.file_path,
            session_id: artifact.session_id,
            timestamp_observation: artifact.timestamp_observation,
            modality: artifact.modality,
            device_id: artifact.device_id,
            updated_at: artifact.updated_at,
            timestamp_observation_end: artifact.timestamp_observation_end,
            has_uploaded_file_to_storage: false,
            upload_url: None,
            upload_url_generated_at: None,
        }
    }
}

impl Artifact {
    pub fn new(
        file_path: String,
        session_id: Option<i64>,
        device_id: i64,
        modality: Option<String>,
        timestamp_observation: Option<String>,
    ) -> Self {
        use chrono::Utc;
        Self {
            id: None,
            created_at: None,
            file_path,
            session_id,
            timestamp_observation,
            modality,
            device_id,
            updated_at: None,
            timestamp_observation_end: Utc::now().to_rfc3339(),
        }
    }
}

impl ArtifactLocal {
    pub fn new(
        file_path: String,
        session_id: Option<i64>,
        device_id: i64,
        modality: Option<String>,
        timestamp_observation: Option<String>,
    ) -> Self {
        use chrono::Utc;
        Self {
            id: None,
            id_local: None,
            ancestor_id_local: None,
            created_at: None,
            file_path,
            session_id,
            timestamp_observation,
            modality,
            device_id,
            updated_at: None,
            timestamp_observation_end: Utc::now().to_rfc3339(),
            has_uploaded_file_to_storage: false,
            upload_url: None,
            upload_url_generated_at: None,
        }
    }
}

// ===== MIGRATION FROM V1 ARTIFACT TO V3 =====
impl From<super::v1::Artifact> for ArtifactLocal {
    fn from(v1: super::v1::Artifact) -> Self {
        use chrono::Utc;
        Self {
            id: v1.id,
            id_local: v1.id_local,
            ancestor_id_local: v1.ancestor_id_local,
            created_at: v1.created_at,
            file_path: v1.file_path,
            session_id: v1.session_id,
            timestamp_observation: v1.timestamp_observation,
            // New fields in v3 - use defaults for migrated data
            modality: None,
            device_id: 0, // This needs to be set properly during migration
            updated_at: None,
            timestamp_observation_end: Utc::now().to_rfc3339(),
            has_uploaded_file_to_storage: false,
            upload_url: None,
            upload_url_generated_at: None,
        }
    }
}

impl From<super::v1::Artifact> for Artifact {
    fn from(v1: super::v1::Artifact) -> Self {
        use chrono::Utc;
        Self {
            id: v1.id,
            created_at: v1.created_at,
            file_path: v1.file_path,
            session_id: v1.session_id,
            timestamp_observation: v1.timestamp_observation,
            // New fields in v3 - use defaults for migrated data
            modality: None,
            device_id: 0, // This needs to be set properly during migration
            updated_at: None,
            timestamp_observation_end: Utc::now().to_rfc3339(),
        }
    }
}

impl ArtifactLocal {
    /// Marks the artifact as having its file uploaded to storage
    pub fn mark_file_uploaded(&mut self) {
        self.has_uploaded_file_to_storage = true;
    }

    /// Marks the artifact as not having its file uploaded to storage
    pub fn mark_file_not_uploaded(&mut self) {
        self.has_uploaded_file_to_storage = false;
    }

    /// Returns whether the artifact's file has been uploaded to storage
    pub fn is_file_uploaded(&self) -> bool {
        self.has_uploaded_file_to_storage
    }

    /// Returns whether the artifact's file needs to be uploaded to storage
    pub fn needs_file_upload(&self) -> bool {
        !self.has_uploaded_file_to_storage
    }
}

// ===== MIGRATION FROM V1 TO V3 =====
impl From<super::v1::ConnectivityLocal> for ConnectivityLocal {
    fn from(v1: super::v1::ConnectivityLocal) -> Self {
        Self {
            id: v1.id,
            id_local: v1.id_local,
            session_id: Some(v1.session_id),
            device_id: None, // New field, default to None for migrated data
            ancestor_id_local: v1.ancestor_id_local,
            inserted_at: v1.inserted_at,
            timestamp_start: v1.timestamp_start,
            signal: v1.signal,
            noise: v1.noise,
            altitude: v1.altitude,
            heading: v1.heading,
            location: v1.location,
            h14_index: v1.h14_index,
            h13_index: v1.h13_index,
            h12_index: v1.h12_index,
            h11_index: v1.h11_index,
            // Default for v2 and v3 fields
            battery_percentage: None,
            frequency_hz: None,
            bandwidth_hz: None,
            associated_station: None,
        }
    }
}

impl From<super::v1::Connectivity> for Connectivity {
    fn from(v1: super::v1::Connectivity) -> Self {
        Self {
            id: v1.id,
            session_id: Some(v1.session_id),
            device_id: None, // New field, default to None for migrated data
            inserted_at: v1.inserted_at,
            timestamp_start: v1.timestamp_start,
            signal: v1.signal,
            noise: v1.noise,
            altitude: v1.altitude,
            heading: v1.heading,
            location: v1.location,
            h14_index: v1.h14_index,
            h13_index: v1.h13_index,
            h12_index: v1.h12_index,
            h11_index: v1.h11_index,
            // Default for v2 and v3 fields
            battery_percentage: None,
            frequency_hz: None,
            bandwidth_hz: None,
            associated_station: None,
        }
    }
}