Search in sources :

Example 1 with ModifyCloudToDeviceConfigRequest

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

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 JsonFactory (com.google.api.client.json.JsonFactory)1 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)1 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)1 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)1 Base64 (java.util.Base64)1