Skip to content
Permalink
Browse files
fix: add fieldMask for getDevice and listDevices (#64)
* Add fieldMask for getDevice and listDevices
  • Loading branch information
gcseh committed Feb 5, 2021
1 parent 1408838 commit 24c9c9a98af4147c77aa632ec4a9c3fb6464f30c
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 246 deletions.
@@ -279,7 +279,31 @@ def get_device(service_account_json, project_id, cloud_region, registry_id, devi
client = iot_v1.DeviceManagerClient()
device_path = client.device_path(project_id, cloud_region, registry_id, device_id)

device = client.get_device(request={"name": device_path})
# See full list of device fields: https://cloud.google.com/iot/docs/reference/cloudiot/rest/v1/projects.locations.registries.devices
# Warning! Use snake_case field names.
field_mask = gp_field_mask.FieldMask(
paths=[
"id",
"name",
"num_id",
"credentials",
"last_heartbeat_time",
"last_event_time",
"last_state_time",
"last_config_ack_time",
"last_config_send_time",
"blocked",
"last_error_time",
"last_error_status",
"config",
"state",
"log_level",
"metadata",
"gateway_config",
]
)

device = client.get_device(request={"name": device_path, "field_mask": field_mask})

print("Id : {}".format(device.id))
print("Name : {}".format(device.name))
@@ -345,9 +369,35 @@ def list_devices(service_account_json, project_id, cloud_region, registry_id):
client = iot_v1.DeviceManagerClient()
registry_path = client.registry_path(project_id, cloud_region, registry_id)

devices = list(client.list_devices(request={"parent": registry_path}))
# See full list of device fields: https://cloud.google.com/iot/docs/reference/cloudiot/rest/v1/projects.locations.registries.devices
# Warning! Use snake_case field names.
field_mask = gp_field_mask.FieldMask(
paths=[
"id",
"name",
"num_id",
"credentials",
"last_heartbeat_time",
"last_event_time",
"last_state_time",
"last_config_ack_time",
"last_config_send_time",
"blocked",
"last_error_time",
"last_error_status",
"config",
"state",
"log_level",
"metadata",
"gateway_config",
]
)

devices = list(
client.list_devices(request={"parent": registry_path, "field_mask": field_mask})
)
for device in devices:
print("Device: {} : {}".format(device.num_id, device.id))
print(device)

return devices
# [END iot_list_devices]
@@ -373,7 +423,7 @@ def list_registries(service_account_json, project_id, cloud_region):
def create_registry(
service_account_json, project_id, cloud_region, pubsub_topic, registry_id
):
""" Creates a registry and returns the result. Returns an empty result if
"""Creates a registry and returns the result. Returns an empty result if
the registry already exists."""
# [START iot_create_registry]
# project_id = 'YOUR_PROJECT_ID'

0 comments on commit 24c9c9a

Please sign in to comment.