use axum::{
Json,
extract::State,
http::{StatusCode, header},
response::{Html, IntoResponse},
};
use log::debug;
use r200_uhf::Rfid;
use serde_json::json;
use std::sync::{Arc, RwLock};
pub async fn index_handler() -> Html<&'static str> {
Html(
r#"
<h1>TAG Reader Service</h1>
<p>Il servizio รจ attivo. Puoi leggere i dati della carta al seguente link:</p>
<a href="/read-tag.json">/read-tag.json</a>
"#,
)
}
pub async fn index_json_identification() -> impl IntoResponse {
let info = json!({ "single_rfid_reader": "ok" });
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json")],
Json(info.clone()),
)
.into_response()
}
pub async fn read_tag_handler(State(state): State<Arc<RwLock<Option<Rfid>>>>) -> impl IntoResponse {
let current = state.read().unwrap();
if current.is_some() {
debug!("Dati TAG presenti");
} else {
debug!("Nessun TAG presente");
}
if let Some(info) = &*current {
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json")],
Json(info.clone()),
)
.into_response()
} else {
(
StatusCode::NO_CONTENT,
[(header::CONTENT_TYPE, "application/json")],
Json(serde_json::json!({ "error": "Nessun TAG presente o dati non ancora letti" })),
)
.into_response()
}
}