diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-07 22:22:51 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-07 22:22:51 +0100 |
commit | 241622ff7fd612eb521dec8829153851406c5884 (patch) | |
tree | 3d7b17caa99c7b08f01d94fad9cbdca249ff2612 /src/ui.rs | |
parent | b40299538a73d25d096d8f58f1468cd7f647a3f9 (diff) | |
download | cats-radio-node-241622ff7fd612eb521dec8829153851406c5884.tar.gz cats-radio-node-241622ff7fd612eb521dec8829153851406c5884.tar.bz2 cats-radio-node-241622ff7fd612eb521dec8829153851406c5884.zip |
Store incoming frames in DB and show most recent 10 in dashboard
Diffstat (limited to 'src/ui.rs')
-rw-r--r-- | src/ui.rs | 55 |
1 files changed, 53 insertions, 2 deletions
@@ -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<UIPacket>, +} + +struct UIPacket { + pub received_at : i64, + + pub from_callsign : String, + pub from_ssid : u8, + + pub comment : Option<String>, } async fn dashboard(State(state): State<SharedState>) -> 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 } } |