Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: v1: rest: mavlink: Add helper endpoint #104

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/lib/web/routes/v1/rest/mavlink.rs
Original file line number Diff line number Diff line change
@@ -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<Path<String>>) -> impl IntoResponse {
let path = match path {
Some(path) => path.0.to_string(),
Expand All @@ -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<MessageInfo>) -> impl IntoResponse {
let message_name = name.0.name.to_ascii_uppercase();

let result = <mavlink::ardupilotmega::MavMessage as mavlink::Message>::message_id_from_name(
&message_name,
)
.and_then(|id| {
<mavlink::ardupilotmega::MavMessage as mavlink::Message>::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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let json = serde_json::to_string_pretty(&MAVLinkJSON { header, message }).unwrap();
let json = serde_json::to_string_pretty(&MAVLinkJSON { header, message })?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this operation will never fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then:

Suggested change
let json = serde_json::to_string_pretty(&MAVLinkJSON { header, message }).unwrap();
let json = serde_json::to_string_pretty(&MAVLinkJSON { header, message }).expect("Should never fail here");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, even if something never fails, it's still okay to use ? if we can

Copy link
Member Author

@patrickelectric patrickelectric Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this will generate an invalid HTTP response that's not a json. Does not make more sense to add verbose code or a possible early return to a clear impossible operation. unwrap is there for a reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this will generate an invalid HTTP response that's not a json. Does not make more sense to add verbose code or a possible early return to a clear impossible operation. unwrap is there for a reason.

right, just use expect, then, so I won't get confused when reading the code in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trivial operation, the type is known in compile time.

([(header::CONTENT_TYPE, "application/json")], json).into_response()
}
Err(error) => {
let error_json = serde_json::to_string_pretty(&error).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let error_json = serde_json::to_string_pretty(&error).unwrap();
let error_json = serde_json::to_string_pretty(&error)?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation should never fail.

(
StatusCode::NOT_FOUND,
[(header::CONTENT_TYPE, "application/json")],
error_json,
)
.into_response()
}
}
}

pub(crate) async fn message_id_from_name(name: Path<String>) -> impl IntoResponse {
use mavlink::{self, Message};
mavlink::ardupilotmega::MavMessage::message_id_from_name(&name.0.to_ascii_uppercase())
Expand Down
1 change: 1 addition & 0 deletions src/lib/web/routes/v1/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading