public static object SetDeviceConfig(string projectId, string cloudRegion, string registryId, string deviceId, string data)
{
var cloudIot = CreateAuthorizedClient();
// The resource name of the location associated with the key rings.
var name = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryId}/devices/{deviceId}";
try
{
ModifyCloudToDeviceConfigRequest req = new ModifyCloudToDeviceConfigRequest()
{
BinaryData = Convert.ToBase64String(Encoding.Unicode.GetBytes(data))
};
var res = cloudIot.Projects.Locations.Registries.Devices.ModifyCloudToDeviceConfig(req, name).Execute();
Console.WriteLine($"Configuration updated to: {res.Version}");
}
catch (Google.GoogleApiException e)
{
Console.WriteLine(e.Message);
if (e.Error != null) return e.Error.Code;
return -1;
}
return 0;
}
/** Set a device configuration to the specified data (string, JSON) and version (0 for latest). */
protected static void setDeviceConfiguration(
String deviceId,
String projectId,
String cloudRegion,
String registryName,
String data,
long version)
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);
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(StandardCharsets.UTF_8.name()));
req.setBinaryData(encPayload);
DeviceConfig config =
service
.projects()
.locations()
.registries()
.devices()
.modifyCloudToDeviceConfig(devicePath, req)
.execute();
System.out.println("Updated: " + config.getVersion());
}
# project_id = "Your Google Cloud project ID"
# location_id = "The Cloud region the registry is located in"
# registry_id = "The registry to get a device from"
# device_id = "The identifier of the device to set configurations on"
# data = "The data, e.g. {fan: on} to send to the device"
require "google/apis/cloudiot_v1"
# Initialize the client and authenticate with the specified scope
Cloudiot = Google::Apis::CloudiotV1
iot_client = Cloudiot::CloudIotService.new
iot_client.authorization = Google::Auth.get_application_default(
"https://www.googleapis.com/auth/cloud-platform"
)
# The resource name of the location associated with the project
parent = "projects/#{project_id}/locations/#{location_id}"
resource = "#{parent}/registries/#{registry_id}/devices/#{device_id}"
config = Cloudiot::DeviceConfig.new
config.binary_data = data
# Set configuration for the provided device
iot_client.modify_cloud_to_device_config resource, config
puts "Configuration updated!"