When I go to the IOT Core Registry page (on the GCP console) and select a device, I can edit it. There's a "Device metadata" section there, reading the following:
You can set custom metadata, such as manufacturer, location, etc. for the device. These can be used to query devices in this registry. Learn more
Where the documentation page shows nothing about querying devices using metadata. Is this possible at all? What can be done using device metadata?
I am asking because I am looking for the following features with Azure IOT Hub has with device twin tags:
One first has to add device meta data, before one can query it:
One can query gcloud iot devices
list (--registry=REGISTRY : --region=REGION)
:
--filter="metadata.items.key['test_metadata'][value]='test_value'"
See gcloud topic filters
for more information about filter expressions.
Or with format: --format='value[](metadata.items.test_metadata)'
It might be easier if you implement this using client libraries. Using the suggestion of @MartinZeitler list
your devices then perform get
for each device and then do the checking on the metadata. See Python code below for the implementation:
from google.cloud import iot_v1
def sample_list_devices(meta_key_name,meta_val_name):
# Create a client
client = iot_v1.DeviceManagerClient()
project_id="your-project-id"
location="asia-east1" #define your device location
registry="your-registry-id"
parent=f"projects/{project_id}/locations/{location}/registries/{registry}"
# Initialize request argument(s)
list_request = iot_v1.ListDevicesRequest(
parent=parent,
)
# Make the request
list_result = client.list_devices(request=list_request)
# Handle the response
for response in list_result:
device=response.num_id
get_request = iot_v1.GetDeviceRequest(
name=f"{parent}/devices/{device}",
)
get_response = client.get_device(request=get_request)
if get_response.metadata[meta_key_name]==meta_val_name:
print(get_response)
return get_response
#define metadata key and metadata value that you want to use for filtering
sample_list_devices(meta_key_name="test_key",meta_val_name="test_val")
Filtered response:
See device configuration:
No, it is not possible to query metadata the way you want it. The doc says the following about the server.
"Cloud IoT Core does not interpret or index device metadata."
As you are already aware as a client-side workaround to simulate a query search, we can list all the devices first and then filter the output by metadata.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.