From 241622ff7fd612eb521dec8829153851406c5884 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 7 Jan 2024 22:22:51 +0100 Subject: Store incoming frames in DB and show most recent 10 in dashboard --- src/ui.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'src/ui.rs') diff --git a/src/ui.rs b/src/ui.rs index aedf56c..3ce2ab8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Context}; use std::str::FromStr; use axum::Json; -use log::info; +use log::{info, warn}; use serde::Deserialize; use askama::Template; use axum::{ @@ -19,7 +19,7 @@ use ham_cats::{ whisker::Identification, }; -use crate::config; +use crate::{config, radio::MAX_PACKET_LEN}; use crate::SharedState; pub async fn serve(port: u16, shared_state: SharedState) { @@ -51,13 +51,64 @@ struct DashboardTemplate<'a> { title: &'a str, page: ActivePage, conf: config::Config, + packets: Vec, +} + +struct UIPacket { + pub received_at : i64, + + pub from_callsign : String, + pub from_ssid : u8, + + pub comment : Option, } async fn dashboard(State(state): State) -> DashboardTemplate<'static> { + let mut db = state.lock().unwrap().db.clone(); + + let packets = match db.get_most_recent_packets(10).await { + Ok(v) => v, + Err(e) => { + warn!("Dashboard will have empty packet list: {}", e); + Vec::new() + }, + }.iter() + .filter_map(|db_packet| { + let mut buf = [0; MAX_PACKET_LEN]; + match ham_cats::packet::Packet::fully_decode(&db_packet.content[2..], &mut buf) { + Ok(p) => { + if let Some(ident) = p.identification() { + + let mut commentbuf = [0; 1024]; + let comment = match p.comment(&mut commentbuf) { + Ok(c) => Some(c.to_owned()), + Err(_) => None, + }; + + Some(UIPacket { + received_at : db_packet.received_at, + from_callsign : ident.callsign.to_string(), + from_ssid : ident.ssid, + comment + }) + } + else { + None + } + }, + Err(e) => { + warn!("Failed to decode packet {}: {}", db_packet.id, e); + None + }, + } + }) + .collect(); + DashboardTemplate { title: "Dashboard", conf: state.lock().unwrap().conf.clone(), page: ActivePage::Dashboard, + packets } } -- cgit v1.2.3