diff --git a/README.md b/README.md index 14f3a85..2808c81 100644 --- a/README.md +++ b/README.md @@ -302,7 +302,7 @@ docker build --build-arg TARGET_ARCH=x86_64-unknown-linux-musl -t mavlink/mavlin ### Websocket -It's also possible to connect multiple websockets with the following path `/v1/ws/mavlink`, the endpoint also accepts the query parameter `filter`, the filter value should be a regex that matches MAVLink message names, E.g: `/v1/ws/mavlink?filter=.*` for all messages, `/v1/ws/mavlink?filter=RC_.*` will match **RC_CHANNELS_RAW** and **RC_CHANNELS**. +It's also possible to connect multiple websockets with the following path `/v1/ws/mavlink`, the endpoint also accepts the query parameter `filter`, the filter value should be a regex that matches MAVLink message names, E.g: `/v1/ws/mavlink?filter=.*` for all messages, `/v1/ws/mavlink?filter=RC_.*` will match **RC_CHANNELS_RAW** and **RC_CHANNELS**. The `system_id` and `component_id` query parameters restrict the messages to a single sender, E.g: `/v1/ws/mavlink?system_id=1&component_id=1`. You can test websockets using [websocat](https://github.com/vi/websocat): ```sh diff --git a/src/endpoints.rs b/src/endpoints.rs index 6e860cd..74faf14 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -43,6 +43,10 @@ pub struct Info { pub struct WebsocketQuery { /// Regex filter to selected the desired MAVLink messages by name filter: Option, + /// Only messages from this system id + system_id: Option, + /// Only messages from this component id + component_id: Option, } #[derive(Apiv2Schema, Deserialize)] @@ -198,14 +202,16 @@ pub async fn websocket( query: web::Query, stream: web::Payload, ) -> Result { - let filter = match query.into_inner().filter { + let query = query.into_inner(); + let filter = match query.filter { Some(filter) => filter, _ => ".*".to_owned(), }; debug!("New websocket with filter {:#?}", &filter); - ws::start(WebsocketActor::new(filter), &req, stream) + let actor = WebsocketActor::new(filter, query.system_id, query.component_id); + ws::start(actor, &req, stream) } async fn not_found_response(message: String) -> actix_web::Result { diff --git a/src/websocket_manager.rs b/src/websocket_manager.rs index ad3048b..82cc92a 100644 --- a/src/websocket_manager.rs +++ b/src/websocket_manager.rs @@ -24,6 +24,8 @@ pub struct WebsocketError { pub struct WebsocketActorContent { pub actor: Addr, pub re: Option, + pub system_id: Option, + pub component_id: Option, } #[derive(Derivative, Default)] @@ -36,14 +38,18 @@ pub struct WebsocketManager { } impl WebsocketManager { - pub fn send(&self, value: &serde_json::Value, name: &str) { + pub fn send(&self, value: &serde_json::Value, header: &mavlink::MavHeader, name: &str) { if self.clients.is_empty() { return; } let string = serde_json::to_string_pretty(value).unwrap(); for client in &self.clients { - let is_match = client.re.as_ref().map_or(false, |regx| regx.is_match(name)); + let is_match = client.re.as_ref().map_or(false, |regx| regx.is_match(name)) + && client.system_id.map_or(true, |id| id == header.system_id) + && client + .component_id + .map_or(true, |id| id == header.component_id); if is_match { client.actor.do_send(StringMessage(string.clone())); } @@ -63,20 +69,24 @@ pub fn manager() -> Arc> { pub fn send(message: &MAVLinkMessage) { let name = message.message.message_name(); let value = serde_json::to_value(message).unwrap(); - MANAGER.lock().unwrap().send(&value, name); + MANAGER.lock().unwrap().send(&value, &message.header, name); } #[derive(Debug)] pub struct WebsocketActor { server: Arc>, pub filter: String, + pub system_id: Option, + pub component_id: Option, } impl WebsocketActor { - pub fn new(message_filter: String) -> Self { + pub fn new(message_filter: String, system_id: Option, component_id: Option) -> Self { Self { server: MANAGER.clone(), filter: message_filter, + system_id, + component_id, } } } @@ -103,6 +113,8 @@ impl StreamHandler> for WebsocketActor { .push(WebsocketActorContent { actor: ctx.address(), re: Regex::new(&self.filter).ok(), + system_id: self.system_id, + component_id: self.component_id, }); }