Search in sources :

Example 16 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method listDevicesForGateway.

/**
 * List devices bound to a gateway.
 */
protected static void listDevicesForGateway(String projectId, String cloudRegion, String registryName, String gatewayId) throws IOException, GeneralSecurityException {
    // [START iot_list_devices_for_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);
    List<Device> deviceNumIds = service.projects().locations().registries().devices().list(registryPath).setGatewayListOptionsAssociationsGatewayId(gatewayId).execute().getDevices();
    if (deviceNumIds != null) {
        System.out.println("Found " + deviceNumIds.size() + " devices");
        for (Device device : deviceNumIds) {
            System.out.println(String.format("ID: %s", device.getId()));
        }
    } else {
        System.out.println("Gateway has no bound devices.");
    }
// [END iot_list_devices_for_gateway]
}
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) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 17 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method listDeviceConfigs.

// [END iot_get_registry]
// [START iot_get_device_configs]
/**
 * List all of the configs for the given device.
 */
protected static void listDeviceConfigs(String deviceId, String projectId, String cloudRegion, 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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    System.out.println("Listing device configs for " + devicePath);
    List<DeviceConfig> deviceConfigs = service.projects().locations().registries().devices().configVersions().list(devicePath).execute().getDeviceConfigs();
    for (DeviceConfig config : deviceConfigs) {
        System.out.println("Config version: " + config.getVersion());
        System.out.println("Contents: " + config.getBinaryData());
        System.out.println();
    }
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) DeviceConfig(com.google.api.services.cloudiot.v1.model.DeviceConfig)

Example 18 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method getDevice.

// [END iot_delete_device]
// [START iot_get_device]
/**
 * Retrieves device metadata from a registry. *
 */
protected static Device getDevice(String deviceId, String projectId, String cloudRegion, 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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    System.out.println("Retrieving device " + devicePath);
    return service.projects().locations().registries().devices().get(devicePath).execute();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 19 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method deleteDevice.

// [END iot_create_unauth_device]
// [START iot_delete_device]
/**
 * Delete the given device from the registry.
 */
protected static void deleteDevice(String deviceId, String projectId, String cloudRegion, 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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    System.out.println("Deleting device " + devicePath);
    service.projects().locations().registries().devices().delete(devicePath).execute();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 20 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithNoAuth.

// [END iot_create_rsa_device]
// [START iot_create_unauth_device]
/**
 * Create a device that has no credentials.
 *
 * This is a valid way to construct a device, however until it is patched with a credential the
 * device will not be able to connect to Cloud IoT.
 */
protected static void createDeviceWithNoAuth(String deviceId, String projectId, String cloudRegion, 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 = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;
    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    device.setCredentials(new ArrayList<DeviceCredential>());
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created device: " + createdDevice.toPrettyString());
}
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) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)20 JsonFactory (com.google.api.client.json.JsonFactory)20 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)20 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)20 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)20 Device (com.google.api.services.cloudiot.v1.model.Device)15 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)7 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)5 File (java.io.File)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 GatewayConfig (com.google.api.services.cloudiot.v1.model.GatewayConfig)3 ListDevicesResponse (com.google.api.services.cloudiot.v1.model.ListDevicesResponse)3 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)3 IOException (java.io.IOException)3 Base64 (java.util.Base64)3 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 HashSet (java.util.HashSet)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 Devices (com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices)1