Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from "@clearblade/iot";
const iotClient = new DeviceManagerClient({
  // optional auth parameters.
});

async function listDevices() {
  // Construct request
  const parentName = iotClient.registryPath(projectId, cloudRegion, registryId);

  // See the full list of device fields: https://clearblade.atlassian.net/wiki/spaces/IC/pages/2207809724/projects.locations.registries.devices#Resource:-Device
  const fieldMask = {
    paths: [
      "id",
      "name",
      "numId",
      "credentials",
      "lastHeartbeatTime",
      "lastEventTime",
      "lastStateTime",
      "lastConfigAckTime",
      "lastConfigSendTime",
      "blocked",
      "lastErrorTime",
      "lastErrorStatus",
      "config",
      "state",
      "logLevel",
      "metadata",
      "gatewayConfig",
    ],
  };

  const [response] = await iotClient.listDevices({
    parent: parentName,
    fieldMask,
  });
  const devices = response;

  if (devices.length > 0) {
    console.log("Current devices in registry:");
  } else {
    console.log("No devices in registry.");
  }

  for (let i = 0; i < devices.length; i++) {
    const device = devices[i];
    console.log(`Device ${i}: `, device);
  }
}

listDevices();

...

Code Block
import os
from clearblade.cloud import iot_v1

def list_devices():
  project_id = 'YOUR_PROJECT_ID'
  cloud_region = 'us-central1'
  registry_id = 'your-registry-id'
  print("Listing devices")

  client = iot_v1.DeviceManagerClient()
  registry_path = client.registry_path(project_id, cloud_region, registry_id)

  # See the 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.
  maskItems = [
    "id",
    "name",
    "numId",
    "credentials",
    "lastHeartbeatTime",
    "lastEventTime",
    "lastStateTime",
    "lastConfigAckTime",
    "lastConfigSendTime",
    "blocked",
    "lastErrorTime",
    "lastErrorStatus",
    "config",
    "state",
    "logLevel",
    "metadata",
    "gatewayConfig",
  ]
  field_mask = ','.join(maskItems)

  request = iot_v1.ListDevicesRequest(
    parent=registry_path,
    fieldMask=field_mask
  )

  devices = list(
      client.list_devices(request=request)
  )
  for device in devices:
      print(device.id)

  return devices

os.environ["CLEARBLADE_CONFIGURATION"] = "/path/to/your-credentials.json"
list_devices()

...

Code Block
// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from "@clearblade/iot";
const iotClient = new DeviceManagerClient({
  // optional auth parameters.
});

async function getDevice() {
  // Construct request
  const devicePath = iotClient.devicePath(
    projectId,
    cloudRegion,
    registryId,
    deviceId
  );

  // See the full list of device fields: https://clearblade.atlassian.net/wiki/spaces/IC/pages/2207809724/projects.locations.registries.devices#Resource:-Device
  const fieldMask = {
    paths: [
      "id",
      "name",
      "numId",
      "credentials",
      "lastHeartbeatTime",
      "lastEventTime",
      "lastStateTime",
      "lastConfigAckTime",
      "lastConfigSendTime",
      "blocked",
      "lastErrorTime",
      "lastErrorStatus",
      "config",
      "state",
      "logLevel",
      "metadata",
      "gatewayConfig",
    ],
  };

  const [response] = await iotClient.getDevice({
    name: devicePath,
    fieldMask,
  });
  const data = response;

  console.log("Found device:", deviceId, data);
}

getDevice();

...

Code Block
import os
from clearblade.cloud import iot_v1

def get_device():
  project_id = 'YOUR_PROJECT_ID'
  cloud_region = 'us-central1'
  registry_id = 'your-registry-id'
  device_id = 'your-device-id'
  print("Getting device")

  client = iot_v1.DeviceManagerClient()
  device_path = client.device_path(project_id, cloud_region, registry_id, device_id)

  # See the full list of device fields: https://clearblade.atlassian.net/wiki/spaces/IC/pages/2207809724/projects.locations.registries.devices
  maskItems = [
    "id",
    "name",
    "numId",
    "credentials",
    "lastHeartbeatTime",
    "lastEventTime",
    "lastStateTime",
    "lastConfigAckTime",
    "lastConfigSendTime",
    "blocked",
    "lastErrorTime",
    "lastErrorStatus",
    "config",
    "state",
    "logLevel",
    "metadata",
    "gatewayConfig",
  ]
  field_mask = ','.join(maskItems)

  request = iot_v1.GetDeviceRequest(
    name=device_path
  )

  device = client.get_device(request=request)

  print("Id : {}".format(device.id))
  print("Name : {}".format(device.name))
  print("Credentials:")

  if device.credentials is not None:
    for credential in device.credentials:
        keyinfo = credential['publicKey']
        print("\tcertificate: \n{}".format(keyinfo['key']))
        print("\tformat : {}".format(keyinfo['format']))
        print("\texpiration: {}".format(credential['expirationTime']))

  print("Config:")
  print("\tdata: {}".format(device.config.get('binaryData')))
  print("\tversion: {}".format(device.config.get('version')))
  print("\tcloudUpdateTime: {}".format(device.config.get('cloudUpdateTime')))

  return device

os.environ["CLEARBLADE_CONFIGURATION"] = "/path/to/your-credentials.json"
get_device()

...