From c3dd48380e5d5782a5430f6e95aeaf77b9aeb23f Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Thu, 1 Aug 2024 17:12:25 -0300 Subject: [PATCH] backup --- Cargo.toml | 1 + src/device/devices.rs | 23 +++++++++++--- src/device/manager.rs | 17 ++++++----- src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 98 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 74502323..a5eccf2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ udp-stream = "0.0.12" uuid = { version = "1.8", features = ["serde"] } validator = "0.18.1" thiserror = "1.0.61" +ts-rs = { version = "9.0.1" , features = ["serde-compat", "uuid-impl"] } [build-dependencies] vergen-gix = { version = "1.0.0-beta.2", default-features = false, features = ["build", "cargo"] } diff --git a/src/device/devices.rs b/src/device/devices.rs index d0217811..b202b458 100644 --- a/src/device/devices.rs +++ b/src/device/devices.rs @@ -2,6 +2,7 @@ use bluerobotics_ping::device::PingDevice; use serde::{Deserialize, Serialize}; use tokio::sync::{mpsc, oneshot}; use tracing::{error, trace, warn}; +use ts_rs::TS; pub struct DeviceActor { pub receiver: mpsc::Receiver, @@ -267,7 +268,7 @@ pub enum UpgradeResult { Ping360, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub enum PingRequest { Ping1D(Ping1DRequest), Ping360(Ping360Request), @@ -277,7 +278,7 @@ pub enum PingRequest { Stop, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub enum Ping1DRequest { DeviceID, ModeAuto, @@ -296,33 +297,47 @@ pub enum Ping1DRequest { GainSetting, PingEnable, DistanceSimple, +#[ts(skip)] SetDeviceId(bluerobotics_ping::ping1d::SetDeviceIdStruct), +#[ts(skip)] SetModeAuto(bluerobotics_ping::ping1d::SetModeAutoStruct), +#[ts(skip)] SetPingInterval(bluerobotics_ping::ping1d::SetPingIntervalStruct), +#[ts(skip)] SetPingEnable(bluerobotics_ping::ping1d::SetPingEnableStruct), +#[ts(skip)] SetSpeedOfSound(bluerobotics_ping::ping1d::SetSpeedOfSoundStruct), +#[ts(skip)] SetRange(bluerobotics_ping::ping1d::SetRangeStruct), +#[ts(skip)] SetGainSetting(bluerobotics_ping::ping1d::SetGainSettingStruct), +#[ts(skip)] ContinuousStart(bluerobotics_ping::ping1d::ContinuousStartStruct), +#[ts(skip)] ContinuousStop(bluerobotics_ping::ping1d::ContinuousStopStruct), GotoBootloader, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub enum Ping360Request { MotorOff, DeviceData, AutoDeviceData, + #[ts(skip)] SetDeviceId(bluerobotics_ping::ping360::SetDeviceIdStruct), + #[ts(skip)] Transducer(bluerobotics_ping::ping360::TransducerStruct), + #[ts(skip)] Reset(bluerobotics_ping::ping360::ResetStruct), + #[ts(skip)] AutoTransmit(bluerobotics_ping::ping360::AutoTransmitStruct), } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub enum PingCommonRequest { DeviceInformation, ProtocolVersion, + #[ts(skip)] SetDeviceId(bluerobotics_ping::common::SetDeviceIdStruct), } diff --git a/src/device/manager.rs b/src/device/manager.rs index 1ea0bcfd..3cf8c987 100644 --- a/src/device/manager.rs +++ b/src/device/manager.rs @@ -1,5 +1,6 @@ use paperclip::actix::Apiv2Schema; use serde::{Deserialize, Serialize}; +use ts_rs::TS; use std::{ collections::{hash_map::DefaultHasher, HashMap}, hash::{Hash, Hasher}, @@ -56,7 +57,7 @@ impl Drop for Device { } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)] pub enum DeviceSelection { Common, Ping1D, @@ -64,7 +65,7 @@ pub enum DeviceSelection { Auto, } -#[derive(Debug, Clone, Deserialize, Serialize, Hash)] +#[derive(Debug, Clone, Deserialize, Serialize, Hash, TS)] pub enum SourceSelection { UdpStream(SourceUdpStruct), SerialStream(SourceSerialStruct), @@ -75,13 +76,13 @@ enum SourceType { Serial(SerialStream), } -#[derive(Clone, Debug, Deserialize, Serialize, Hash, Apiv2Schema)] +#[derive(Clone, Debug, Deserialize, Serialize, Hash, Apiv2Schema, TS)] pub struct SourceUdpStruct { pub ip: Ipv4Addr, pub port: u16, } -#[derive(Clone, Debug, Deserialize, Serialize, Hash, Apiv2Schema)] +#[derive(Clone, Debug, Deserialize, Serialize, Hash, Apiv2Schema, TS)] pub struct SourceSerialStruct { pub path: String, pub baudrate: u32, @@ -137,7 +138,7 @@ pub struct DeviceAnswer { pub device_id: Uuid, } -#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema, TS)] #[serde(tag = "type", content = "payload")] pub enum Request { Create(CreateStruct), @@ -151,7 +152,7 @@ pub enum Request { DisableContinuousMode(UuidWrapper), } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub struct UuidWrapper { pub uuid: Uuid, } @@ -174,13 +175,13 @@ impl From for Uuid { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub struct CreateStruct { pub source: SourceSelection, pub device_selection: DeviceSelection, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize,TS)] pub struct DeviceRequestStruct { pub uuid: Uuid, pub device_request: crate::device::devices::PingRequest, diff --git a/src/main.rs b/src/main.rs index 4c7dd828..27e8a02b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,9 @@ +use std::{net::Ipv4Addr, str::FromStr}; + +use device::manager::{CreateStruct, DeviceRequestStruct, UuidWrapper}; +use serde::{Deserialize, Serialize}; use tracing::info; +use ts_rs::TS; #[macro_use] extern crate lazy_static; @@ -9,16 +14,23 @@ mod device; mod logger; mod server; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, TS)] +#[ts(export, export_to = "RequestModels.ts")] pub struct Command { pub command: CommandType, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, TS)] +#[serde(tag = "module")] pub enum CommandType { DeviceManager(device::manager::Request), } +// #[derive(Debug, Clone, Serialize, Deserialize)] +// pub enum Command { +// DeviceManager(device::manager::Request), +// } + #[tokio::main] async fn main() { // CLI should be started before logger to allow control over verbosity @@ -34,6 +46,61 @@ async fn main() { "DeviceManager initialized with following devices: {:?}", handler.send(crate::device::manager::Request::List).await ); + use uuid::Uuid; + use serde_json::json; + + // Define your requests + let requests = vec![ + crate::device::manager::Request::Ping( + DeviceRequestStruct { + uuid: Uuid::parse_str("00000000-0000-0000-001e-10da679f8cee").unwrap(), + device_request: crate::device::devices::PingRequest::Ping360( + crate::device::devices::Ping360Request::Transducer( + bluerobotics_ping::ping360::TransducerStruct { + mode: 1, + gain_setting: 2, + angle: 0, + transmit_duration: 500, + sample_period: 80, + transmit_frequency: 700, + number_of_samples: 1200, + transmit: 1, + reserved: 1, + }, + ), + ), + }, + ), + crate::device::manager::Request::EnableContinuousMode( + UuidWrapper { + uuid: Uuid::parse_str("00000000-0000-0000-001e-10da679f8cee").unwrap(), + } + ), + crate::device::manager::Request::List, + crate::device::manager::Request::Create(CreateStruct { + source: device::manager::SourceSelection::SerialStream( + device::manager::SourceSerialStruct { + path: "/dev/ttyUSB0".to_string(), + baudrate: 115200, + } + ), + device_selection: device::manager::DeviceSelection::Auto, + }), + crate::device::manager::Request::Create(CreateStruct { + source: device::manager::SourceSelection::UdpStream( + device::manager::SourceUdpStruct { + ip: "192.168.0.1".parse().unwrap(), + port: 9092, + } + ), + device_selection: device::manager::DeviceSelection::Auto, + }), + ]; + + // Print each request as JSON + for request in requests { + println!("{}", json!(Command{command : CommandType::DeviceManager(request)})); + } server::manager::run(&cli::manager::server_address(), handler) .await