From 2ccf904f8fbf5f7503bc879fda8c51460387e2d8 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Wed, 24 Jul 2024 22:03:33 -0300 Subject: [PATCH] src: device: manager: Add: Info method --- src/device/manager.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/device/manager.rs b/src/device/manager.rs index 1348a976..4fc28552 100644 --- a/src/device/manager.rs +++ b/src/device/manager.rs @@ -125,7 +125,7 @@ pub enum Request { Create(CreateStruct), Delete(Uuid), List, - Status, + Info(Uuid), Search, Ping(DeviceRequestStruct), GetDeviceHandler(Uuid), @@ -165,6 +165,12 @@ impl DeviceManager { error!("DeviceManager: Failed to return List response: {e:?}"); } } + Request::Info(device_id) => { + let result = self.info(device_id).await; + if let Err(e) = actor_request.respond_to.send(result) { + error!("DeviceManager: Failed to return Info response: {:?}", e); + } + } Request::GetDeviceHandler(id) => { let answer = self.get_device_handler(id).await; if let Err(e) = actor_request.respond_to.send(answer) { @@ -345,6 +351,30 @@ impl DeviceManager { Ok(Answer::DeviceInfo(list)) } + pub async fn info(&self, device_id: Uuid) -> Result { + self.check_device_uuid(device_id)?; + Ok(Answer::DeviceInfo(vec![self.get_device(device_id)?.info()])) + } + + fn check_device_uuid(&self, device_id: Uuid) -> Result<(), ManagerError> { + if self.device.contains_key(&device_id) { + return Ok(()); + } + error!( + "Getting device handler for device: {:?} : Error, device doesn't exist", + device_id + ); + Err(ManagerError::DeviceNotExist(device_id)) + } + + fn get_device(&self, device_id: Uuid) -> Result<&Device, ManagerError> { + let device = self + .device + .get(&device_id) + .ok_or(ManagerError::DeviceNotExist(device_id))?; + Ok(device) + } + pub async fn delete(&mut self, device_id: Uuid) -> Result { match self.device.remove(&device_id) { Some(device) => {