latest pushes
This commit is contained in:
@@ -19,49 +19,122 @@ use std::error::Error;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "bellande_particle", about = "Bellande Particles Tool")]
|
||||
struct Opt {
|
||||
#[structopt(
|
||||
long,
|
||||
help = "Particle state as JSON-formatted list [x, y, heading, weight]"
|
||||
)]
|
||||
particle: String,
|
||||
enum Command {
|
||||
#[structopt(name = "move", about = "Move particle")]
|
||||
Move {
|
||||
#[structopt(
|
||||
long,
|
||||
help = "Particle state as JSON-formatted list [x, y, heading, weight]"
|
||||
)]
|
||||
particle_state: String,
|
||||
|
||||
#[structopt(
|
||||
long,
|
||||
help = "Movement parameters as JSON-formatted list [rotation1, translation, rotation2]"
|
||||
)]
|
||||
movement: Option<String>,
|
||||
#[structopt(long, help = "First rotation angle")]
|
||||
rotation1: f64,
|
||||
|
||||
#[structopt(
|
||||
long,
|
||||
help = "World information as JSON object with width, height, and markers"
|
||||
)]
|
||||
world: Option<String>,
|
||||
#[structopt(long, help = "Translation distance")]
|
||||
translation: f64,
|
||||
|
||||
#[structopt(long, help = "Particle count for random generation")]
|
||||
count: Option<i32>,
|
||||
#[structopt(long, help = "Second rotation angle")]
|
||||
rotation2: f64,
|
||||
},
|
||||
|
||||
#[structopt(name = "read-markers", about = "Read markers")]
|
||||
ReadMarkers {
|
||||
#[structopt(
|
||||
long,
|
||||
help = "Particle state as JSON-formatted list [x, y, heading, weight]"
|
||||
)]
|
||||
particle_state: String,
|
||||
|
||||
#[structopt(long, help = "World information as JSON object")]
|
||||
world: String,
|
||||
},
|
||||
|
||||
#[structopt(name = "create-random", about = "Create random particles")]
|
||||
CreateRandom {
|
||||
#[structopt(long, help = "Number of particles to create")]
|
||||
count: i32,
|
||||
|
||||
#[structopt(long, help = "World information as JSON object")]
|
||||
world: String,
|
||||
},
|
||||
}
|
||||
|
||||
async fn make_bellande_particles_request(
|
||||
particle: Value,
|
||||
movement: Option<Value>,
|
||||
world: Option<Value>,
|
||||
count: Option<i32>,
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "bellande_particle", about = "Bellande Particle Tool")]
|
||||
struct Opt {
|
||||
#[structopt(subcommand)]
|
||||
cmd: Command,
|
||||
}
|
||||
|
||||
async fn make_bellande_particle_move_request(
|
||||
particle_state: Value,
|
||||
rotation1: f64,
|
||||
translation: f64,
|
||||
rotation2: f64,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = "https://bellande-robotics-sensors-research-innovation-center.org/api/Bellande_Particle/bellande_particle";
|
||||
let url = "https://bellande-robotics-sensors-research-innovation-center.org/api/Bellande_Particle/move";
|
||||
|
||||
let payload = json!({
|
||||
"particle": particle,
|
||||
"movement": movement,
|
||||
"world": world,
|
||||
"count": count,
|
||||
"particle": {
|
||||
"x": particle_state[0],
|
||||
"y": particle_state[1],
|
||||
"heading": particle_state[2],
|
||||
"weight": particle_state.get(3).unwrap_or(&json!(1.0))
|
||||
},
|
||||
"rotation1": rotation1,
|
||||
"translation": translation,
|
||||
"rotation2": rotation2,
|
||||
"auth": {
|
||||
"authorization_key": "bellande_web_api_opensource"
|
||||
}
|
||||
});
|
||||
|
||||
send_request(url, payload).await
|
||||
}
|
||||
|
||||
async fn make_bellande_particle_read_markers_request(
|
||||
particle_state: Value,
|
||||
world: Value,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
let url = "https://bellande-robotics-sensors-research-innovation-center.org/api/Bellande_Particle/read_markers";
|
||||
|
||||
let payload = json!({
|
||||
"particle": {
|
||||
"x": particle_state[0],
|
||||
"y": particle_state[1],
|
||||
"heading": particle_state[2],
|
||||
"weight": particle_state.get(3).unwrap_or(&json!(1.0))
|
||||
},
|
||||
"world": world,
|
||||
"auth": {
|
||||
"authorization_key": "bellande_web_api_opensource"
|
||||
}
|
||||
});
|
||||
|
||||
send_request(url, payload).await
|
||||
}
|
||||
|
||||
async fn make_bellande_particle_create_random_request(
|
||||
count: i32,
|
||||
world: Value,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
let url = "https://bellande-robotics-sensors-research-innovation-center.org/api/Bellande_Particle/create_random";
|
||||
|
||||
let payload = json!({
|
||||
"count": count,
|
||||
"world": world,
|
||||
"auth": {
|
||||
"authorization_key": "bellande_web_api_opensource"
|
||||
}
|
||||
});
|
||||
|
||||
send_request(url, payload).await
|
||||
}
|
||||
|
||||
async fn send_request(url: &str, payload: Value) -> Result<Value, Box<dyn Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = client
|
||||
.post(url)
|
||||
.header("accept", "application/json")
|
||||
@@ -79,36 +152,38 @@ async fn make_bellande_particles_request(
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
// Parse JSON strings to Values for validation
|
||||
let particle: Value = serde_json::from_str(&opt.particle)
|
||||
.map_err(|e| format!("Error parsing particle: {}", e))?;
|
||||
let result = match opt.cmd {
|
||||
Command::Move {
|
||||
particle_state,
|
||||
rotation1,
|
||||
translation,
|
||||
rotation2,
|
||||
} => {
|
||||
let particle_state: Value = serde_json::from_str(&particle_state)
|
||||
.map_err(|e| format!("Error parsing particle state: {}", e))?;
|
||||
|
||||
// Parse optional movement
|
||||
let movement: Option<Value> = match opt.movement {
|
||||
Some(ref m) => {
|
||||
Some(serde_json::from_str(m).map_err(|e| format!("Error parsing movement: {}", e))?)
|
||||
make_bellande_particle_move_request(particle_state, rotation1, translation, rotation2)
|
||||
.await?
|
||||
}
|
||||
Command::ReadMarkers {
|
||||
particle_state,
|
||||
world,
|
||||
} => {
|
||||
let particle_state: Value = serde_json::from_str(&particle_state)
|
||||
.map_err(|e| format!("Error parsing particle state: {}", e))?;
|
||||
let world: Value = serde_json::from_str(&world)
|
||||
.map_err(|e| format!("Error parsing world info: {}", e))?;
|
||||
|
||||
make_bellande_particle_read_markers_request(particle_state, world).await?
|
||||
}
|
||||
Command::CreateRandom { count, world } => {
|
||||
let world: Value = serde_json::from_str(&world)
|
||||
.map_err(|e| format!("Error parsing world info: {}", e))?;
|
||||
|
||||
make_bellande_particle_create_random_request(count, world).await?
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Parse optional world
|
||||
let world: Option<Value> = match opt.world {
|
||||
Some(ref w) => {
|
||||
Some(serde_json::from_str(w).map_err(|e| format!("Error parsing world: {}", e))?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Run using API
|
||||
match make_bellande_particles_request(particle, movement, world, opt.count).await {
|
||||
Ok(result) => {
|
||||
println!("{}", serde_json::to_string_pretty(&result)?);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&result)?);
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user