-
Notifications
You must be signed in to change notification settings - Fork 4
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
joaoantoniocardoso
merged 2 commits into
bluerobotics:master
from
patrickelectric:helper
Dec 12, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||||||
|
@@ -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(); | ||||||
([(header::CONTENT_TYPE, "application/json")], json).into_response() | ||||||
} | ||||||
Err(error) => { | ||||||
let error_json = serde_json::to_string_pretty(&error).unwrap(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then:
There was a problem hiding this comment.
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 canThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, just use expect, then, so I won't get confused when reading the code in the future.
There was a problem hiding this comment.
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.