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());
}
Aggregations