From acdf15f32d4d810d82b8a426cfbb32daf6809ddc Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 25 Apr 2024 02:05:03 +0200 Subject: [PATCH] Added helper type to convert from ip addr to socketaddr with the correct port --- src/devices/hue_bridge.rs | 11 ++++++----- src/devices/hue_light.rs | 9 +++++---- src/devices/kasa_outlet.rs | 11 ++++++----- src/helper.rs | 10 ++++++++++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/devices/hue_bridge.rs b/src/devices/hue_bridge.rs index 12ffda7..f5fb870 100644 --- a/src/devices/hue_bridge.rs +++ b/src/devices/hue_bridge.rs @@ -1,4 +1,4 @@ -use std::net::Ipv4Addr; +use std::net::SocketAddr; use async_trait::async_trait; use automation_macro::{LuaDevice, LuaDeviceConfig}; @@ -9,6 +9,7 @@ use crate::device_manager::DeviceConfig; use crate::devices::Device; use crate::error::DeviceConfigError; use crate::event::{OnDarkness, OnPresence}; +use crate::helper::Ipv4SocketAddr; #[derive(Debug)] pub enum Flag { @@ -24,8 +25,8 @@ pub struct FlagIDs { #[derive(Debug, LuaDeviceConfig, Clone)] pub struct HueBridgeConfig { - // TODO: Add helper type that converts this to a socketaddr automatically - pub ip: Ipv4Addr, + #[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")] + pub addr: SocketAddr, pub login: String, pub flags: FlagIDs, } @@ -62,8 +63,8 @@ impl HueBridge { }; let url = format!( - "http://{}:80/api/{}/sensors/{flag_id}/state", - self.config.ip, self.config.login + "http://{}/api/{}/sensors/{flag_id}/state", + self.config.addr, self.config.login ); trace!(?flag, flag_id, value, "Sending request to change flag"); diff --git a/src/devices/hue_light.rs b/src/devices/hue_light.rs index 3f7f13c..3856135 100644 --- a/src/devices/hue_light.rs +++ b/src/devices/hue_light.rs @@ -1,4 +1,4 @@ -use std::net::Ipv4Addr; +use std::net::SocketAddr; use std::time::Duration; use anyhow::{anyhow, Context, Result}; @@ -14,13 +14,14 @@ use crate::config::MqttDeviceConfig; use crate::device_manager::DeviceConfig; use crate::error::DeviceConfigError; use crate::event::OnMqtt; +use crate::helper::Ipv4SocketAddr; use crate::messages::{RemoteAction, RemoteMessage}; use crate::traits::Timeout; #[derive(Debug, Clone, LuaDeviceConfig)] pub struct HueGroupConfig { - // TODO: Add helper type that converts this to a socketaddr automatically - pub ip: Ipv4Addr, + #[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")] + pub addr: SocketAddr, pub login: String, pub group_id: isize, pub timer_id: isize, @@ -51,7 +52,7 @@ pub struct HueGroup { // Couple of helper function to get the correct urls impl HueGroup { 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 { diff --git a/src/devices/kasa_outlet.rs b/src/devices/kasa_outlet.rs index 82699c0..4f3ec3f 100644 --- a/src/devices/kasa_outlet.rs +++ b/src/devices/kasa_outlet.rs @@ -1,4 +1,4 @@ -use std::net::{Ipv4Addr, SocketAddr}; +use std::net::SocketAddr; use std::str::Utf8Error; use async_trait::async_trait; @@ -15,11 +15,12 @@ use tracing::trace; use super::Device; use crate::device_manager::DeviceConfig; use crate::error::DeviceConfigError; +use crate::helper::Ipv4SocketAddr; #[derive(Debug, Clone, LuaDeviceConfig)] pub struct KasaOutletConfig { - // TODO: Add helper type that converts this to a socketaddr automatically - ip: Ipv4Addr, + #[device_config(rename = "ip", with = "Ipv4SocketAddr<9999>")] + addr: SocketAddr, } #[async_trait] @@ -213,7 +214,7 @@ impl Response { #[async_trait] impl traits::OnOff for KasaOutlet { async fn is_on(&self) -> Result { - let mut stream = TcpStream::connect::((self.config.ip, 9999).into()) + let mut stream = TcpStream::connect(self.config.addr) .await .or::(Err(DeviceError::DeviceOffline))?; @@ -247,7 +248,7 @@ impl traits::OnOff for KasaOutlet { } async fn set_on(&mut self, on: bool) -> Result<(), errors::ErrorCode> { - let mut stream = TcpStream::connect::((self.config.ip, 9999).into()) + let mut stream = TcpStream::connect(self.config.addr) .await .or::(Err(DeviceError::DeviceOffline))?; diff --git a/src/helper.rs b/src/helper.rs index 68dde86..6879739 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,3 +1,4 @@ +use std::net::{Ipv4Addr, SocketAddr}; use std::time::Duration; use serde::Deserialize; @@ -10,3 +11,12 @@ impl From for Duration { Self::from_secs(value.0) } } + +#[derive(Debug, Deserialize)] +pub struct Ipv4SocketAddr(Ipv4Addr); + +impl From> for SocketAddr { + fn from(ip: Ipv4SocketAddr) -> Self { + Self::from((ip.0, PORT)) + } +}