From 812c6b0e85a8a9ee34c5c440b75148873f78ad68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 29 Oct 2024 17:21:25 +0900 Subject: [PATCH] web: Add mavlink post endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib/web/endpoints.rs | 10 +++++++++- src/lib/web/mod.rs | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/web/endpoints.rs b/src/lib/web/endpoints.rs index 4e0c317b..88312161 100644 --- a/src/lib/web/endpoints.rs +++ b/src/lib/web/endpoints.rs @@ -1,5 +1,5 @@ use axum::{ - extract::Path, + extract::{Path, State}, http::{header, StatusCode}, response::IntoResponse, Json, @@ -7,8 +7,10 @@ use axum::{ use include_dir::{include_dir, Dir}; use mime_guess::from_path; use serde::Serialize; +use tracing::*; use crate::stats; +use crate::web::AppState; static HTML_DIST: Dir = include_dir!("src/webpage/dist"); @@ -81,6 +83,12 @@ pub async fn mavlink(path: Option>) -> impl IntoResponse { crate::drivers::rest::data::messages(&path) } +pub async fn post_mavlink(State(state): State, message: String) { + if let Err(error) = state.message_tx.send(message) { + error!("Failed to send message to main loop: {error:?}"); + } +} + pub async fn message_id_from_name(name: Path) -> impl IntoResponse { use mavlink::{self, Message}; mavlink::ardupilotmega::MavMessage::message_id_from_name(&name.0.to_ascii_uppercase()) diff --git a/src/lib/web/mod.rs b/src/lib/web/mod.rs index ff9203a1..69dca5fc 100644 --- a/src/lib/web/mod.rs +++ b/src/lib/web/mod.rs @@ -12,7 +12,7 @@ use axum::{ }, http::StatusCode, response::Response, - routing::get, + routing::{get, post}, Router, }; use futures::{sink::SinkExt, stream::StreamExt}; @@ -40,6 +40,7 @@ fn default_router(state: AppState) -> Router { .route("/rest/ws", get(websocket_handler)) // We are matching all possible keys for the user .route("/rest/mavlink", get(endpoints::mavlink)) + .route("/rest/mavlink", post(endpoints::post_mavlink)) .route("/rest/mavlink/", get(endpoints::mavlink)) .route("/rest/mavlink/*path", get(endpoints::mavlink)) .route(