API tweaks

This commit is contained in:
Maxime Augier 2024-08-25 13:23:37 +02:00
parent 7881799e84
commit 44d9f9bb41
3 changed files with 69 additions and 21 deletions

View File

@ -1,5 +1,7 @@
use std::{ use std::{
io, ops::{Add, Mul, Sub}, time::{Duration, Instant, SystemTime, UNIX_EPOCH} io,
ops::{Add, Mul, Sub},
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
}; };
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
@ -54,7 +56,7 @@ impl<'de> Deserialize<'de> for UtcDateTime {
} }
} }
#[derive(Clone, Copy, Default, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub struct Triphase { pub struct Triphase {
pub phase1: f64, pub phase1: f64,
pub phase2: f64, pub phase2: f64,
@ -86,7 +88,7 @@ impl Sub<Triphase> for Triphase {
} }
impl Mul<f64> for Triphase { impl Mul<f64> for Triphase {
type Output= Triphase; type Output = Triphase;
fn mul(self, rhs: f64) -> Self::Output { fn mul(self, rhs: f64) -> Self::Output {
Triphase { Triphase {
@ -99,7 +101,11 @@ impl Mul<f64> for Triphase {
impl From<f64> for Triphase { impl From<f64> for Triphase {
fn from(value: f64) -> Self { fn from(value: f64) -> Self {
Triphase { phase1: value, phase2: value, phase3: value } Triphase {
phase1: value,
phase2: value,
phase3: value,
}
} }
} }
@ -107,7 +113,7 @@ impl From<f64> for Triphase {
pub struct SetCurrent { pub struct SetCurrent {
pub time_to_live: Option<i32>, pub time_to_live: Option<i32>,
#[serde(flatten)] #[serde(flatten)]
pub current: Triphase pub current: Triphase,
} }
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd)]
@ -272,9 +278,9 @@ pub struct SiteDetails {
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Circuit { pub struct Circuit {
pub id: i64, pub id: u32,
pub uuid: String, pub uuid: String,
pub site_id: i64, pub site_id: u32,
pub circuit_panel_id: i64, pub circuit_panel_id: i64,
pub panel_name: String, pub panel_name: String,
pub rated_current: f64, pub rated_current: f64,
@ -464,6 +470,10 @@ impl Context {
self.get("sites") self.get("sites")
} }
pub fn site(&mut self, id: i32) -> Result<SiteDetails, ApiError> {
self.get(&format!("sites/{id}"))
}
/// List all chargers available to the user /// List all chargers available to the user
pub fn chargers(&mut self) -> Result<Vec<Charger>, ApiError> { pub fn chargers(&mut self) -> Result<Vec<Charger>, ApiError> {
self.get("chargers") self.get("chargers")
@ -476,6 +486,32 @@ impl Context {
self.get(&format!("chargers/{}", id)) self.get(&format!("chargers/{}", id))
} }
pub fn circuit(&mut self, site_id: u32, circuit_id: u32) -> Result<Circuit, ApiError> {
self.get(&format!("site/{site_id}/circuit/{circuit_id}"))
}
pub fn circuit_dynamic_current(
&mut self,
site_id: u32,
circuit_id: u32,
) -> Result<Triphase, ApiError> {
self.get(&format!(
"sites/{site_id}/circuits/{circuit_id}/dynamicCurrent"
))
}
pub fn set_circuit_dynamic_current(
&mut self,
site_id: u32,
circuit_id: u32,
current: SetCurrent,
) -> Result<(), ApiError> {
self.post(
&format!("sites/{site_id}/circuits/{circuit_id}/dynamicCurrent"),
&current,
)
}
#[instrument] #[instrument]
fn get<T: DeserializeOwned>(&mut self, path: &str) -> Result<T, ApiError> { fn get<T: DeserializeOwned>(&mut self, path: &str) -> Result<T, ApiError> {
self.check_expired()?; self.check_expired()?;
@ -555,21 +591,22 @@ impl Site {
pub fn details(&self, ctx: &mut Context) -> Result<SiteDetails, ApiError> { pub fn details(&self, ctx: &mut Context) -> Result<SiteDetails, ApiError> {
ctx.get(&format!("sites/{}", self.id)) ctx.get(&format!("sites/{}", self.id))
} }
} }
impl Circuit { impl Circuit {
fn dynamic_current_path(&self) -> String { fn dynamic_current_path(&self) -> String {
format!("sites/{}/circuits/{}/dynamicCurrent", format!("sites/{}/circuits/{}/dynamicCurrent", self.site_id, self.id)
self.site_id, self.id)
} }
pub fn dynamic_current(&self, ctx: &mut Context) -> Result<Triphase, ApiError> { pub fn dynamic_current(&self, ctx: &mut Context) -> Result<Triphase, ApiError> {
ctx.get(&self.dynamic_current_path()) ctx.circuit_dynamic_current(self.site_id, self.id)
} }
pub fn set_dynamic_current(&self, ctx: &mut Context, current: SetCurrent) -> Result<(), ApiError> { pub fn set_dynamic_current(
&self,
ctx: &mut Context,
current: SetCurrent,
) -> Result<(), ApiError> {
ctx.post(&self.dynamic_current_path(), &current) ctx.post(&self.dynamic_current_path(), &current)
} }
} }

View File

@ -6,4 +6,5 @@ pub mod stream;
#[cfg(feature = "tungstenite")] #[cfg(feature = "tungstenite")]
pub mod signalr; pub mod signalr;
#[cfg(feature = "tungstenite")]
pub mod observation; pub mod observation;

View File

@ -83,12 +83,22 @@ pub enum ParseError {
impl ObservationData { impl ObservationData {
fn from_dynamic(value: String, data_type: DataType) -> Result<ObservationData, ParseError> { fn from_dynamic(value: String, data_type: DataType) -> Result<ObservationData, ParseError> {
Ok(match data_type { Ok(match data_type {
DataType::Boolean => ObservationData::Boolean(value.parse::<i64>() DataType::Boolean => ObservationData::Boolean(
.map_err(move |e| ParseError::Integer(value, e))? != 0), value
DataType::Double => ObservationData::Double(value.parse() .parse::<i64>()
.map_err(move|e| ParseError::Double(value, e))?), .map_err(move |e| ParseError::Integer(value, e))?
DataType::Integer => ObservationData::Integer(value.parse() != 0,
.map_err(move |e| ParseError::Integer(value, e))?), ),
DataType::Double => ObservationData::Double(
value
.parse()
.map_err(move |e| ParseError::Double(value, e))?,
),
DataType::Integer => ObservationData::Integer(
value
.parse()
.map_err(move |e| ParseError::Integer(value, e))?,
),
DataType::String => ObservationData::String(value), DataType::String => ObservationData::String(value),
}) })
} }