api: List devices by device-profile + expose tags.

This commit is contained in:
Orne Brocaar 2025-02-11 14:09:37 +00:00
parent 1d76fabdb0
commit 43753958ef
4 changed files with 30 additions and 0 deletions

View File

@ -262,6 +262,9 @@ message DeviceListItem {
// Device status. // Device status.
DeviceStatus device_status = 9; DeviceStatus device_status = 9;
// Device tags.
map<string, string> tags = 10;
} }
message DeviceKeys { message DeviceKeys {
@ -349,6 +352,9 @@ message ListDevicesRequest {
// Tags to filter devices on. // Tags to filter devices on.
map<string, string> tags = 8; map<string, string> tags = 8;
// Device-profile ID (UUID) to filter devices on.
string device_profile_id = 9;
} }
message ListDevicesResponse { message ListDevicesResponse {

View File

@ -262,6 +262,9 @@ message DeviceListItem {
// Device status. // Device status.
DeviceStatus device_status = 9; DeviceStatus device_status = 9;
// Device tags.
map<string, string> tags = 10;
} }
message DeviceKeys { message DeviceKeys {
@ -349,6 +352,9 @@ message ListDevicesRequest {
// Tags to filter devices on. // Tags to filter devices on.
map<string, string> tags = 8; map<string, string> tags = 8;
// Device-profile ID (UUID) to filter devices on.
string device_profile_id = 9;
} }
message ListDevicesResponse { message ListDevicesResponse {

View File

@ -250,6 +250,11 @@ impl DeviceService for Device {
} else { } else {
Some(Uuid::from_str(&req.multicast_group_id).map_err(|e| e.status())?) Some(Uuid::from_str(&req.multicast_group_id).map_err(|e| e.status())?)
}; };
let dp_id: Option<Uuid> = if req.device_profile_id.is_empty() {
None
} else {
Some(Uuid::from_str(&req.device_profile_id).map_err(|e| e.status())?)
};
self.validator self.validator
.validate( .validate(
@ -270,6 +275,7 @@ impl DeviceService for Device {
let filters = device::Filters { let filters = device::Filters {
application_id: Some(app_id), application_id: Some(app_id),
multicast_group_id: mg_id, multicast_group_id: mg_id,
device_profile_id: dp_id,
search: if req.search.is_empty() { search: if req.search.is_empty() {
None None
} else { } else {
@ -316,6 +322,7 @@ impl DeviceService for Device {
}), }),
false => None, false => None,
}, },
tags: d.tags.into_hashmap(),
}) })
.collect(), .collect(),
}); });

View File

@ -207,12 +207,14 @@ pub struct DeviceListItem {
pub margin: Option<i32>, pub margin: Option<i32>,
pub external_power_source: bool, pub external_power_source: bool,
pub battery_level: Option<fields::BigDecimal>, pub battery_level: Option<fields::BigDecimal>,
pub tags: fields::KeyValue,
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct Filters { pub struct Filters {
pub application_id: Option<Uuid>, pub application_id: Option<Uuid>,
pub multicast_group_id: Option<Uuid>, pub multicast_group_id: Option<Uuid>,
pub device_profile_id: Option<Uuid>,
pub search: Option<String>, pub search: Option<String>,
pub tags: HashMap<String, String>, pub tags: HashMap<String, String>,
} }
@ -591,6 +593,10 @@ pub async fn get_count(filters: &Filters) -> Result<i64, Error> {
q = q.filter(device::dsl::application_id.eq(fields::Uuid::from(application_id))); q = q.filter(device::dsl::application_id.eq(fields::Uuid::from(application_id)));
} }
if let Some(device_profile_id) = &filters.device_profile_id {
q = q.filter(device::dsl::device_profile_id.eq(fields::Uuid::from(device_profile_id)));
}
if let Some(search) = &filters.search { if let Some(search) = &filters.search {
#[cfg(feature = "postgres")] #[cfg(feature = "postgres")]
{ {
@ -650,6 +656,7 @@ pub async fn list(
device::margin, device::margin,
device::external_power_source, device::external_power_source,
device::battery_level, device::battery_level,
device::tags,
)) ))
.distinct() .distinct()
.into_boxed(); .into_boxed();
@ -658,6 +665,10 @@ pub async fn list(
q = q.filter(device::dsl::application_id.eq(fields::Uuid::from(application_id))); q = q.filter(device::dsl::application_id.eq(fields::Uuid::from(application_id)));
} }
if let Some(device_profile_id) = &filters.device_profile_id {
q = q.filter(device::dsl::device_profile_id.eq(fields::Uuid::from(device_profile_id)));
}
if let Some(search) = &filters.search { if let Some(search) = &filters.search {
#[cfg(feature = "postgres")] #[cfg(feature = "postgres")]
{ {