use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method getDevice.
// [END iot_delete_device]
// [START iot_get_device]
/**
* Retrieves device metadata from a registry. *
*/
public static Device getDevice(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("Retrieving device " + devicePath);
return service.projects().locations().registries().devices().get(devicePath).execute();
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method listDevices.
// [END iot_delete_registry]
// [START iot_list_devices]
/**
* Print all of the devices in this registry to standard out.
*/
public static void listDevices(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 registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
List<Device> devices = service.projects().locations().registries().devices().list(registryPath).execute().getDevices();
if (devices != null) {
System.out.println("Found " + devices.size() + " devices");
for (Device d : devices) {
System.out.println("Id: " + d.getId());
if (d.getConfig() != null) {
// Note that this will show the device config in Base64 encoded format.
System.out.println("Config: " + d.getConfig().toPrettyString());
}
System.out.println();
}
} else {
System.out.println("Registry has no devices.");
}
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method deleteDevice.
// [END iot_create_unauth_device]
// [START iot_delete_device]
/**
* Delete the given device from the registry.
*/
public static void deleteDevice(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("Deleting device " + devicePath);
service.projects().locations().registries().devices().delete(devicePath).execute();
}
use of com.google.api.services.cloudiot.v1.CloudIot 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();
}
}
use of com.google.api.services.cloudiot.v1.CloudIot in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method listRegistries.
// [END iot_get_device_configs]
// [START iot_list_registries]
/**
* Lists all of the registries associated with the given project.
*/
public static void listRegistries(String projectId, String cloudRegion) 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 projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
List<DeviceRegistry> registries = service.projects().locations().registries().list(projectPath).execute().getDeviceRegistries();
if (registries != null) {
System.out.println("Found " + registries.size() + " registries");
for (DeviceRegistry r : registries) {
System.out.println("Id: " + r.getId());
System.out.println("Name: " + r.getName());
if (r.getMqttConfig() != null) {
System.out.println("Config: " + r.getMqttConfig().toPrettyString());
}
System.out.println();
}
} else {
System.out.println("Project has no registries.");
}
}
Aggregations