Skip to content

Commit

Permalink
No internal index (#86)
Browse files Browse the repository at this point in the history
* safer hardware

* Update windows.rs

* Update lib.rs

* clippy
  • Loading branch information
wiiznokes authored Mar 28, 2024
1 parent c30870e commit ac5f85f
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 408 deletions.
379 changes: 191 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions data/src/config/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use hardware::{HItem, Hardware, HardwareBridge, Mode, Value};
use hardware::{HControl, Hardware, HardwareBridge, Mode, Value};
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -19,7 +19,7 @@ pub struct Control {
pub active: bool,

#[serde(skip)]
pub control_h: Option<Rc<HItem>>,
pub control_h: Option<Rc<HControl>>,

#[serde(skip)]
pub mode_set: Option<Mode>,
Expand All @@ -31,7 +31,7 @@ impl Control {
hardware_id: Option<String>,
input: Option<String>,
active: bool,
control_h: Option<Rc<HItem>>,
control_h: Option<Rc<HControl>>,
) -> Self {
Self {
name: name.clone(),
Expand All @@ -54,7 +54,7 @@ impl Control {

match &self.control_h {
Some(control_h) => {
bridge.set_value(&control_h.internal_index, value)?;
bridge.set_value(control_h, value)?;
Ok(value)
}
None => Err(UpdateError::NodeIsInvalid(self.name.clone())),
Expand All @@ -74,7 +74,7 @@ impl Control {
}

match &self.control_h {
Some(control_h) => bridge.set_mode(&control_h.internal_index, &mode)?,
Some(control_h) => bridge.set_mode(control_h, &mode)?,
None => return Err(UpdateError::NodeIsInvalid(self.name.clone())),
};

Expand All @@ -86,7 +86,7 @@ impl Control {
pub fn get_value<H: HardwareBridge>(&self, bridge: &mut H) -> Result<Value, UpdateError> {
match &self.control_h {
Some(control_h) => bridge
.get_value(&control_h.internal_index)
.get_control_value(control_h)
.map_err(UpdateError::Hardware),
None => Err(UpdateError::NodeIsInvalid(self.name.clone())),
}
Expand Down
6 changes: 3 additions & 3 deletions data/src/config/fan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
node::{IsValid, Node, NodeType, ToNode},
update::UpdateError,
};
use hardware::{HItem, Hardware, HardwareBridge, Value};
use hardware::{HSensor, Hardware, HardwareBridge, Value};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
Expand All @@ -16,14 +16,14 @@ pub struct Fan {
pub hardware_id: Option<String>,

#[serde(skip)]
pub fan_h: Option<Rc<HItem>>,
pub fan_h: Option<Rc<HSensor>>,
}

impl Fan {
pub fn get_value<H: HardwareBridge>(&self, bridge: &mut H) -> Result<Value, UpdateError> {
match &self.fan_h {
Some(fan_h) => bridge
.get_value(&fan_h.internal_index)
.get_sensor_value(fan_h)
.map_err(UpdateError::Hardware),
None => Err(UpdateError::NodeIsInvalid(self.name.clone())),
}
Expand Down
11 changes: 9 additions & 2 deletions data/src/config/serde_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(dead_code)]
#![allow(unused_imports)]

use const_format::formatcp;
use hardware::{HItem, Hardware};
use hardware::{HControl, HSensor, Hardware};
use serial_test::serial;
use std::fmt::Debug;
use std::fs::{self, File};
Expand Down Expand Up @@ -67,10 +70,12 @@ fn serialize() {
toml::to_string_pretty(&settings)
});

/*
write_file(HARDWARE_PATH, || {
let hardware1 = hardware1();
toml::to_string_pretty(&hardware1)
});
*/

let config1 = config1();

Expand All @@ -95,9 +100,10 @@ fn write_file<E: Debug>(path: &str, content_generation: impl Fn() -> Result<Stri
println!("file {} succesfully writed!", path);
}

/*
fn hardware1() -> Hardware {
Hardware {
controls: vec![HItem {
controls: vec![HControl {
name: "ControlH".into(),
hardware_id: "ControlH".into(),
info: "ControlH".into(),
Expand All @@ -120,6 +126,7 @@ fn hardware1() -> Hardware {
.into()],
}
}
*/

fn config1() -> Config {
Config {
Expand Down
6 changes: 3 additions & 3 deletions data/src/config/temp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use hardware::{HItem, Hardware, HardwareBridge, Value};
use hardware::{HSensor, Hardware, HardwareBridge, Value};
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -17,14 +17,14 @@ pub struct Temp {
pub hardware_id: Option<String>,

#[serde(skip)]
pub temp_h: Option<Rc<HItem>>,
pub temp_h: Option<Rc<HSensor>>,
}

impl Temp {
pub fn get_value<H: HardwareBridge>(&self, bridge: &mut H) -> Result<Value, UpdateError> {
match &self.temp_h {
Some(temp_h) => bridge
.get_value(&temp_h.internal_index)
.get_sensor_value(temp_h)
.map_err(UpdateError::Hardware),
None => Err(UpdateError::NodeIsInvalid(self.name.clone())),
}
Expand Down
29 changes: 14 additions & 15 deletions hardware/src/fake_hardware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;

use rand::Rng;

use crate::{HItem, Hardware, HardwareBridge, Mode, Value};
use crate::{HControl, HSensor, Hardware, HardwareBridge, Mode, Value};

pub struct FakeHardwareBridge {
hardware: Hardware,
Expand All @@ -22,39 +22,39 @@ impl HardwareBridge for FakeHardwareBridge {
fn new() -> crate::Result<Self> {
let mut hardware = Hardware::default();

let temp1 = HItem {
let temp1 = HSensor {
name: "temp1".into(),
hardware_id: "temp1".into(),
info: String::new(),
internal_index: TEMP_INTERNAL_INDEX,
};
hardware.temps.push(temp1.into());

let temp2 = HItem {
let temp2 = HSensor {
name: "temp2".into(),
hardware_id: "temp2".into(),
info: String::new(),
internal_index: TEMP_INTERNAL_INDEX,
};
hardware.temps.push(temp2.into());

let fan1 = HItem {
let fan1 = HSensor {
name: "fan1".into(),
hardware_id: "fan1".into(),
info: String::new(),
internal_index: FAN_INTERNAL_INDEX,
};
hardware.fans.push(fan1.into());

let control1 = HItem {
let control1 = HControl {
name: "control1".into(),
hardware_id: "control1".into(),
info: String::new(),
internal_index: CONTROL_INTERNAL_INDEX,
};
hardware.controls.push(control1.into());

let control2 = HItem {
let control2 = HControl {
name: "control2".into(),
hardware_id: "control2".into(),
info: String::new(),
Expand All @@ -68,23 +68,22 @@ impl HardwareBridge for FakeHardwareBridge {
&self.hardware
}

fn get_value(&mut self, _internal_index: &usize) -> crate::Result<Value> {
fn get_sensor_value(&mut self, _sensor: &HSensor) -> crate::Result<Value> {
let nb = rand::thread_rng().gen_range(30..80);
Ok(nb)
}

fn set_value(&mut self, internal_index: &usize, value: Value) -> crate::Result<()> {
if internal_index != &CONTROL_INTERNAL_INDEX {
panic!("set value to hardware != Control")
}
fn get_control_value(&mut self, _control: &HControl) -> crate::Result<Value> {
let nb = rand::thread_rng().gen_range(30..80);
Ok(nb)
}

fn set_value(&mut self, _control: &HControl, value: Value) -> crate::Result<()> {
debug!("set value {}", value);
Ok(())
}

fn set_mode(&mut self, internal_index: &usize, mode: &Mode) -> crate::Result<()> {
if internal_index != &CONTROL_INTERNAL_INDEX {
panic!("set mode to hardware != Control")
}
fn set_mode(&mut self, _control: &HControl, mode: &Mode) -> crate::Result<()> {
debug!("set mode {}", mode);
Ok(())
}
Expand Down
79 changes: 63 additions & 16 deletions hardware/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub mod fake_hardware;

#[derive(Error, Debug)]
pub enum HardwareError {
#[error("Internal index not found")]
InternalIndexNotFound,
#[cfg(target_os = "linux")]
#[error(transparent)]
Linux(#[from] linux::LinuxError),
Expand All @@ -29,8 +27,14 @@ pub enum HardwareError {

type Result<T> = std::result::Result<T, HardwareError>;

#[derive(Serialize, Debug, Clone, Eq)]
pub struct HItem {
pub trait HItem {
fn name(&self) -> &String;
fn id(&self) -> &String;
fn info(&self) -> &String;
}

#[derive(Serialize, Debug, Eq)]
pub struct HSensor {
pub name: String,
#[serde(rename = "id")]
pub hardware_id: String,
Expand All @@ -39,29 +43,70 @@ pub struct HItem {
pub info: String,

#[serde(skip)]
pub internal_index: usize,
internal_index: usize,
}

impl ToString for HItem {
fn to_string(&self) -> String {
self.name.clone()
impl HItem for HSensor {
fn id(&self) -> &String {
&self.hardware_id
}

fn name(&self) -> &String {
&self.name
}

fn info(&self) -> &String {
&self.info
}
}

impl PartialEq for HItem {
#[derive(Serialize, Debug, Eq)]
pub struct HControl {
pub name: String,
#[serde(rename = "id")]
pub hardware_id: String,

#[serde(skip)]
pub info: String,

#[serde(skip)]
internal_index: usize,
}

impl HItem for HControl {
fn id(&self) -> &String {
&self.hardware_id
}

fn name(&self) -> &String {
&self.name
}

fn info(&self) -> &String {
&self.info
}
}

impl PartialEq for HControl {
fn eq(&self, other: &Self) -> bool {
self.internal_index == other.internal_index
}
}

#[derive(Serialize, Debug, Clone, Default)]
impl PartialEq for HSensor {
fn eq(&self, other: &Self) -> bool {
self.internal_index == other.internal_index
}
}

#[derive(Serialize, Debug, Default)]
pub struct Hardware {
#[serde(default, rename = "Control")]
pub controls: Vec<Rc<HItem>>,
pub controls: Vec<Rc<HControl>>,
#[serde(default, rename = "Fan")]
pub fans: Vec<Rc<HItem>>,
pub fans: Vec<Rc<HSensor>>,
#[serde(default, rename = "Temp")]
pub temps: Vec<Rc<HItem>>,
pub temps: Vec<Rc<HSensor>>,
}

pub type Value = i32;
Expand Down Expand Up @@ -95,9 +140,11 @@ pub trait HardwareBridge {

fn hardware(&self) -> &Hardware;

fn get_value(&mut self, internal_index: &usize) -> Result<Value>;
fn set_value(&mut self, internal_index: &usize, value: Value) -> Result<()>;
fn set_mode(&mut self, internal_index: &usize, mode: &Mode) -> Result<()>;
fn get_sensor_value(&mut self, sensor: &HSensor) -> Result<Value>;
fn get_control_value(&mut self, control: &HControl) -> Result<Value>;

fn set_value(&mut self, control: &HControl, value: Value) -> Result<()>;
fn set_mode(&mut self, control: &HControl, mode: &Mode) -> Result<()>;

/// Used on Windows, because we update all sensors in one function, so
/// we don't want to update at each call, instead, we call this function
Expand Down
Loading

0 comments on commit ac5f85f

Please sign in to comment.