Skip to content

Commit

Permalink
src: device: manager: Add: Info method
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin authored and patrickelectric committed Jul 29, 2024
1 parent a455550 commit 2ccf904
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/device/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub enum Request {
Create(CreateStruct),
Delete(Uuid),
List,
Status,
Info(Uuid),
Search,
Ping(DeviceRequestStruct),
GetDeviceHandler(Uuid),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -345,6 +351,30 @@ impl DeviceManager {
Ok(Answer::DeviceInfo(list))
}

pub async fn info(&self, device_id: Uuid) -> Result<Answer, ManagerError> {
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<Answer, ManagerError> {
match self.device.remove(&device_id) {
Some(device) => {
Expand Down

0 comments on commit 2ccf904

Please sign in to comment.