2023-01-09 20:23:55 +01:00
|
|
|
//! The supported cloud services.
|
|
|
|
|
2023-01-10 15:38:24 +01:00
|
|
|
pub(crate) mod hoymiles;
|
2023-01-09 20:23:55 +01:00
|
|
|
pub(crate) mod my_autarco;
|
|
|
|
|
|
|
|
use enum_dispatch::enum_dispatch;
|
|
|
|
use rocket::async_trait;
|
2023-01-09 21:25:35 +01:00
|
|
|
use serde::Deserialize;
|
2023-01-09 20:23:55 +01:00
|
|
|
|
2023-01-09 21:25:35 +01:00
|
|
|
use crate::Status;
|
|
|
|
|
|
|
|
/// The service-specific configuration necessary to access a cloud service API.
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(crate = "rocket::serde", tag = "kind")]
|
|
|
|
pub(crate) enum Config {
|
2023-01-10 15:38:24 +01:00
|
|
|
/// Hoymiles (<https://global.hoymiles.com>)
|
|
|
|
Hoymiles(hoymiles::Config),
|
|
|
|
/// My Autarco (<https://my.autarco.com>)
|
2023-01-09 21:25:35 +01:00
|
|
|
MyAutarco(my_autarco::Config),
|
|
|
|
}
|
2023-01-09 20:23:55 +01:00
|
|
|
|
|
|
|
/// Retrieves the service for the provided name (if supported).
|
2023-01-09 21:25:35 +01:00
|
|
|
pub(crate) fn get(config: Config) -> color_eyre::Result<Services> {
|
|
|
|
match config {
|
2023-01-10 15:38:24 +01:00
|
|
|
Config::Hoymiles(config) => Ok(Services::Hoymiles(hoymiles::service(config)?)),
|
2023-01-09 21:25:35 +01:00
|
|
|
Config::MyAutarco(config) => Ok(Services::MyAutarco(my_autarco::service(config)?)),
|
2023-01-09 20:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 16:31:37 +01:00
|
|
|
/// The errors that can occur during service API transactions.
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum Error {
|
|
|
|
/// The service is not or no longer authorized to perform requests.
|
|
|
|
///
|
|
|
|
/// This usually indicates that the service needs to login again.
|
|
|
|
#[error("not/no longer authorized")]
|
|
|
|
NotAuthorized,
|
2023-01-16 19:57:05 +01:00
|
|
|
/// The service encountered some other API request error.
|
2023-01-15 16:44:45 +01:00
|
|
|
#[error("API request error: {0}")]
|
2023-01-16 19:57:05 +01:00
|
|
|
Request(#[from] reqwest::Error),
|
|
|
|
/// The service encountered an unsupported API response.
|
|
|
|
#[error("API service error: {0}")]
|
|
|
|
Response(String),
|
2023-01-15 16:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Type alias for service results.
|
|
|
|
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
/// The supported cloud services.
|
|
|
|
#[enum_dispatch(Service)]
|
|
|
|
pub(crate) enum Services {
|
2023-01-10 15:38:24 +01:00
|
|
|
/// Hoymiles (<https://global.hoymiles.com>)
|
|
|
|
Hoymiles(hoymiles::Service),
|
2023-01-09 20:23:55 +01:00
|
|
|
/// My Autarco (<https://my.autarco.com>)
|
|
|
|
MyAutarco(my_autarco::Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Functionality trait of a cloud service.
|
|
|
|
#[async_trait]
|
|
|
|
#[enum_dispatch]
|
|
|
|
pub(crate) trait Service {
|
|
|
|
/// The interval between data polls (in seconds).
|
|
|
|
fn poll_interval(&self) -> u64;
|
|
|
|
|
|
|
|
/// Perfoms a login on the cloud service (if necessary).
|
2023-01-15 16:31:37 +01:00
|
|
|
async fn login(&mut self) -> Result<()>;
|
2023-01-09 20:23:55 +01:00
|
|
|
|
|
|
|
/// Retrieves a status update using the API of the cloud service.
|
2023-01-15 16:31:37 +01:00
|
|
|
async fn update(&mut self, timestamp: u64) -> Result<Status>;
|
2023-01-09 20:23:55 +01:00
|
|
|
}
|