diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c7c6a4d..ed58580 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -42,11 +42,6 @@ jobs: uses: actions-rs/cargo@v1 with: command: clippy - - name: Clippy webapi with websocket - uses: actions-rs/cargo@v1 - with: - command: clippy - args: -p rustmas-webapi --features websocket - name: Test uses: actions-rs/cargo@v1 with: diff --git a/Cargo.lock b/Cargo.lock index c653e70..58ff509 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,7 +166,7 @@ dependencies = [ "actix-service", "actix-utils", "ahash 0.8.3", - "base64 0.21.2", + "base64", "bitflags 1.3.2", "brotli", "bytes", @@ -778,12 +778,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.2" @@ -1126,7 +1120,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "uuid 1.4.1", + "uuid", ] [[package]] @@ -1241,7 +1235,7 @@ dependencies = [ "petgraph", "thiserror", "tracing", - "uuid 1.4.1", + "uuid", ] [[package]] @@ -1255,27 +1249,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "bevy_websocket_adapter" -version = "0.1.6" -source = "git+https://github.com/mrozycki/bevy-websocket-adapter?branch=bump-bevy-0.10#89c1879d9ba897c84cb814e10e06edbf0d6ad9ba" -dependencies = [ - "anyhow", - "bevy", - "crossbeam-channel", - "futures", - "futures-util", - "log", - "serde", - "serde_json", - "thiserror", - "tokio", - "tokio-tungstenite", - "tungstenite", - "url", - "uuid 0.8.2", -] - [[package]] name = "bevy_window" version = "0.10.1" @@ -1372,15 +1345,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "generic-array", -] - [[package]] name = "block-buffer" version = "0.10.4" @@ -2002,6 +1966,12 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" +[[package]] +name = "data-encoding" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" + [[package]] name = "der" version = "0.7.8" @@ -2032,22 +2002,13 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "digest" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" -dependencies = [ - "generic-array", -] - [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer 0.10.4", + "block-buffer", "const-oid", "crypto-common", "subtle", @@ -2059,6 +2020,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b" +[[package]] +name = "document-features" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e493c573fce17f00dcab13b6ac057994f3ce17d1af4dc39bfd482b83c6eb6157" +dependencies = [ + "litrs", +] + [[package]] name = "dotenvy" version = "0.15.7" @@ -2214,6 +2184,21 @@ dependencies = [ "tokio", ] +[[package]] +name = "ewebsock" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7340666282406cb008c4d7206e6d7e157a82486afddd26deaad3ce9ebff3f238" +dependencies = [ + "document-features", + "js-sys", + "log", + "tungstenite", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -2287,21 +2272,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" -[[package]] -name = "futures" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - [[package]] name = "futures-channel" version = "0.3.28" @@ -2390,7 +2360,6 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ - "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -2806,7 +2775,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" dependencies = [ - "digest 0.10.7", + "digest", ] [[package]] @@ -3045,6 +3014,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -3217,6 +3195,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" +[[package]] +name = "litrs" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9275e0933cf8bb20f008924c0cb07a0692fe54d8064996520bf998de9eb79aa" + [[package]] name = "local-channel" version = "0.1.3" @@ -3303,7 +3287,7 @@ version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca" dependencies = [ - "digest 0.10.7", + "digest", ] [[package]] @@ -3828,12 +3812,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - [[package]] name = "opencv" version = "0.84.4" @@ -4391,7 +4369,7 @@ version = "0.11.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cde824a14b7c14f85caff81225f411faacc04a2013f41670f41443742b1c1c55" dependencies = [ - "base64 0.21.2", + "base64", "bytes", "encoding_rs", "futures-core", @@ -4445,7 +4423,7 @@ checksum = "6ab43bb47d23c1a631b4b680199a45255dce26fa9ab2fa902581f624ff13e6a8" dependencies = [ "byteorder", "const-oid", - "digest 0.10.7", + "digest", "num-bigint-dig", "num-integer", "num-iter", @@ -4526,7 +4504,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" dependencies = [ - "base64 0.21.2", + "base64", ] [[package]] @@ -4564,9 +4542,11 @@ name = "rustmas-bevy-visualizer" version = "0.1.0" dependencies = [ "bevy", - "bevy_websocket_adapter", "csv", + "ewebsock", + "itertools 0.12.0", "serde", + "serde_json", ] [[package]] @@ -4594,7 +4574,6 @@ name = "rustmas-light-client" version = "0.1.0" dependencies = [ "async-trait", - "bevy_websocket_adapter", "csv", "futures-util", "lightfx 0.1.0", @@ -4764,9 +4743,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.104" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", @@ -4802,19 +4781,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "sha-1" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" -dependencies = [ - "block-buffer 0.9.0", - "cfg-if", - "cpufeatures", - "digest 0.9.0", - "opaque-debug", -] - [[package]] name = "sha1" version = "0.10.5" @@ -4823,7 +4789,7 @@ checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.10.7", + "digest", ] [[package]] @@ -4834,7 +4800,7 @@ checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.10.7", + "digest", ] [[package]] @@ -4867,7 +4833,7 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" dependencies = [ - "digest 0.10.7", + "digest", "rand_core 0.6.4", ] @@ -5068,12 +5034,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ca69bf415b93b60b80dc8fda3cb4ef52b2336614d8da2de5456cc942a110482" dependencies = [ "atoi", - "base64 0.21.2", + "base64", "bitflags 2.0.2", "byteorder", "bytes", "crc", - "digest 0.10.7", + "digest", "dotenvy", "either", "futures-channel", @@ -5110,7 +5076,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0db2df1b8731c3651e204629dd55e52adbae0462fa1bdcbed56a2302c18181e" dependencies = [ "atoi", - "base64 0.21.2", + "base64", "bitflags 2.0.2", "byteorder", "crc", @@ -5392,19 +5358,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-tungstenite" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "511de3f85caf1c98983545490c3d09685fa8eb634e57eec22bb4db271f46cbd8" -dependencies = [ - "futures-util", - "log", - "pin-project", - "tokio", - "tungstenite", -] - [[package]] name = "tokio-util" version = "0.7.8" @@ -5534,18 +5487,18 @@ checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" [[package]] name = "tungstenite" -version = "0.14.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0b2d8558abd2e276b0a8df5c05a2ec762609344191e5fd23e292c910e9165b5" +checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" dependencies = [ - "base64 0.13.1", "byteorder", "bytes", + "data-encoding", "http", "httparse", "log", "rand 0.8.5", - "sha-1", + "sha1", "thiserror", "url", "utf-8", @@ -5640,16 +5593,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "uuid" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" -dependencies = [ - "getrandom", - "serde", -] - [[package]] name = "uuid" version = "1.4.1" @@ -5711,9 +5654,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -5721,9 +5664,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" dependencies = [ "bumpalo", "log", @@ -5748,9 +5691,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5758,9 +5701,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", @@ -5771,9 +5714,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.87" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "wasm-logger" diff --git a/animator/Cargo.toml b/animator/Cargo.toml index 0fd61bd..0bb6e48 100644 --- a/animator/Cargo.toml +++ b/animator/Cargo.toml @@ -20,7 +20,3 @@ tokio = { version = "1", features = ["full"] } log = "0.4.17" env_logger = "0.10.0" thiserror = "1.0.40" - -[features] -default = [] -websocket = ["rustmas-light-client/websocket"] diff --git a/animator/src/lib.rs b/animator/src/lib.rs index c1d5bea..1b7c94e 100644 --- a/animator/src/lib.rs +++ b/animator/src/lib.rs @@ -363,26 +363,6 @@ impl ControllerBuilder { Ok(self) } - #[cfg(feature = "visualiser")] - pub fn visualiser_lights(mut self) -> Result> { - info!("Using local visualiser"); - self.client_builder = - self.client_builder - .with(Box::new(client::VisualiserLightClient::new( - self.points.as_ref().unwrap().clone(), - )?)); - Ok(self) - } - - #[cfg(feature = "websocket")] - pub fn websocket_lights(mut self, url: String) -> Self { - info!("Using websocket visualiser"); - self.client_builder = self - .client_builder - .with(Box::new(client::websocket::WebsocketLightClient::new(url))); - self - } - pub fn local_lights(mut self) -> Result> { info!("Using tty lights client"); self.client_builder = self diff --git a/bevy-visualizer/Cargo.toml b/bevy-visualizer/Cargo.toml index b166d25..4675eca 100644 --- a/bevy-visualizer/Cargo.toml +++ b/bevy-visualizer/Cargo.toml @@ -4,9 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] -bevy_websocket_adapter = { git = "https://github.com/mrozycki/bevy-websocket-adapter", branch = "bump-bevy-0.10" } csv = "1.2.1" +ewebsock = "0.4.0" +itertools = "0.12.0" serde = { version = "1.0.164", features = ["derive"] } +serde_json = "1.0.108" [dependencies.bevy] version = "0.10.0" diff --git a/bevy-visualizer/README.md b/bevy-visualizer/README.md index 9b829e4..006c429 100644 --- a/bevy-visualizer/README.md +++ b/bevy-visualizer/README.md @@ -2,13 +2,27 @@ ## Running -You can run the visualizer with: +You can run the visualizer as a native binary with: ``` cargo run [--release] --bin rustmas-bevy-visualizer ``` -Visualizer needs to be started before WebAPI. +Alternatively, you can run it in the web browser. First you will have to +install `wasm-server-runner`: + +``` +cargo install wasm-server-runner +``` + +and then you can start the visualizer with: + +``` +cd bevy-visualizer +cargo run [--release] --target wasm32-unknown-unknown --bin rustmas-bevy-visualizer +``` + +Visualizer needs to be started after WebAPI. ## Controls diff --git a/bevy-visualizer/src/main.rs b/bevy-visualizer/src/main.rs index f5212e5..4a1862b 100644 --- a/bevy-visualizer/src/main.rs +++ b/bevy-visualizer/src/main.rs @@ -4,7 +4,6 @@ mod websocket; use bevy::prelude::*; use bevy::window::PresentMode; -use bevy_websocket_adapter::bevy::{WebSocketServer, WsMessageInserter}; use csv::ReaderBuilder; use pan_orbit_camera::{pan_orbit_camera, spawn_camera}; @@ -78,19 +77,19 @@ fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { + title: "Rustmas Visualizer".to_string(), present_mode: PresentMode::AutoNoVsync, ..default() }), ..default() })) - .add_plugin(WebSocketServer::default()) + .add_plugin(websocket::WebsocketPlugin::new( + "ws://127.0.0.1:8081/frames", + )) .insert_resource(Msaa::Off) .add_startup_system(create_plane_and_light) .add_startup_system(spawn_camera) .add_startup_system(add_lights) - .add_startup_system(websocket::start_listen) - .add_message_type::() .add_system(pan_orbit_camera) - .add_system(websocket::listen_for_frame) .run(); } diff --git a/bevy-visualizer/src/websocket.rs b/bevy-visualizer/src/websocket.rs index 0ee8930..46f41db 100644 --- a/bevy-visualizer/src/websocket.rs +++ b/bevy-visualizer/src/websocket.rs @@ -1,9 +1,42 @@ use bevy::prelude::*; -use bevy_websocket_adapter::{impl_message_type, server::Server, shared::ConnectionHandle}; +use ewebsock::{WsEvent, WsMessage}; +use itertools::Itertools; use serde::{Deserialize, Serialize}; +use std::sync::{mpsc, Mutex}; use crate::Led; +pub(crate) struct WebsocketPlugin { + endpoint: String, +} + +impl WebsocketPlugin { + pub(crate) fn new(url: &str) -> Self { + Self { + endpoint: url.to_string(), + } + } +} + +impl Plugin for WebsocketPlugin { + fn build(&self, app: &mut App) { + let (sender, receiver) = mpsc::channel(); + ewebsock::ws_receive( + self.endpoint.clone(), + Box::new(move |event| { + let _ = sender.send(event); + std::ops::ControlFlow::Continue(()) + }), + ) + .unwrap(); + app.insert_resource(Receiver(Mutex::new(receiver))) + .add_system(listen_for_frame); + } +} + +struct Receiver(Mutex>); +impl Resource for Receiver {} + #[derive(Serialize, Deserialize, Debug)] pub(crate) struct Color { r: u8, @@ -15,30 +48,28 @@ pub(crate) struct Color { pub(crate) struct FrameEvent { pixels: Vec, } -impl_message_type!(FrameEvent, "frame"); - -pub(crate) fn start_listen(mut ws: ResMut) { - ws.listen("0.0.0.0:12345") - .expect("failed to start websocket server"); -} -pub(crate) fn listen_for_frame( - mut evs: EventReader<(ConnectionHandle, FrameEvent)>, +fn listen_for_frame( + recv: Res, query: Query<(&Handle, &Led)>, mut materials: ResMut>, ) { - if let Some((handle, ev)) = evs.iter().last() { - trace!( - "received FrameEvent from {:?}; size = {}", - handle, - ev.pixels.len() - ); + let mut last_frame = None; + + while let Ok(event) = recv.0.lock().unwrap().try_recv() { + if let WsEvent::Message(WsMessage::Binary(bytes)) = event { + last_frame = Some(bytes); + } + } + + if let Some(frame) = last_frame { + let colors: Vec<_> = frame.into_iter().tuples::<(u8, u8, u8)>().collect(); for (material, led) in query.iter() { - let Some(color) = ev.pixels.get(led.0) else { + let Some((r, g, b)) = colors.get(led.0) else { continue; }; materials.get_mut(material).unwrap().base_color = - bevy::prelude::Color::rgb_u8(color.r, color.g, color.b); + bevy::prelude::Color::rgb_u8(*r, *g, *b); } } } diff --git a/light-client/Cargo.toml b/light-client/Cargo.toml index 8327437..b9623a8 100755 --- a/light-client/Cargo.toml +++ b/light-client/Cargo.toml @@ -16,14 +16,3 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1", features = ["full"] } serialport = { git = "https://github.com/mrozycki/serialport-rs", rev = "86cbf11" } - -[dependencies.bevy_websocket_adapter] -git = "https://github.com/mrozycki/bevy-websocket-adapter" -branch = "bump-bevy-0.10" -optional = true -default_features = false -features = ["client"] - -[features] -default = [] -websocket = ["bevy_websocket_adapter"] \ No newline at end of file diff --git a/light-client/src/lib.rs b/light-client/src/lib.rs index 40bf442..37910a3 100755 --- a/light-client/src/lib.rs +++ b/light-client/src/lib.rs @@ -4,8 +4,6 @@ pub mod http; pub mod tcp; pub mod tty; pub mod udp; -#[cfg(feature = "websocket")] -pub mod websocket; use std::{ fmt, diff --git a/light-client/src/websocket.rs b/light-client/src/websocket.rs deleted file mode 100644 index 542cd8e..0000000 --- a/light-client/src/websocket.rs +++ /dev/null @@ -1,38 +0,0 @@ -use async_trait::async_trait; -use bevy_websocket_adapter::{client::Client, impl_message_type}; -use serde::{Deserialize, Serialize}; - -use crate::{LightClient, LightClientError}; - -pub struct WebsocketLightClient { - client: Client, -} - -impl WebsocketLightClient { - pub fn new(url: String) -> Self { - let mut client = Client::new(); - client.connect(url); - Self { client } - } -} - -#[derive(Serialize, Deserialize, Clone)] -#[serde(rename = "frame", tag = "type")] -struct FrameEvent { - pixels: Vec, -} -impl_message_type!(FrameEvent, "frame"); - -#[async_trait] -impl LightClient for WebsocketLightClient { - async fn display_frame(&self, frame: &lightfx::Frame) -> Result<(), LightClientError> { - if !self.client.is_running() { - return Err(LightClientError::ProcessExited); - } - - self.client.send_message(&FrameEvent { - pixels: frame.pixels_iter().cloned().collect(), - }); - Ok(()) - } -} diff --git a/webapi/Cargo.toml b/webapi/Cargo.toml index b7d1443..bb940ad 100644 --- a/webapi/Cargo.toml +++ b/webapi/Cargo.toml @@ -22,7 +22,3 @@ tokio = "1.27.0" log = "0.4.17" env_logger = "0.10.0" - -[features] -default = [] -websocket = ["rustmas-animator/websocket"] diff --git a/webapi/README.md b/webapi/README.md index d0430fc..191fff3 100644 --- a/webapi/README.md +++ b/webapi/README.md @@ -40,11 +40,10 @@ Once everything is set up, you can start the WebAPI by simply running: cargo run --release -p rustmas-webapi ``` -If you want to use the visualizer, you will need to start it first, and then start the WebAPI -with the `websocket` feature: +If you want to use the visualizer, you can start it once the WebAPI is running. ``` -cargo run --release -p rustmas-webapi --features websocket +cargo run --release -p rustmas-bevy-visualizer ``` ### Running WebUI diff --git a/webapi/src/frame_broadcaster.rs b/webapi/src/frame_broadcaster.rs index 0fc48c2..ac7bc04 100644 --- a/webapi/src/frame_broadcaster.rs +++ b/webapi/src/frame_broadcaster.rs @@ -7,7 +7,6 @@ use tokio::sync::mpsc; #[derive(Message)] #[rtype(result = "()")] struct Frame { - #[allow(dead_code)] frame: lightfx::Frame, } @@ -106,7 +105,12 @@ impl Handler for FrameBroadcasterSession { type Result = (); fn handle(&mut self, msg: Frame, ctx: &mut Self::Context) -> Self::Result { - ctx.text(serde_json::to_string(&msg.frame).unwrap()); + let bytes: Vec<_> = msg + .frame + .pixels_iter() + .flat_map(|c| [c.r, c.g, c.b]) + .collect(); + ctx.binary(bytes); } } diff --git a/webapi/src/main.rs b/webapi/src/main.rs index 8ca1ece..be64a25 100644 --- a/webapi/src/main.rs +++ b/webapi/src/main.rs @@ -217,14 +217,6 @@ async fn main() -> Result<(), Box> { if let Ok(url) = env::var("RUSTMAS_LIGHTS_URL") { if url.starts_with("http://") { builder = builder.http_lights(&url)?; - } else if url.starts_with("ws://") { - #[cfg(feature = "websocket")] - { - builder = builder.websocket_lights(url); - } - - #[cfg(not(feature = "websocket"))] - error!("Web API built without websocket support, ignoring"); } else if url.starts_with("tcp://") { builder = builder.tcp_lights(&url)?; } else if url.starts_with("udp://") { @@ -240,11 +232,6 @@ async fn main() -> Result<(), Box> { builder = builder.local_lights()?; } - #[cfg(feature = "visualiser")] - { - builder = builder.visualiser_lights()?; - } - let mut controller = builder.build(); controller.discover_animations()?; info!("Discovered {} plugins", controller.list_animations().len());