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

Versionize API + Improvements #100

Merged
merged 11 commits into from
Nov 16, 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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ shellexpand = "3.1"
tokio = { version = "1", features = ["full"] }
tokio-serial = "5.4.4"
tokio-util = { version = "0.7", features = [ "codec", "net" ] }
tower = { version = "0.5" }
tower-http = { version = "0.6", features = ["normalize-path", "trace", "cors"] }
once_cell = "1.20"
url = { version = "2.5.2", features = ["serde"] }
uuid = { version = "1", features = ["v5", "v4", "serde"] }
Expand Down
21 changes: 11 additions & 10 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub struct Args {
/// Sets the MAVLink version used to communicate. This changes the heartbeat messages sent
#[arg(long, default_value = "2", value_names = ["1", "2"])]
mavlink_version: u8,

/// Sets the default version used by the REST API, this will remove the prefix used by its path.
#[arg(long, default_value = "1", value_names = ["1"])]
default_api_version: u8,
}

#[instrument(level = "trace")]
Expand Down Expand Up @@ -162,11 +166,7 @@ pub fn is_verbose() -> bool {

#[instrument(level = "debug")]
pub fn is_tracing() -> bool {
MANAGER
.get()
.unwrap()
.clap_matches
.enable_tracing_level_log_file
args().enable_tracing_level_log_file
}

#[instrument(level = "debug")]
Expand Down Expand Up @@ -227,11 +227,7 @@ pub fn mavlink_component_id() -> u8 {

#[instrument(level = "debug")]
pub fn mavlink_heartbeat_frequency() -> f32 {
MANAGER
.get()
.unwrap()
.clap_matches
.mavlink_heartbeat_frequency
args().mavlink_heartbeat_frequency
}

#[instrument(level = "debug")]
Expand All @@ -244,6 +240,11 @@ pub fn mavlink_version() -> u8 {
args().mavlink_version
}

#[instrument(level = "debug")]
pub fn default_api_version() -> u8 {
args().default_api_version
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
11 changes: 8 additions & 3 deletions src/lib/drivers/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod data;
use std::sync::Arc;

use anyhow::Result;
use axum::extract::ws;
use tokio::sync::{broadcast, RwLock};
use tracing::*;

Expand All @@ -15,6 +16,7 @@ use crate::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
},
web::routes::v1::rest::websocket,
};

#[derive(Debug)]
Expand Down Expand Up @@ -111,6 +113,9 @@ impl Rest {
async fn send_task(context: &SendReceiveContext) -> Result<()> {
let mut hub_receiver = context.hub_sender.subscribe();

let origin = "Ws";
let uuid = uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, origin.as_bytes());

loop {
let message = match hub_receiver.recv().await {
Ok(message) => message,
Expand All @@ -124,7 +129,7 @@ impl Rest {
}
};

if message.origin.eq("Ws") {
if message.origin.eq(origin) {
continue; // Don't do loopback
}

Expand All @@ -146,7 +151,7 @@ impl Rest {
let json_string = parse_query(&mavlink_json);
data::update((mavlink_json.header, mavlink_json.message));

crate::web::send_message(json_string).await;
websocket::broadcast(uuid, ws::Message::Text(json_string)).await;
}

debug!("Driver sender task stopped!");
Expand Down Expand Up @@ -181,7 +186,7 @@ impl Driver for Rest {
interval.tick().await;
}

let mut ws_receiver = crate::web::create_message_receiver();
let mut ws_receiver = websocket::create_message_receiver();

tokio::select! {
result = Rest::send_task(&context) => {
Expand Down
111 changes: 0 additions & 111 deletions src/lib/web/endpoints.rs

This file was deleted.

106 changes: 0 additions & 106 deletions src/lib/web/mavlink_endpoints.rs

This file was deleted.

Loading