Search in sources :

Example 11 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot 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. *
 */
public static Device getDevice(String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(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) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 12 with CloudIot

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

the class DeviceRegistryExample method listDevices.

// [END iot_delete_registry]
// [START iot_list_devices]
/**
 * Print all of the devices in this registry to standard out.
 */
public static void listDevices(String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(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> devices = service.projects().locations().registries().devices().list(registryPath).execute().getDevices();
    if (devices != null) {
        System.out.println("Found " + devices.size() + " devices");
        for (Device d : devices) {
            System.out.println("Id: " + d.getId());
            if (d.getConfig() != null) {
                // Note that this will show the device config in Base64 encoded format.
                System.out.println("Config: " + d.getConfig().toPrettyString());
            }
            System.out.println();
        }
    } else {
        System.out.println("Registry has no devices.");
    }
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 13 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot 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.
 */
public static void deleteDevice(String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(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) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 14 with CloudIot

use of com.google.api.services.cloudiot.v1.CloudIot 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.
 */
public static void listDeviceConfigs(String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(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) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) DeviceConfig(com.google.api.services.cloudiot.v1.model.DeviceConfig)

Example 15 with CloudIot

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

the class DeviceRegistryExample method listRegistries.

// [END iot_get_device_configs]
// [START iot_list_registries]
/**
 * Lists all of the registries associated with the given project.
 */
public static void listRegistries(String projectId, String cloudRegion) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
    List<DeviceRegistry> registries = service.projects().locations().registries().list(projectPath).execute().getDeviceRegistries();
    if (registries != null) {
        System.out.println("Found " + registries.size() + " registries");
        for (DeviceRegistry r : registries) {
            System.out.println("Id: " + r.getId());
            System.out.println("Name: " + r.getName());
            if (r.getMqttConfig() != null) {
                System.out.println("Config: " + r.getMqttConfig().toPrettyString());
            }
            System.out.println();
        }
    } else {
        System.out.println("Project has no registries.");
    }
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceRegistry(com.google.api.services.cloudiot.v1.model.DeviceRegistry) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)17 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)17 JsonFactory (com.google.api.client.json.JsonFactory)17 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)17 Device (com.google.api.services.cloudiot.v1.model.Device)6 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)5 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)4 File (java.io.File)4 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 DeviceRegistry (com.google.api.services.cloudiot.v1.model.DeviceRegistry)2 GetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest)2 ArrayList (java.util.ArrayList)2 EventNotificationConfig (com.google.api.services.cloudiot.v1.model.EventNotificationConfig)1 ListDeviceStatesResponse (com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)1 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)1 SetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest)1 Binding (com.google.iam.v1.Binding)1 Base64 (java.util.Base64)1