chore: refactor impl
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -1,28 +1,20 @@
|
||||
use anyhow::{anyhow, bail};
|
||||
|
||||
use crate::hardware::{CameraImpl, SUPPORTED_CAMERAS};
|
||||
use crate::camera::Camera;
|
||||
|
||||
pub fn get_connected_camers() -> Result<Vec<Box<dyn crate::hardware::CameraImpl>>, anyhow::Error> {
|
||||
pub fn get_connected_cameras() -> anyhow::Result<Vec<Camera>> {
|
||||
let mut connected_cameras = Vec::new();
|
||||
|
||||
for device in rusb::devices()?.iter() {
|
||||
let Ok(descriptor) = device.device_descriptor() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
for camera in SUPPORTED_CAMERAS {
|
||||
if camera.matches_descriptor(&descriptor) {
|
||||
let camera = (camera.factory)(device)?;
|
||||
connected_cameras.push(camera);
|
||||
break;
|
||||
}
|
||||
if let Ok(camera) = Camera::from_device(&device) {
|
||||
connected_cameras.push(camera);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(connected_cameras)
|
||||
}
|
||||
|
||||
pub fn get_connected_camera_by_id(id: &str) -> Result<Box<dyn CameraImpl>, anyhow::Error> {
|
||||
pub fn get_connected_camera_by_id(id: &str) -> anyhow::Result<Camera> {
|
||||
let parts: Vec<&str> = id.split('.').collect();
|
||||
if parts.len() != 2 {
|
||||
bail!("Invalid device id format: {id}");
|
||||
@@ -33,26 +25,17 @@ pub fn get_connected_camera_by_id(id: &str) -> Result<Box<dyn CameraImpl>, anyho
|
||||
|
||||
for device in rusb::devices()?.iter() {
|
||||
if device.bus_number() == bus && device.address() == address {
|
||||
let descriptor = device.device_descriptor()?;
|
||||
|
||||
for camera in SUPPORTED_CAMERAS {
|
||||
if camera.matches_descriptor(&descriptor) {
|
||||
let camera = (camera.factory)(device)?;
|
||||
return Ok(camera);
|
||||
}
|
||||
}
|
||||
|
||||
bail!("Device found at {id} but is not supported");
|
||||
return Camera::from_device(&device);
|
||||
}
|
||||
}
|
||||
|
||||
bail!("No device found with id: {id}");
|
||||
}
|
||||
|
||||
pub fn get_camera(device_id: Option<&str>) -> Result<Box<dyn CameraImpl>, anyhow::Error> {
|
||||
pub fn get_camera(device_id: Option<&str>) -> anyhow::Result<Camera> {
|
||||
match device_id {
|
||||
Some(id) => get_connected_camera_by_id(id),
|
||||
None => get_connected_camers()?
|
||||
None => get_connected_cameras()?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("No supported devices connected.")),
|
||||
|
Reference in New Issue
Block a user