Switched to proper Ipv4Addr type

This commit is contained in:
Dreaded_X 2023-01-05 02:09:36 +01:00
parent c9b2127eed
commit 69abaf98d7
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
3 changed files with 10 additions and 24 deletions

View File

@ -10,7 +10,7 @@ username="Dreaded_X"
topic = "automation_dev/presence" topic = "automation_dev/presence"
[light_sensor] [light_sensor]
topic = "zigbee2mqtt/living/light" topic = "zigbee2mqtt_dev/living/light"
min = 23_000 min = 23_000
max = 25_000 max = 25_000
@ -37,5 +37,5 @@ mac_address = "30:9c:23:60:9c:13"
[devices.audio] [devices.audio]
type = "AudioSetup" type = "AudioSetup"
topic = "zigbee2mqtt/living/remote" topic = "zigbee2mqtt/living/remote"
mixer = [10, 0, 0, 49] mixer = "10.0.0.49"
speakers = [10, 0, 0, 182] speakers = "10.0.0.182"

View File

@ -1,4 +1,4 @@
use std::{fs, error::Error, collections::HashMap}; use std::{fs, error::Error, collections::HashMap, net::Ipv4Addr};
use tracing::{debug, trace}; use tracing::{debug, trace};
use rumqttc::AsyncClient; use rumqttc::AsyncClient;
@ -100,8 +100,8 @@ pub enum Device {
AudioSetup { AudioSetup {
#[serde(flatten)] #[serde(flatten)]
mqtt: MqttDeviceConfig, mqtt: MqttDeviceConfig,
mixer: [u8; 4], mixer: Ipv4Addr,
speakers: [u8; 4], speakers: Ipv4Addr,
} }
} }

View File

@ -1,5 +1,5 @@
use std::io::{Write, Read}; use std::io::{Write, Read};
use std::net::{TcpStream, SocketAddr}; use std::net::{TcpStream, SocketAddr, Ipv4Addr};
use bytes::{BufMut, Buf}; use bytes::{BufMut, Buf};
use google_home::errors::{ErrorCode, DeviceError}; use google_home::errors::{ErrorCode, DeviceError};
@ -19,25 +19,12 @@ struct TPLinkOutlet {
} }
impl TPLinkOutlet { impl TPLinkOutlet {
pub fn new(ip: [u8; 4]) -> Self { pub fn new(ip: Ipv4Addr) -> Self {
// @TODO Get the current state of the outlet // @TODO Get the current state of the outlet
Self { addr: (ip, 9999).into() } Self { addr: (ip, 9999).into() }
} }
pub fn encrypt(data: bytes::Bytes) -> bytes::Bytes {
let mut key: u8 = 171;
let mut encrypted = bytes::BytesMut::with_capacity(data.len() + 4);
encrypted.put_u32(data.len() as u32);
for c in data {
key = key ^ c;
encrypted.put_u8(key);
} }
return encrypted.freeze();
}
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct RequestRelayState { struct RequestRelayState {
state: isize, state: isize,
@ -80,7 +67,6 @@ impl Request {
} }
} }
fn encrypt(&self) -> bytes::Bytes { fn encrypt(&self) -> bytes::Bytes {
let data: bytes::Bytes = serde_json::to_string(self).unwrap().into(); let data: bytes::Bytes = serde_json::to_string(self).unwrap().into();
@ -221,7 +207,7 @@ pub struct AudioSetup {
} }
impl AudioSetup { impl AudioSetup {
pub fn new(identifier: String, mqtt: MqttDeviceConfig, mixer_ip: [u8; 4], speakers_ip: [u8; 4], client: AsyncClient) -> Self { pub fn new(identifier: String, mqtt: MqttDeviceConfig, mixer_ip: Ipv4Addr, speakers_ip: Ipv4Addr, client: AsyncClient) -> Self {
let mixer = TPLinkOutlet::new(mixer_ip); let mixer = TPLinkOutlet::new(mixer_ip);
let speakers = TPLinkOutlet::new(speakers_ip); let speakers = TPLinkOutlet::new(speakers_ip);