aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/db.rs b/src/db.rs
index d5285ba..2e07792 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -2,7 +2,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use std::io;
use log::debug;
-use sqlx::SqlitePool;
+use sqlx::{SqlitePool, sqlite::SqliteRow, Row};
#[derive(Clone)]
pub struct Database {
@@ -10,13 +10,27 @@ pub struct Database {
num_frames_received : u64,
}
-#[derive(sqlx::FromRow, Debug)]
+#[derive(Debug)]
pub struct Packet {
pub id : i64,
- pub received_at : i64,
+ pub received_at: chrono::DateTime<chrono::Utc>,
pub content : Vec<u8>,
}
+impl sqlx::FromRow<'_, SqliteRow> for Packet {
+ fn from_row(row: &SqliteRow) -> Result<Self, sqlx::Error> {
+ Ok(Self {
+ id: row.try_get("id")?,
+ received_at: {
+ let row : i64 = row.try_get("received_at")?;
+ chrono::DateTime::from_timestamp(row, 0).expect("Convert timestamp to chrono")
+ },
+ content: row.try_get("content")?,
+
+ })
+ }
+}
+
impl Database {
pub async fn new() -> Self {
{
@@ -83,4 +97,17 @@ impl Database {
Ok(results)
}
+
+ pub async fn get_packets_since(&mut self, unix_timestamp: i64) -> anyhow::Result<Vec<Packet>> {
+ let results = sqlx::query_as(r#"
+ SELECT id, received_at, content
+ FROM frames_received
+ WHERE received_at > ?1
+ ORDER BY received_at DESC"#)
+ .bind(unix_timestamp)
+ .fetch_all(&self.pool)
+ .await?;
+
+ Ok(results)
+ }
}