Use JSON web API

This commit is contained in:
Austen Adler 2021-08-09 23:13:38 -04:00
parent 03aef246c1
commit d46f307f83
4 changed files with 36 additions and 12 deletions

View File

@ -11,6 +11,7 @@ serde = {version = "1.0", features = ["derive"]}
actix-web = {version = "3", default_features = false} actix-web = {version = "3", default_features = false}
rust-embed="6.0.0" rust-embed="6.0.0"
hex = "0.4.3" hex = "0.4.3"
serde_json = "1"
[target.armv7-unknown-linux-gnueabihf] [target.armv7-unknown-linux-gnueabihf]
linker = "armv7-unknown-linux-gnueabihf" linker = "armv7-unknown-linux-gnueabihf"

View File

@ -1,8 +1,9 @@
use hex; use hex;
use std::num::ParseIntError; use std::num::ParseIntError;
use std::str::FromStr; use std::str::FromStr;
use serde::Deserialize;
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq, Deserialize)]
pub struct Rgb(pub u8, pub u8, pub u8); pub struct Rgb(pub u8, pub u8, pub u8);
impl Rgb { impl Rgb {
pub const fn to_tuple(self) -> (u8, u8, u8) { pub const fn to_tuple(self) -> (u8, u8, u8) {

View File

@ -1,4 +1,5 @@
use crate::color::Rgb; use crate::color::Rgb;
use serde::Deserialize;
pub mod collide; pub mod collide;
pub mod fade; pub mod fade;
@ -13,6 +14,20 @@ pub use moving_rainbow::MovingRainbow;
pub use orb::Orb; pub use orb::Orb;
pub use solid::Solid; pub use solid::Solid;
#[derive(Deserialize, Debug)]
enum PatternParameters {
Collide(Rgb, Rgb, Rgb),
}
pub enum PatternParameters {
Collide(Rgb,Rgb,Rgb),
Fade(Rgb),
MovingPixel(Rgb),
MovingRainbow(u8, bool),
Orb(Rgb, u8,u8),
Solid(Rgb),
}
pub trait Pattern: std::fmt::Debug { pub trait Pattern: std::fmt::Debug {
fn init(&mut self, num_lights: u16) -> Result<(), ()>; fn init(&mut self, num_lights: u16) -> Result<(), ()>;
fn step(&mut self) -> Result<bool, ()>; fn step(&mut self) -> Result<bool, ()>;

View File

@ -2,11 +2,13 @@ use crate::color::Rgb;
use crate::pattern; use crate::pattern;
use crate::strip; use crate::strip;
use actix_web::web::Form; use actix_web::web::Form;
use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder}; use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder, Result, };
// use actix_web::error::InternalError;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
use serde::Deserialize; use serde::Deserialize;
use std::str::FromStr; use std::str::FromStr;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::io;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -19,21 +21,26 @@ struct AppState {
strip_tx: Arc<Mutex<Sender<strip::Message>>>, strip_tx: Arc<Mutex<Sender<strip::Message>>>,
} }
#[derive(Deserialize, Debug)] // #[derive(Deserialize, Debug)]
struct ColorForm { // struct ColorForm {
color: String, // color: String,
} // e: E
// }
#[post("/setcolor")] #[post("/setcolor")]
async fn set_color(data: web::Data<AppState>, params: Form<ColorForm>) -> impl Responder { async fn set_color(data: web::Data<AppState>, params: web::Json<PatternParameters>) -> Result<impl Responder> {
println!("{:?}", params);
let pattern = match params.0 {
PatternParameters::Collide(l, r, c) => pattern::Collide::new(l,r,c)
};
data.strip_tx data.strip_tx
.lock() .lock().or(Err(io::Error::new(io::ErrorKind::Other, "Failed to get a lock")))?
.unwrap()
.send(strip::Message::ChangePattern(Box::new( .send(strip::Message::ChangePattern(Box::new(
pattern::Solid::new(Rgb::from_str(&params.color).unwrap()), // pattern::Solid::new(Rgb::from_str(&params.color).or(Err(io::Error::new(io::ErrorKind::Other, "Failed to parse")))?),
pattern
))) )))
.unwrap(); .or(Err(io::Error::new(io::ErrorKind::Other, "Failed to send to channel")))?;
format!("{:?}", params) Ok(format!("{:?}", params))
} }
#[get("/")] #[get("/")]