lj_rust/src/redis_ctrl.rs

121 lines
2.8 KiB
Rust
Raw Normal View History

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-12 17:23:59 +00:00
use crate::device::Status;
use crate::errors::{LJError, LJResult};
2023-06-29 21:24:56 +00:00
use crate::worldstate::{WorldState,EDH};
2023-06-29 21:27:54 +00:00
use log::info;
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 {
2023-06-12 17:23:59 +00:00
Draw = 0,
Edh,
//homography
Black,
Grid,
Resampler,
ClientKey,
Intensity,
Kpps,
ColorBalance,
2023-06-03 14:20:35 +00:00
}
impl TryFrom<u8> for Order {
2023-06-12 17:23:59 +00:00
type Error = String;
2023-06-03 16:54:41 +00:00
2023-06-12 17:23:59 +00:00
fn try_from(value: u8) -> Result<Self, Self::Error> {
use Order::*;
2023-06-03 14:20:35 +00:00
2023-06-12 17:23:59 +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,
_ => 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 {
2023-06-12 17:23:59 +00:00
pub client: Client,
pub connection: Connection,
laser_id: u8,
2023-06-03 13:21:36 +00:00
}
impl RedisCtrl {
2023-06-12 17:23:59 +00:00
pub fn new(url: &str, laser_id: &u8) -> LJResult<Self> {
let client = Client::open(url)
.map_err(LJError::RedisConnect)?;
let connection = client.get_connection()
.map_err(LJError::RedisConnect)?;
Ok(RedisCtrl { client, connection, laser_id: *laser_id })
}
pub fn get(&mut self, key: &str) -> LJResult<Line> {
let val: String = self.connection.get(key)?;
let line: Line = from_str(&val)?;
Ok(line)
}
2023-06-03 13:21:36 +00:00
2023-06-12 17:23:59 +00:00
pub fn set(&mut self, key: String, value: String) -> LJResult<()> {
self.connection.set(key, value)?;
Ok(())
}
pub fn get_order(&mut self, id: u8) -> LJResult<Order> {
let path = format!("/order/{id}");
let val: u8 = self.connection.get(path.clone())?;
2023-06-03 14:20:35 +00:00
2023-06-12 17:23:59 +00:00
if val == 1 || val >= 4 {
self.connection.set(path, 0)?;
}
2023-06-03 16:54:41 +00:00
2023-06-12 17:23:59 +00:00
Ok(val.try_into()?)
}
2023-06-03 13:21:36 +00:00
2023-06-12 17:23:59 +00:00
/**
/lstt/lasernumber etherdream last_status.playback_state (0: idle 1: prepare 2: playing)
/cap/lasernumber number of empty points sent to fill etherdream buffer (up to 1799)
/lack/lasernumber "a": ACK "F": Full "I": invalid. 64 or 35 for no connection.
**/
pub fn set_status(&mut self, status: Status) -> LJResult<()> {
let lstt_key = format!("/lstt/{}", self.laser_id);
let cap_key = format!("/cap/{}", self.laser_id);
let lack_key = format!("/lack/{}", self.laser_id);
self.set(lstt_key, status.playback_state.to_string())?;
self.set(cap_key, status.capacity.to_string())?;
self.set(lack_key, status.lack.to_string())?;
Ok(())
}
2023-06-29 20:36:00 +00:00
pub fn init_world_state( &mut self) -> LJResult<WorldState>{
2023-06-29 21:27:54 +00:00
let edh = self.get_edh()?;
info!("EDH: {:?}", edh);
Ok(WorldState {
edh,
..WorldState::default()
})
2023-06-29 20:36:00 +00:00
}
2023-06-29 21:24:56 +00:00
pub fn get_edh( &mut self ) -> LJResult<EDH> {
// Get new EDH
2023-07-01 12:54:37 +00:00
let edh_key = format!("/EDH/{}", self.laser_id);
let edh : String = self.connection.get(edh_key)?;
2023-06-29 21:24:56 +00:00
let edh : Vec<Vec<f32>> = from_str(&edh)?;
let edh = EDH::new(edh)?;
Ok(edh)
2023-06-29 20:36:00 +00:00
}
2023-06-03 16:54:41 +00:00
}