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

This commit is contained in:
Orne Brocaar 2025-02-11 14:09:37 +00:00
parent e7644bb2d1
commit d958bbe5f1
4 changed files with 30 additions and 0 deletions
api
proto/api
rust/proto/chirpstack/api
chirpstack/src

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

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

@ -250,6 +250,11 @@ impl DeviceService for Device {
} else {
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
.validate(
@ -270,6 +275,7 @@ impl DeviceService for Device {
let filters = device::Filters {
application_id: Some(app_id),
multicast_group_id: mg_id,
device_profile_id: dp_id,
search: if req.search.is_empty() {
None
} else {
@ -316,6 +322,7 @@ impl DeviceService for Device {
}),
false => None,
},
tags: d.tags.into_hashmap(),
})
.collect(),
});

@ -207,12 +207,14 @@ pub struct DeviceListItem {
pub margin: Option<i32>,
pub external_power_source: bool,
pub battery_level: Option<fields::BigDecimal>,
pub tags: fields::KeyValue,
}
#[derive(Default, Clone)]
pub struct Filters {
pub application_id: Option<Uuid>,
pub multicast_group_id: Option<Uuid>,
pub device_profile_id: Option<Uuid>,
pub search: Option<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)));
}
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 {
#[cfg(feature = "postgres")]
{
@ -650,6 +656,7 @@ pub async fn list(
device::margin,
device::external_power_source,
device::battery_level,
device::tags,
))
.distinct()
.into_boxed();
@ -658,6 +665,10 @@ pub async fn list(
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 {
#[cfg(feature = "postgres")]
{