From 43753958ef0973c30483c87c9cf69d3eee3e32dd Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Tue, 11 Feb 2025 14:09:37 +0000 Subject: [PATCH] api: List devices by device-profile + expose tags. --- api/proto/api/device.proto | 6 ++++++ api/rust/proto/chirpstack/api/device.proto | 6 ++++++ chirpstack/src/api/device.rs | 7 +++++++ chirpstack/src/storage/device.rs | 11 +++++++++++ 4 files changed, 30 insertions(+) diff --git a/api/proto/api/device.proto b/api/proto/api/device.proto index bd7553fb..11d5f04d 100644 --- a/api/proto/api/device.proto +++ b/api/proto/api/device.proto @@ -262,6 +262,9 @@ message DeviceListItem { // Device status. DeviceStatus device_status = 9; + + // Device tags. + map tags = 10; } message DeviceKeys { @@ -349,6 +352,9 @@ message ListDevicesRequest { // Tags to filter devices on. map tags = 8; + + // Device-profile ID (UUID) to filter devices on. + string device_profile_id = 9; } message ListDevicesResponse { diff --git a/api/rust/proto/chirpstack/api/device.proto b/api/rust/proto/chirpstack/api/device.proto index bd7553fb..11d5f04d 100644 --- a/api/rust/proto/chirpstack/api/device.proto +++ b/api/rust/proto/chirpstack/api/device.proto @@ -262,6 +262,9 @@ message DeviceListItem { // Device status. DeviceStatus device_status = 9; + + // Device tags. + map tags = 10; } message DeviceKeys { @@ -349,6 +352,9 @@ message ListDevicesRequest { // Tags to filter devices on. map tags = 8; + + // Device-profile ID (UUID) to filter devices on. + string device_profile_id = 9; } message ListDevicesResponse { diff --git a/chirpstack/src/api/device.rs b/chirpstack/src/api/device.rs index 63301865..1664c0fa 100644 --- a/chirpstack/src/api/device.rs +++ b/chirpstack/src/api/device.rs @@ -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 = 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(), }); diff --git a/chirpstack/src/storage/device.rs b/chirpstack/src/storage/device.rs index 6a64a42b..a8261e0c 100644 --- a/chirpstack/src/storage/device.rs +++ b/chirpstack/src/storage/device.rs @@ -207,12 +207,14 @@ pub struct DeviceListItem { pub margin: Option, pub external_power_source: bool, pub battery_level: Option, + pub tags: fields::KeyValue, } #[derive(Default, Clone)] pub struct Filters { pub application_id: Option, pub multicast_group_id: Option, + pub device_profile_id: Option, pub search: Option, pub tags: HashMap, } @@ -591,6 +593,10 @@ pub async fn get_count(filters: &Filters) -> Result { 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")] {