Compare commits

..

5 commits

Author SHA1 Message Date
2f0822ff4a Add API struct 2024-02-02 15:27:46 +00:00
227c9fef43 Avoid unnecessary item paths 2024-02-02 15:27:17 +00:00
42d2f29bfc Add error enum 2024-02-02 15:26:44 +00:00
057b952d11 Fix serialization/deserialization of status codes 2024-02-02 15:25:26 +00:00
15d9d21c9b Use type-based data collections 2024-02-02 14:31:20 +00:00

View file

@ -4,6 +4,17 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use std::{borrow::Borrow, collections::HashMap, net::IpAddr};
use thiserror::Error;
#[derive(Debug)]
pub struct ValueError(String);
impl std::fmt::Display for ValueError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Invalid value given! Reason: {}", self.0)
}
}
impl std::error::Error for ValueError {}
#[derive(Debug, Error)]
pub enum Error {
#[error("unsupported API version {0}")]
@ -76,7 +87,7 @@ impl Fronius {
pub fn get_inverter_realtime_data_device<C: DataCollection>(
&self,
device_id: DeviceId,
) -> Result<C, Error> {
) -> Result<CumulationInverterData, Error> {
let device_id = u8::from(device_id).to_string();
let response: CommonResponseBody<_> = self.make_request(
@ -167,18 +178,14 @@ struct ApiVersion {
pub struct DeviceId(u8);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("invalid device ID, must be less than 100: {0}")]
pub struct InvalidDeviceId(u8);
impl TryFrom<u8> for DeviceId {
type Error = InvalidDeviceId;
type Error = ValueError;
fn try_from(device_id: u8) -> Result<Self, InvalidDeviceId> {
fn try_from(device_id: u8) -> Result<Self, ValueError> {
if device_id <= 99 {
Ok(Self(device_id))
} else {
Err(InvalidDeviceId(device_id))
Err(ValueError("device id not in range!".into()))
}
}
}