|
1 | 1 | use axum::Json; |
2 | 2 | use axum::extract::State; |
3 | 3 | use axum::http::StatusCode; |
4 | | -use chrono::{DateTime, FixedOffset, Utc}; |
| 4 | +use chrono::Utc; |
5 | 5 | use log::error; |
6 | 6 | use serde::{Deserialize, Deserializer, Serialize}; |
7 | 7 | use std::sync::Arc; |
@@ -40,29 +40,13 @@ pub struct GpsData { |
40 | 40 | pub timestamp: i64, |
41 | 41 | } |
42 | 42 |
|
43 | | -impl GpsData { |
44 | | - pub fn to_datetime(&self) -> DateTime<FixedOffset> { |
45 | | - DateTime::from_timestamp(self.timestamp, 0) |
46 | | - .unwrap_or_default() |
47 | | - .fixed_offset() |
48 | | - } |
49 | | -} |
50 | | - |
51 | 43 | #[derive(Serialize, Deserialize)] |
52 | 44 | pub struct GpsRecord { |
53 | 45 | pub unix_ts: i64, |
54 | 46 | pub lat: f64, |
55 | 47 | pub lon: f64, |
56 | 48 | } |
57 | 49 |
|
58 | | -impl GpsRecord { |
59 | | - pub fn to_datetime(&self) -> DateTime<FixedOffset> { |
60 | | - DateTime::from_timestamp(self.unix_ts, 0) |
61 | | - .unwrap_or_default() |
62 | | - .fixed_offset() |
63 | | - } |
64 | | -} |
65 | | - |
66 | 50 | /// Reads all GPS records from a sidecar NDJSON file, skipping malformed lines. |
67 | 51 | pub async fn load_gps_records(file: tokio::fs::File) -> Vec<GpsRecord> { |
68 | 52 | let reader = BufReader::new(file); |
@@ -91,21 +75,21 @@ pub async fn post_gps( |
91 | 75 | drop(gps); |
92 | 76 |
|
93 | 77 | let qmdl_store = state.qmdl_store_lock.read().await; |
94 | | - if let Some((entry_idx, _)) = qmdl_store.get_current_entry() { |
95 | | - if let Ok(mut file) = qmdl_store.open_entry_gps_for_append(entry_idx).await { |
96 | | - let record = GpsRecord { |
97 | | - unix_ts: Utc::now().timestamp(), |
98 | | - lat: gps_data.latitude, |
99 | | - lon: gps_data.longitude, |
100 | | - }; |
101 | | - match serde_json::to_string(&record) { |
102 | | - Ok(json) => { |
103 | | - if let Err(e) = file.write_all(format!("{json}\n").as_bytes()).await { |
104 | | - error!("failed to write GPS record to sidecar: {e}"); |
105 | | - } |
| 78 | + if let Some((entry_idx, _)) = qmdl_store.get_current_entry() |
| 79 | + && let Ok(mut file) = qmdl_store.open_entry_gps_for_append(entry_idx).await |
| 80 | + { |
| 81 | + let record = GpsRecord { |
| 82 | + unix_ts: Utc::now().timestamp(), |
| 83 | + lat: gps_data.latitude, |
| 84 | + lon: gps_data.longitude, |
| 85 | + }; |
| 86 | + match serde_json::to_string(&record) { |
| 87 | + Ok(json) => { |
| 88 | + if let Err(e) = file.write_all(format!("{json}\n").as_bytes()).await { |
| 89 | + error!("failed to write GPS record to sidecar: {e}"); |
106 | 90 | } |
107 | | - Err(e) => error!("failed to serialize GPS record: {e}"), |
108 | 91 | } |
| 92 | + Err(e) => error!("failed to serialize GPS record: {e}"), |
109 | 93 | } |
110 | 94 | } |
111 | 95 |
|
|
0 commit comments