Added config option to control discovery

This commit is contained in:
QuantumEntangledAndy authored and Andrew King committed 2023-02-22 09:55:28 +07:00
1 parent 3949dca1af
commit b49ce87f32
6 files changed
+95 -22

No files matched your search

+29 -9
View File
@@ -4,31 +4,47 @@ use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::BcCamera;
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods};
use std::fmt::{Display, Error as FmtError, Formatter};
pub(crate) enum AddressOrUid {
Address(String),
Uid(String),
Uid(String, DiscoveryMethods),
}
impl Display for AddressOrUid {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
AddressOrUid::Address(host) => write!(f, "Address: {}", host),
AddressOrUid::Uid(host) => write!(f, "UID: {}", host),
AddressOrUid::Uid(host, _) => write!(f, "UID: {}", host),
}
}
}
impl AddressOrUid {
// Created by translating the config fields directly
pub(crate) fn new(address: &Option<String>, uid: &Option<String>) -> Result<Self, Error> {
pub(crate) fn new(
address: &Option<String>,
uid: &Option<String>,
disc_method: &str,
) -> Result<Self, Error> {
match (address, uid) {
(None, None) => Err(anyhow!("Neither address or uid given")),
(Some(_), Some(_)) => Err(anyhow!("Either address or uid should be given not both")),
(Some(host), None) => Ok(AddressOrUid::Address(host.clone())),
(None, Some(host)) => Ok(AddressOrUid::Uid(host.clone())),
(None, Some(host)) => {
let method = match disc_method.to_lowercase().as_str() {
"none" => DiscoveryMethods::None,
"local" => DiscoveryMethods::Local,
"remote" => DiscoveryMethods::Remote,
"relay" => DiscoveryMethods::Relay,
n => {
warn!("Unrecognised discovery method: {}. Using Local", n);
DiscoveryMethods::Local
}
};
Ok(AddressOrUid::Uid(host.clone(), method))
}
}
}
@@ -43,8 +59,8 @@ impl AddressOrUid {
AddressOrUid::Address(host) => {
Ok(BcCamera::new_with_addr(host, channel_id, username, passwd).await?)
}
AddressOrUid::Uid(host) => {
Ok(BcCamera::new_with_uid(host, channel_id, username, passwd).await?)
AddressOrUid::Uid(host, method) => {
Ok(BcCamera::new_with_uid(host, channel_id, username, passwd, *method).await?)
}
}
}
@@ -56,8 +72,12 @@ pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result<BcCa
}
pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<BcCamera> {
let camera_addr =
AddressOrUid::new(&camera_config.camera_addr, &camera_config.camera_uid).unwrap();
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
&camera_config.camera_uid,
&camera_config.discovery,
)
.unwrap();
info!(
"{}: Connecting to camera at {}",
camera_config.name, camera_addr