Versions Compared

Key

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

...

Code Block
import os
from clearblade.cloud import iot_v1

def bind_device_to_gateway():
  project_id = 'YOUR_PROJECT_ID'
  cloud_region = 'us-central1'
  registry_id = 'your-registry-id'
  device_id = 'your-device-id'
  gateway_id = 'your-gateway-id'
  client = iot_v1.DeviceManagerClient()

  parent = client.registry_path(project_id, cloud_region, registry_id)

  request = iot_v1.BindDeviceToGatewayRequest(
    parent=parent,
    gatewayId=gateway_id,
    deviceId=device_id
  )
  res = client.bind_device_to_gateway(request=request)

  print("Device Bound! {}".format(res))

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

Go
Code Block
// bindDeviceToGateway creates an association between an existing device and gateway.
func bindDeviceToGateway(w io.Writer, projectID string, region string, registryID string, gatewayID string, deviceID string) (*iot.BindDeviceToGatewayResponse, error) {
    ctx := context.Background()
    service, err := iot.NewService(ctx)

    if err != nil {
        return nil, err
    }
    parent := fmt.Sprintf("projects/%s/locations/%s/registries/%s", projectID, region, registryID)
    bindRequest := &iot.BindDeviceToGatewayRequest{
        DeviceId:  deviceID,
        GatewayId: gatewayID,
    }

    response, err := service.Projects.Locations.Registries.BindDeviceToGateway(parent, bindRequest).Do()

    if err != nil {
        return nil, fmt.Errorf("BindDeviceToGateway: %v", err)
    }

    if response.HTTPStatusCode/100 != 2 {
        return nil, fmt.Errorf("BindDeviceToGateway: HTTP status code not 2xx\n %v", response)
    }

    fmt.Fprintf(w, "Bound %s to %s", deviceID, gatewayID)

    return response, nil
}

Listing all devices bound to a gateway

...

Code Block
import os
from clearblade.cloud import iot_v1

def delete_gateway():
  project_id = 'YOUR_PROJECT_ID'
  cloud_region = 'us-central1'
  registry_id = 'your-registry-id'
  gateway_id = 'your-gateway-id'
  
  print("Delete gateway")
  client = iot_v1.DeviceManagerClient()

  device_path = client.device_path(project_id, cloud_region, registry_id, gateway_id)

  request = iot_v1.DeleteDeviceRequest(name=device_path)

  return client.delete_device(request=request)

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

Go
Code Block
// deleteDevice deletes a device from a registry.
func deleteDevice(w io.Writer, projectID string, region string, registryID string, deviceID string) (*iot.Empty, error) {
    ctx := context.Background()
    service, err := iot.NewService(ctx)
    if err != nil {
        return nil, err
    }

    path := fmt.Sprintf("projects/%s/locations/%s/registries/%s/devices/%s", projectID, region, registryID, deviceID)
    response, err := service.Projects.Locations.Registries.Devices.Delete(path).Do()
    if err != nil {
        return nil, err
    }

    fmt.Fprintf(w, "Deleted device: %s\n", deviceID)

    return response, nil
}

...