use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method listDevicesForGateway.
/**
* List devices bound to a gateway.
*/
protected static void listDevicesForGateway(String projectId, String cloudRegion, String registryName, String gatewayId) throws IOException, GeneralSecurityException {
// [START iot_list_devices_for_gateway]
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 registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
List<Device> deviceNumIds = service.projects().locations().registries().devices().list(registryPath).setGatewayListOptionsAssociationsGatewayId(gatewayId).execute().getDevices();
if (deviceNumIds != null) {
System.out.println("Found " + deviceNumIds.size() + " devices");
for (Device device : deviceNumIds) {
System.out.println(String.format("ID: %s", device.getId()));
}
} else {
System.out.println("Gateway has no bound devices.");
}
// [END iot_list_devices_for_gateway]
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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.
*/
protected static void listDeviceConfigs(String deviceId, String projectId, String cloudRegion, String registryName) 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);
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.Projects.Locations.Registries.Devices 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. *
*/
protected static Device getDevice(String deviceId, String projectId, String cloudRegion, String registryName) 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);
System.out.println("Retrieving device " + devicePath);
return service.projects().locations().registries().devices().get(devicePath).execute();
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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.
*/
protected static void deleteDevice(String deviceId, String projectId, String cloudRegion, String registryName) 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);
System.out.println("Deleting device " + devicePath);
service.projects().locations().registries().devices().delete(devicePath).execute();
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createDeviceWithNoAuth.
// [END iot_create_rsa_device]
// [START iot_create_unauth_device]
/**
* Create a device that has no credentials.
*
* This is a valid way to construct a device, however until it is patched with a credential the
* device will not be able to connect to Cloud IoT.
*/
protected static void createDeviceWithNoAuth(String deviceId, String projectId, String cloudRegion, String registryName) 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 registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;
System.out.println("Creating device with id: " + deviceId);
Device device = new Device();
device.setId(deviceId);
device.setCredentials(new ArrayList<DeviceCredential>());
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created device: " + createdDevice.toPrettyString());
}
Aggregations