How to query devices using metadata in GCP IOT Core?

150 Views Asked by At

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:

  1. Ideally I would like to enrich messages the device sends (state, events) with corresponding metadata.
  2. Querying for multiple devices based on a metadata field.

enter image description here

3

There are 3 best solutions below

6
Martin Zeitler On

One first has to add device meta data, before one can query it:


One can query gcloud iot deviceslist (--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)'

0
Ricco D On

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:

enter image description here

See device configuration:

enter image description here

0
WuA On

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.