Skip to content
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
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ jobs:
- name: Run SITL & MAVLink2Rest
timeout-minutes: 5
run: |
pip install --user aiohttp asyncio requests
pip install --user aiohttp asyncio requests uv
wget https://firmware.ardupilot.org/Sub/stable-4.0.2/SITL_x86_64_linux_gnu/ardusub
chmod +x ardusub
./ardusub --model vectored &
sleep 5
cargo run -- --connect tcpout:0.0.0.0:5760 &
sleep 5
./tests/run.py
./tests/websocket_forward.py
3 changes: 2 additions & 1 deletion src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

use super::data;
use super::mavlink_vehicle::MAVLinkVehicleArcMutex;
use super::websocket_manager::WebsocketActor;
use super::websocket_manager::{self, WebsocketActor};

use log::*;
use mavlink::Message;
Expand Down Expand Up @@ -174,6 +174,7 @@ pub async fn mavlink_post(
) {
Ok(content) => match data.lock().unwrap().send(&content.header, &content.message) {
Ok(_result) => {
websocket_manager::send(&content);
data::update((content.header, content.message));
return HttpResponse::Ok().await;
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn ws_callback(
.send(&content.header, &content.message);
match result {
Ok(_) => {
websocket_manager::send(&content);
data::update((content.header, content.message));
serde_json::json!({ "result": "ok" }).to_string()
}
Expand Down
4 changes: 3 additions & 1 deletion src/websocket_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebsocketActor {
match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
Ok(ws::Message::Text(text)) => {
let text = match &self.server.lock().unwrap().new_message_callback {
// Release the manager lock before the callback, it sends to the other clients
let callback = self.server.lock().unwrap().new_message_callback.clone();
let text = match callback {
Some(callback) => callback(&text),
None => serde_json::to_string(&WebsocketError {
error: "MAVLink callback does not exist.".to_string(),
Expand Down
41 changes: 41 additions & 0 deletions tests/websocket_forward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env -S uv run --script
# /// script
# dependencies = ["requests", "websockets"]
# ///

import asyncio
import json

import requests
import websockets

API = "http://0.0.0.0:8088/v1"
# DEBUG is never sent by the vehicle, so only our messages go through the filter
WS = "ws://0.0.0.0:8088/v1/ws/mavlink?filter=DEBUG"

message = {
"header": {"system_id": 255, "component_id": 240, "sequence": 0},
"message": {"type": "DEBUG", "time_boot_ms": 0, "value": 42.0, "ind": 1},
}


async def receive(listener):
try:
received = json.loads(await asyncio.wait_for(listener.recv(), 5))
except asyncio.TimeoutError:
return None
return received["message"]["type"], received["header"]["component_id"]


async def main():
async with websockets.connect(WS) as listener, websockets.connect(WS) as sender:
print("Test REST forward..")
assert requests.post(f"{API}/mavlink", json=message).ok, "Failed to post message."
assert await receive(listener) == ("DEBUG", 240), "REST message was not forwarded."

print("Test websocket forward..")
await sender.send(json.dumps(message))
assert await receive(listener) == ("DEBUG", 240), "Websocket message was not forwarded."


asyncio.run(main())
Loading