Added helper type to convert from ip addr to socketaddr with the correct port

This commit is contained in:
Dreaded_X 2024-04-25 02:05:03 +02:00
parent a2ee2ad71d
commit 51f689b199
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
4 changed files with 27 additions and 14 deletions

View File

@ -1,4 +1,4 @@
use std::net::Ipv4Addr; use std::net::SocketAddr;
use async_trait::async_trait; use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig}; use automation_macro::{LuaDevice, LuaDeviceConfig};
@ -9,6 +9,7 @@ use crate::device_manager::DeviceConfig;
use crate::devices::Device; use crate::devices::Device;
use crate::error::DeviceConfigError; use crate::error::DeviceConfigError;
use crate::event::{OnDarkness, OnPresence}; use crate::event::{OnDarkness, OnPresence};
use crate::helper::Ipv4SocketAddr;
#[derive(Debug)] #[derive(Debug)]
pub enum Flag { pub enum Flag {
@ -24,8 +25,8 @@ pub struct FlagIDs {
#[derive(Debug, LuaDeviceConfig, Clone)] #[derive(Debug, LuaDeviceConfig, Clone)]
pub struct HueBridgeConfig { pub struct HueBridgeConfig {
// TODO: Add helper type that converts this to a socketaddr automatically #[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")]
pub ip: Ipv4Addr, pub addr: SocketAddr,
pub login: String, pub login: String,
pub flags: FlagIDs, pub flags: FlagIDs,
} }
@ -62,8 +63,8 @@ impl HueBridge {
}; };
let url = format!( let url = format!(
"http://{}:80/api/{}/sensors/{flag_id}/state", "http://{}/api/{}/sensors/{flag_id}/state",
self.config.ip, self.config.login self.config.addr, self.config.login
); );
trace!(?flag, flag_id, value, "Sending request to change flag"); trace!(?flag, flag_id, value, "Sending request to change flag");

View File

@ -1,4 +1,4 @@
use std::net::Ipv4Addr; use std::net::SocketAddr;
use std::time::Duration; use std::time::Duration;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
@ -14,13 +14,14 @@ use crate::config::MqttDeviceConfig;
use crate::device_manager::DeviceConfig; use crate::device_manager::DeviceConfig;
use crate::error::DeviceConfigError; use crate::error::DeviceConfigError;
use crate::event::OnMqtt; use crate::event::OnMqtt;
use crate::helper::Ipv4SocketAddr;
use crate::messages::{RemoteAction, RemoteMessage}; use crate::messages::{RemoteAction, RemoteMessage};
use crate::traits::Timeout; use crate::traits::Timeout;
#[derive(Debug, Clone, LuaDeviceConfig)] #[derive(Debug, Clone, LuaDeviceConfig)]
pub struct HueGroupConfig { pub struct HueGroupConfig {
// TODO: Add helper type that converts this to a socketaddr automatically #[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")]
pub ip: Ipv4Addr, pub addr: SocketAddr,
pub login: String, pub login: String,
pub group_id: isize, pub group_id: isize,
pub timer_id: isize, pub timer_id: isize,
@ -51,7 +52,7 @@ pub struct HueGroup {
// Couple of helper function to get the correct urls // Couple of helper function to get the correct urls
impl HueGroup { impl HueGroup {
fn url_base(&self) -> String { fn url_base(&self) -> String {
format!("http://{}:80/api/{}", self.config.ip, self.config.login) format!("http://{}/api/{}", self.config.addr, self.config.login)
} }
fn url_set_schedule(&self) -> String { fn url_set_schedule(&self) -> String {

View File

@ -1,4 +1,4 @@
use std::net::{Ipv4Addr, SocketAddr}; use std::net::SocketAddr;
use std::str::Utf8Error; use std::str::Utf8Error;
use async_trait::async_trait; use async_trait::async_trait;
@ -15,11 +15,12 @@ use tracing::trace;
use super::Device; use super::Device;
use crate::device_manager::DeviceConfig; use crate::device_manager::DeviceConfig;
use crate::error::DeviceConfigError; use crate::error::DeviceConfigError;
use crate::helper::Ipv4SocketAddr;
#[derive(Debug, Clone, LuaDeviceConfig)] #[derive(Debug, Clone, LuaDeviceConfig)]
pub struct KasaOutletConfig { pub struct KasaOutletConfig {
// TODO: Add helper type that converts this to a socketaddr automatically #[device_config(rename = "ip", with = "Ipv4SocketAddr<9999>")]
ip: Ipv4Addr, addr: SocketAddr,
} }
#[async_trait] #[async_trait]
@ -213,7 +214,7 @@ impl Response {
#[async_trait] #[async_trait]
impl traits::OnOff for KasaOutlet { impl traits::OnOff for KasaOutlet {
async fn is_on(&self) -> Result<bool, errors::ErrorCode> { async fn is_on(&self) -> Result<bool, errors::ErrorCode> {
let mut stream = TcpStream::connect::<SocketAddr>((self.config.ip, 9999).into()) let mut stream = TcpStream::connect(self.config.addr)
.await .await
.or::<DeviceError>(Err(DeviceError::DeviceOffline))?; .or::<DeviceError>(Err(DeviceError::DeviceOffline))?;
@ -247,7 +248,7 @@ impl traits::OnOff for KasaOutlet {
} }
async fn set_on(&mut self, on: bool) -> Result<(), errors::ErrorCode> { async fn set_on(&mut self, on: bool) -> Result<(), errors::ErrorCode> {
let mut stream = TcpStream::connect::<SocketAddr>((self.config.ip, 9999).into()) let mut stream = TcpStream::connect(self.config.addr)
.await .await
.or::<DeviceError>(Err(DeviceError::DeviceOffline))?; .or::<DeviceError>(Err(DeviceError::DeviceOffline))?;

View File

@ -1,3 +1,4 @@
use std::net::{Ipv4Addr, SocketAddr};
use std::time::Duration; use std::time::Duration;
use serde::Deserialize; use serde::Deserialize;
@ -10,3 +11,12 @@ impl From<DurationSeconds> for Duration {
Self::from_secs(value.0) Self::from_secs(value.0)
} }
} }
#[derive(Debug, Deserialize)]
pub struct Ipv4SocketAddr<const PORT: u16>(Ipv4Addr);
impl<const PORT: u16> From<Ipv4SocketAddr<PORT>> for SocketAddr {
fn from(ip: Ipv4SocketAddr<PORT>) -> Self {
Self::from((ip.0, PORT))
}
}