Skip to content

Commit

Permalink
Wrk (#8)
Browse files Browse the repository at this point in the history
* api cleanup
* Added trait tests.
* Removed allow non_snake_case.
  • Loading branch information
grtwje authored May 8, 2022
1 parent 00bb9d2 commit 48b7fe9
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 79 deletions.
25 changes: 19 additions & 6 deletions src/current_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ use crate::SolaredgeCredentials;
use serde::{Deserialize, Serialize};

/// Current version request
pub struct CurrentVersionReq {}
#[derive(Clone, Debug, PartialEq)]
pub struct CurrentVersionReq;

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// Current version response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct CurrentVersionResp {
/// The API version running on the server
pub version: Version,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// The release version of the server
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Version {
/// The release number running on the server in <major.minor.revision> format.
pub release: String,
Expand All @@ -35,7 +34,8 @@ impl CurrentVersionReq {
/// * `solaredge` - SolarEdge credentials to use for sending
///
/// # Returns
/// the SolarEdge response or an error string
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<CurrentVersionResp, String> {
let url = format!(
"{}version/current?{}",
Expand All @@ -61,3 +61,16 @@ impl Default for CurrentVersionReq {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;

#[test]
fn normal_types_unit_test() {
is_normal::<CurrentVersionReq>();
is_normal::<CurrentVersionResp>();
is_normal::<Version>();
}
}
13 changes: 12 additions & 1 deletion src/date_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
/// A date and value pair returned from the monitoring API. The value units are specified by the unit
/// field elsewhere in the response.
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct DateValue {
/// YYYY-mm-dd HH:MM:SS
pub date: String,

/// Often an integer, but can be float too. Meaning defined by the context of the response.
pub value: Option<f32>,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;

#[test]
fn normal_types_unit_test() {
is_normal::<DateValue>();
}
}
8 changes: 7 additions & 1 deletion src/meter_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
/// Meters supported by SolarEdge.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum MeterType {
/// Solar energy produced.
Production,
Expand Down Expand Up @@ -36,10 +36,16 @@ impl std::fmt::Display for MeterType {
#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;

#[test]
fn meter_type_fmt_unit_test() {
let t = MeterType::Production;
assert_eq!(format!("{}", t), "Production");
}

#[test]
fn normal_types_unit_test() {
is_normal::<MeterType>();
}
}
16 changes: 14 additions & 2 deletions src/meter_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ pub use crate::date_value::DateValue;
use crate::meter_type::MeterType;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
/// Values for the meter type over a range of dates.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct MeterValue {
/// The meter type of the associated values.
pub r#type: MeterType, // had to escape the keyword type to use as a json identifier
#[serde(rename = "type")]
pub meter_type: MeterType,

/// Meter readings for each date.
pub values: Vec<DateValue>,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;

#[test]
fn normal_types_unit_test() {
is_normal::<MeterValue>();
}
}
37 changes: 37 additions & 0 deletions src/se_ms_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,37 @@
//!}
//! ```
//! Supported API requests/responses include:
//! * [CurrentVersionReq]/[CurrentVersionResp]
//! * [SiteDetailsReq] / [SiteDetailsResp]
//! * [SiteEnergyDetailedReq] / [SiteEnergyDetailedResp]
//! * [SupportedVersionsReq]/[SupportedVersionsResp]
//!
//! TODO:
//! SitesList,
//! SiteDataPeriod start/end dates,
//! SiteDataPeriod bulk,
//! SiteEnergy,
//! SiteEnergy bulk,
//! SiteTimeFrameEnergy,
//! SiteTimeFrameEnergy bulk,
//! SitePower,
//! SitePower bulk,
//! SiteOverview,
//! SiteOverview bulk,
//! SitePowerDetailed,
//! SitePowerFlow,
//! SiteStorageInformation,
//! SiteImage,
//! SiteEnvironmentalBenefits,
//! SiteInstallerImage,
//! SiteEquipmentList,
//! SiteInventory,
//! SiteInverterTechnicalData,
//! SiteEquipmentChangeLog,
//! AccountsList,
//! SiteMetersData,
//! SiteSensorList,
//! SiteSensorData
//#![warn(unused_crate_dependencies)]
#![deny(unused_extern_crates)]
Expand Down Expand Up @@ -63,6 +91,7 @@ const URL_TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
/// Struct for accessing SolarEdge's monitoring server for a given site and api key.
///
/// Used as the parameter for the send() function of all of the possible requests.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SolaredgeCredentials {
url_start: String,
site_id: String,
Expand Down Expand Up @@ -91,6 +120,9 @@ impl SolaredgeCredentials {
}
}

#[cfg(test)]
pub(crate) fn is_normal<T: Sized + Send + Sync + Unpin>() {}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -103,4 +135,9 @@ mod tests {
assert_eq!(se.site_id(), "id");
assert_eq!(se.url_end, "api_key=key");
}

#[test]
fn normal_types_unit_test() {
is_normal::<SolaredgeCredentials>();
}
}
47 changes: 31 additions & 16 deletions src/site_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// site_details request
pub struct SiteDetailsReq {}
#[derive(Clone, Debug, PartialEq)]
pub struct SiteDetailsReq;

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// site_details response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct SiteDetailsResp {
/// Detailed information about the monitoring site
pub details: SiteDetails,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// Detailed information for a single site.
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SiteDetails {
/// ID of the site. Should match the site_id specified in the Solaredge request.
pub id: i32,
Expand All @@ -29,49 +29,50 @@ pub struct SiteDetails {
pub name: String,

/// Account the site belongs to.
pub accountId: i32,
pub account_id: i32,

/// Site status, either Active or Pending Communication.
pub status: String,

/// Maximum power that can be generated by the site.
pub peakPower: f32,
pub peak_power: f32,

/// Last time the site reported in to SolarEdge.
pub lastUpdateTime: String,
pub last_update_time: String,

/// Currency type (money) used at the site location
pub currency: String,

/// Date site was initially brought on line.
pub installationDate: String,
pub installation_date: String,

/// Date site was given permission to operate.
pub ptoDate: Option<String>,
pub pto_date: Option<String>,

/// Free form notes about the site.
pub notes: String,

/// Site type, ("Optimizers & Inverters", "Safety & Monitoring Interface", "Monitoring Combiner Boxes")
pub r#type: String, // had to escape the keyword type to use as an identifier
#[serde(rename = "type")]
pub site_type: String,

/// Site location (mailing address)
pub location: SiteLocation,

/// Main module type used at the site (e.g solar panel model)
pub primaryModule: SiteModule,
pub primary_module: SiteModule,

/// Number of open alerts at the site.
pub alertQuantity: Option<i32>,
pub alert_quantity: Option<i32>,

/// Highest alert severity at the site.
pub alertSeverity: Option<String>,
pub alert_severity: Option<String>,

/// Miscellaneous uris associated with the web page for the site.
pub uris: HashMap<String, String>,

/// Public settings of the web page for the site.
pub publicSettings: SitePublicSettings,
pub public_settings: SitePublicSettings,
}

impl SiteDetailsReq {
Expand All @@ -87,7 +88,8 @@ impl SiteDetailsReq {
/// * `solaredge` - SolarEdge credentials to use for sending
///
/// # Returns
/// the SolarEdge response or an error string
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteDetailsResp, String> {
let url = format!(
"{}site/{}/details?{}",
Expand All @@ -113,3 +115,16 @@ impl Default for SiteDetailsReq {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;

#[test]
fn normal_types_unit_test() {
is_normal::<SiteDetailsReq>();
is_normal::<SiteDetailsResp>();
is_normal::<SiteDetails>();
}
}
24 changes: 17 additions & 7 deletions src/site_energy_detailed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ use crate::URL_TIME_FORMAT;
use serde::{Deserialize, Serialize};

/// site_energyDetails request
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SiteEnergyDetailedReq {
start_time: String,
end_time: String,
time_unit: String,
meters: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// site_energyDetails response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SiteEnergyDetailedResp {
/// Energy details
pub energyDetails: EnergyDetails,
pub energy_details: EnergyDetails,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
/// Energy details
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EnergyDetails {
/// Granularity of the energy detail values (should match the request)
pub timeUnit: String,
pub time_unit: String,

/// Measurement unit (e.g. Wh)
pub unit: String,
Expand Down Expand Up @@ -89,7 +90,8 @@ impl SiteEnergyDetailedReq {
/// * `solaredge` - SolarEdge credentials to use for sending
///
/// # Returns
/// the SolarEdge response or an error string
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteEnergyDetailedResp, String> {
let url = format!(
"{}site/{}/energyDetails?{}{}{}{}{}",
Expand Down Expand Up @@ -121,6 +123,7 @@ impl SiteEnergyDetailedReq {
#[cfg(test)]
mod tests {
use super::*;
use crate::is_normal;
use chrono::NaiveDateTime;

#[test]
Expand All @@ -136,4 +139,11 @@ mod tests {
assert_eq!(req.time_unit, "");
assert_eq!(req.meters, "");
}

#[test]
fn normal_types_unit_test() {
is_normal::<SiteEnergyDetailedReq>();
is_normal::<SiteEnergyDetailedResp>();
is_normal::<EnergyDetails>();
}
}
Loading

0 comments on commit 48b7fe9

Please sign in to comment.