Merge pull request #99 from kevans91/dns

config: allow hostnames for camera addresses
This commit is contained in:
George Hilliard authored and GitHub committed 2020-12-08 10:29:17 -06:00
commit f88b1a6ce6
3 files changed
+32 -18

No files matched your search

+29 -14
View File
@@ -58,6 +58,9 @@ pub enum Error {
#[error(display = "Credential error")]
AuthFailed,
#[error(display = "Failed to translate camera address")]
AddrResolutionError,
#[error(display = "ADPCM Decoding Error")]
AdpcmDecodingError(&'static str),
@@ -81,22 +84,34 @@ impl Drop for BcCamera {
}
impl BcCamera {
pub fn new_with_addr<T: ToSocketAddrs>(hostname: T) -> Result<Self> {
let address = hostname
.to_socket_addrs()?
.next()
.ok_or(Error::Other("Address resolution failed"))?;
pub fn connect<T: ToSocketAddrs>(host: T) -> Result<BcCamera> {
let addr_iter = match host.to_socket_addrs() {
Ok(iter) => iter,
Err(_) => return Err(Error::AddrResolutionError),
};
Ok(Self {
address,
connection: None,
logged_in: false,
})
}
for addr in addr_iter {
debug!("Trying {}", addr);
let conn = match BcConnection::new(addr, RX_TIMEOUT) {
Ok(conn) => conn,
Err(err) => match err {
connection::Error::CommunicationError(ref err) => {
debug!("Assuming timeout from {}", err);
continue;
}
err => return Err(err.into()),
},
};
pub fn connect(&mut self) -> Result<()> {
self.connection = Some(BcConnection::new(self.address, RX_TIMEOUT)?);
Ok(())
debug!("Success: {}", addr);
return Ok(Self {
address: addr,
connection: Some(conn),
logged_in: false,
});
}
return Err(Error::Timeout);
}
pub fn disconnect(&mut self) {
+1 -2
View File
@@ -2,7 +2,6 @@ use lazy_static::lazy_static;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
use std::net::SocketAddr;
use std::time::Duration;
use validator::{Validate, ValidationError};
use validator_derive::Validate;
@@ -45,7 +44,7 @@ pub struct CameraConfig {
pub name: String,
#[serde(rename = "address")]
pub camera_addr: SocketAddr,
pub camera_addr: String,
pub username: String,
pub password: Option<String>,
+2 -2
View File
@@ -198,7 +198,6 @@ fn camera_main(
) -> Result<Never, CameraErr> {
let mut connected = false;
(|| {
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
if camera_config.timeout.is_some() {
warn!("The undocumented `timeout` config option has been removed and is no longer needed.");
warn!("Please update your config file.");
@@ -208,7 +207,8 @@ fn camera_main(
"{}: Connecting to camera at {}",
camera_config.name, camera_config.camera_addr
);
camera.connect()?;
let mut camera = BcCamera::connect(&camera_config.camera_addr)?;
camera.login(&camera_config.username, camera_config.password.as_deref())?;