Working version for RaaS
parent
c5854ee5ae
commit
e3713f2512
|
@ -657,9 +657,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "raas_types"
|
name = "raas_types"
|
||||||
version = "0.0.2"
|
version = "0.0.5"
|
||||||
source = "registry+https://git.jojodev.com/joeyahines/_cargo-index.git"
|
source = "registry+https://git.jojodev.com/joeyahines/_cargo-index.git"
|
||||||
checksum = "6104c0c441473bc9c0817f9958a1c1e0db9ea7c72a7fee826b84a1d9d1baea62"
|
checksum = "7d0a6f7cba2fbba29bee36deb3880dcb3a5022e898a2f71a7a4e377f816aced1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"prost",
|
"prost",
|
||||||
|
@ -697,7 +697,7 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "roll_bot"
|
name = "roll_bot"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"config",
|
"config",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "roll_bot"
|
name = "roll_bot"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -9,7 +9,7 @@ edition = "2021"
|
||||||
structopt = "0.3.26"
|
structopt = "0.3.26"
|
||||||
config = "0.14.0"
|
config = "0.14.0"
|
||||||
rppal = "0.18.0"
|
rppal = "0.18.0"
|
||||||
raas_types = { version = "0.0.2", registry = "jojo-dev"}
|
raas_types = { version = "0.0.5", registry = "jojo-dev"}
|
||||||
log = "0.4.21"
|
log = "0.4.21"
|
||||||
env_logger = "0.11.3"
|
env_logger = "0.11.3"
|
||||||
thiserror = "1.0.61"
|
thiserror = "1.0.61"
|
||||||
|
|
|
@ -3,14 +3,13 @@ use crate::roll_bot::RollBot;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
use raas_types::raas::bot::roll::{roll_cmd, Roll, RollCmd, RollImage, RollResponse};
|
use raas_types::raas::bot::roll::{roll_cmd, Roll, RollCmd, RollImage, RollResponse};
|
||||||
use raas_types::raas::cmd::command::Cmd;
|
use raas_types::raas::cmd::request::Cmd;
|
||||||
use raas_types::raas::cmd::Command;
|
use raas_types::raas::cmd::Command;
|
||||||
use raas_types::raas::ping::{Ping, Pong};
|
use raas_types::raas::ping::{Ping, Pong};
|
||||||
use raas_types::raas::register::{BotTypes, Register, RegisterResponse};
|
use raas_types::raas::register::{BotTypes, Register, RegisterResponse};
|
||||||
use raas_types::raas::resp::response::Resp;
|
use raas_types::raas::resp::response::Resp;
|
||||||
use raas_types::raas::resp::Response;
|
use raas_types::raas::resp::Response;
|
||||||
use raas_types::raas::RaasMessage;
|
use raas_types::raas::{recv_raas_msg, send_raas_msg};
|
||||||
use std::io::{Read, Write};
|
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -73,9 +72,9 @@ impl Client {
|
||||||
let mut message = Vec::new();
|
let mut message = Vec::new();
|
||||||
register_msg.encode(&mut message)?;
|
register_msg.encode(&mut message)?;
|
||||||
|
|
||||||
Self::send_packet(socket, message)?;
|
send_raas_msg(socket, message)?;
|
||||||
|
|
||||||
let resp = Self::receive_packet(socket)?;
|
let resp = recv_raas_msg(socket)?;
|
||||||
|
|
||||||
let register_msg = RegisterResponse::decode(&*resp.msg)?;
|
let register_msg = RegisterResponse::decode(&*resp.msg)?;
|
||||||
|
|
||||||
|
@ -89,34 +88,15 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_packet(socket: &mut TcpStream) -> Result<RaasMessage, Error> {
|
|
||||||
let mut message_len_bytes = [0u8; 4];
|
|
||||||
socket.read_exact(&mut message_len_bytes)?;
|
|
||||||
|
|
||||||
let len = u32::from_be_bytes(message_len_bytes);
|
|
||||||
|
|
||||||
let mut message = vec![0u8; len as usize];
|
|
||||||
|
|
||||||
socket.read_exact(&mut message)?;
|
|
||||||
|
|
||||||
Ok(RaasMessage { len, msg: message })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send_packet(socket: &mut TcpStream, data: Vec<u8>) -> Result<(), Error> {
|
|
||||||
let msg = RaasMessage::new(data);
|
|
||||||
|
|
||||||
socket.write_all(&msg.into_bytes())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_next_command(&mut self, socket: &mut TcpStream) -> Result<RollCmd, Error> {
|
fn get_next_command(&mut self, socket: &mut TcpStream) -> Result<RollCmd, Error> {
|
||||||
let msg = Self::receive_packet(socket)?;
|
let msg = recv_raas_msg(socket)?;
|
||||||
|
|
||||||
let command = Command::decode(&*msg.msg)?;
|
let command = Command::decode(&*msg.msg)?;
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Received command for id={} timestamp={}",
|
"Received command for id={} timestamp={}",
|
||||||
command.id, command.timestamp
|
command.id,
|
||||||
|
&command.request.as_ref().unwrap().timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
if command.id != self.id.unwrap() {
|
if command.id != self.id.unwrap() {
|
||||||
|
@ -124,7 +104,7 @@ impl Client {
|
||||||
return Err(Error::InvalidCommandID);
|
return Err(Error::InvalidCommandID);
|
||||||
}
|
}
|
||||||
|
|
||||||
match command.cmd.unwrap() {
|
match command.request.unwrap().cmd.unwrap() {
|
||||||
Cmd::RollCmd(cmd) => Ok(cmd),
|
Cmd::RollCmd(cmd) => Ok(cmd),
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(unreachable_patterns)]
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -155,10 +135,23 @@ impl Client {
|
||||||
info!("Doing roll...");
|
info!("Doing roll...");
|
||||||
self.roll_bot.roll(roll.rotations)?;
|
self.roll_bot.roll(roll.rotations)?;
|
||||||
|
|
||||||
|
let capture_exit = std::process::Command::new("libcamera-still")
|
||||||
|
.arg("--output")
|
||||||
|
.arg("/tmp/out.jpg")
|
||||||
|
.status()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let img_data = if !capture_exit.success() {
|
||||||
|
error!("Failed to capture image ret={}", capture_exit);
|
||||||
|
vec![]
|
||||||
|
} else {
|
||||||
|
std::fs::read("/tmp/out.jpg")?
|
||||||
|
};
|
||||||
|
|
||||||
let resp = RollResponse {
|
let resp = RollResponse {
|
||||||
response: Some(
|
response: Some(
|
||||||
raas_types::raas::bot::roll::roll_response::Response::RollImage(RollImage {
|
raas_types::raas::bot::roll::roll_response::Response::RollImage(RollImage {
|
||||||
img: vec![],
|
img: img_data,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
@ -183,11 +176,11 @@ impl Client {
|
||||||
resp: Some(Resp::RollResp(roll_resp)),
|
resp: Some(Resp::RollResp(roll_resp)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut message = vec![0u8; 1024];
|
let mut message = Vec::new();
|
||||||
|
|
||||||
resp.encode(&mut message)?;
|
resp.encode(&mut message)?;
|
||||||
|
|
||||||
Self::send_packet(socket, message)?;
|
send_raas_msg(socket, message)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ use structopt::StructOpt;
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
pub config_file: PathBuf,
|
pub config_file: PathBuf,
|
||||||
|
#[structopt(short, long)]
|
||||||
|
pub test: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -37,15 +37,23 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
roll_bot.set_pwm_output(Duration::from_micros(config.pwm_config.pulse_min_us))?;
|
roll_bot.set_pwm_output(Duration::from_micros(config.pwm_config.pulse_min_us))?;
|
||||||
|
|
||||||
let mut client = Client::new(config, roll_bot);
|
if args.test {
|
||||||
|
let res = roll_bot.roll(3);
|
||||||
|
|
||||||
let res = client.run_client();
|
match res {
|
||||||
|
Ok(_) => info!("Test completed, exiting..."),
|
||||||
|
Err(err) => error!("Error rolling die: {}", err),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut client = Client::new(config, roll_bot);
|
||||||
|
let res = client.run_client();
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
error!("Error in client: {}", err);
|
error!("Error in client: {}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Roll Bot shutting down...");
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Roll Bot shutting down...");
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue