use std::collections::HashMap;
use crate::client::api::HiveApi;
use crate::client::api::error::ApiError;
use crate::client::authentication::Tokens;
use crate::helper::url::{Url, get_base_url};
use chrono::{DateTime, Utc, serde::ts_milliseconds};
use serde::Deserialize;
use serde_json::Value;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
#[allow(missing_docs)]
pub enum PowerType {
Battery,
Mains,
}
#[derive(Deserialize, Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub struct Properties {
#[serde(rename = "online")]
pub is_online: bool,
pub power: Option<PowerType>,
#[serde(rename = "battery")]
pub battery_percentage: Option<i32>,
#[serde(rename = "zone")]
pub zone_id: Option<String>,
#[serde(flatten)]
#[allow(missing_docs)]
pub extra: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
#[allow(missing_docs)]
pub struct State {
pub name: String,
pub zone_name: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Thermostat {
pub id: String,
#[serde(with = "ts_milliseconds")]
pub last_seen: DateTime<Utc>,
#[serde(with = "ts_milliseconds")]
#[serde(rename = "created")]
pub created_at: DateTime<Utc>,
#[serde(rename = "props")]
pub properties: Properties,
pub state: State,
#[serde(flatten)]
#[allow(missing_docs)]
pub extra: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Hub {
pub id: String,
#[serde(with = "ts_milliseconds")]
pub last_seen: DateTime<Utc>,
#[serde(with = "ts_milliseconds")]
#[serde(rename = "created")]
pub created_at: DateTime<Utc>,
#[serde(rename = "props")]
pub properties: Properties,
pub state: State,
#[serde(flatten)]
#[allow(missing_docs)]
pub extra: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct BoilerModule {
pub id: String,
#[serde(with = "ts_milliseconds")]
pub last_seen: DateTime<Utc>,
#[serde(with = "ts_milliseconds")]
#[serde(rename = "created")]
pub created_at: DateTime<Utc>,
#[serde(rename = "props")]
pub properties: Properties,
pub state: State,
#[serde(flatten)]
#[allow(missing_docs)]
pub extra: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
#[serde(tag = "type")]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum DeviceData {
#[serde(rename = "thermostatui")]
Thermostat(Thermostat),
Hub(Hub),
BoilerModule(BoilerModule),
#[serde(other)]
Unknown,
}
#[derive(Debug)]
pub struct Device {
#[allow(missing_docs)]
pub data: DeviceData,
}
impl Device {
pub(crate) const fn new(data: DeviceData) -> Self {
Self { data }
}
}
impl HiveApi {
pub(crate) async fn get_devices(&self, tokens: &Tokens) -> Result<Vec<DeviceData>, ApiError> {
let response = self
.client
.get(get_base_url(&Url::Device))
.header("Authorization", &tokens.id_token)
.send()
.await;
let body = response?.text().await?;
Ok(serde_json::from_str(&body)?)
}
}