O Google Cloud IoT Core será desativado em 16 de agosto de 2023. Entre em contato com sua equipe de conta do Google Cloud para mais informações.

Limpar registro

Mantenha tudo organizado com as coleções Salve e categorize o conteúdo com base nas suas preferências.

Remova todos os dispositivos de um registro e exclua o registro.

Exemplo de código

C#

public static object ClearRegistry(string projectId, string cloudRegion, string registryId)
{
    var parentName = $"projects/{projectId}/locations/{cloudRegion}";
    var registryName = $"{parentName}/registries/{registryId}";
    var cloudIot = CreateAuthorizedClient();
    string responseRegistry = "";

    // The resource name of the location associated with the key rings.
    var parent = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryId}";
    try
    {
        var devices = cloudIot
            .Projects
            .Locations
            .Registries
            .Devices
            .List(parent)
            .Execute()
            .Devices;
        Console.WriteLine("Devices: {0}", parent);

        if (devices != null)
        {
            foreach (var response in devices)
            {
                DeleteDevice(projectId, cloudRegion, registryId, response.Id);
            }
        }
    }
    catch (Google.GoogleApiException e)
    {
        Console.WriteLine(e.Message);
        return e.Error.Code;
    }

    try
    {
        DeleteRegistry(projectId, cloudRegion, registryId);
        Console.WriteLine($"Successfully deleted registry {registryName}");
        Console.WriteLine(responseRegistry);
    }
    catch
    (Google.GoogleApiException e)
    {
        Console.WriteLine("Could not delete registry");
        Console.WriteLine(e.Message);
    }

    return 0;
}

Java

protected static void clearRegistry(String cloudRegion, String projectId, String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();
  final String registryPath =
      String.format(
          "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);

  CloudIot.Projects.Locations.Registries regAlias = service.projects().locations().registries();
  CloudIot.Projects.Locations.Registries.Devices devAlias = regAlias.devices();

  ListDevicesResponse listGatewaysRes =
      devAlias.list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute();
  List<Device> gateways = listGatewaysRes.getDevices();

  // Unbind all devices from all gateways
  if (gateways != null) {
    System.out.println("Found " + gateways.size() + " devices");
    for (Device g : gateways) {
      String gatewayId = g.getId();
      System.out.println("Id: " + gatewayId);

      ListDevicesResponse res =
          devAlias
              .list(registryPath)
              .setGatewayListOptionsAssociationsGatewayId(gatewayId)
              .execute();
      List<Device> deviceNumIds = res.getDevices();

      if (deviceNumIds != null) {
        System.out.println("Found " + deviceNumIds.size() + " devices");
        for (Device device : deviceNumIds) {
          String deviceId = device.getId();
          System.out.println(String.format("ID: %s", deviceId));

          // Remove any bindings from the device
          UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest();
          request.setDeviceId(deviceId);
          request.setGatewayId(gatewayId);
          regAlias.unbindDeviceFromGateway(registryPath, request).execute();
        }
      } else {
        System.out.println("Gateway has no bound devices.");
      }
    }
  }

  // Remove all devices from the regsitry
  List<Device> devices = devAlias.list(registryPath).execute().getDevices();

  if (devices != null) {
    System.out.println("Found " + devices.size() + " devices");
    for (Device d : devices) {
      String deviceId = d.getId();
      String devicePath = String.format("%s/devices/%s", registryPath, deviceId);
      service.projects().locations().registries().devices().delete(devicePath).execute();
    }
  }

  // Delete the registry
  service.projects().locations().registries().delete(registryPath).execute();
}

A seguir

Para pesquisar e filtrar amostras de código para outros produtos do Google Cloud, consulte o navegador de amostra do Google Cloud.