Do discovery of TCP if possible and order UDP disc

This allows either UDP or TCP if both an address
and a UID are given. They are resolved with this
preference: TCP, Local, Remote, Map, Relay
This commit is contained in:
QuantumEntangledAndy authored and Andrew committed 2023-04-05 16:18:48 +07:00
1 parent 3771098c2f
commit 6c87d81a81
6 files changed
+350 -350

No files matched your search

+51 -39
View File
@@ -4,8 +4,14 @@ use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::{BcCamera, DiscoveryMethods, MaxEncryption, PrintFormat};
use std::fmt::{Display, Error as FmtError, Formatter};
use neolink_core::bc_protocol::{
BcCamera, BcCameraOpt, ConnectionProtocol, DiscoveryMethods, MaxEncryption, Credentials,
};
use std::{
fmt::{Display, Error as FmtError, Formatter},
net::{IpAddr, ToSocketAddrs},
str::FromStr,
};
pub(crate) enum AddressOrUid {
Address(String),
@@ -45,40 +51,51 @@ impl AddressOrUid {
}
// Convience method to get the BcCamera with the appropiate method
pub(crate) async fn connect_camera<T: Into<String>, U: Into<String>>(
// from a camera_config
pub(crate) async fn connect_camera(
&self,
channel_id: u8,
username: T,
passwd: Option<U>,
aux_print_format: PrintFormat,
camera_config: &CameraConfig,
) -> Result<BcCamera, Error> {
match self {
AddressOrUid::AddressWithUid(addr, uid, method) => Ok(BcCamera::new_with_uid_and_addr(
uid,
addr,
channel_id,
username,
passwd,
*method,
aux_print_format,
)
.await?),
AddressOrUid::Address(host) => {
Ok(
BcCamera::new_with_addr(host, channel_id, username, passwd, aux_print_format)
.await?,
)
let (port, addrs) = {
if let Some(addr_str) = camera_config.camera_addr.as_ref() {
match addr_str.to_socket_addrs() {
Ok(addr_iter) => {
let mut port = None;
let mut ipaddrs = vec![];
for addr in addr_iter {
port = Some(addr.port());
ipaddrs.push(addr.ip());
}
Ok((port, ipaddrs))
}
Err(_) => match IpAddr::from_str(addr_str) {
Ok(ip) => Ok((None, vec![ip])),
Err(_) => Err(anyhow!("Could not parse address in config")),
},
}
} else {
Ok((None, vec![]))
}
AddressOrUid::Uid(host, method) => Ok(BcCamera::new_with_uid(
host,
channel_id,
username,
passwd,
*method,
aux_print_format,
)
.await?),
}
}?;
let options = BcCameraOpt {
name: camera_config.name.clone(),
channel_id: camera_config.channel_id,
addrs,
port,
uid: camera_config.camera_uid.clone(),
protocol: ConnectionProtocol::TcpUdp,
discovery: camera_config.discovery,
aux_printing: camera_config.print_format,
credentials: Credentials {
username: camera_config.username.clone(),
password: camera_config.password.clone(),
},
};
trace!("Camera Info: {:?}", options);
Ok(BcCamera::new(options).await?)
}
}
@@ -100,12 +117,7 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
);
let camera = camera_addr
.connect_camera(
camera_config.channel_id,
&camera_config.username,
camera_config.password.as_ref(),
camera_config.print_format,
)
.connect_camera(camera_config)
.await
.with_context(|| {
format!(