diff --git a/Cargo.lock b/Cargo.lock index bbbb450..9df4904 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,7 @@ dependencies = [ "fallible-streaming-iterator", "hashlink", "libsqlite3-sys", + "serde_json", "smallvec", ] diff --git a/Cargo.toml b/Cargo.toml index 212eaf7..59bf953 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] -rusqlite = "0.37.0" +rusqlite = {version = "0.37.0", features = ["serde_json"]} serde_json = "1.0.145" diff --git a/src/main.rs b/src/main.rs index fc9add0..f34c54a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use rusqlite::{params, Connection, Result}; // Database operations and result ha use std::time::{SystemTime, UNIX_EPOCH}; use serde_json::{Value}; - fn main() -> std::io::Result<()> { // Set up db and make sure it works @@ -39,8 +38,12 @@ fn main() -> std::io::Result<()> { // look for type code in JSON here, only insert if data let p: Value = serde_json::from_str(p_raw)?; + //p.what_is_this; + + //println!("{}",p["type"]); + if p["type"] != "status" { - let _ = insert_packet(p_raw); + let _ = insert_packet(p); } // Echo the data back to the client @@ -58,8 +61,11 @@ fn create_database() -> Result<()> { conn.execute( "CREATE TABLE IF NOT EXISTS packets ( id INTEGER PRIMARY KEY AUTOINCREMENT, - raw TEXT NOT NULL, - date INTEGER NOT NULL + date INTEGER NOT NULL, + source TEXT, + summary TEXT, + final_destination TEXT, + text TEXT )", [], // No parameters needed )?; @@ -68,16 +74,29 @@ fn create_database() -> Result<()> { Ok(()) } -fn insert_packet(raw: &str) -> Result<()> { +fn insert_packet(p_json: serde_json::Value) -> Result<()> { let conn = Connection::open("pkt_database.db")?; let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + //println!{"{}", p_json["type"]} + + //println!("{}", p_json); + //println!("{}", &p_json["source"]); + // Insert conn.execute( - "INSERT INTO packets (raw, date) VALUES (?1, ?2)", - params![raw, ds], // Bind parameters - )?; + "INSERT INTO packets (date, + source, + summary, + final_destination, + text) VALUES (?1, ?2, ?3, ?4, ?5)", + params![ds, + &p_json["source"], + &p_json["summary"], + &p_json["final_destination"], + &p_json["text"]], // Bind parameters + )?; //println!("Row inserted successfully."); Ok(())