public static object GetDeviceStates(string projectId, string cloudRegion, string registryId, string deviceId)
{
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
{
Console.WriteLine("States: ");
var res = cloudIot.Projects.Locations.Registries.Devices.States.List(name).Execute();
res.DeviceStates.ToList().ForEach(state =>
{
Console.WriteLine($"\t{state.UpdateTime}: {state.BinaryData}");
});
}
catch (Google.GoogleApiException e)
{
Console.WriteLine(e.Message);
if (e.Error != null) return e.Error.Code;
return -1;
}
return 0;
}
# project_id = "Your Google Cloud project ID"
# location_id = "The Cloud region the registry is located in"
# registry_id = "The registry to get device states from"
# device_id = "The identifier of the device to get states for"
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}"
# List the configurations for the provided device
result = iot_client.list_project_location_registry_device_states(
resource
)
if result.device_states
result.device_states.each do |state|
puts "#{state.update_time}: #{state.binary_data}"
end
else
puts "No state messages"
end