Added helper type to convert from ip addr to socketaddr with the correct port
This commit is contained in:
parent
5e4a9ee4c9
commit
acdf15f32d
|
@ -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");
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<bool, errors::ErrorCode> {
|
||||
let mut stream = TcpStream::connect::<SocketAddr>((self.config.ip, 9999).into())
|
||||
let mut stream = TcpStream::connect(self.config.addr)
|
||||
.await
|
||||
.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> {
|
||||
let mut stream = TcpStream::connect::<SocketAddr>((self.config.ip, 9999).into())
|
||||
let mut stream = TcpStream::connect(self.config.addr)
|
||||
.await
|
||||
.or::<DeviceError>(Err(DeviceError::DeviceOffline))?;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
@ -10,3 +11,12 @@ impl From<DurationSeconds> for Duration {
|
|||
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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user