Search in sources :

Example 1 with DeviceConfig

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

the class DeviceRegistryExample method setDeviceConfiguration.

// [END iot_patch_rsa]
// [START iot_set_device_config]
/**
 * Set a device configuration to the specified data (string, JSON) and version (0 for latest).
 */
public static void setDeviceConfiguration(String deviceId, String projectId, String cloudRegion, String registryName, String data, long version) 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);
    ModifyCloudToDeviceConfigRequest req = new ModifyCloudToDeviceConfigRequest();
    req.setVersionToUpdate(version);
    // Data sent through the wire has to be base64 encoded.
    Base64.Encoder encoder = Base64.getEncoder();
    String encPayload = encoder.encodeToString(data.getBytes("UTF-8"));
    req.setBinaryData(encPayload);
    DeviceConfig config = service.projects().locations().registries().devices().modifyCloudToDeviceConfig(devicePath, req).execute();
    System.out.println("Updated: " + config.getVersion());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) Base64(java.util.Base64) 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) ModifyCloudToDeviceConfigRequest(com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)

Example 2 with DeviceConfig

use of com.google.api.services.cloudiot.v1.model.DeviceConfig 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)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 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 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)1 Base64 (java.util.Base64)1