Search in sources :

Example 1 with UnbindDeviceFromGatewayRequest

use of com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method clearRegistry.

// [END iot_delete_registry]
/**
 * clearRegistry
 *
 * <ul>
 *   <li>Registries can't be deleted if they contain devices,
 *   <li>Gateways (a type of device) can't be deleted if they have bound devices
 *   <li>Devices can't be deleted if bound to gateways...
 * </ul>
 *
 * To completely remove a registry, you must unbind all devices from gateways, then remove all
 * devices in a registry before removing the registry. As pseudocode: <code>
 *   ForAll gateways
 *     ForAll devicesBoundToGateway
 *       unbindDeviceFromGateway
 *   ForAll devices
 *     Delete device by ID
 *   Delete registry
 *  </code>
 */
// [START iot_clear_registry]
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();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) UnbindDeviceFromGatewayRequest(com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest) ListDevicesResponse(com.google.api.services.cloudiot.v1.model.ListDevicesResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 2 with UnbindDeviceFromGatewayRequest

use of com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method unbindDeviceFromGateway.

protected static void unbindDeviceFromGateway(String projectId, String cloudRegion, String registryName, String deviceId, String gatewayId) throws GeneralSecurityException, IOException {
    // [START iot_unbind_device_from_gateway]
    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);
    UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest();
    request.setDeviceId(deviceId);
    request.setGatewayId(gatewayId);
    UnbindDeviceFromGatewayResponse response = service.projects().locations().registries().unbindDeviceFromGateway(registryPath, request).execute();
    System.out.println(String.format("Device unbound: %s", response.toPrettyString()));
// [END iot_unbind_device_from_gateway]
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) UnbindDeviceFromGatewayRequest(com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) UnbindDeviceFromGatewayResponse(com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayResponse) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 JsonFactory (com.google.api.client.json.JsonFactory)2 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)2 UnbindDeviceFromGatewayRequest (com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest)2 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 Device (com.google.api.services.cloudiot.v1.model.Device)1 ListDevicesResponse (com.google.api.services.cloudiot.v1.model.ListDevicesResponse)1 UnbindDeviceFromGatewayResponse (com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayResponse)1