mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-10-13 15:06:39 +00:00
implemented SSE
This commit is contained in:
parent
aa91d69f0b
commit
9dd2f88cbc
@ -60,7 +60,7 @@ async fn main(mut spawner: Spawner) {
|
||||
|
||||
wait_for_stack_up(stack).await;
|
||||
|
||||
start_webserver(&mut spawner, stack, shared_store.clone());
|
||||
start_webserver(&mut spawner, stack, shared_store.clone(), chan);
|
||||
|
||||
/****************************** Spawning tasks ***********************************/
|
||||
debug!("spawing NFC reader task...");
|
||||
|
@ -4,7 +4,6 @@ use picoserve::{
|
||||
response::{self, IntoResponse},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
store::{Name, hex_string_to_tally_id},
|
||||
webserver::{app::AppState, sse::IDEvents},
|
||||
@ -40,3 +39,7 @@ pub async fn add_mapping(
|
||||
let tally_id = hex_string_to_tally_id(&data.id).unwrap();
|
||||
store.mapping.add_mapping(tally_id, data.name);
|
||||
}
|
||||
|
||||
pub async fn get_idevent(State(state): State<AppState>) -> impl IntoResponse{
|
||||
response::EventStream(IDEvents(state.chan.subscriber().unwrap()))
|
||||
}
|
||||
|
@ -3,15 +3,17 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
|
||||
use picoserve::{AppWithStateBuilder, routing::get};
|
||||
|
||||
use crate::{
|
||||
TallyChannel, UsedStore,
|
||||
webserver::{
|
||||
api::{add_mapping, get_mapping},
|
||||
api::{add_mapping, get_idevent, get_mapping},
|
||||
assets::Assets,
|
||||
}, UsedStore,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub store: Rc<Mutex<CriticalSectionRawMutex, UsedStore>>,
|
||||
pub chan: &'static TallyChannel,
|
||||
}
|
||||
|
||||
pub struct AppProps;
|
||||
@ -23,9 +25,6 @@ impl AppWithStateBuilder for AppProps {
|
||||
fn build_app(self) -> picoserve::Router<Self::PathRouter, AppState> {
|
||||
picoserve::Router::from_service(Assets)
|
||||
.route("/api/mapping", get(get_mapping).post(add_mapping))
|
||||
// .route(
|
||||
// "/api/idevent",
|
||||
// get(move || response::EventStream(Events(self.chan))),
|
||||
// )
|
||||
.route("/api/idevent", get(get_idevent))
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ use picoserve::{AppRouter, AppWithStateBuilder};
|
||||
use static_cell::make_static;
|
||||
|
||||
use crate::{
|
||||
UsedStore,
|
||||
TallyChannel, UsedStore,
|
||||
webserver::app::{AppProps, AppState},
|
||||
};
|
||||
|
||||
mod assets;
|
||||
// mod sse;
|
||||
mod api;
|
||||
mod app;
|
||||
mod assets;
|
||||
mod sse;
|
||||
|
||||
pub const WEB_TAKS_SIZE: usize = 3; // Up this number if request start fail with Timeouts.
|
||||
|
||||
@ -22,10 +22,11 @@ pub fn start_webserver(
|
||||
spawner: &mut Spawner,
|
||||
stack: Stack<'static>,
|
||||
store: Rc<Mutex<CriticalSectionRawMutex, UsedStore>>,
|
||||
chan: &'static TallyChannel,
|
||||
) {
|
||||
let app = make_static!(AppProps.build_app());
|
||||
|
||||
let state = make_static!(AppState { store });
|
||||
let state = make_static!(AppState { store, chan });
|
||||
|
||||
let config = make_static!(picoserve::Config::new(picoserve::Timeouts {
|
||||
start_read_request: Some(Duration::from_secs(5)),
|
||||
|
@ -2,9 +2,11 @@ use embassy_time::{Duration, Timer};
|
||||
use log::warn;
|
||||
use picoserve::response;
|
||||
|
||||
pub struct Events(pub TallySubscriber);
|
||||
use crate::{TallySubscriber, store::tally_id_to_hex_string};
|
||||
|
||||
impl response::sse::EventSource for Events {
|
||||
pub struct IDEvents(pub TallySubscriber);
|
||||
|
||||
impl response::sse::EventSource for IDEvents {
|
||||
async fn write_events<W: picoserve::io::Write>(
|
||||
mut self,
|
||||
mut writer: response::sse::EventWriter<W>,
|
||||
@ -16,7 +18,9 @@ impl response::sse::EventSource for Events {
|
||||
match sel.await {
|
||||
embassy_futures::select::Either::First(msg) => match msg {
|
||||
embassy_sync::pubsub::WaitResult::Message(id) => {
|
||||
writer.write_event("msg", id.to_string().as_str()).await?
|
||||
writer
|
||||
.write_event("msg", tally_id_to_hex_string(id).unwrap().as_str())
|
||||
.await?
|
||||
}
|
||||
embassy_sync::pubsub::WaitResult::Lagged(_) => {
|
||||
warn!("SSE subscriber got lagged");
|
||||
|
Loading…
x
Reference in New Issue
Block a user