mirror of
https://github.com/EFForg/rayhunter.git
synced 2026-06-07 05:31:54 -07:00
new merge conflict addressed, to_datetime unused code removed, some refactoring to appease tests
This commit is contained in:
committed by
Will Greenberg
parent
fee082cde4
commit
ba78c7bd01
+12
-15
@@ -164,21 +164,18 @@ impl DiagTask {
|
|||||||
// For fixed-mode sessions, write the configured coordinates to the sidecar
|
// For fixed-mode sessions, write the configured coordinates to the sidecar
|
||||||
// immediately so the per-session GPS is stored durably and isn't affected
|
// immediately so the per-session GPS is stored durably and isn't affected
|
||||||
// by future config changes or GPS API calls.
|
// by future config changes or GPS API calls.
|
||||||
if self.gps_mode == GpsMode::Fixed {
|
if self.gps_mode == GpsMode::Fixed
|
||||||
if let Some((lat, lon)) = self.gps_fixed_coords {
|
&& let Some((lat, lon)) = self.gps_fixed_coords
|
||||||
if let Some((entry_idx, _)) = qmdl_store.get_current_entry() {
|
&& let Some((entry_idx, _)) = qmdl_store.get_current_entry()
|
||||||
if let Ok(mut gps_file) = qmdl_store.open_entry_gps_for_append(entry_idx).await
|
&& let Ok(mut gps_file) = qmdl_store.open_entry_gps_for_append(entry_idx).await
|
||||||
{
|
{
|
||||||
let record = GpsRecord {
|
let record = GpsRecord {
|
||||||
unix_ts: 0,
|
unix_ts: 0,
|
||||||
lat,
|
lat,
|
||||||
lon,
|
lon,
|
||||||
};
|
};
|
||||||
if let Ok(json) = serde_json::to_string(&record) {
|
if let Ok(json) = serde_json::to_string(&record) {
|
||||||
let _ = gps_file.write_all(format!("{json}\n").as_bytes()).await;
|
let _ = gps_file.write_all(format!("{json}\n").as_bytes()).await;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.stop_current_recording().await;
|
self.stop_current_recording().await;
|
||||||
|
|||||||
+14
-30
@@ -1,7 +1,7 @@
|
|||||||
use axum::Json;
|
use axum::Json;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use chrono::{DateTime, FixedOffset, Utc};
|
use chrono::Utc;
|
||||||
use log::error;
|
use log::error;
|
||||||
use serde::{Deserialize, Deserializer, Serialize};
|
use serde::{Deserialize, Deserializer, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -40,14 +40,6 @@ pub struct GpsData {
|
|||||||
pub timestamp: i64,
|
pub timestamp: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GpsData {
|
|
||||||
pub fn to_datetime(&self) -> DateTime<FixedOffset> {
|
|
||||||
DateTime::from_timestamp(self.timestamp, 0)
|
|
||||||
.unwrap_or_default()
|
|
||||||
.fixed_offset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct GpsRecord {
|
pub struct GpsRecord {
|
||||||
pub unix_ts: i64,
|
pub unix_ts: i64,
|
||||||
@@ -55,14 +47,6 @@ pub struct GpsRecord {
|
|||||||
pub lon: f64,
|
pub lon: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GpsRecord {
|
|
||||||
pub fn to_datetime(&self) -> DateTime<FixedOffset> {
|
|
||||||
DateTime::from_timestamp(self.unix_ts, 0)
|
|
||||||
.unwrap_or_default()
|
|
||||||
.fixed_offset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reads all GPS records from a sidecar NDJSON file, skipping malformed lines.
|
/// Reads all GPS records from a sidecar NDJSON file, skipping malformed lines.
|
||||||
pub async fn load_gps_records(file: tokio::fs::File) -> Vec<GpsRecord> {
|
pub async fn load_gps_records(file: tokio::fs::File) -> Vec<GpsRecord> {
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
@@ -91,21 +75,21 @@ pub async fn post_gps(
|
|||||||
drop(gps);
|
drop(gps);
|
||||||
|
|
||||||
let qmdl_store = state.qmdl_store_lock.read().await;
|
let qmdl_store = state.qmdl_store_lock.read().await;
|
||||||
if let Some((entry_idx, _)) = qmdl_store.get_current_entry() {
|
if let Some((entry_idx, _)) = qmdl_store.get_current_entry()
|
||||||
if let Ok(mut file) = qmdl_store.open_entry_gps_for_append(entry_idx).await {
|
&& let Ok(mut file) = qmdl_store.open_entry_gps_for_append(entry_idx).await
|
||||||
let record = GpsRecord {
|
{
|
||||||
unix_ts: Utc::now().timestamp(),
|
let record = GpsRecord {
|
||||||
lat: gps_data.latitude,
|
unix_ts: Utc::now().timestamp(),
|
||||||
lon: gps_data.longitude,
|
lat: gps_data.latitude,
|
||||||
};
|
lon: gps_data.longitude,
|
||||||
match serde_json::to_string(&record) {
|
};
|
||||||
Ok(json) => {
|
match serde_json::to_string(&record) {
|
||||||
if let Err(e) = file.write_all(format!("{json}\n").as_bytes()).await {
|
Ok(json) => {
|
||||||
error!("failed to write GPS record to sidecar: {e}");
|
if let Err(e) = file.write_all(format!("{json}\n").as_bytes()).await {
|
||||||
}
|
error!("failed to write GPS record to sidecar: {e}");
|
||||||
}
|
}
|
||||||
Err(e) => error!("failed to serialize GPS record: {e}"),
|
|
||||||
}
|
}
|
||||||
|
Err(e) => error!("failed to serialize GPS record: {e}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-9
@@ -96,17 +96,17 @@ pub(crate) async fn load_gps_records_for_entry(
|
|||||||
// not the current config, so old fixed-mode sessions still get coordinates even
|
// not the current config, so old fixed-mode sessions still get coordinates even
|
||||||
// if the mode has since been changed. Use the configured fixed coords directly
|
// if the mode has since been changed. Use the configured fixed coords directly
|
||||||
// rather than gps_state, which can be overwritten by API calls or be None.
|
// rather than gps_state, which can be overwritten by API calls or be None.
|
||||||
if entry_gps_mode == Some(GpsMode::Fixed) {
|
if entry_gps_mode == Some(GpsMode::Fixed)
|
||||||
if let (Some(lat), Some(lon)) = (
|
&& let (Some(lat), Some(lon)) = (
|
||||||
state.config.gps_fixed_latitude,
|
state.config.gps_fixed_latitude,
|
||||||
state.config.gps_fixed_longitude,
|
state.config.gps_fixed_longitude,
|
||||||
) {
|
)
|
||||||
return vec![GpsRecord {
|
{
|
||||||
unix_ts: 0,
|
return vec![GpsRecord {
|
||||||
lat,
|
unix_ts: 0,
|
||||||
lon,
|
lat,
|
||||||
}];
|
lon,
|
||||||
}
|
}];
|
||||||
}
|
}
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -551,7 +551,7 @@ mod tests {
|
|||||||
async fn test_creating_updating_and_closing_entries() {
|
async fn test_creating_updating_and_closing_entries() {
|
||||||
let dir = make_temp_dir();
|
let dir = make_temp_dir();
|
||||||
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
||||||
let _ = store.new_entry(0).await.unwrap();
|
let _ = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
let entry_index = store.current_entry.unwrap();
|
let entry_index = store.current_entry.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
RecordingStore::read_manifest(dir.path()).await.unwrap(),
|
RecordingStore::read_manifest(dir.path()).await.unwrap(),
|
||||||
@@ -588,7 +588,7 @@ mod tests {
|
|||||||
async fn test_create_on_existing_store() {
|
async fn test_create_on_existing_store() {
|
||||||
let dir = make_temp_dir();
|
let dir = make_temp_dir();
|
||||||
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
||||||
let _ = store.new_entry(0).await.unwrap();
|
let _ = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
let entry_index = store.current_entry.unwrap();
|
let entry_index = store.current_entry.unwrap();
|
||||||
store
|
store
|
||||||
.update_entry_qmdl_size(entry_index, 1000)
|
.update_entry_qmdl_size(entry_index, 1000)
|
||||||
@@ -602,9 +602,9 @@ mod tests {
|
|||||||
async fn test_repeated_new_entries() {
|
async fn test_repeated_new_entries() {
|
||||||
let dir = make_temp_dir();
|
let dir = make_temp_dir();
|
||||||
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
||||||
let _ = store.new_entry(0).await.unwrap();
|
let _ = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
let entry_index = store.current_entry.unwrap();
|
let entry_index = store.current_entry.unwrap();
|
||||||
let _ = store.new_entry(0).await.unwrap();
|
let _ = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
let new_entry_index = store.current_entry.unwrap();
|
let new_entry_index = store.current_entry.unwrap();
|
||||||
assert_ne!(entry_index, new_entry_index);
|
assert_ne!(entry_index, new_entry_index);
|
||||||
assert_eq!(store.manifest.entries.len(), 2);
|
assert_eq!(store.manifest.entries.len(), 2);
|
||||||
@@ -614,7 +614,7 @@ mod tests {
|
|||||||
async fn test_delete_all_entries() {
|
async fn test_delete_all_entries() {
|
||||||
let dir = make_temp_dir();
|
let dir = make_temp_dir();
|
||||||
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
let mut store = RecordingStore::create(dir.path()).await.unwrap();
|
||||||
let _ = store.new_entry(0).await.unwrap();
|
let _ = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
assert!(store.current_entry.is_some());
|
assert!(store.current_entry.is_some());
|
||||||
|
|
||||||
store.delete_all_entries().await.unwrap();
|
store.delete_all_entries().await.unwrap();
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ use crate::analysis::{AnalysisCtrlMessage, AnalysisStatus};
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::diag::DiagDeviceCtrlMessage;
|
use crate::diag::DiagDeviceCtrlMessage;
|
||||||
use crate::display::DisplayState;
|
use crate::display::DisplayState;
|
||||||
use crate::notifications::DEFAULT_NOTIFICATION_TIMEOUT;
|
|
||||||
use crate::gps::GpsData;
|
use crate::gps::GpsData;
|
||||||
|
use crate::notifications::DEFAULT_NOTIFICATION_TIMEOUT;
|
||||||
use crate::pcap::{generate_pcap_data, load_gps_records_for_entry};
|
use crate::pcap::{generate_pcap_data, load_gps_records_for_entry};
|
||||||
use crate::qmdl_store::RecordingStore;
|
use crate::qmdl_store::RecordingStore;
|
||||||
|
|
||||||
@@ -509,6 +509,7 @@ pub async fn debug_set_display_state(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::config::GpsMode;
|
||||||
use async_zip::base::read::mem::ZipFileReader;
|
use async_zip::base::read::mem::ZipFileReader;
|
||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
@@ -528,7 +529,7 @@ mod tests {
|
|||||||
) -> String {
|
) -> String {
|
||||||
let entry_name = {
|
let entry_name = {
|
||||||
let mut store = store_lock.write().await;
|
let mut store = store_lock.write().await;
|
||||||
let (mut qmdl_file, _analysis_file) = store.new_entry(0).await.unwrap();
|
let (mut qmdl_file, _analysis_file) = store.new_entry(GpsMode::Disabled).await.unwrap();
|
||||||
|
|
||||||
if !test_data.is_empty() {
|
if !test_data.is_empty() {
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
|
|||||||
Reference in New Issue
Block a user