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

Improve mavlink json #94

Merged
24 changes: 10 additions & 14 deletions src/lib/drivers/rest/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use lazy_static::lazy_static;
use mavlink::{self, Message};
use serde::{Deserialize, Serialize};

use crate::mavlink_json::{MAVLinkJSON, MAVLinkJSONHeader};

lazy_static! {
static ref DATA: Data = Data {
messages: Arc::new(Mutex::new(MAVLinkVehiclesData::default())),
Expand All @@ -24,8 +26,8 @@ pub struct MAVLinkVehiclesData {
}

impl MAVLinkVehiclesData {
fn update(&mut self, message: MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
let vehicle_id = message.header.system_id;
fn update(&mut self, message: MAVLinkJSON<mavlink::ardupilotmega::MavMessage>) {
let vehicle_id = message.header.inner.system_id;
self.vehicles
.entry(vehicle_id)
.or_insert(MAVLinkVehicleData {
Expand Down Expand Up @@ -63,8 +65,8 @@ struct MAVLinkVehicleData {
}

impl MAVLinkVehicleData {
fn update(&mut self, message: MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
let component_id = message.header.component_id;
fn update(&mut self, message: MAVLinkJSON<mavlink::ardupilotmega::MavMessage>) {
let component_id = message.header.inner.component_id;
self.components
.entry(component_id)
.or_insert(MAVLinkVehicleComponentData {
Expand All @@ -82,7 +84,7 @@ struct MAVLinkVehicleComponentData {
}

impl MAVLinkVehicleComponentData {
fn update(&mut self, message: MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
fn update(&mut self, message: MAVLinkJSON<mavlink::ardupilotmega::MavMessage>) {
let message_name = message.message.message_name().to_string();
self.messages
.entry(message_name)
Expand All @@ -101,18 +103,12 @@ struct MAVLinkMessageStatus {
}

impl MAVLinkMessageStatus {
fn update(&mut self, message: MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
fn update(&mut self, message: MAVLinkJSON<mavlink::ardupilotmega::MavMessage>) {
self.message = message.message;
self.status.update();
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct MAVLinkMessage<T> {
pub header: mavlink::MavHeader,
pub message: T,
}

#[derive(Default, Debug, Deserialize, Serialize)]
struct Status {
time: Temporal,
Expand Down Expand Up @@ -154,11 +150,11 @@ impl Temporal {
}
}

pub fn update((header, message): (mavlink::MavHeader, mavlink::ardupilotmega::MavMessage)) {
pub fn update((header, message): (MAVLinkJSONHeader, mavlink::ardupilotmega::MavMessage)) {
DATA.messages
.lock()
.unwrap()
.update(MAVLinkMessage { header, message });
.update(MAVLinkJSON { header, message });
}

pub fn messages(path: &str) -> String {
Expand Down
20 changes: 10 additions & 10 deletions src/lib/drivers/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@ use std::sync::Arc;
use anyhow::Result;
use mavlink::ardupilotmega::MavMessage;
use mavlink_codec::Packet;
use serde::{Deserialize, Serialize};
use tokio::sync::{broadcast, RwLock};
use tracing::*;

use crate::{
callbacks::{Callbacks, MessageCallback},
drivers::{Driver, DriverInfo},
mavlink_json::{MAVLinkJSON, MAVLinkJSONHeader},
protocol::Protocol,
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
},
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MAVLinkMessage<T> {
pub header: mavlink::MavHeader,
pub message: T,
}

#[derive(Debug)]
pub struct Rest {
name: arc_swap::ArcSwap<String>,
Expand Down Expand Up @@ -82,10 +76,10 @@ impl Rest {
match ws_receiver.recv().await {
Ok(message) => {
if let Ok(content) =
json5::from_str::<MAVLinkMessage<mavlink::ardupilotmega::MavMessage>>(&message)
json5::from_str::<MAVLinkJSON<mavlink::ardupilotmega::MavMessage>>(&message)
{
let mut message_raw = mavlink::MAVLinkV2MessageRaw::new();
message_raw.serialize_message(content.header, &content.message);
message_raw.serialize_message(content.header.inner, &content.message);
let bus_message = Arc::new(Protocol::new("Ws", Packet::from(message_raw)));
stats.write().await.stats.update_input(&bus_message);

Expand Down Expand Up @@ -139,7 +133,13 @@ impl Rest {
continue;
};

let mavlink_message = MAVLinkMessage { header, message };
let header = MAVLinkJSONHeader {
inner: header,
message_id: Some(mavlink::Message::message_id(&message)),
};

let mavlink_message = MAVLinkJSON { header, message };

let json_string = parse_query(&mavlink_message);
data::update((header, mavlink_message.message));
crate::web::send_message(json_string).await;
Expand Down
21 changes: 10 additions & 11 deletions src/lib/drivers/zenoh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ use std::sync::Arc;
use anyhow::Result;
use mavlink::{self, Message};
use mavlink_codec::Packet;
use serde::{Deserialize, Serialize};
use tokio::sync::{broadcast, RwLock};
use tracing::*;
use zenoh;

use crate::{
callbacks::{Callbacks, MessageCallback},
drivers::{generic_tasks::SendReceiveContext, Driver, DriverInfo},
mavlink_json::{MAVLinkJSON, MAVLinkJSONHeader},
protocol::Protocol,
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
},
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MAVLinkMessage<T> {
pub header: mavlink::MavHeader,
pub message: T,
}

#[derive(Debug)]
pub struct Zenoh {
name: arc_swap::ArcSwap<String>,
Expand Down Expand Up @@ -89,7 +83,7 @@ impl Zenoh {
};

while let Ok(sample) = subscriber.recv_async().await {
let Ok(content) = json5::from_str::<MAVLinkMessage<mavlink::ardupilotmega::MavMessage>>(
let Ok(content) = json5::from_str::<MAVLinkJSON<mavlink::ardupilotmega::MavMessage>>(
std::str::from_utf8(&sample.payload().to_bytes()).unwrap(),
) else {
debug!("Failed to parse message, not a valid MAVLinkMessage: {sample:?}");
Expand All @@ -98,7 +92,7 @@ impl Zenoh {

let bus_message = {
let mut message_raw = mavlink::MAVLinkV2MessageRaw::new();
message_raw.serialize_message(content.header, &content.message);
message_raw.serialize_message(content.header.inner, &content.message);

Arc::new(Protocol::new("zenoh", Packet::from(message_raw)))
};
Expand Down Expand Up @@ -167,8 +161,13 @@ impl Zenoh {
continue;
};

let header = MAVLinkJSONHeader {
inner: header,
message_id: Some(mavlink::Message::message_id(&message)),
};

let message_name = message.message_name();
let mavlink_message = MAVLinkMessage { header, message };
let mavlink_message = MAVLinkJSON { header, message };
let json_string = &match json5::to_string(&mavlink_message) {
Ok(json) => json,
Err(error) => {
Expand All @@ -186,7 +185,7 @@ impl Zenoh {

let topic_name = &format!(
"mavlink/{}/{}/{}",
header.system_id, header.component_id, message_name
header.inner.system_id, header.inner.component_id, message_name
);
if let Err(error) = session.put(topic_name, json_string).await {
error!("Failed to send message to {topic_name}: {error:?}");
Expand Down
18 changes: 18 additions & 0 deletions src/lib/mavlink_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};

/// Improved and back-compatible with our previous struct called `MAVLinkMessage`
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub struct MAVLinkJSON<T: mavlink::Message> {
pub header: MAVLinkJSONHeader,
pub message: T,
}

/// Improved and back-compatible with mavlink::MavHeader
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub struct MAVLinkJSONHeader {
#[serde(flatten)]
/// The original MavHeader
pub inner: mavlink::MavHeader,
/// Optional Message ID, missing in the original header
pub message_id: Option<u32>,
}
1 change: 1 addition & 0 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod cli;
pub mod drivers;
pub mod hub;
pub mod logger;
pub mod mavlink_json;
pub mod protocol;
pub mod stats;
pub mod web;
Loading