diff --git a/mytest/src/main.rs b/mytest/src/main.rs index c30bd3d..cf03339 100644 --- a/mytest/src/main.rs +++ b/mytest/src/main.rs @@ -18,6 +18,6 @@ fn main() { let tp = a.get_tank_parameters().unwrap(); println!("Tank: {:?}", tp); - //let hp = a.get_heating_parameters().unwrap(); - //println!("Heating: {:?}", hp); + let hp = a.get_heating_parameters().unwrap(); + println!("Heating: {:?}", hp); } diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..6765b88 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,19 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum DAError { + #[error("Communication error")] + CommunicationError, + #[error("Conversion error")] + ConversionError, + #[error("Set value error")] + SetValueError(String), + #[error("No such field")] + NoSuchFieldError, + #[error("Value conversion error")] + ValueConversionError, + #[error("Url Parse error")] + UrlParseError, + #[error("WebSocket Error")] + WebSocketError, +} diff --git a/src/lib.rs b/src/lib.rs index 4817701..f1f253a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,99 +1,21 @@ -use std::{error::Error, fmt::Debug, net::TcpStream}; -use thiserror::Error; +use std::{fmt::Debug, net::TcpStream}; use uuid::Uuid; use serde_json::{json, Value}; use tungstenite::{connect, stream, Message, WebSocket}; use url::Url; -#[derive(Error, Debug)] -pub enum DAError { - #[error("Communication error")] - CommunicationError, - #[error("Conversion error")] - ConversionError, - #[error("Set value error")] - SetValueError(String), - #[error("No such field")] - NoSuchFieldError, - #[error("Value conversion error")] - ValueConversionError, - #[error("Url Parse error")] - UrlParseError, - #[error("WebSocket Error")] - WebSocketError, -} +mod errors; +mod params; +mod traits; +use crate::errors::DAError; +use crate::params::HeatingParameters; +use crate::params::TankParameters; pub struct DaikinAlthermaClient { ws_client: WebSocket>, } -// -#[derive(Debug)] -pub struct TankParameters { - /// The current temperature of the water tank, in °C - pub temperature: f64, - /// The setpoint (wanted) temperature of the water tank, in °C - pub setpoint_temperature: f64, - /// Is the tank heating enabled - pub enabled: bool, - /// Is it on powerful (quick heating) mode - pub powerful: bool, -} - -#[derive(Debug)] -pub struct HeatingParameters { - /// The current indoor temperature, in °C - pub indoor_temperature: f64, - /// The current outdoor temperature, in °C - pub outdoor_temperature: f64, - /// The current indoor setpoint (target) temperature, in °C - pub indoor_setpoint_temperature: f64, - /// The leaving water temperature, in °C - pub leaving_water_temperature: f64, - - /// Is the heating enabled - pub enabled: bool, - - /// Is the heating on holiday (disabled) - pub on_holiday: bool, - // Is it on powerful (quick heating) mode - //mode: , -} - -trait FromJsonValue: Sized { - fn from_json_value(value: &Value) -> Result; -} - -// Implement the trait for i64 -impl FromJsonValue for i64 { - fn from_json_value(value: &Value) -> Result { - value.as_i64().ok_or(DAError::ValueConversionError) - } -} - -// Implement the trait for f64 -impl FromJsonValue for f64 { - fn from_json_value(value: &Value) -> Result { - value.as_f64().ok_or(DAError::ValueConversionError) - } -} - -// Implement the trait for String -impl FromJsonValue for String { - fn from_json_value(value: &Value) -> Result { - let v = value.as_str().ok_or(DAError::ValueConversionError)?; - Ok(v.to_string()) - } -} - -// Implement the trait for bool -impl FromJsonValue for bool { - fn from_json_value(value: &Value) -> Result { - value.as_bool().ok_or(DAError::ValueConversionError) - } -} - impl DaikinAlthermaClient { /// Creates a new client to a Daikin Altherma LAN adapter. pub fn new(adapter_hostname: String) -> Result { @@ -228,7 +150,10 @@ impl DaikinAlthermaClient { self.set_value_hp("1/Operation/Power", Some(payload), "/") } - fn request_value_hp_dft>(&mut self, item: &str) -> Result { + fn request_value_hp_dft>( + &mut self, + item: &str, + ) -> Result { let hp_item = format!("MNAE/{item}"); let json_val = self.request_value(hp_item.as_str(), None, "/m2m:rsp/pc/m2m:cin/con")?; T::from_json_value(&json_val) diff --git a/src/params.rs b/src/params.rs new file mode 100644 index 0000000..0902975 --- /dev/null +++ b/src/params.rs @@ -0,0 +1,33 @@ +use std::fmt::Debug; + +#[derive(Debug)] +pub struct TankParameters { + /// The current temperature of the water tank, in °C + pub temperature: f64, + /// The setpoint (wanted) temperature of the water tank, in °C + pub setpoint_temperature: f64, + /// Is the tank heating enabled + pub enabled: bool, + /// Is it on powerful (quick heating) mode + pub powerful: bool, +} + +#[derive(Debug)] +pub struct HeatingParameters { + /// The current indoor temperature, in °C + pub indoor_temperature: f64, + /// The current outdoor temperature, in °C + pub outdoor_temperature: f64, + /// The current indoor setpoint (target) temperature, in °C + pub indoor_setpoint_temperature: f64, + /// The leaving water temperature, in °C + pub leaving_water_temperature: f64, + + /// Is the heating enabled + pub enabled: bool, + + /// Is the heating on holiday (disabled) + pub on_holiday: bool, + // Is it on powerful (quick heating) mode + //mode: , +} diff --git a/src/traits.rs b/src/traits.rs new file mode 100644 index 0000000..4e6fb97 --- /dev/null +++ b/src/traits.rs @@ -0,0 +1,35 @@ +use crate::errors::DAError; +use serde_json::Value; + +pub trait FromJsonValue: Sized { + fn from_json_value(value: &Value) -> Result; +} + +// Implement the trait for i64 +impl FromJsonValue for i64 { + fn from_json_value(value: &Value) -> Result { + value.as_i64().ok_or(DAError::ValueConversionError) + } +} + +// Implement the trait for f64 +impl FromJsonValue for f64 { + fn from_json_value(value: &Value) -> Result { + value.as_f64().ok_or(DAError::ValueConversionError) + } +} + +// Implement the trait for String +impl FromJsonValue for String { + fn from_json_value(value: &Value) -> Result { + let v = value.as_str().ok_or(DAError::ValueConversionError)?; + Ok(v.to_string()) + } +} + +// Implement the trait for bool +impl FromJsonValue for bool { + fn from_json_value(value: &Value) -> Result { + value.as_bool().ok_or(DAError::ValueConversionError) + } +}