2023-06-03 16:54:41 +00:00
|
|
|
use redis::{Client, Commands, Connection};
|
2023-06-03 13:21:36 +00:00
|
|
|
use ron::de::from_str;
|
2023-06-04 09:50:05 +00:00
|
|
|
use crate::errors::LJError;
|
2023-06-03 13:21:36 +00:00
|
|
|
|
2023-06-03 14:20:35 +00:00
|
|
|
#[repr(u8)]
|
2023-06-03 16:54:41 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2023-06-03 14:20:35 +00:00
|
|
|
pub enum Order {
|
|
|
|
Draw = 0,
|
|
|
|
Edh, //homography
|
|
|
|
Black,
|
|
|
|
Grid,
|
|
|
|
Resampler,
|
|
|
|
ClientKey,
|
|
|
|
Intensity,
|
|
|
|
Kpps,
|
2023-06-03 16:54:41 +00:00
|
|
|
ColorBalance,
|
2023-06-03 14:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<u8> for Order {
|
|
|
|
type Error = String;
|
2023-06-03 16:54:41 +00:00
|
|
|
|
2023-06-03 14:20:35 +00:00
|
|
|
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
2023-06-03 16:54:41 +00:00
|
|
|
use Order::*;
|
2023-06-03 14:20:35 +00:00
|
|
|
|
|
|
|
if value > 8 {
|
|
|
|
return Err("order out of range".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(match value {
|
|
|
|
0 => Draw,
|
|
|
|
1 => Edh,
|
|
|
|
2 => Black,
|
|
|
|
3 => Grid,
|
|
|
|
4 => Resampler,
|
|
|
|
5 => ClientKey,
|
|
|
|
6 => Intensity,
|
|
|
|
7 => Kpps,
|
|
|
|
8 => ColorBalance,
|
2023-06-04 09:32:11 +00:00
|
|
|
_ => unreachable!()
|
2023-06-03 14:20:35 +00:00
|
|
|
})
|
|
|
|
}
|
2023-06-03 16:54:41 +00:00
|
|
|
}
|
2023-06-03 14:20:35 +00:00
|
|
|
|
2023-06-03 16:54:41 +00:00
|
|
|
pub type Line = Vec<(f32, f32, u32)>;
|
2023-06-03 13:21:36 +00:00
|
|
|
|
|
|
|
pub struct RedisCtrl {
|
|
|
|
pub client: Client,
|
2023-06-03 16:54:41 +00:00
|
|
|
pub connection: Connection,
|
2023-06-03 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedisCtrl {
|
|
|
|
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
2023-06-04 09:50:05 +00:00
|
|
|
let client = Client::open("redis://127.0.0.1/")
|
2023-06-04 10:56:24 +00:00
|
|
|
.map_err(LJError::RedisConnect)?;
|
2023-06-04 09:50:05 +00:00
|
|
|
let connection = client.get_connection()
|
2023-06-04 10:56:24 +00:00
|
|
|
.map_err(LJError::RedisConnect)?;
|
2023-06-03 16:54:41 +00:00
|
|
|
Ok(RedisCtrl { client, connection })
|
2023-06-03 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 16:54:41 +00:00
|
|
|
pub fn get(&mut self, key: &str) -> Result<Line, Box<dyn std::error::Error>> {
|
|
|
|
let val: String = self.connection.get(key)?;
|
|
|
|
let line: Line = from_str(&val)?;
|
|
|
|
Ok(line)
|
2023-06-03 14:20:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 16:54:41 +00:00
|
|
|
pub fn get_order(&mut self, id: u8) -> Result<Order, Box<dyn std::error::Error>> {
|
|
|
|
let path = format!("/order/{id}");
|
|
|
|
let val: u8 = self.connection.get(path.clone())?;
|
|
|
|
|
|
|
|
if val == 1 || val >= 4 {
|
|
|
|
self.connection.set(path, 0)?;
|
|
|
|
}
|
2023-06-03 13:21:36 +00:00
|
|
|
|
2023-06-03 16:54:41 +00:00
|
|
|
Ok(val.try_into()?)
|
|
|
|
}
|
|
|
|
}
|