use serde::{Deserialize, Deserializer, Serialize};
use serde::de::Error;
use serde_json::Value;
use std::collections::HashMap;
#[derive(Serialize)]
pub struct RequestDeviceHistoryData<'a> {
pub sn: &'a str,
pub variables: Vec<&'a str>,
pub begin: i64,
pub end: i64,
}
#[derive(Serialize)]
pub struct RequestDeviceRealTimeData<'a> {
pub variables: Vec<&'a str>,
pub sns: Vec<&'a str>,
}
#[derive(Serialize)]
pub struct RequestSettingsData<'a> {
pub sn: &'a str,
pub key: &'a str,
}
#[derive(Serialize)]
pub struct SetSetting<'a> {
pub sn: &'a str,
pub key: &'a str,
pub value: &'a str,
}
#[derive(Serialize)]
pub struct ChargingTimeSchedule {
#[serde(skip_deserializing)]
pub sn: String,
#[serde(rename = "enable1")]
pub enable_1: bool,
#[serde(rename = "startTime1")]
pub start_time_1: ChargingTime,
#[serde(rename = "endTime1")]
pub end_time_1: ChargingTime,
#[serde(rename = "enable2")]
pub enable_2: bool,
#[serde(rename = "startTime2")]
pub start_time_2: ChargingTime,
#[serde(rename = "endTime2")]
pub end_time_2: ChargingTime,
}
#[derive(Serialize)]
pub struct ChargingTime {
pub hour: u8,
pub minute: u8,
}
#[derive(Serialize, Deserialize)]
pub struct Data {
pub time: String,
#[serde(deserialize_with = "deserialize_scientific_notation")]
pub value: f64,
}
#[derive(Deserialize)]
pub struct DataSet {
pub data: Vec<Data>,
pub variable: String,
}
#[derive(Deserialize)]
pub struct DeviceHistoryData {
#[serde(rename = "datas")]
pub data_set: Vec<DataSet>,
}
#[derive(Deserialize)]
pub struct DeviceHistoryResult {
pub result: Vec<DeviceHistoryData>,
}
#[derive(Deserialize)]
pub struct RealTimeData {
pub variable: String,
#[serde(deserialize_with = "deserialize_scientific_notation")]
pub value: f64,
}
#[derive(Deserialize)]
pub struct RealTimeVariables {
pub datas: Vec<RealTimeData>,
}
#[derive(Deserialize)]
pub struct DeviceRealTimeResult {
pub result: Vec<RealTimeVariables>,
}
#[derive(Deserialize)]
pub struct SettingsData {
pub value: String,
}
#[derive(Deserialize)]
pub struct DeviceSettingsResult {
pub result: SettingsData,
}
#[derive(Deserialize)]
pub struct DeviceVariableInfo {
pub unit: Option<String>,
pub name: HashMap<String, String>,
#[serde(rename = "enum")]
pub enumeration: Option<HashMap<String, String>>,
}
#[derive(Deserialize)]
pub struct DeviceVariablesResult {
pub result: Vec<HashMap<String, DeviceVariableInfo>>,
}
#[derive(Deserialize)]
pub struct ErrorCodeInformation {
pub en: String,
}
#[derive(Deserialize)]
pub struct ErrorCodeInformationResult {
pub result: HashMap<String,ErrorCodeInformation>,
}
#[derive(Deserialize)]
pub struct MainSwitchStatus {
pub support: bool,
pub enable: bool,
}
#[derive(Deserialize)]
pub struct MainSwitchStatusResult {
pub result: MainSwitchStatus,
}
#[derive(Serialize)]
pub struct GetMainSwitchStatus {
#[serde(rename = "deviceSN")]
pub device_sn: String,
}
#[derive(Serialize)]
pub struct SetMainSwitchStatus {
#[serde(rename = "deviceSN")]
pub device_sn: String,
pub enable: u8,
}
fn deserialize_scientific_notation<'de, D>(deserializer: D) -> Result<f64, D::Error>
where D: Deserializer<'de> {
let v = Value::deserialize(deserializer)?;
let x = v.as_f64()
.or_else(|| v.as_str().and_then(|s| s.parse().ok()))
.ok_or_else(|| Error::custom("non-f64"))?
.try_into()
.map_err(|_| Error::custom("overflow"))?;
Ok(x)
}