From 37b16a6ae12b874ba203778e69424dc0bce44ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 11 Dec 2024 17:06:52 -0300 Subject: [PATCH 1/2] web: v1: rest: mavlink: Add helper 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/routes/v1/rest/mavlink.rs | 47 +++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/lib/web/routes/v1/rest/mavlink.rs b/src/lib/web/routes/v1/rest/mavlink.rs index 12d2824..16363d4 100644 --- a/src/lib/web/routes/v1/rest/mavlink.rs +++ b/src/lib/web/routes/v1/rest/mavlink.rs @@ -1,13 +1,20 @@ use std::net::SocketAddr; use axum::{ - extract::{ConnectInfo, Path}, - http::StatusCode, + extract::{ConnectInfo, Path, Query}, + http::{header, StatusCode}, response::IntoResponse, Json, }; +use clap::error; +use serde::Deserialize; use tracing::*; +use crate::{ + drivers::rest::parse_query, + mavlink_json::{MAVLinkJSON, MAVLinkJSONHeader}, +}; + pub(crate) async fn mavlink(path: Option>) -> impl IntoResponse { let path = match path { Some(path) => path.0.to_string(), @@ -28,6 +35,42 @@ pub(crate) async fn post_mavlink( } } +#[derive(Deserialize)] +pub struct MessageInfo { + pub name: String, +} + +pub(crate) async fn helper(name: Query) -> impl IntoResponse { + let message_name = name.0.name.to_ascii_uppercase(); + + let result = ::message_id_from_name( + &message_name, + ) + .and_then(|id| { + ::default_message_from_id(id) + }); + + match result { + Ok(message) => { + let header = MAVLinkJSONHeader { + inner: Default::default(), + message_id: Some(mavlink::Message::message_id(&message)), + }; + let json = serde_json::to_string_pretty(&MAVLinkJSON { header, message }).unwrap(); + ([(header::CONTENT_TYPE, "application/json")], json).into_response() + } + Err(error) => { + let error_json = serde_json::to_string_pretty(&error).unwrap(); + ( + StatusCode::NOT_FOUND, + [(header::CONTENT_TYPE, "application/json")], + error_json, + ) + .into_response() + } + } +} + pub(crate) 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()) From ad01dd6eb5d045f8588607deb4d20ebdcc776b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 11 Dec 2024 17:07:04 -0300 Subject: [PATCH 2/2] web: routes: v1: Add rest/helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib/web/routes/v1/rest/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/web/routes/v1/rest/mod.rs b/src/lib/web/routes/v1/rest/mod.rs index 16f0c0c..e86e234 100644 --- a/src/lib/web/routes/v1/rest/mod.rs +++ b/src/lib/web/routes/v1/rest/mod.rs @@ -8,6 +8,7 @@ use tracing::*; pub fn router() -> Router { Router::new() .route("/ws", get(websocket::websocket_handler)) + .route("/helper", get(mavlink::helper)) .route( "/mavlink", get(mavlink::mavlink).post(mavlink::post_mavlink),