Skip to content

Commit

Permalink
Support connecting to inverter by hostname (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM authored Dec 7, 2023
1 parent af386d0 commit e4ef9ad
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions hms2mqtt/src/inverter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::protos::hoymiles::RealData::{HMSStateResponse, RealDataResDTO};
use crc16::{State, MODBUS};
use log::{debug, info, warn};
use log::{debug, error, info, warn};
use protobuf::Message;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::net::{IpAddr, SocketAddr};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;

static INVERTER_PORT: u16 = 10081;
static INVERTER_PORT: &str = "10081";

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum NetworkState {
Expand Down Expand Up @@ -61,11 +60,22 @@ impl<'a> Inverter<'a> {
message.extend_from_slice(&len.to_be_bytes());
message.extend_from_slice(&request_as_bytes);

let ip = self.host.parse().expect("Unable to parse socket address");
let address = SocketAddr::new(IpAddr::V4(ip), INVERTER_PORT);
let stream = TcpStream::connect_timeout(&address, Duration::from_millis(500));
let inverter_host = self.host.to_string() + ":" + INVERTER_PORT;
let address = match inverter_host.to_socket_addrs() {
Ok(mut a) => a.next(),
Err(e) => {
error!("Unable to resolve domain: {e}");
return None;
}
};
if address.is_none() {
error!("Unable to parse name");
return None;
}

let stream = TcpStream::connect_timeout(&address.unwrap(), Duration::from_millis(500));
if let Err(e) = stream {
debug!("{e}");
error!("could not connect: {e}");
self.set_state(NetworkState::Offline);
return None;
}
Expand Down

0 comments on commit e4ef9ad

Please sign in to comment.